Make more use of RELKIND_HAS_STORAGE()

Started by Peter Eisentrautover 5 years ago4 messages
#1Peter Eisentraut
peter.eisentraut@2ndquadrant.com
1 attachment(s)

This is a patch to make use of RELKIND_HAS_STORAGE() where appropriate,
instead of listing out the relkinds individually. No behavior change is
intended.

This was originally part of the patch from [0]/messages/by-id/dc35a398-37d0-75ce-07ea-1dd71d98f8ec@2ndquadrant.com, but it seems worth
moving forward independently.

[0]: /messages/by-id/dc35a398-37d0-75ce-07ea-1dd71d98f8ec@2ndquadrant.com
/messages/by-id/dc35a398-37d0-75ce-07ea-1dd71d98f8ec@2ndquadrant.com

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

0001-Make-more-use-of-RELKIND_HAS_STORAGE.patchtext/plain; charset=UTF-8; name=0001-Make-more-use-of-RELKIND_HAS_STORAGE.patch; x-mac-creator=0; x-mac-type=0Download
From e5135e2324ff38dc03998af00268f96b6725cd23 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Fri, 5 Jun 2020 08:57:28 +0200
Subject: [PATCH] Make more use of RELKIND_HAS_STORAGE()

Make use of RELKIND_HAS_STORAGE() where appropriate, instead of
listing out the relkinds individually.  No behavior change intended.
---
 src/backend/catalog/heap.c      |  7 +---
 src/backend/postmaster/pgstat.c |  6 +--
 src/backend/utils/adt/dbsize.c  | 73 +++++++++++++--------------------
 3 files changed, 31 insertions(+), 55 deletions(-)

diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index e393c93a45..9c45544815 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -1943,13 +1943,8 @@ heap_drop_with_catalog(Oid relid)
 	/*
 	 * Schedule unlinking of the relation's physical files at commit.
 	 */
-	if (rel->rd_rel->relkind != RELKIND_VIEW &&
-		rel->rd_rel->relkind != RELKIND_COMPOSITE_TYPE &&
-		rel->rd_rel->relkind != RELKIND_FOREIGN_TABLE &&
-		rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
-	{
+	if (RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
 		RelationDropStorage(rel);
-	}
 
 	/*
 	 * Close relcache entry, but *keep* AccessExclusiveLock on the relation
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index d7f99d9944..166d8e3d15 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -1807,11 +1807,7 @@ pgstat_initstats(Relation rel)
 	char		relkind = rel->rd_rel->relkind;
 
 	/* We only count stats for things that have storage */
-	if (!(relkind == RELKIND_RELATION ||
-		  relkind == RELKIND_MATVIEW ||
-		  relkind == RELKIND_INDEX ||
-		  relkind == RELKIND_TOASTVALUE ||
-		  relkind == RELKIND_SEQUENCE))
+	if (!RELKIND_HAS_STORAGE(relkind))
 	{
 		rel->pgstat_info = NULL;
 		return;
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index 840664429e..2320c06a9b 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -874,25 +874,18 @@ pg_relation_filenode(PG_FUNCTION_ARGS)
 		PG_RETURN_NULL();
 	relform = (Form_pg_class) GETSTRUCT(tuple);
 
-	switch (relform->relkind)
+	if (RELKIND_HAS_STORAGE(relform->relkind))
 	{
-		case RELKIND_RELATION:
-		case RELKIND_MATVIEW:
-		case RELKIND_INDEX:
-		case RELKIND_SEQUENCE:
-		case RELKIND_TOASTVALUE:
-			/* okay, these have storage */
-			if (relform->relfilenode)
-				result = relform->relfilenode;
-			else				/* Consult the relation mapper */
-				result = RelationMapOidToFilenode(relid,
-												  relform->relisshared);
-			break;
-
-		default:
-			/* no storage, return NULL */
-			result = InvalidOid;
-			break;
+		if (relform->relfilenode)
+			result = relform->relfilenode;
+		else				/* Consult the relation mapper */
+			result = RelationMapOidToFilenode(relid,
+											  relform->relisshared);
+	}
+	else
+	{
+		/* no storage, return NULL */
+		result = InvalidOid;
 	}
 
 	ReleaseSysCache(tuple);
@@ -951,38 +944,30 @@ pg_relation_filepath(PG_FUNCTION_ARGS)
 		PG_RETURN_NULL();
 	relform = (Form_pg_class) GETSTRUCT(tuple);
 
-	switch (relform->relkind)
+	if (RELKIND_HAS_STORAGE(relform->relkind))
+	{
+		/* This logic should match RelationInitPhysicalAddr */
+		if (relform->reltablespace)
+			rnode.spcNode = relform->reltablespace;
+		else
+			rnode.spcNode = MyDatabaseTableSpace;
+		if (rnode.spcNode == GLOBALTABLESPACE_OID)
+			rnode.dbNode = InvalidOid;
+		else
+			rnode.dbNode = MyDatabaseId;
+		if (relform->relfilenode)
+			rnode.relNode = relform->relfilenode;
+		else				/* Consult the relation mapper */
+			rnode.relNode = RelationMapOidToFilenode(relid,
+													 relform->relisshared);
+	}
+	else
 	{
-		case RELKIND_RELATION:
-		case RELKIND_MATVIEW:
-		case RELKIND_INDEX:
-		case RELKIND_SEQUENCE:
-		case RELKIND_TOASTVALUE:
-			/* okay, these have storage */
-
-			/* This logic should match RelationInitPhysicalAddr */
-			if (relform->reltablespace)
-				rnode.spcNode = relform->reltablespace;
-			else
-				rnode.spcNode = MyDatabaseTableSpace;
-			if (rnode.spcNode == GLOBALTABLESPACE_OID)
-				rnode.dbNode = InvalidOid;
-			else
-				rnode.dbNode = MyDatabaseId;
-			if (relform->relfilenode)
-				rnode.relNode = relform->relfilenode;
-			else				/* Consult the relation mapper */
-				rnode.relNode = RelationMapOidToFilenode(relid,
-														 relform->relisshared);
-			break;
-
-		default:
 			/* no storage, return NULL */
 			rnode.relNode = InvalidOid;
 			/* some compilers generate warnings without these next two lines */
 			rnode.dbNode = InvalidOid;
 			rnode.spcNode = InvalidOid;
-			break;
 	}
 
 	if (!OidIsValid(rnode.relNode))
-- 
2.26.2

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#1)
Re: Make more use of RELKIND_HAS_STORAGE()

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

This is a patch to make use of RELKIND_HAS_STORAGE() where appropriate,
instead of listing out the relkinds individually. No behavior change is
intended.
This was originally part of the patch from [0], but it seems worth
moving forward independently.

Passes eyeball examination. I did not try to search for other places
where RELKIND_HAS_STORAGE should be used.

regards, tom lane

#3Peter Eisentraut
peter.eisentraut@2ndquadrant.com
In reply to: Tom Lane (#2)
Re: Make more use of RELKIND_HAS_STORAGE()

On 2020-06-05 18:05, Tom Lane wrote:

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

This is a patch to make use of RELKIND_HAS_STORAGE() where appropriate,
instead of listing out the relkinds individually. No behavior change is
intended.
This was originally part of the patch from [0], but it seems worth
moving forward independently.

Passes eyeball examination. I did not try to search for other places
where RELKIND_HAS_STORAGE should be used.

committed

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#4Michael Paquier
michael@paquier.xyz
In reply to: Peter Eisentraut (#3)
Re: Make more use of RELKIND_HAS_STORAGE()

On Fri, Jun 12, 2020 at 09:16:04AM +0200, Peter Eisentraut wrote:

committed

Yeah!
--
Michael