Skip to content

Commit f9f47f0

Browse files
committed
Cope with inplace update making catcache stale during TOAST fetch.
This extends ad98fb1 to invals of inplace updates. Trouble requires an inplace update of a catalog having a TOAST table, so only pg_database was at risk. (The other catalog on which core code performs inplace updates, pg_class, has no TOAST table.) Trouble would require something like the inplace-inval.spec test. Consider GRANT ... ON DATABASE fetching a stale row from cache and discarding a datfrozenxid update that vac_truncate_clog() has already relied upon. Back-patch to v12 (all supported versions). Reviewed (in an earlier version) by Robert Haas. Discussion: https://postgr.es/m/20240114201411.d0@rfd.leadboat.com Discussion: https://postgr.es/m/20240512232923.aa.nmisch@google.com
1 parent 5b823b1 commit f9f47f0

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

src/backend/catalog/catalog.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,27 @@ IsCatalogRelationOid(Oid relid)
135135
return (relid < (Oid) FirstUnpinnedObjectId);
136136
}
137137

138+
/*
139+
* IsInplaceUpdateRelation
140+
* True iff core code performs inplace updates on the relation.
141+
*/
142+
bool
143+
IsInplaceUpdateRelation(Relation relation)
144+
{
145+
return IsInplaceUpdateOid(RelationGetRelid(relation));
146+
}
147+
148+
/*
149+
* IsInplaceUpdateOid
150+
* Like the above, but takes an OID as argument.
151+
*/
152+
bool
153+
IsInplaceUpdateOid(Oid relid)
154+
{
155+
return (relid == RelationRelationId ||
156+
relid == DatabaseRelationId);
157+
}
158+
138159
/*
139160
* IsToastRelation
140161
* True iff relation is a TOAST support relation (or index).

src/backend/utils/cache/catcache.c

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "access/relscan.h"
2020
#include "access/table.h"
2121
#include "access/xact.h"
22+
#include "catalog/catalog.h"
2223
#include "catalog/pg_collation.h"
2324
#include "catalog/pg_type.h"
2425
#include "common/hashfn.h"
@@ -2007,6 +2008,23 @@ ReleaseCatCacheListWithOwner(CatCList *list, ResourceOwner resowner)
20072008
}
20082009

20092010

2011+
/*
2012+
* equalTuple
2013+
* Are these tuples memcmp()-equal?
2014+
*/
2015+
static bool
2016+
equalTuple(HeapTuple a, HeapTuple b)
2017+
{
2018+
uint32 alen;
2019+
uint32 blen;
2020+
2021+
alen = a->t_len;
2022+
blen = b->t_len;
2023+
return (alen == blen &&
2024+
memcmp((char *) a->t_data,
2025+
(char *) b->t_data, blen) == 0);
2026+
}
2027+
20102028
/*
20112029
* CatalogCacheCreateEntry
20122030
* Create a new CatCTup entry, copying the given HeapTuple and other
@@ -2057,14 +2075,34 @@ CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp, SysScanDesc scandesc,
20572075
*/
20582076
if (HeapTupleHasExternal(ntp))
20592077
{
2078+
bool need_cmp = IsInplaceUpdateOid(cache->cc_reloid);
2079+
HeapTuple before = NULL;
2080+
bool matches = true;
2081+
2082+
if (need_cmp)
2083+
before = heap_copytuple(ntp);
20602084
dtp = toast_flatten_tuple(ntp, cache->cc_tupdesc);
20612085

20622086
/*
20632087
* The tuple could become stale while we are doing toast table
2064-
* access (since AcceptInvalidationMessages can run then), so we
2065-
* must recheck its visibility afterwards.
2088+
* access (since AcceptInvalidationMessages can run then).
2089+
* equalTuple() detects staleness from inplace updates, while
2090+
* systable_recheck_tuple() detects staleness from normal updates.
2091+
*
2092+
* While this equalTuple() follows the usual rule of reading with
2093+
* a pin and no buffer lock, it warrants suspicion since an
2094+
* inplace update could appear at any moment. It's safe because
2095+
* the inplace update sends an invalidation that can't reorder
2096+
* before the inplace heap change. If the heap change reaches
2097+
* this process just after equalTuple() looks, we've not missed
2098+
* its inval.
20662099
*/
2067-
if (!systable_recheck_tuple(scandesc, ntp))
2100+
if (need_cmp)
2101+
{
2102+
matches = equalTuple(before, ntp);
2103+
heap_freetuple(before);
2104+
}
2105+
if (!matches || !systable_recheck_tuple(scandesc, ntp))
20682106
{
20692107
heap_freetuple(dtp);
20702108
return NULL;

src/include/catalog/catalog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
extern bool IsSystemRelation(Relation relation);
2222
extern bool IsToastRelation(Relation relation);
2323
extern bool IsCatalogRelation(Relation relation);
24+
extern bool IsInplaceUpdateRelation(Relation relation);
2425

2526
extern bool IsSystemClass(Oid relid, Form_pg_class reltuple);
2627
extern bool IsToastClass(Form_pg_class reltuple);
2728

2829
extern bool IsCatalogRelationOid(Oid relid);
30+
extern bool IsInplaceUpdateOid(Oid relid);
2931

3032
extern bool IsCatalogNamespace(Oid namespaceId);
3133
extern bool IsToastNamespace(Oid namespaceId);

0 commit comments

Comments
 (0)