timescaledb/sql/hypertable_constraint.sql
Sven Klemm 6dddfaa54e Lock down search_path in install scripts
This patch locks down search_path in extension install and update
scripts to only contain pg_catalog, this requires that any reference
in those scripts is fully qualified. Additionally we add explicit
create commands to all update scripts for objects added to the
public schema. This change will make update scripts fail if a
function with identical signature already exists when installing
or upgrading instead reusing the existing object.
2022-02-09 17:53:20 +01:00

42 lines
1.5 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@;
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;