Skip to content

Commit a29834b

Browse files
committed
Allow table AM's to use rd_amcache, too.
The rd_amcache allows an index AM to cache arbitrary information in a relcache entry. This commit moves the cleanup of rd_amcache so that it can also be used by table AMs. Nothing takes advantage of that yet, but I'm sure it'll come handy for anyone writing new table AMs. Backpatch to v12, where table AM interface was introduced. Reviewed-by: Julien Rouhaud
1 parent d8b094d commit a29834b

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/backend/utils/cache/relcache.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,10 @@ RelationDestroyRelation(Relation relation, bool remember_tupdesc)
23572357
pfree(relation->rd_options);
23582358
if (relation->rd_indextuple)
23592359
pfree(relation->rd_indextuple);
2360+
if (relation->rd_amcache)
2361+
pfree(relation->rd_amcache);
2362+
if (relation->rd_fdwroutine)
2363+
pfree(relation->rd_fdwroutine);
23602364
if (relation->rd_indexcxt)
23612365
MemoryContextDelete(relation->rd_indexcxt);
23622366
if (relation->rd_rulescxt)
@@ -2369,8 +2373,6 @@ RelationDestroyRelation(Relation relation, bool remember_tupdesc)
23692373
MemoryContextDelete(relation->rd_pdcxt);
23702374
if (relation->rd_partcheckcxt)
23712375
MemoryContextDelete(relation->rd_partcheckcxt);
2372-
if (relation->rd_fdwroutine)
2373-
pfree(relation->rd_fdwroutine);
23742376
pfree(relation);
23752377
}
23762378

@@ -2415,6 +2417,11 @@ RelationClearRelation(Relation relation, bool rebuild)
24152417
*/
24162418
RelationCloseSmgr(relation);
24172419

2420+
/* Free AM cached data, if any */
2421+
if (relation->rd_amcache)
2422+
pfree(relation->rd_amcache);
2423+
relation->rd_amcache = NULL;
2424+
24182425
/*
24192426
* Treat nailed-in system relations separately, they always need to be
24202427
* accessible, so we can't blow them away.

src/include/utils/rel.h

+11-8
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,6 @@ typedef struct RelationData
152152
* those with lefttype and righttype equal to the opclass's opcintype. The
153153
* arrays are indexed by support function number, which is a sufficient
154154
* identifier given that restriction.
155-
*
156-
* Note: rd_amcache is available for index AMs to cache private data about
157-
* an index. This must be just a cache since it may get reset at any time
158-
* (in particular, it will get reset by a relcache inval message for the
159-
* index). If used, it must point to a single memory chunk palloc'd in
160-
* rd_indexcxt. A relcache reset will include freeing that chunk and
161-
* setting rd_amcache = NULL.
162155
*/
163156
MemoryContext rd_indexcxt; /* private memory cxt for this stuff */
164157
/* use "struct" here to avoid needing to include amapi.h: */
@@ -173,9 +166,19 @@ typedef struct RelationData
173166
Oid *rd_exclops; /* OIDs of exclusion operators, if any */
174167
Oid *rd_exclprocs; /* OIDs of exclusion ops' procs, if any */
175168
uint16 *rd_exclstrats; /* exclusion ops' strategy numbers, if any */
176-
void *rd_amcache; /* available for use by index AM */
177169
Oid *rd_indcollation; /* OIDs of index collations */
178170

171+
/*
172+
* rd_amcache is available for index and table AMs to cache private data
173+
* about the relation. This must be just a cache since it may get reset
174+
* at any time (in particular, it will get reset by a relcache inval
175+
* message for the relation). If used, it must point to a single memory
176+
* chunk palloc'd in CacheMemoryContext, or in rd_indexcxt for an index
177+
* relation. A relcache reset will include freeing that chunk and setting
178+
* rd_amcache = NULL.
179+
*/
180+
void *rd_amcache; /* available for use by index/table AM */
181+
179182
/*
180183
* foreign-table support
181184
*

0 commit comments

Comments
 (0)