Add macro RelationIsPermanent() to report relation permanence
authorBruce Momjian <bruce@momjian.us>
Tue, 23 Mar 2021 00:22:48 +0000 (20:22 -0400)
committerBruce Momjian <bruce@momjian.us>
Tue, 23 Mar 2021 00:23:52 +0000 (20:23 -0400)
Previously, to check relation permanence, the Relation's Form_pg_class
structure member relpersistence was compared to the value
RELPERSISTENCE_PERMANENT ("p"). This commit adds the macro
RelationIsPermanent() and is used in appropirate places to simplify the
code.  This matches other RelationIs* macros.

This macro will be used in more places in future cluster file encryption
patches.

Discussion: https://postgr.es/m/20210318153134.GH20766@tamriel.snowman.net

src/backend/access/gist/gistutil.c
src/backend/access/heap/heapam_handler.c
src/backend/catalog/pg_publication.c
src/backend/commands/tablecmds.c
src/backend/optimizer/util/plancat.c
src/backend/utils/cache/relcache.c
src/include/utils/rel.h
src/include/utils/snapmgr.h

index a3ec9f2cfe4249d7e80ffcbc91d840a2f316b9bb..1ff1bf816f1b45f6e342a83b971e6726f9a94529 100644 (file)
@@ -1036,7 +1036,7 @@ gistGetFakeLSN(Relation rel)
 
                return counter++;
        }
-       else if (rel->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT)
+       else if (RelationIsPermanent(rel))
        {
                /*
                 * WAL-logging on this relation will start after commit, so its LSNs
index 7b475f2950ce86d5a012f6a370f2469d3950735c..7a9a640989ab64ae036ab3e721bdbb41eb928cc6 100644 (file)
@@ -662,7 +662,7 @@ heapam_relation_copy_data(Relation rel, const RelFileNode *newrnode)
                         * WAL log creation if the relation is persistent, or this is the
                         * init fork of an unlogged relation.
                         */
-                       if (rel->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT ||
+                       if (RelationIsPermanent(rel) ||
                                (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
                                 forkNum == INIT_FORKNUM))
                                log_smgrcreate(newrnode, forkNum);
index 84d2efcfd2f1624c25574daa80c9ffc52558894f..86e415af8923f6cbb1e3487bf63ac11def1915a4 100644 (file)
@@ -67,7 +67,7 @@ check_publication_add_relation(Relation targetrel)
                                 errdetail("System tables cannot be added to publications.")));
 
        /* UNLOGGED and TEMP relations cannot be part of publication. */
-       if (targetrel->rd_rel->relpersistence != RELPERSISTENCE_PERMANENT)
+       if (!RelationIsPermanent(targetrel))
                ereport(ERROR,
                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                 errmsg("table \"%s\" cannot be replicated",
index 54fea31e43fd6700ea639cd7e4683984f6a7ed95..3349bcfaa747aa5ab6b18a79cf5d05655b51f8f3 100644 (file)
@@ -8650,13 +8650,13 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
        switch (rel->rd_rel->relpersistence)
        {
                case RELPERSISTENCE_PERMANENT:
-                       if (pkrel->rd_rel->relpersistence != RELPERSISTENCE_PERMANENT)
+                       if (!RelationIsPermanent(pkrel))
                                ereport(ERROR,
                                                (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                                                 errmsg("constraints on permanent tables may reference only permanent tables")));
                        break;
                case RELPERSISTENCE_UNLOGGED:
-                       if (pkrel->rd_rel->relpersistence != RELPERSISTENCE_PERMANENT
+                       if (!RelationIsPermanent(pkrel)
                                && pkrel->rd_rel->relpersistence != RELPERSISTENCE_UNLOGGED)
                                ereport(ERROR,
                                                (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
@@ -13712,7 +13712,7 @@ index_copy_data(Relation rel, RelFileNode newrnode)
                         * WAL log creation if the relation is persistent, or this is the
                         * init fork of an unlogged relation.
                         */
-                       if (rel->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT ||
+                       if (RelationIsPermanent(rel) ||
                                (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
                                 forkNum == INIT_FORKNUM))
                                log_smgrcreate(&newrnode, forkNum);
@@ -15230,7 +15230,7 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
 
                        if (toLogged)
                        {
-                               if (foreignrel->rd_rel->relpersistence != RELPERSISTENCE_PERMANENT)
+                               if (!RelationIsPermanent(foreignrel))
                                        ereport(ERROR,
                                                        (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                                                         errmsg("could not change table \"%s\" to logged because it references unlogged table \"%s\"",
@@ -15240,7 +15240,7 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
                        }
                        else
                        {
-                               if (foreignrel->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT)
+                               if (RelationIsPermanent(foreignrel))
                                        ereport(ERROR,
                                                        (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
                                                         errmsg("could not change table \"%s\" to unlogged because it references logged table \"%s\"",
index c5947fa41851bed1fe97ac996f7e03c8acc13d33..7f2e40ae39e9f7c0e84c8baa2e0d4367fbc61637 100644 (file)
@@ -126,8 +126,7 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
        relation = table_open(relationObjectId, NoLock);
 
        /* Temporary and unlogged relations are inaccessible during recovery. */
-       if (relation->rd_rel->relpersistence != RELPERSISTENCE_PERMANENT &&
-               RecoveryInProgress())
+       if (!RelationIsPermanent(relation) && RecoveryInProgress())
                ereport(ERROR,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                 errmsg("cannot access temporary or unlogged relations during recovery")));
index 20be094f460ebad7298893b0d38d7f5e93945dc0..ff7395c85b5c1156d4b0ca9e70d5e23a6d06c3fe 100644 (file)
@@ -2990,7 +2990,7 @@ static void
 AssertPendingSyncConsistency(Relation relation)
 {
        bool            relcache_verdict =
-       relation->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT &&
+       RelationIsPermanent(relation) &&
        ((relation->rd_createSubid != InvalidSubTransactionId &&
          RELKIND_HAS_STORAGE(relation->rd_rel->relkind)) ||
         relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId);
index 5375a37dd19b210f4632bb4436ba1b972e44f77a..8eee1c1a835c6907509ece4f8c3808530dafe7dc 100644 (file)
@@ -577,6 +577,13 @@ typedef struct PartitionedTableRdOptions
                (relation)->rd_smgr->smgr_targblock = (targblock); \
        } while (0)
 
+/*
+ * RelationIsPermanent
+ *             True if relation is permanent.
+ */
+#define RelationIsPermanent(relation) \
+       ((relation)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT)
+
 /*
  * RelationNeedsWAL
  *             True if relation needs WAL.
@@ -586,8 +593,7 @@ typedef struct PartitionedTableRdOptions
  * RelFileNode" in src/backend/access/transam/README.
  */
 #define RelationNeedsWAL(relation)                                                                             \
-       ((relation)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT &&      \
-        (XLogIsNeeded() ||                                                                                                     \
+       (RelationIsPermanent(relation) && (XLogIsNeeded() ||                            \
          (relation->rd_createSubid == InvalidSubTransactionId &&                       \
           relation->rd_firstRelfilenodeSubid == InvalidSubTransactionId)))
 
index 2c8b881a09daedbcd77f7475cf6a263eea296dce..f66ac5818859460bbc1a40fd11f2893cfe47e90c 100644 (file)
@@ -37,8 +37,7 @@
  */
 #define RelationAllowsEarlyPruning(rel) \
 ( \
-        (rel)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT      \
-  && !IsCatalogRelation(rel) \
+        RelationIsPermanent(rel) && !IsCatalogRelation(rel) \
   && !RelationIsAccessibleInLogicalDecoding(rel) \
 )