timescaledb/sql/hypertable_constraint.sql
Sven Klemm a4081516ca Append pg_temp to search_path
Postgres will prepend pg_temp to the effective search_path if it
is not present in the search_path. While pg_temp will never be
used to look up functions or operators unless explicitly requested
pg_temp will be used to look up relations. Putting pg_temp in
search_path makes sure objects in pg_temp will be considered last
and pg_temp cannot be used to mask existing objects.
2022-05-03 07:55:43 +02:00

42 lines
1.6 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
-- to allow for custom types with operators outside of pg_catalog
-- we set search_path to @extschema@
SET LOCAL search_path TO @extschema@, pg_temp;
EXECUTE pg_catalog.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$ SET search_path TO pg_catalog, pg_temp;