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

Commitc2a14bc

Browse files
committed
Protect against SnapshotNow race conditions in pg_tablespace scans.
Use of SnapshotNow is known to expose us to race conditions if the tuple(s)being sought could be updated by concurrently-committing transactions.CREATE DATABASE and DROP DATABASE are particularly exposed because they doheavyweight filesystem operations during their scans of pg_tablespace,so that the scans run for a very long time compared to most. Furthermore,the potential consequences of a missed or twice-visited row are nastierthan average:* createdb() could fail with a bogus "file already exists" error, or silently fail to copy one or more tablespace's worth of files into the new database.* remove_dbtablespaces() could miss one or more tablespaces, thus failing to free filesystem space for the dropped database.* check_db_file_conflict() could likewise miss a tablespace, leading to an OID conflict that could result in data loss either immediately or in future operations. (This seems of very low probability, though, since a duplicate database OID would be unlikely to start with.)Hence, it seems worth fixing these three places to use MVCC snapshots, eventhough this will someday be superseded by a generic solution to SnapshotNowrace conditions.Back-patch to all active branches.Stephen Frost and Tom Lane
1 parent530bbfa commitc2a14bc

File tree

1 file changed

+54
-3
lines changed

1 file changed

+54
-3
lines changed

‎src/backend/commands/dbcommands.c

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ createdb(const CreatedbStmt *stmt)
131131
intnotherbackends;
132132
intnpreparedxacts;
133133
createdb_failure_paramsfparms;
134+
Snapshotsnapshot;
134135

135136
/* Extract options from the statement node tree */
136137
foreach(option,stmt->options)
@@ -534,6 +535,29 @@ createdb(const CreatedbStmt *stmt)
534535
*/
535536
RequestCheckpoint(CHECKPOINT_IMMEDIATE |CHECKPOINT_FORCE |CHECKPOINT_WAIT);
536537

538+
/*
539+
* Take an MVCC snapshot to use while scanning through pg_tablespace. For
540+
* safety, register the snapshot (this prevents it from changing if
541+
* something else were to request a snapshot during the loop).
542+
*
543+
* Traversing pg_tablespace with an MVCC snapshot is necessary to provide
544+
* us with a consistent view of the tablespaces that exist. Using
545+
* SnapshotNow here would risk seeing the same tablespace multiple times,
546+
* or worse not seeing a tablespace at all, if its tuple is moved around
547+
* by a concurrent update (eg an ACL change).
548+
*
549+
* Inconsistency of this sort is inherent to all SnapshotNow scans, unless
550+
* some lock is held to prevent concurrent updates of the rows being
551+
* sought.There should be a generic fix for that, but in the meantime
552+
* it's worth fixing this case in particular because we are doing very
553+
* heavyweight operations within the scan, so that the elapsed time for
554+
* the scan is vastly longer than for most other catalog scans. That
555+
* means there's a much wider window for concurrent updates to cause
556+
* trouble here than anywhere else. XXX this code should be changed
557+
* whenever a generic fix is implemented.
558+
*/
559+
snapshot=RegisterSnapshot(GetLatestSnapshot());
560+
537561
/*
538562
* Once we start copying subdirectories, we need to be able to clean 'em
539563
* up if we fail. Use an ENSURE block to make sure this happens. (This
@@ -551,7 +575,7 @@ createdb(const CreatedbStmt *stmt)
551575
* each one to the new database.
552576
*/
553577
rel=heap_open(TableSpaceRelationId,AccessShareLock);
554-
scan=heap_beginscan(rel,SnapshotNow,0,NULL);
578+
scan=heap_beginscan(rel,snapshot,0,NULL);
555579
while ((tuple=heap_getnext(scan,ForwardScanDirection))!=NULL)
556580
{
557581
Oidsrctablespace=HeapTupleGetOid(tuple);
@@ -656,6 +680,9 @@ createdb(const CreatedbStmt *stmt)
656680
PG_END_ENSURE_ERROR_CLEANUP(createdb_failure_callback,
657681
PointerGetDatum(&fparms));
658682

683+
/* Free our snapshot */
684+
UnregisterSnapshot(snapshot);
685+
659686
returndboid;
660687
}
661688

@@ -1715,9 +1742,20 @@ remove_dbtablespaces(Oid db_id)
17151742
Relationrel;
17161743
HeapScanDescscan;
17171744
HeapTupletuple;
1745+
Snapshotsnapshot;
1746+
1747+
/*
1748+
* As in createdb(), we'd better use an MVCC snapshot here, since this
1749+
* scan can run for a long time. Duplicate visits to tablespaces would be
1750+
* harmless, but missing a tablespace could result in permanently leaked
1751+
* files.
1752+
*
1753+
* XXX change this when a generic fix for SnapshotNow races is implemented
1754+
*/
1755+
snapshot=RegisterSnapshot(GetLatestSnapshot());
17181756

17191757
rel=heap_open(TableSpaceRelationId,AccessShareLock);
1720-
scan=heap_beginscan(rel,SnapshotNow,0,NULL);
1758+
scan=heap_beginscan(rel,snapshot,0,NULL);
17211759
while ((tuple=heap_getnext(scan,ForwardScanDirection))!=NULL)
17221760
{
17231761
Oiddsttablespace=HeapTupleGetOid(tuple);
@@ -1763,6 +1801,7 @@ remove_dbtablespaces(Oid db_id)
17631801

17641802
heap_endscan(scan);
17651803
heap_close(rel,AccessShareLock);
1804+
UnregisterSnapshot(snapshot);
17661805
}
17671806

17681807
/*
@@ -1784,9 +1823,19 @@ check_db_file_conflict(Oid db_id)
17841823
Relationrel;
17851824
HeapScanDescscan;
17861825
HeapTupletuple;
1826+
Snapshotsnapshot;
1827+
1828+
/*
1829+
* As in createdb(), we'd better use an MVCC snapshot here; missing a
1830+
* tablespace could result in falsely reporting the OID is unique, with
1831+
* disastrous future consequences per the comment above.
1832+
*
1833+
* XXX change this when a generic fix for SnapshotNow races is implemented
1834+
*/
1835+
snapshot=RegisterSnapshot(GetLatestSnapshot());
17871836

17881837
rel=heap_open(TableSpaceRelationId,AccessShareLock);
1789-
scan=heap_beginscan(rel,SnapshotNow,0,NULL);
1838+
scan=heap_beginscan(rel,snapshot,0,NULL);
17901839
while ((tuple=heap_getnext(scan,ForwardScanDirection))!=NULL)
17911840
{
17921841
Oiddsttablespace=HeapTupleGetOid(tuple);
@@ -1812,6 +1861,8 @@ check_db_file_conflict(Oid db_id)
18121861

18131862
heap_endscan(scan);
18141863
heap_close(rel,AccessShareLock);
1864+
UnregisterSnapshot(snapshot);
1865+
18151866
returnresult;
18161867
}
18171868

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp