Skip to content

Commit 411e2b0

Browse files
committed
In pg_dump, ensure that view triggers are processed after view rules.
If a view is split into CREATE TABLE + CREATE RULE to break a circular dependency, then any triggers on the view must be dumped/reloaded after the CREATE RULE; else the backend may reject the CREATE TRIGGER because it's the wrong type of trigger for a plain table. This works all right in plain dump/restore because of pg_dump's sorting heuristic that places triggers after rules. However, when using parallel restore, the ordering must be enforced by a dependency --- and we didn't have one. Fixing this is a mere matter of adding an addObjectDependency() call, except that we need to be able to find all the triggers belonging to the view relation, and there was no easy way to do that. Add fields to pg_dump's TableInfo struct to remember where the associated TriggerInfo struct(s) are. Per bug report from Dennis Kögel. The failure can be exhibited at least as far back as 9.1, so back-patch to all supported branches.
1 parent c27fda6 commit 411e2b0

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

src/bin/pg_dump/pg_dump.c

+3
Original file line numberDiff line numberDiff line change
@@ -5905,6 +5905,9 @@ getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
59055905

59065906
tginfo = (TriggerInfo *) pg_malloc(ntups * sizeof(TriggerInfo));
59075907

5908+
tbinfo->numTriggers = ntups;
5909+
tbinfo->triggers = tginfo;
5910+
59085911
for (j = 0; j < ntups; j++)
59095912
{
59105913
tginfo[j].dobj.objType = DO_TRIGGER;

src/bin/pg_dump/pg_dump.h

+2
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ typedef struct _tableInfo
289289
int numParents; /* number of (immediate) parent tables */
290290
struct _tableInfo **parents; /* TableInfos of immediate parents */
291291
struct _tableDataInfo *dataObj; /* TableDataInfo, if dumping its data */
292+
int numTriggers; /* number of triggers for table */
293+
struct _triggerInfo *triggers; /* array of TriggerInfo structs */
292294
} TableInfo;
293295

294296
typedef struct _attrDefInfo

src/bin/pg_dump/pg_dump_sort.c

+4
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,7 @@ repairViewRuleMultiLoop(DumpableObject *viewobj,
878878
{
879879
TableInfo *viewinfo = (TableInfo *) viewobj;
880880
RuleInfo *ruleinfo = (RuleInfo *) ruleobj;
881+
int i;
881882

882883
/* remove view's dependency on rule */
883884
removeObjectDependency(viewobj, ruleobj->dumpId);
@@ -895,6 +896,9 @@ repairViewRuleMultiLoop(DumpableObject *viewobj,
895896
addObjectDependency(ruleobj, viewobj->dumpId);
896897
/* now that rule is separate, it must be post-data */
897898
addObjectDependency(ruleobj, postDataBoundId);
899+
/* also, any triggers on the view must be dumped after the rule */
900+
for (i = 0; i < viewinfo->numTriggers; i++)
901+
addObjectDependency(&(viewinfo->triggers[i].dobj), ruleobj->dumpId);
898902
}
899903

900904
/*

0 commit comments

Comments
 (0)