8000 Fix race condition in enum value comparisons. · lhcezar/postgres@af4f029 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit af4f029

Browse files
committed
Fix race condition in enum value comparisons.
When (re) loading the typcache comparison cache for an enum type's values, use an up-to-date MVCC snapshot, not the transaction's existing snapshot. This avoids problems if we encounter an enum OID that was created since our transaction started. Per report from Andres Freund and diagnosis by Robert Haas. To ensure this is safe even if enum comparison manages to get invoked before we've set a transaction snapshot, tweak GetLatestSnapshot to redirect to GetTransactionSnapshot instead of throwing error when FirstSnapshotSet is false. The existing uses of GetLatestSnapshot (in ri_triggers.c) don't care since they couldn't be invoked except in a transaction that's already done some work --- but it seems just conceivable that this might not be true of enums, especially if we ever choose to use enums in system catalogs. Note that the comparable coding in enum_endpoint and enum_range_internal remains GetTransactionSnapshot; this is perhaps debatable, but if we changed it those functions would have to be marked volatile, which doesn't seem attractive. Back-patch to 9.1 where ALTER TYPE ADD VALUE was added.
1 parent 81aa961 commit af4f029

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

src/backend/utils/cache/typcache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ load_enum_cache_data(TypeCacheEntry *tcache)
10211021
enum_rel = heap_open(EnumRelationId, AccessShareLock);
10221022
enum_scan = systable_beginscan(enum_rel,
10231023
EnumTypIdLabelIndexId,
1024-
true, GetTransactionSnapshot(),
1024+
true, GetLatestSnapshot(),
10251025
1, &skey);
10261026

10271027
while (HeapTupleIsValid(enum_tuple = systable_getnext(enum_scan)))

src/backend/utils/time/snapmgr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ GetTransactionSnapshot(void)
167167
Snapshot
168168
GetLatestSnapshot(void)
169169
{
170-
/* Should not be first call in transaction */
170+
/* If first call in transaction, go ahead and set the xact snapshot */
171171
if (!FirstSnapshotSet)
172-
elog(ERROR, "no snapshot has been set");
172+
return GetTransactionSnapshot();
173173

174174
SecondarySnapshot = GetSnapshotData(&SecondarySnapshotData);
175175

0 commit comments

Comments
 (0)
0