timescaledb/sql/hypertable_constraint.sql
gayyappan 72588a2382 Restrict constraints on compressed hypertables.
Primary and unqiue constraints are limited to segment_by and order_by
columns and foreign key constraints are limited to segment_by columns
when creating a compressed hypertable. There are no restrictions on
check constraints.
2019-10-29 19:02:58 -04:00

39 lines
1.3 KiB
PL/PgSQL

-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
-- Clone fk constraint from a hypertable
CREATE OR REPLACE FUNCTION _timescaledb_internal.hypertable_constraint_add_table_fk_constraint(
user_ht_constraint_name NAME,
user_ht_schema_name NAME,
user_ht_table_name NAME,
compress_ht_id INTEGER
)
RETURNS VOID LANGUAGE PLPGSQL AS
$BODY$
DECLARE
compressed_ht_row _timescaledb_catalog.hypertable;
constraint_oid OID;
check_sql TEXT;
def TEXT;
BEGIN
SELECT * INTO STRICT compressed_ht_row FROM _timescaledb_catalog.hypertable h
WHERE h.id = compress_ht_id;
IF user_ht_constraint_name IS NOT NULL THEN
SELECT oid INTO STRICT constraint_oid FROM pg_constraint
WHERE conname=user_ht_constraint_name AND contype = 'f' AND
conrelid = format('%I.%I', user_ht_schema_name, user_ht_table_name)::regclass::oid;
def := pg_get_constraintdef(constraint_oid);
ELSE
RAISE 'unknown constraint type';
END IF;
IF def IS NOT NULL THEN
EXECUTE format(
$$ ALTER TABLE %I.%I ADD CONSTRAINT %I %s $$,
compressed_ht_row.schema_name, compressed_ht_row.table_name, user_ht_constraint_name, def
);
END IF;
END
$BODY$;