Skip to content

Commit cb7d8fa

Browse files
amulsulCommitfest Bot
authored and
Commitfest Bot
committed
Remove hastriggers flag check before fetching FK constraints.
With NOT ENFORCED, foreign key constraints will be created without triggers. Therefore, the criteria for fetching foreign keys based on the presence of triggers no longer apply. ---- NOTE: This patch is intended to reduce the diff noise from the main patch and is not meant to be committed separately. It should be squashed with the main patch that adds ENFORCED/NOT ENFORCED. ----
1 parent 2a5e709 commit cb7d8fa

File tree

3 files changed

+103
-126
lines changed

3 files changed

+103
-126
lines changed

src/backend/utils/cache/relcache.c

-5
Original file line numberDiff line numberDiff line change
@@ -4666,11 +4666,6 @@ RelationGetFKeyList(Relation relation)
46664666
if (relation->rd_fkeyvalid)
46674667
return relation->rd_fkeylist;
46684668

4669-
/* Fast path: non-partitioned tables without triggers can't have FKs */
4670-
if (!relation->rd_rel->relhastriggers &&
4671-
relation->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
4672-
return NIL;
4673-
46744669
/*
46754670
* We build the list we intend to return (in the caller's context) while
46764671
* doing the scan. After successfully completing the scan, we copy that

src/bin/pg_dump/pg_dump.c

+1-7
Original file line numberDiff line numberDiff line change
@@ -8062,13 +8062,7 @@ getConstraints(Archive *fout, TableInfo tblinfo[], int numTables)
80628062
{
80638063
TableInfo *tinfo = &tblinfo[i];
80648064

8065-
/*
8066-
* For partitioned tables, foreign keys have no triggers so they must
8067-
* be included anyway in case some foreign keys are defined.
8068-
*/
8069-
if ((!tinfo->hastriggers &&
8070-
tinfo->relkind != RELKIND_PARTITIONED_TABLE) ||
8071-
!(tinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
8065+
if (!(tinfo->dobj.dump & DUMP_COMPONENT_DEFINITION))
80728066
continue;
80738067

80748068
/* OK, we need info for this table */

src/bin/psql/describe.c

+102-114
Original file line numberDiff line numberDiff line change
@@ -2550,136 +2550,124 @@ describeOneTableDetails(const char *schemaname,
25502550
PQclear(result);
25512551
}
25522552

2553-
/*
2554-
* Print foreign-key constraints (there are none if no triggers,
2555-
* except if the table is partitioned, in which case the triggers
2556-
* appear in the partitions)
2557-
*/
2558-
if (tableinfo.hastriggers ||
2559-
tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2553+
/* Print foreign-key constraints */
2554+
if (pset.sversion >= 120000 &&
2555+
(tableinfo.ispartition || tableinfo.relkind == RELKIND_PARTITIONED_TABLE))
25602556
{
2561-
if (pset.sversion >= 120000 &&
2562-
(tableinfo.ispartition || tableinfo.relkind == RELKIND_PARTITIONED_TABLE))
2563-
{
2564-
/*
2565-
* Put the constraints defined in this table first, followed
2566-
* by the constraints defined in ancestor partitioned tables.
2567-
*/
2568-
printfPQExpBuffer(&buf,
2569-
"SELECT conrelid = '%s'::pg_catalog.regclass AS sametable,\n"
2570-
" conname,\n"
2571-
" pg_catalog.pg_get_constraintdef(oid, true) AS condef,\n"
2572-
" conrelid::pg_catalog.regclass AS ontable\n"
2573-
" FROM pg_catalog.pg_constraint,\n"
2574-
" pg_catalog.pg_partition_ancestors('%s')\n"
2575-
" WHERE conrelid = relid AND contype = " CppAsString2(CONSTRAINT_FOREIGN) " AND conparentid = 0\n"
2576-
"ORDER BY sametable DESC, conname;",
2577-
oid, oid);
2578-
}
2579-
else
2580-
{
2581-
printfPQExpBuffer(&buf,
2582-
"SELECT true as sametable, conname,\n"
2583-
" pg_catalog.pg_get_constraintdef(r.oid, true) as condef,\n"
2584-
" conrelid::pg_catalog.regclass AS ontable\n"
2585-
"FROM pg_catalog.pg_constraint r\n"
2586-
"WHERE r.conrelid = '%s' AND r.contype = " CppAsString2(CONSTRAINT_FOREIGN) "\n",
2587-
oid);
2588-
2589-
if (pset.sversion >= 120000)
2590-
appendPQExpBufferStr(&buf, " AND conparentid = 0\n");
2591-
appendPQExpBufferStr(&buf, "ORDER BY conname");
2592-
}
2557+
/*
2558+
* Put the constraints defined in this table first, followed
2559+
* by the constraints defined in ancestor partitioned tables.
2560+
*/
2561+
printfPQExpBuffer(&buf,
2562+
"SELECT conrelid = '%s'::pg_catalog.regclass AS sametable,\n"
2563+
" conname,\n"
2564+
" pg_catalog.pg_get_constraintdef(oid, true) AS condef,\n"
2565+
" conrelid::pg_catalog.regclass AS ontable\n"
2566+
" FROM pg_catalog.pg_constraint,\n"
2567+
" pg_catalog.pg_partition_ancestors('%s')\n"
2568+
" WHERE conrelid = relid AND contype = " CppAsString2(CONSTRAINT_FOREIGN) " AND conparentid = 0\n"
2569+
"ORDER BY sametable DESC, conname;",
2570+
oid, oid);
2571+
}
2572+
else
2573+
{
2574+
printfPQExpBuffer(&buf,
2575+
"SELECT true as sametable, conname,\n"
2576+
" pg_catalog.pg_get_constraintdef(r.oid, true) as condef,\n"
2577+
" conrelid::pg_catalog.regclass AS ontable\n"
2578+
"FROM pg_catalog.pg_constraint r\n"
2579+
"WHERE r.conrelid = '%s' AND r.contype = " CppAsString2(CONSTRAINT_FOREIGN) "\n",
2580+
oid);
25932581

2594-
result = PSQLexec(buf.data);
2595-
if (!result)
2596-
goto error_return;
2597-
else
2598-
tuples = PQntuples(result);
2582+
if (pset.sversion >= 120000)
2583+
appendPQExpBufferStr(&buf, " AND conparentid = 0\n");
2584+
appendPQExpBufferStr(&buf, "ORDER BY conname");
2585+
}
25992586

2600-
if (tuples > 0)
2601-
{
2602-
int i_sametable = PQfnumber(result, "sametable"),
2603-
i_conname = PQfnumber(result, "conname"),
2604-
i_condef = PQfnumber(result, "condef"),
2605-
i_ontable = PQfnumber(result, "ontable");
2587+
result = PSQLexec(buf.data);
2588+
if (!result)
2589+
goto error_return;
2590+
else
2591+
tuples = PQntuples(result);
26062592

2607-
printTableAddFooter(&cont, _("Foreign-key constraints:"));
2608-
for (i = 0; i < tuples; i++)
2609-
{
2610-
/*
2611-
* Print untranslated constraint name and definition. Use
2612-
* a "TABLE tab" prefix when the constraint is defined in
2613-
* a parent partitioned table.
2614-
*/
2615-
if (strcmp(PQgetvalue(result, i, i_sametable), "f") == 0)
2616-
printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2617-
PQgetvalue(result, i, i_ontable),
2618-
PQgetvalue(result, i, i_conname),
2619-
PQgetvalue(result, i, i_condef));
2620-
else
2621-
printfPQExpBuffer(&buf, " \"%s\" %s",
2622-
PQgetvalue(result, i, i_conname),
2623-
PQgetvalue(result, i, i_condef));
2593+
if (tuples > 0)
2594+
{
2595+
int i_sametable = PQfnumber(result, "sametable"),
2596+
i_conname = PQfnumber(result, "conname"),
2597+
i_condef = PQfnumber(result, "condef"),
2598+
i_ontable = PQfnumber(result, "ontable");
26242599

2625-
printTableAddFooter(&cont, buf.data);
2626-
}
2600+
printTableAddFooter(&cont, _("Foreign-key constraints:"));
2601+
for (i = 0; i < tuples; i++)
2602+
{
2603+
/*
2604+
* Print untranslated constraint name and definition. Use
2605+
* a "TABLE tab" prefix when the constraint is defined in
2606+
* a parent partitioned table.
2607+
*/
2608+
if (strcmp(PQgetvalue(result, i, i_sametable), "f") == 0)
2609+
printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2610+
PQgetvalue(result, i, i_ontable),
2611+
PQgetvalue(result, i, i_conname),
2612+
PQgetvalue(result, i, i_condef));
2613+
else
2614+
printfPQExpBuffer(&buf, " \"%s\" %s",
2615+
PQgetvalue(result, i, i_conname),
2616+
PQgetvalue(result, i, i_condef));
2617+
2618+
printTableAddFooter(&cont, buf.data);
26272619
}
2628-
PQclear(result);
26292620
}
2621+
PQclear(result);
26302622

26312623
/* print incoming foreign-key references */
2632-
if (tableinfo.hastriggers ||
2633-
tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
2624+
if (pset.sversion >= 120000)
26342625
{
2635-
if (pset.sversion >= 120000)
2636-
{
2637-
printfPQExpBuffer(&buf,
2638-
"SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2639-
" pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2640-
" FROM pg_catalog.pg_constraint c\n"
2641-
" WHERE confrelid IN (SELECT pg_catalog.pg_partition_ancestors('%s')\n"
2642-
" UNION ALL VALUES ('%s'::pg_catalog.regclass))\n"
2643-
" AND contype = " CppAsString2(CONSTRAINT_FOREIGN) " AND conparentid = 0\n"
2644-
"ORDER BY conname;",
2645-
oid, oid);
2646-
}
2647-
else
2648-
{
2649-
printfPQExpBuffer(&buf,
2650-
"SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2651-
" pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2652-
" FROM pg_catalog.pg_constraint\n"
2653-
" WHERE confrelid = %s AND contype = " CppAsString2(CONSTRAINT_FOREIGN) "\n"
2654-
"ORDER BY conname;",
2655-
oid);
2656-
}
2626+
printfPQExpBuffer(&buf,
2627+
"SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2628+
" pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2629+
" FROM pg_catalog.pg_constraint c\n"
2630+
" WHERE confrelid IN (SELECT pg_catalog.pg_partition_ancestors('%s')\n"
2631+
" UNION ALL VALUES ('%s'::pg_catalog.regclass))\n"
2632+
" AND contype = " CppAsString2(CONSTRAINT_FOREIGN) " AND conparentid = 0\n"
2633+
"ORDER BY conname;",
2634+
oid, oid);
2635+
}
2636+
else
2637+
{
2638+
printfPQExpBuffer(&buf,
2639+
"SELECT conname, conrelid::pg_catalog.regclass AS ontable,\n"
2640+
" pg_catalog.pg_get_constraintdef(oid, true) AS condef\n"
2641+
" FROM pg_catalog.pg_constraint\n"
2642+
" WHERE confrelid = %s AND contype = " CppAsString2(CONSTRAINT_FOREIGN) "\n"
2643+
"ORDER BY conname;",
2644+
oid);
2645+
}
26572646

2658-
result = PSQLexec(buf.data);
2659-
if (!result)
2660-
goto error_return;
2661-
else
2662-
tuples = PQntuples(result);
2647+
result = PSQLexec(buf.data);
2648+
if (!result)
2649+
goto error_return;
2650+
else
2651+
tuples = PQntuples(result);
26632652

2664-
if (tuples > 0)
2665-
{
2666-
int i_conname = PQfnumber(result, "conname"),
2667-
i_ontable = PQfnumber(result, "ontable"),
2668-
i_condef = PQfnumber(result, "condef");
2653+
if (tuples > 0)
2654+
{
2655+
int i_conname = PQfnumber(result, "conname"),
2656+
i_ontable = PQfnumber(result, "ontable"),
2657+
i_condef = PQfnumber(result, "condef");
26692658

2670-
printTableAddFooter(&cont, _("Referenced by:"));
2671-
for (i = 0; i < tuples; i++)
2672-
{
2673-
printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2674-
PQgetvalue(result, i, i_ontable),
2675-
PQgetvalue(result, i, i_conname),
2676-
PQgetvalue(result, i, i_condef));
2659+
printTableAddFooter(&cont, _("Referenced by:"));
2660+
for (i = 0; i < tuples; i++)
2661+
{
2662+
printfPQExpBuffer(&buf, " TABLE \"%s\" CONSTRAINT \"%s\" %s",
2663+
PQgetvalue(result, i, i_ontable),
2664+
PQgetvalue(result, i, i_conname),
2665+
PQgetvalue(result, i, i_condef));
26772666

2678-
printTableAddFooter(&cont, buf.data);
2679-
}
2667+
printTableAddFooter(&cont, buf.data);
26802668
}
2681-
PQclear(result);
26822669
}
2670+
PQclear(result);
26832671

26842672
/* print any row-level policies */
26852673
if (pset.sversion >= 90500)

0 commit comments

Comments
 (0)