]> granicus.if.org Git - postgresql/commitdiff
In pg_dump, ensure that view triggers are processed after view rules.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 4 Feb 2016 05:26:10 +0000 (00:26 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 4 Feb 2016 05:26:10 +0000 (00:26 -0500)
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.

src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_dump.h
src/bin/pg_dump/pg_dump_sort.c

index aa01d6a66c7d9be557bb1f67d3c9d1820e8f235d..64c2673f9a598d748b611e6781a964c483ea6515 100644 (file)
@@ -6261,6 +6261,9 @@ getTriggers(Archive *fout, TableInfo tblinfo[], int numTables)
 
                tginfo = (TriggerInfo *) pg_malloc(ntups * sizeof(TriggerInfo));
 
+               tbinfo->numTriggers = ntups;
+               tbinfo->triggers = tginfo;
+
                for (j = 0; j < ntups; j++)
                {
                        tginfo[j].dobj.objType = DO_TRIGGER;
index 78b2584ce6299c2a8b70ebb0db45ec6086262a45..9a1d8f863cc3d3ec83c9fe7a0d83ef46bbcf3ff1 100644 (file)
@@ -257,6 +257,8 @@ typedef struct _tableInfo
        int                     numParents;             /* number of (immediate) parent tables */
        struct _tableInfo **parents;    /* TableInfos of immediate parents */
        struct _tableDataInfo *dataObj;         /* TableDataInfo, if dumping its data */
+       int                     numTriggers;    /* number of triggers for table */
+       struct _triggerInfo *triggers;          /* array of TriggerInfo structs */
 } TableInfo;
 
 typedef struct _attrDefInfo
index b0f6ace9a40f669c802db33492d07d9ee3af197b..78ff59c342998295ceaee04f1a074b90787842c7 100644 (file)
@@ -884,6 +884,7 @@ repairViewRuleMultiLoop(DumpableObject *viewobj,
 {
        TableInfo  *viewinfo = (TableInfo *) viewobj;
        RuleInfo   *ruleinfo = (RuleInfo *) ruleobj;
+       int                     i;
 
        /* remove view's dependency on rule */
        removeObjectDependency(viewobj, ruleobj->dumpId);
@@ -901,6 +902,9 @@ repairViewRuleMultiLoop(DumpableObject *viewobj,
        addObjectDependency(ruleobj, viewobj->dumpId);
        /* now that rule is separate, it must be post-data */
        addObjectDependency(ruleobj, postDataBoundId);
+       /* also, any triggers on the view must be dumped after the rule */
+       for (i = 0; i < viewinfo->numTriggers; i++)
+               addObjectDependency(&(viewinfo->triggers[i].dobj), ruleobj->dumpId);
 }
 
 /*