From 83a1ab6081f70d0eed820b2b13bc3ab6e93af8ec Mon Sep 17 00:00:00 2001 From: Lukas Fittl Date: Tue, 9 Aug 2022 16:55:35 -0700 Subject: [PATCH v1] pg_get_constraintdef: Schema qualify foreign tables by default This matches pg_get_constraintdef to behave the same way as pg_get_indexdef, which is to schema qualify referenced objects in the definition, unless pretty printing is explicitly requested. For pretty printing the previous behaviour is retained, which is to only include the schema information if the referenced object is not in the current search path. --- src/backend/utils/adt/ruleutils.c | 5 +++-- src/test/regress/expected/foreign_key.out | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index d575aa0066..b7a2a356b4 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -2249,8 +2249,9 @@ pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, /* add foreign relation name */ appendStringInfo(&buf, ") REFERENCES %s(", - generate_relation_name(conForm->confrelid, - NIL)); + (prettyFlags & PRETTYFLAG_SCHEMA) ? + generate_relation_name(conForm->confrelid, NIL) : + generate_qualified_relation_name(conForm->confrelid)); /* Fetch and build referenced-column list */ val = SysCacheGetAttr(CONSTROID, tup, diff --git a/src/test/regress/expected/foreign_key.out b/src/test/regress/expected/foreign_key.out index da26f083bc..a70f7c2491 100644 --- a/src/test/regress/expected/foreign_key.out +++ b/src/test/regress/expected/foreign_key.out @@ -775,10 +775,10 @@ CREATE TABLE FKTABLE ( FOREIGN KEY (tid, fk_id_del_set_default) REFERENCES PKTABLE ON DELETE SET DEFAULT (fk_id_del_set_default) ); SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'fktable'::regclass::oid ORDER BY oid; - pg_get_constraintdef --------------------------------------------------------------------------------------------------------------------- - FOREIGN KEY (tid, fk_id_del_set_null) REFERENCES pktable(tid, id) ON DELETE SET NULL (fk_id_del_set_null) - FOREIGN KEY (tid, fk_id_del_set_default) REFERENCES pktable(tid, id) ON DELETE SET DEFAULT (fk_id_del_set_default) + pg_get_constraintdef +--------------------------------------------------------------------------------------------------------------------------- + FOREIGN KEY (tid, fk_id_del_set_null) REFERENCES public.pktable(tid, id) ON DELETE SET NULL (fk_id_del_set_null) + FOREIGN KEY (tid, fk_id_del_set_default) REFERENCES public.pktable(tid, id) ON DELETE SET DEFAULT (fk_id_del_set_default) (2 rows) INSERT INTO PKTABLE VALUES (1, 0), (1, 1), (1, 2); -- 2.34.0