Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf5d49f2

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 riskysince an event trigger, once installed, could interfere with subsequentrestore commands. Worse, because event triggers don't have anyparticular dependencies on any post-data objects, a parallel restorewould consider them eligible to be restored the moment the post-dataphase starts, allowing them to also interfere with restoration of awhole bunch of objects that would have been restored before them ina serial restore. There's no way to completely remove the risk of amisguided event trigger breaking the restore, since if nothing elseit could break other event triggers. But we can certainly push themto later in the process to minimize the hazard.To fix, tweak the RestorePass mechanism introduced by commit3eb9a5eso that event triggers are handled as part of the post-ACL processingpass (renaming the "REFRESH" pass to "POST_ACL" to reflect its moregeneral use). This will cause them to restore after everything exceptmatview refreshes, which seems OK since matview refreshes really oughtto run in the post-restore state of the database. In a parallelrestore, 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 ideaof how things are sorted agrees with what actually happens due tothe RestorePass mechanism. This is mostly cosmetic: it'll affect theorder of objects in a dump's TOC, but not the actual restore order.But not changing that would be quite confusing to somebody readingthe code.Back-patch to all supported branches.Fabrízio de Royes Mello, tweaked a bit by meDiscussion:https://postgr.es/m/CAFcNs+ow1hmFox8P--3GSdtwz-S3Binb6ZmoP6Vk+Xg=K6eZNA@mail.gmail.com
1 parente54183c commitf5d49f2

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
@@ -658,11 +658,11 @@ RestoreArchive(Archive *AHX)
658658
{
659659
/*
660660
* In serial mode, process everything in three phases: normal items,
661-
* then ACLs, thenmatview refreshitems. We might be able to skip
662-
*one orboth extra phases in some cases, eg data-only restores.
661+
* then ACLs, thenpost-ACLitems. We might be able to skip one or
662+
* both extra phases in some cases, eg data-only restores.
663663
*/
664664
boolhaveACL= false;
665-
boolhaveRefresh= false;
665+
boolhavePostACL= false;
666666

667667
for (te=AH->toc->next;te!=AH->toc;te=te->next)
668668
{
@@ -677,8 +677,8 @@ RestoreArchive(Archive *AHX)
677677
caseRESTORE_PASS_ACL:
678678
haveACL= true;
679679
break;
680-
caseRESTORE_PASS_REFRESH:
681-
haveRefresh= true;
680+
caseRESTORE_PASS_POST_ACL:
681+
havePostACL= true;
682682
break;
683683
}
684684
}
@@ -693,12 +693,12 @@ RestoreArchive(Archive *AHX)
693693
}
694694
}
695695

696-
if (haveRefresh)
696+
if (havePostACL)
697697
{
698698
for (te=AH->toc->next;te!=AH->toc;te=te->next)
699699
{
700700
if ((te->reqs& (REQ_SCHEMA |REQ_DATA))!=0&&
701-
_tocEntryRestorePass(te)==RESTORE_PASS_REFRESH)
701+
_tocEntryRestorePass(te)==RESTORE_PASS_POST_ACL)
702702
(void)restore_toc_entry(AH,te, false);
703703
}
704704
}
@@ -3089,8 +3089,9 @@ _tocEntryRestorePass(TocEntry *te)
30893089
strcmp(te->desc,"ACL LANGUAGE")==0||
30903090
strcmp(te->desc,"DEFAULT ACL")==0)
30913091
returnRESTORE_PASS_ACL;
3092-
if (strcmp(te->desc,"MATERIALIZED VIEW DATA")==0)
3093-
returnRESTORE_PASS_REFRESH;
3092+
if (strcmp(te->desc,"EVENT TRIGGER")==0||
3093+
strcmp(te->desc,"MATERIALIZED VIEW DATA")==0)
3094+
returnRESTORE_PASS_POST_ACL;
30943095
returnRESTORE_PASS_MAIN;
30953096
}
30963097

‎src/bin/pg_dump/pg_backup_archiver.h

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

229-
#defineRESTORE_PASS_LASTRESTORE_PASS_REFRESH
233+
#defineRESTORE_PASS_LASTRESTORE_PASS_POST_ACL
230234
}RestorePass;
231235

232236
typedefenum

‎src/bin/pg_dump/pg_dump_sort.c

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

9198
staticDumpIdpreDataBoundId;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp