relkind check in DefineIndex

Started by Alvaro Herreraabout 8 years ago4 messages
#1Alvaro Herrera
alvherre@2ndquadrant.com
1 attachment(s)

The relkind check in DefineIndex has grown into an ugly rats nest of
'if' statements. I propose to change it into a switch, as per the
attached.

--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

0001-reword-kind-check-using-switch.patchtext/plain; charset=us-asciiDownload
From 1a7420321f7f48bbc87dfdd38ba10fa57c6513da Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Fri, 13 Oct 2017 17:56:44 +0200
Subject: [PATCH] reword kind check using switch

---
 src/backend/commands/indexcmds.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index b61aaac284..3f615b6260 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -375,25 +375,24 @@ DefineIndex(Oid relationId,
 	relationId = RelationGetRelid(rel);
 	namespaceId = RelationGetNamespace(rel);
 
-	if (rel->rd_rel->relkind != RELKIND_RELATION &&
-		rel->rd_rel->relkind != RELKIND_MATVIEW)
+	/* Ensure that it makes sense to index this kind of relation */
+	switch (rel->rd_rel->relkind)
 	{
-		if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
-
-			/*
-			 * Custom error message for FOREIGN TABLE since the term is close
-			 * to a regular table and can confuse the user.
-			 */
+		case RELKIND_RELATION:
+		case RELKIND_MATVIEW:
+			/* OK */
+			break;
+		case RELKIND_FOREIGN_TABLE:
 			ereport(ERROR,
 					(errcode(ERRCODE_WRONG_OBJECT_TYPE),
 					 errmsg("cannot create index on foreign table \"%s\"",
 							RelationGetRelationName(rel))));
-		else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
+		case RELKIND_PARTITIONED_TABLE:
 			ereport(ERROR,
 					(errcode(ERRCODE_WRONG_OBJECT_TYPE),
 					 errmsg("cannot create index on partitioned table \"%s\"",
 							RelationGetRelationName(rel))));
-		else
+		default:
 			ereport(ERROR,
 					(errcode(ERRCODE_WRONG_OBJECT_TYPE),
 					 errmsg("\"%s\" is not a table or materialized view",
-- 
2.11.0

#2Robert Haas
robertmhaas@gmail.com
In reply to: Alvaro Herrera (#1)
Re: relkind check in DefineIndex

On Fri, Oct 13, 2017 at 12:38 PM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:

The relkind check in DefineIndex has grown into an ugly rats nest of
'if' statements. I propose to change it into a switch, as per the
attached.

wfm

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Amit Langote
Langote_Amit_f8@lab.ntt.co.jp
In reply to: Robert Haas (#2)
Re: relkind check in DefineIndex

On 2017/10/14 4:32, Robert Haas wrote:

On Fri, Oct 13, 2017 at 12:38 PM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:

The relkind check in DefineIndex has grown into an ugly rats nest of
'if' statements. I propose to change it into a switch, as per the
attached.

wfm

+1

Thanks,
Amit

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#4Michael Paquier
michael.paquier@gmail.com
In reply to: Amit Langote (#3)
Re: relkind check in DefineIndex

On Mon, Oct 16, 2017 at 2:56 PM, Amit Langote
<Langote_Amit_f8@lab.ntt.co.jp> wrote:

On 2017/10/14 4:32, Robert Haas wrote:

On Fri, Oct 13, 2017 at 12:38 PM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:

The relkind check in DefineIndex has grown into an ugly rats nest of
'if' statements. I propose to change it into a switch, as per the
attached.

wfm

+1

+1. There is as well CreateTrigger(), analyze_rel() or
ATRewriteCatalogs() that do similar things but those are not using
multiple layers of checks.
-- 
Michael

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers