Skip to content

Commit 4c40b27

Browse files
committed
Fix pg_dump/pg_restore to restore event triggers later.
Previously, event triggers were restored just after regular triggers (and FK constraints, which are basically triggers). This is risky since an event trigger, once installed, could interfere with subsequent restore commands. Worse, because event triggers don't have any particular dependencies on any post-data objects, a parallel restore would consider them eligible to be restored the moment the post-data phase starts, allowing them to also interfere with restoration of a whole bunch of objects that would have been restored before them in a serial restore. There's no way to completely remove the risk of a misguided event trigger breaking the restore, since if nothing else it could break other event triggers. But we can certainly push them to later in the process to minimize the hazard. To fix, tweak the RestorePass mechanism introduced by commit 3eb9a5e so that event triggers are handled as part of the post-ACL processing pass (renaming the "REFRESH" pass to "POST_ACL" to reflect its more general use). This will cause them to restore after everything except matview refreshes, which seems OK since matview refreshes really ought to run in the post-restore state of the database. In a parallel restore, event triggers and matview refreshes might be intermixed, but that seems all right as well. Also update the code and comments in pg_dump_sort.c so that its idea of how things are sorted agrees with what actually happens due to the RestorePass mechanism. This is mostly cosmetic: it'll affect the order of objects in a dump's TOC, but not the actual restore order. But not changing that would be quite confusing to somebody reading the code. Back-patch to all supported branches. Fabrízio de Royes Mello, tweaked a bit by me Discussion: https://postgr.es/m/CAFcNs+ow1hmFox8P--3GSdtwz-S3Binb6ZmoP6Vk+Xg=K6eZNA@mail.gmail.com
1 parent 82c04e4 commit 4c40b27

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -671,11 +671,11 @@ RestoreArchive(Archive *AHX)
671671
{
672672
/*
673673
* In serial mode, process everything in three phases: normal items,
674-
* then ACLs, then matview refresh items. We might be able to skip
675-
* one or both extra phases in some cases, eg data-only restores.
674+
* then ACLs, then post-ACL items. We might be able to skip one or
675+
* both extra phases in some cases, eg data-only restores.
676676
*/
677677
bool haveACL = false;
678-
bool haveRefresh = false;
678+
bool havePostACL = false;
679679

680680
for (te = AH->toc->next; te != AH->toc; te = te->next)
681681
{
@@ -690,8 +690,8 @@ RestoreArchive(Archive *AHX)
690690
case RESTORE_PASS_ACL:
691691
haveACL = true;
692692
break;
693-
case RESTORE_PASS_REFRESH:
694-
haveRefresh = true;
693+
case RESTORE_PASS_POST_ACL:
694+
havePostACL = true;
695695
break;
696696
}
697697
}
@@ -706,12 +706,12 @@ RestoreArchive(Archive *AHX)
706706
}
707707
}
708708

709-
if (haveRefresh)
709+
if (havePostACL)
710710
{
711711
for (te = AH->toc->next; te != AH->toc; te = te->next)
712712
{
713713
if ((te->reqs & (REQ_SCHEMA | REQ_DATA)) != 0 &&
714-
_tocEntryRestorePass(te) == RESTORE_PASS_REFRESH)
714+
_tocEntryRestorePass(te) == RESTORE_PASS_POST_ACL)
715715
(void) restore_toc_entry(AH, te, false);
716716
}
717717
}
@@ -3088,8 +3088,9 @@ _tocEntryRestorePass(TocEntry *te)
30883088
strcmp(te->desc, "ACL LANGUAGE") == 0 ||
30893089
strcmp(te->desc, "DEFAULT ACL") == 0)
30903090
return RESTORE_PASS_ACL;
3091-
if (strcmp(te->desc, "MATERIALIZED VIEW DATA") == 0)
3092-
return RESTORE_PASS_REFRESH;
3091+
if (strcmp(te->desc, "EVENT TRIGGER") == 0 ||
3092+
strcmp(te->desc, "MATERIALIZED VIEW DATA") == 0)
3093+
return RESTORE_PASS_POST_ACL;
30933094
return RESTORE_PASS_MAIN;
30943095
}
30953096

src/bin/pg_dump/pg_backup_archiver.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,24 @@ typedef enum
211211
* data restore failures. On the other hand, matview REFRESH commands should
212212
* come out after ACLs, as otherwise non-superuser-owned matviews might not
213213
* be able to execute. (If the permissions at the time of dumping would not
214-
* allow a REFRESH, too bad; we won't fix that for you.) These considerations
215-
* force us to make three passes over the TOC, restoring the appropriate
216-
* subset of items in each pass. We assume that the dependency sort resulted
217-
* in an appropriate ordering of items within each subset.
214+
* allow a REFRESH, too bad; we won't fix that for you.) We also want event
215+
* triggers to be restored after ACLs, so that they can't mess those up.
216+
*
217+
* These considerations force us to make three passes over the TOC,
218+
* restoring the appropriate subset of items in each pass. We assume that
219+
* the dependency sort resulted in an appropriate ordering of items within
220+
* each subset.
221+
*
218222
* XXX This mechanism should be superseded by tracking dependencies on ACLs
219223
* properly; but we'll still need it for old dump files even after that.
220224
*/
221225
typedef enum
222226
{
223227
RESTORE_PASS_MAIN = 0, /* Main pass (most TOC item types) */
224228
RESTORE_PASS_ACL, /* ACL item types */
225-
RESTORE_PASS_REFRESH /* Matview REFRESH items */
229+
RESTORE_PASS_POST_ACL /* Event trigger and matview refresh items */
226230

227-
#define RESTORE_PASS_LAST RESTORE_PASS_REFRESH
231+
#define RESTORE_PASS_LAST RESTORE_PASS_POST_ACL
228232
} RestorePass;
229233

230234
typedef enum

src/bin/pg_dump/pg_dump_sort.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@
2525
* Sort priority for database object types.
2626
* Objects are sorted by type, and within a type by name.
2727
*
28-
* Because materialized views can potentially reference system views,
29-
* DO_REFRESH_MATVIEW should always be the last thing on the list.
28+
* Triggers, event triggers, and materialized views are intentionally sorted
29+
* late. Triggers must be restored after all data modifications, so that
30+
* they don't interfere with loading data. Event triggers are restored
31+
* next-to-last so that they don't interfere with object creations of any
32+
* kind. Matview refreshes are last because they should execute in the
33+
* database's normal state (e.g., they must come after all ACLs are restored;
34+
* also, if they choose to look at system catalogs, they should see the final
35+
* restore state). If you think to change this, see also the RestorePass
36+
* mechanism in pg_backup_archiver.c.
3037
*
3138
* NOTE: object-type priorities must match the section assignments made in
3239
* pg_dump.c; that is, PRE_DATA objects must sort before DO_PRE_DATA_BOUNDARY,
@@ -67,18 +74,18 @@ static const int dbObjectTypePriority[] =
6774
15, /* DO_TSCONFIG */
6875
16, /* DO_FDW */
6976
17, /* DO_FOREIGN_SERVER */
70-
33, /* DO_DEFAULT_ACL */
77+
38, /* DO_DEFAULT_ACL --- done in ACL pass */
7178
3, /* DO_TRANSFORM */
7279
21, /* DO_BLOB */
7380
25, /* DO_BLOB_DATA */
7481
22, /* DO_PRE_DATA_BOUNDARY */
7582
26, /* DO_POST_DATA_BOUNDARY */
76-
34, /* DO_EVENT_TRIGGER */
77-
39, /* DO_REFRESH_MATVIEW */
78-
35, /* DO_POLICY */
79-
36, /* DO_PUBLICATION */
80-
37, /* DO_PUBLICATION_REL */
81-
38 /* DO_SUBSCRIPTION */
83+
39, /* DO_EVENT_TRIGGER --- next to last! */
84+
40, /* DO_REFRESH_MATVIEW --- last! */
85+
34, /* DO_POLICY */
86+
35, /* DO_PUBLICATION */
87+
36, /* DO_PUBLICATION_REL */
88+
37 /* DO_SUBSCRIPTION */
8289
};
8390

8491
static DumpId preDataBoundId;

0 commit comments

Comments
 (0)