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

Commit7a21306

Browse files
committed
Cope with inplace update making catcache stale during TOAST fetch.
This extendsad98fb1 to invals ofinplace updates. Trouble requires an inplace update of a catalog havinga TOAST table, so only pg_database was at risk. (The other catalog onwhich 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 anddiscarding a datfrozenxid update that vac_truncate_clog() has alreadyrelied 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.comDiscussion:https://postgr.es/m/20240512232923.aa.nmisch@google.com
1 parentb899cde commit7a21306

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
@@ -130,6 +130,27 @@ IsCatalogRelationOid(Oid relid)
130130
return (relid< (Oid)FirstBootstrapObjectId);
131131
}
132132

133+
/*
134+
* IsInplaceUpdateRelation
135+
*True iff core code performs inplace updates on the relation.
136+
*/
137+
bool
138+
IsInplaceUpdateRelation(Relationrelation)
139+
{
140+
returnIsInplaceUpdateOid(RelationGetRelid(relation));
141+
}
142+
143+
/*
144+
* IsInplaceUpdateOid
145+
*Like the above, but takes an OID as argument.
146+
*/
147+
bool
148+
IsInplaceUpdateOid(Oidrelid)
149+
{
150+
return (relid==RelationRelationId||
151+
relid==DatabaseRelationId);
152+
}
153+
133154
/*
134155
* IsToastRelation
135156
*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
@@ -21,6 +21,7 @@
2121
#include"access/table.h"
2222
#include"access/valid.h"
2323
#include"access/xact.h"
24+
#include"catalog/catalog.h"
2425
#include"catalog/pg_collation.h"
2526
#include"catalog/pg_operator.h"
2627
#include"catalog/pg_type.h"
@@ -1847,6 +1848,23 @@ ReleaseCatCacheList(CatCList *list)
18471848
}
18481849

18491850

1851+
/*
1852+
* equalTuple
1853+
*Are these tuples memcmp()-equal?
1854+
*/
1855+
staticbool
1856+
equalTuple(HeapTuplea,HeapTupleb)
1857+
{
1858+
uint32alen;
1859+
uint32blen;
1860+
1861+
alen=a->t_len;
1862+
blen=b->t_len;
1863+
return (alen==blen&&
1864+
memcmp((char*)a->t_data,
1865+
(char*)b->t_data,blen)==0);
1866+
}
1867+
18501868
/*
18511869
* CatalogCacheCreateEntry
18521870
*Create a new CatCTup entry, copying the given HeapTuple and other
@@ -1897,14 +1915,34 @@ CatalogCacheCreateEntry(CatCache *cache, HeapTuple ntp, SysScanDesc scandesc,
18971915
*/
18981916
if (HeapTupleHasExternal(ntp))
18991917
{
1918+
boolneed_cmp=IsInplaceUpdateOid(cache->cc_reloid);
1919+
HeapTuplebefore=NULL;
1920+
boolmatches= true;
1921+
1922+
if (need_cmp)
1923+
before=heap_copytuple(ntp);
19001924
dtp=toast_flatten_tuple(ntp,cache->cc_tupdesc);
19011925

19021926
/*
19031927
* The tuple could become stale while we are doing toast table
1904-
* access (since AcceptInvalidationMessages can run then), so we
1905-
* must recheck its visibility afterwards.
1928+
* access (since AcceptInvalidationMessages can run then).
1929+
* equalTuple() detects staleness from inplace updates, while
1930+
* systable_recheck_tuple() detects staleness from normal updates.
1931+
*
1932+
* While this equalTuple() follows the usual rule of reading with
1933+
* a pin and no buffer lock, it warrants suspicion since an
1934+
* inplace update could appear at any moment. It's safe because
1935+
* the inplace update sends an invalidation that can't reorder
1936+
* before the inplace heap change. If the heap change reaches
1937+
* this process just after equalTuple() looks, we've not missed
1938+
* its inval.
19061939
*/
1907-
if (!systable_recheck_tuple(scandesc,ntp))
1940+
if (need_cmp)
1941+
{
1942+
matches=equalTuple(before,ntp);
1943+
heap_freetuple(before);
1944+
}
1945+
if (!matches|| !systable_recheck_tuple(scandesc,ntp))
19081946
{
19091947
heap_freetuple(dtp);
19101948
returnNULL;

‎src/include/catalog/catalog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
externboolIsSystemRelation(Relationrelation);
2222
externboolIsToastRelation(Relationrelation);
2323
externboolIsCatalogRelation(Relationrelation);
24+
externboolIsInplaceUpdateRelation(Relationrelation);
2425

2526
externboolIsSystemClass(Oidrelid,Form_pg_classreltuple);
2627
externboolIsToastClass(Form_pg_classreltuple);
2728

2829
externboolIsCatalogRelationOid(Oidrelid);
30+
externboolIsInplaceUpdateOid(Oidrelid);
2931

3032
externboolIsCatalogNamespace(OidnamespaceId);
3133
externboolIsToastNamespace(OidnamespaceId);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp