Skip to content

Commit 60a1d96

Browse files
committed
Rework DefineIndex relkind check
Simplify coding using a switch rather than nested if tests. Author: Álvaro Reviewed-by: Robert Haas, Amit Langote, Michaël Paquier Discussion: https://postgr.es/m/20171013163820.pai7djcaxrntaxtn@alvherre.pgsql
1 parent 5fc438f commit 60a1d96

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/backend/commands/indexcmds.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -375,25 +375,24 @@ DefineIndex(Oid relationId,
375375
relationId = RelationGetRelid(rel);
376376
namespaceId = RelationGetNamespace(rel);
377377

378-
if (rel->rd_rel->relkind != RELKIND_RELATION &&
379-
rel->rd_rel->relkind != RELKIND_MATVIEW)
378+
/* Ensure that it makes sense to index this kind of relation */
379+
switch (rel->rd_rel->relkind)
380380
{
381-
if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
382-
383-
/*
384-
* Custom error message for FOREIGN TABLE since the term is close
385-
* to a regular table and can confuse the user.
386-
*/
381+
case RELKIND_RELATION:
382+
case RELKIND_MATVIEW:
383+
/* OK */
384+
break;
385+
case RELKIND_FOREIGN_TABLE:
387386
ereport(ERROR,
388387
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
389388
errmsg("cannot create index on foreign table \"%s\"",
390389
RelationGetRelationName(rel))));
391-
else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
390+
case RELKIND_PARTITIONED_TABLE:
392391
ereport(ERROR,
393392
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
394393
errmsg("cannot create index on partitioned table \"%s\"",
395394
RelationGetRelationName(rel))));
396-
else
395+
default:
397396
ereport(ERROR,
398397
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
399398
errmsg("\"%s\" is not a table or materialized view",

0 commit comments

Comments
 (0)