Add macro RelationIsPermanent() to report relation permanence

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
This commit is contained in:
Bruce Momjian 2021-03-22 20:22:48 -04:00
parent 8e4b332e88
commit 95d77149c5
8 changed files with 19 additions and 15 deletions

View File

@ -1036,7 +1036,7 @@ gistGetFakeLSN(Relation rel)
return counter++; 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 * WAL-logging on this relation will start after commit, so its LSNs

View 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 * WAL log creation if the relation is persistent, or this is the
* init fork of an unlogged relation. * init fork of an unlogged relation.
*/ */
if (rel->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT || if (RelationIsPermanent(rel) ||
(rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED && (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
forkNum == INIT_FORKNUM)) forkNum == INIT_FORKNUM))
log_smgrcreate(newrnode, forkNum); log_smgrcreate(newrnode, forkNum);

View File

@ -67,7 +67,7 @@ check_publication_add_relation(Relation targetrel)
errdetail("System tables cannot be added to publications."))); errdetail("System tables cannot be added to publications.")));
/* UNLOGGED and TEMP relations cannot be part of publication. */ /* UNLOGGED and TEMP relations cannot be part of publication. */
if (targetrel->rd_rel->relpersistence != RELPERSISTENCE_PERMANENT) if (!RelationIsPermanent(targetrel))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("table \"%s\" cannot be replicated", errmsg("table \"%s\" cannot be replicated",

View File

@ -8650,13 +8650,13 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
switch (rel->rd_rel->relpersistence) switch (rel->rd_rel->relpersistence)
{ {
case RELPERSISTENCE_PERMANENT: case RELPERSISTENCE_PERMANENT:
if (pkrel->rd_rel->relpersistence != RELPERSISTENCE_PERMANENT) if (!RelationIsPermanent(pkrel))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION), (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
errmsg("constraints on permanent tables may reference only permanent tables"))); errmsg("constraints on permanent tables may reference only permanent tables")));
break; break;
case RELPERSISTENCE_UNLOGGED: case RELPERSISTENCE_UNLOGGED:
if (pkrel->rd_rel->relpersistence != RELPERSISTENCE_PERMANENT if (!RelationIsPermanent(pkrel)
&& pkrel->rd_rel->relpersistence != RELPERSISTENCE_UNLOGGED) && pkrel->rd_rel->relpersistence != RELPERSISTENCE_UNLOGGED)
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION), (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 * WAL log creation if the relation is persistent, or this is the
* init fork of an unlogged relation. * init fork of an unlogged relation.
*/ */
if (rel->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT || if (RelationIsPermanent(rel) ||
(rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED && (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED &&
forkNum == INIT_FORKNUM)) forkNum == INIT_FORKNUM))
log_smgrcreate(&newrnode, forkNum); log_smgrcreate(&newrnode, forkNum);
@ -15230,7 +15230,7 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
if (toLogged) if (toLogged)
{ {
if (foreignrel->rd_rel->relpersistence != RELPERSISTENCE_PERMANENT) if (!RelationIsPermanent(foreignrel))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION), (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
errmsg("could not change table \"%s\" to logged because it references unlogged table \"%s\"", errmsg("could not change table \"%s\" to logged because it references unlogged table \"%s\"",
@ -15240,7 +15240,7 @@ ATPrepChangePersistence(Relation rel, bool toLogged)
} }
else else
{ {
if (foreignrel->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT) if (RelationIsPermanent(foreignrel))
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_INVALID_TABLE_DEFINITION), (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
errmsg("could not change table \"%s\" to unlogged because it references logged table \"%s\"", errmsg("could not change table \"%s\" to unlogged because it references logged table \"%s\"",

View File

@ -126,8 +126,7 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
relation = table_open(relationObjectId, NoLock); relation = table_open(relationObjectId, NoLock);
/* Temporary and unlogged relations are inaccessible during recovery. */ /* Temporary and unlogged relations are inaccessible during recovery. */
if (relation->rd_rel->relpersistence != RELPERSISTENCE_PERMANENT && if (!RelationIsPermanent(relation) && RecoveryInProgress())
RecoveryInProgress())
ereport(ERROR, ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot access temporary or unlogged relations during recovery"))); errmsg("cannot access temporary or unlogged relations during recovery")));

View File

@ -2990,7 +2990,7 @@ static void
AssertPendingSyncConsistency(Relation relation) AssertPendingSyncConsistency(Relation relation)
{ {
bool relcache_verdict = bool relcache_verdict =
relation->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT && RelationIsPermanent(relation) &&
((relation->rd_createSubid != InvalidSubTransactionId && ((relation->rd_createSubid != InvalidSubTransactionId &&
RELKIND_HAS_STORAGE(relation->rd_rel->relkind)) || RELKIND_HAS_STORAGE(relation->rd_rel->relkind)) ||
relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId); relation->rd_firstRelfilenodeSubid != InvalidSubTransactionId);

View File

@ -577,6 +577,13 @@ typedef struct PartitionedTableRdOptions
(relation)->rd_smgr->smgr_targblock = (targblock); \ (relation)->rd_smgr->smgr_targblock = (targblock); \
} while (0) } while (0)
/*
* RelationIsPermanent
* True if relation is permanent.
*/
#define RelationIsPermanent(relation) \
((relation)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT)
/* /*
* RelationNeedsWAL * RelationNeedsWAL
* True if relation needs WAL. * True if relation needs WAL.
@ -586,8 +593,7 @@ typedef struct PartitionedTableRdOptions
* RelFileNode" in src/backend/access/transam/README. * RelFileNode" in src/backend/access/transam/README.
*/ */
#define RelationNeedsWAL(relation) \ #define RelationNeedsWAL(relation) \
((relation)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT && \ (RelationIsPermanent(relation) && (XLogIsNeeded() || \
(XLogIsNeeded() || \
(relation->rd_createSubid == InvalidSubTransactionId && \ (relation->rd_createSubid == InvalidSubTransactionId && \
relation->rd_firstRelfilenodeSubid == InvalidSubTransactionId))) relation->rd_firstRelfilenodeSubid == InvalidSubTransactionId)))

View File

@ -37,8 +37,7 @@
*/ */
#define RelationAllowsEarlyPruning(rel) \ #define RelationAllowsEarlyPruning(rel) \
( \ ( \
(rel)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT \ RelationIsPermanent(rel) && !IsCatalogRelation(rel) \
&& !IsCatalogRelation(rel) \
&& !RelationIsAccessibleInLogicalDecoding(rel) \ && !RelationIsAccessibleInLogicalDecoding(rel) \
) )