Allow table AM's to use rd_amcache, too.
authorHeikki Linnakangas
Tue, 30 Jul 2019 18:43:27 +0000 (21:43 +0300)
committerHeikki Linnakangas
Tue, 30 Jul 2019 18:43:27 +0000 (21:43 +0300)
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
src/backend/utils/cache/relcache.c
src/include/utils/rel.h

index 7aa5d7c7fa8520391dcca495aa0f5a708dca0b38..248860758ca21cfddd08f600aab284ec1b1e1766 100644 (file)
@@ -2357,6 +2357,10 @@ RelationDestroyRelation(Relation relation, bool remember_tupdesc)
        pfree(relation->rd_options);
    if (relation->rd_indextuple)
        pfree(relation->rd_indextuple);
+   if (relation->rd_amcache)
+       pfree(relation->rd_amcache);
+   if (relation->rd_fdwroutine)
+       pfree(relation->rd_fdwroutine);
    if (relation->rd_indexcxt)
        MemoryContextDelete(relation->rd_indexcxt);
    if (relation->rd_rulescxt)
@@ -2369,8 +2373,6 @@ RelationDestroyRelation(Relation relation, bool remember_tupdesc)
        MemoryContextDelete(relation->rd_pdcxt);
    if (relation->rd_partcheckcxt)
        MemoryContextDelete(relation->rd_partcheckcxt);
-   if (relation->rd_fdwroutine)
-       pfree(relation->rd_fdwroutine);
    pfree(relation);
 }
 
@@ -2415,6 +2417,11 @@ RelationClearRelation(Relation relation, bool rebuild)
     */
    RelationCloseSmgr(relation);
 
+   /* Free AM cached data, if any */
+   if (relation->rd_amcache)
+       pfree(relation->rd_amcache);
+   relation->rd_amcache = NULL;
+
    /*
     * Treat nailed-in system relations separately, they always need to be
     * accessible, so we can't blow them away.
index d35b4a5061c179fcdf8faae3df5fd4083cfb909a..b0fe19ebc549fd8de24f085d9470d0418ab39c8d 100644 (file)
@@ -152,13 +152,6 @@ typedef struct RelationData
     * those with lefttype and righttype equal to the opclass's opcintype. The
     * arrays are indexed by support function number, which is a sufficient
     * identifier given that restriction.
-    *
-    * Note: rd_amcache is available for index AMs to cache private data about
-    * an index.  This must be just a cache since it may get reset at any time
-    * (in particular, it will get reset by a relcache inval message for the
-    * index).  If used, it must point to a single memory chunk palloc'd in
-    * rd_indexcxt.  A relcache reset will include freeing that chunk and
-    * setting rd_amcache = NULL.
     */
    MemoryContext rd_indexcxt;  /* private memory cxt for this stuff */
    /* use "struct" here to avoid needing to include amapi.h: */
@@ -173,9 +166,19 @@ typedef struct RelationData
    Oid        *rd_exclops;     /* OIDs of exclusion operators, if any */
    Oid        *rd_exclprocs;   /* OIDs of exclusion ops' procs, if any */
    uint16     *rd_exclstrats;  /* exclusion ops' strategy numbers, if any */
-   void       *rd_amcache;     /* available for use by index AM */
    Oid        *rd_indcollation;    /* OIDs of index collations */
 
+   /*
+    * rd_amcache is available for index and table AMs to cache private data
+    * about the relation.  This must be just a cache since it may get reset
+    * at any time (in particular, it will get reset by a relcache inval
+    * message for the relation).  If used, it must point to a single memory
+    * chunk palloc'd in CacheMemoryContext, or in rd_indexcxt for an index
+    * relation.  A relcache reset will include freeing that chunk and setting
+    * rd_amcache = NULL.
+    */
+   void       *rd_amcache;     /* available for use by index/table AM */
+
    /*
     * foreign-table support
     *