Skip to content

Commit 2a2e2e8

Browse files
committed
Ignore CatalogSnapshot when checking COPY FREEZE prerequisites.
This restores the ability, essentially lost in commit ffaa44c, to use COPY FREEZE under REPEATABLE READ isolation. Back-patch to 9.4, like that commit. Reviewed by Tom Lane. Discussion: https://postgr.es/m/CA+TgmoahWDm-7fperBxzU9uZ99LPMUmEpSXLTw9TmrOgzwnORw@mail.gmail.com
1 parent 946f165 commit 2a2e2e8

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/backend/commands/copy.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,13 +2167,25 @@ CopyFrom(CopyState cstate)
21672167
/*
21682168
* Optimize if new relfilenode was created in this subxact or one of its
21692169
* committed children and we won't see those rows later as part of an
2170-
* earlier scan or command. This ensures that if this subtransaction
2171-
* aborts then the frozen rows won't be visible after xact cleanup. Note
2170+
* earlier scan or command. The subxact test ensures that if this subxact
2171+
* aborts then the frozen rows won't be visible after xact cleanup. Note
21722172
* that the stronger test of exactly which subtransaction created it is
2173-
* crucial for correctness of this optimisation.
2173+
* crucial for correctness of this optimisation. The test for an earlier
2174+
* scan or command tolerates false negatives. FREEZE causes other sessions
2175+
* to see rows they would not see under MVCC, and a false negative merely
2176+
* spreads that anomaly to the current session.
21742177
*/
21752178
if (cstate->freeze)
21762179
{
2180+
/*
2181+
* Tolerate one registration for the benefit of FirstXactSnapshot.
2182+
* Scan-bearing queries generally create at least two registrations,
2183+
* though relying on that is fragile, as is ignoring ActiveSnapshot.
2184+
* Clear CatalogSnapshot to avoid counting its registration. We'll
2185+
* still detect ongoing catalog scans, each of which separately
2186+
* registers the snapshot it uses.
2187+
*/
2188+
InvalidateCatalogSnapshot();
21772189
if (!ThereAreNoPriorRegisteredSnapshots() || !ThereAreNoReadyPortals())
21782190
ereport(ERROR,
21792191
(ERRCODE_INVALID_TRANSACTION_STATE,

src/backend/utils/time/snapmgr.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,14 @@ DeleteAllExportedSnapshotFiles(void)
13431343
FreeDir(s_dir);
13441344
}
13451345

1346+
/*
1347+
* ThereAreNoPriorRegisteredSnapshots
1348+
* Is the registered snapshot count less than or equal to one?
1349+
*
1350+
* Don't use this to settle important decisions. While zero registrations and
1351+
* no ActiveSnapshot would confirm a certain idleness, the system makes no
1352+
* guarantees about the significance of one registered snapshot.
1353+
*/
13461354
bool
13471355
ThereAreNoPriorRegisteredSnapshots(void)
13481356
{

0 commit comments

Comments
 (0)