diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2016-11-25 13:44:48 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2016-11-25 13:44:48 -0500 |
commit | 6cbe84c826b51a159825e9843184c7b4a740e4ae (patch) | |
tree | 6de01057ba40f732d6731f73efd4ff4c7fd20ea1 /src/backend/commands/tablecmds.c | |
parent | 7816d13563b74379c9db618e46883c6db5fc0680 (diff) | |
download | postgresql-6cbe84c826b51a159825e9843184c7b4a740e4ae.tar.gz postgresql-6cbe84c826b51a159825e9843184c7b4a740e4ae.zip |
Check for pending trigger events on far end when dropping an FK constraint.
When dropping a foreign key constraint with ALTER TABLE DROP CONSTRAINT,
we refuse the drop if there are any pending trigger events on the named
table; this ensures that we won't remove the pg_trigger row that will be
consulted by those events. But we should make the same check for the
referenced relation, else we might remove a due-to-be-referenced pg_trigger
row for that relation too, resulting in "could not find trigger NNN" or
"relation NNN has no triggers" errors at commit. Per bug #14431 from
Benjie Gillam. Back-patch to all supported branches.
Report: <20161124114911.6530.31200@wrigleys.postgresql.org>
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r-- | src/backend/commands/tablecmds.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 35fc9f0548d..e70d75260bd 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -7689,6 +7689,24 @@ ATExecDropConstraint(Relation rel, const char *constrName, is_no_inherit_constraint = con->connoinherit; /* + * If it's a foreign-key constraint, we'd better lock the referenced + * table and check that that's not in use, just as we've already done + * for the constrained table (else we might, eg, be dropping a trigger + * that has unfired events). But we can/must skip that in the + * self-referential case. + */ + if (con->contype == CONSTRAINT_FOREIGN && + con->confrelid != RelationGetRelid(rel)) + { + Relation frel; + + /* Must match lock taken by RemoveTriggerById: */ + frel = heap_open(con->confrelid, AccessExclusiveLock); + CheckTableNotInUse(frel, "ALTER TABLE"); + heap_close(frel, NoLock); + } + + /* * Perform the actual constraint deletion */ conobj.classId = ConstraintRelationId; |