Skip to content

Commit 37b2764

Browse files
committed
Some RELKIND macro refactoring
Add more macros to group some RELKIND_* macros: - RELKIND_HAS_PARTITIONS() - RELKIND_HAS_TABLESPACE() - RELKIND_HAS_TABLE_AM() Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://www.postgresql.org/message-id/flat/a574c8f1-9c84-93ad-a9e5-65233d6fc00f%40enterprisedb.com
1 parent 49422ad commit 37b2764

File tree

14 files changed

+173
-254
lines changed

14 files changed

+173
-254
lines changed

contrib/amcheck/verify_heapam.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,7 @@ verify_heapam(PG_FUNCTION_ARGS)
306306
/*
307307
* Check that a relation's relkind and access method are both supported.
308308
*/
309-
if (ctx.rel->rd_rel->relkind != RELKIND_RELATION &&
310-
ctx.rel->rd_rel->relkind != RELKIND_MATVIEW &&
311-
ctx.rel->rd_rel->relkind != RELKIND_TOASTVALUE &&
309+
if (!RELKIND_HAS_TABLE_AM(ctx.rel->rd_rel->relkind) &&
312310
ctx.rel->rd_rel->relkind != RELKIND_SEQUENCE)
313311
ereport(ERROR,
314312
(errcode(ERRCODE_WRONG_OBJECT_TYPE),

contrib/pg_surgery/heap_surgery.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ heap_force_common(FunctionCallInfo fcinfo, HeapTupleForceOption heap_force_opt)
103103
/*
104104
* Check target relation.
105105
*/
106-
if (rel->rd_rel->relkind != RELKIND_RELATION &&
107-
rel->rd_rel->relkind != RELKIND_MATVIEW &&
108-
rel->rd_rel->relkind != RELKIND_TOASTVALUE)
106+
if (!RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind))
109107
ereport(ERROR,
110108
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
111109
errmsg("cannot operate on relation \"%s\"",

contrib/pg_visibility/pg_visibility.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -776,9 +776,7 @@ tuple_all_visible(HeapTuple tup, TransactionId OldestXmin, Buffer buffer)
776776
static void
777777
check_relation_relkind(Relation rel)
778778
{
779-
if (rel->rd_rel->relkind != RELKIND_RELATION &&
780-
rel->rd_rel->relkind != RELKIND_MATVIEW &&
781-
rel->rd_rel->relkind != RELKIND_TOASTVALUE)
779+
if (!RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind))
782780
ereport(ERROR,
783781
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
784782
errmsg("relation \"%s\" is of wrong relation kind",

contrib/pgstattuple/pgstattuple.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,13 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
252252
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
253253
errmsg("cannot access temporary tables of other sessions")));
254254

255-
switch (rel->rd_rel->relkind)
255+
if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind) ||
256+
rel->rd_rel->relkind == RELKIND_SEQUENCE)
256257
{
257-
case RELKIND_RELATION:
258-
case RELKIND_MATVIEW:
259-
case RELKIND_TOASTVALUE:
260-
case RELKIND_SEQUENCE:
261258
return pgstat_heap(rel, fcinfo);
262-
case RELKIND_INDEX:
259+
}
260+
else if (rel->rd_rel->relkind == RELKIND_INDEX)
261+
{
263262
switch (rel->rd_rel->relam)
264263
{
265264
case BTREE_AM_OID:
@@ -288,9 +287,9 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
288287
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
289288
errmsg("index \"%s\" (%s) is not supported",
290289
RelationGetRelationName(rel), err)));
291-
break;
292-
293-
default:
290+
}
291+
else
292+
{
294293
ereport(ERROR,
295294
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
296295
errmsg("cannot get tuple-level statistics for relation \"%s\"",

src/backend/catalog/heap.c

Lines changed: 56 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -336,35 +336,12 @@ heap_create(const char *relname,
336336
*relfrozenxid = InvalidTransactionId;
337337
*relminmxid = InvalidMultiXactId;
338338

339-
/* Handle reltablespace for specific relkinds. */
340-
switch (relkind)
341-
{
342-
case RELKIND_VIEW:
343-
case RELKIND_COMPOSITE_TYPE:
344-
case RELKIND_FOREIGN_TABLE:
345-
346-
/*
347-
* Force reltablespace to zero if the relation has no physical
348-
* storage. This is mainly just for cleanliness' sake.
349-
*
350-
* Partitioned tables and indexes don't have physical storage
351-
* either, but we want to keep their tablespace settings so that
352-
* their children can inherit it.
353-
*/
354-
reltablespace = InvalidOid;
355-
break;
356-
357-
case RELKIND_SEQUENCE:
358-
359-
/*
360-
* Force reltablespace to zero for sequences, since we don't
361-
* support moving them around into different tablespaces.
362-
*/
363-
reltablespace = InvalidOid;
364-
break;
365-
default:
366-
break;
367-
}
339+
/*
340+
* Force reltablespace to zero if the relation kind does not support
341+
* tablespaces. This is mainly just for cleanliness' sake.
342+
*/
343+
if (!RELKIND_HAS_TABLESPACE(relkind))
344+
reltablespace = InvalidOid;
368345

369346
/*
370347
* Decide whether to create storage. If caller passed a valid relfilenode,
@@ -409,35 +386,20 @@ heap_create(const char *relname,
409386
/*
410387
* Have the storage manager create the relation's disk file, if needed.
411388
*
412-
* For relations the callback creates both the main and the init fork, for
413-
* indexes only the main fork is created. The other forks will be created
414-
* on demand.
389+
* For tables, the AM callback creates both the main and the init fork.
390+
* For others, only the main fork is created; the other forks will be
391+
* created on demand.
415392
*/
416393
if (create_storage)
417394
{
418-
switch (rel->rd_rel->relkind)
419-
{
420-
case RELKIND_VIEW:
421-
case RELKIND_COMPOSITE_TYPE:
422-
case RELKIND_FOREIGN_TABLE:
423-
case RELKIND_PARTITIONED_TABLE:
424-
case RELKIND_PARTITIONED_INDEX:
425-
Assert(false);
426-
break;
427-
428-
case RELKIND_INDEX:
429-
case RELKIND_SEQUENCE:
430-
RelationCreateStorage(rel->rd_node, relpersistence);
431-
break;
432-
433-
case RELKIND_RELATION:
434-
case RELKIND_TOASTVALUE:
435-
case RELKIND_MATVIEW:
436-
table_relation_set_new_filenode(rel, &rel->rd_node,
437-
relpersistence,
438-
relfrozenxid, relminmxid);
439-
break;
440-
}
395+
if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind))
396+
table_relation_set_new_filenode(rel, &rel->rd_node,
397+
relpersistence,
398+
relfrozenxid, relminmxid);
399+
else if (RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
400+
RelationCreateStorage(rel->rd_node, relpersistence);
401+
else
402+
Assert(false);
441403
}
442404

443405
/*
@@ -1015,29 +977,16 @@ AddNewRelationTuple(Relation pg_class_desc,
1015977
*/
1016978
new_rel_reltup = new_rel_desc->rd_rel;
1017979

1018-
switch (relkind)
980+
/* The relation is empty */
981+
new_rel_reltup->relpages = 0;
982+
new_rel_reltup->reltuples = -1;
983+
new_rel_reltup->relallvisible = 0;
984+
985+
/* Sequences always have a known size */
986+
if (relkind == RELKIND_SEQUENCE)
1019987
{
1020-
case RELKIND_RELATION:
1021-
case RELKIND_MATVIEW:
1022-
case RELKIND_INDEX:
1023-
case RELKIND_TOASTVALUE:
1024-
/* The relation is real, but as yet empty */
1025-
new_rel_reltup->relpages = 0;
1026-
new_rel_reltup->reltuples = -1;
1027-
new_rel_reltup->relallvisible = 0;
1028-
break;
1029-
case RELKIND_SEQUENCE:
1030-
/* Sequences always have a known size */
1031-
new_rel_reltup->relpages = 1;
1032-
new_rel_reltup->reltuples = 1;
1033-
new_rel_reltup->relallvisible = 0;
1034-
break;
1035-
default:
1036-
/* Views, etc, have no disk storage */
1037-
new_rel_reltup->relpages = 0;
1038-
new_rel_reltup->reltuples = -1;
1039-
new_rel_reltup->relallvisible = 0;
1040-
break;
988+
new_rel_reltup->relpages = 1;
989+
new_rel_reltup->reltuples = 1;
1041990
}
1042991

1043992
new_rel_reltup->relfrozenxid = relfrozenxid;
@@ -1235,29 +1184,37 @@ heap_create_with_catalog(const char *relname,
12351184
if (!OidIsValid(relid))
12361185
{
12371186
/* Use binary-upgrade override for pg_class.oid/relfilenode? */
1238-
if (IsBinaryUpgrade &&
1239-
(relkind == RELKIND_RELATION || relkind == RELKIND_SEQUENCE ||
1240-
relkind == RELKIND_VIEW || relkind == RELKIND_MATVIEW ||
1241-
relkind == RELKIND_COMPOSITE_TYPE || relkind == RELKIND_FOREIGN_TABLE ||
1242-
relkind == RELKIND_PARTITIONED_TABLE))
1187+
if (IsBinaryUpgrade)
12431188
{
1244-
if (!OidIsValid(binary_upgrade_next_heap_pg_class_oid))
1245-
ereport(ERROR,
1246-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1247-
errmsg("pg_class heap OID value not set when in binary upgrade mode")));
1189+
/*
1190+
* Indexes are not supported here; they use
1191+
* binary_upgrade_next_index_pg_class_oid.
1192+
*/
1193+
Assert(relkind != RELKIND_INDEX);
1194+
Assert(relkind != RELKIND_PARTITIONED_INDEX);
12481195

1249-
relid = binary_upgrade_next_heap_pg_class_oid;
1250-
binary_upgrade_next_heap_pg_class_oid = InvalidOid;
1251-
}
1252-
/* There might be no TOAST table, so we have to test for it. */
1253-
else if (IsBinaryUpgrade &&
1254-
OidIsValid(binary_upgrade_next_toast_pg_class_oid) &&
1255-
relkind == RELKIND_TOASTVALUE)
1256-
{
1257-
relid = binary_upgrade_next_toast_pg_class_oid;
1258-
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
1196+
if (relkind == RELKIND_TOASTVALUE)
1197+
{
1198+
/* There might be no TOAST table, so we have to test for it. */
1199+
if (OidIsValid(binary_upgrade_next_toast_pg_class_oid))
1200+
{
1201+
relid = binary_upgrade_next_toast_pg_class_oid;
1202+
binary_upgrade_next_toast_pg_class_oid = InvalidOid;
1203+
}
1204+
}
1205+
else
1206+
{
1207+
if (!OidIsValid(binary_upgrade_next_heap_pg_class_oid))
1208+
ereport(ERROR,
1209+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1210+
errmsg("pg_class heap OID value not set when in binary upgrade mode")));
1211+
1212+
relid = binary_upgrade_next_heap_pg_class_oid;
1213+
binary_upgrade_next_heap_pg_class_oid = InvalidOid;
1214+
}
12591215
}
1260-
else
1216+
1217+
if (!OidIsValid(relid))
12611218
relid = GetNewRelFileNode(reltablespace, pg_class_desc,
12621219
relpersistence);
12631220
}
@@ -1468,13 +1425,12 @@ heap_create_with_catalog(const char *relname,
14681425

14691426
/*
14701427
* Make a dependency link to force the relation to be deleted if its
1471-
* access method is. Do this only for relation and materialized views.
1428+
* access method is.
14721429
*
14731430
* No need to add an explicit dependency for the toast table, as the
14741431
* main table depends on it.
14751432
*/
1476-
if (relkind == RELKIND_RELATION ||
1477-
relkind == RELKIND_MATVIEW)
1433+
if (RELKIND_HAS_TABLE_AM(relkind) && relkind != RELKIND_TOASTVALUE)
14781434
{
14791435
ObjectAddressSet(referenced, AccessMethodRelationId, accessmtd);
14801436
add_exact_object_address(&referenced, addrs);

src/backend/catalog/index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2293,7 +2293,7 @@ index_drop(Oid indexId, bool concurrent, bool concurrent_lock_mode)
22932293
/*
22942294
* Schedule physical removal of the files (if any)
22952295
*/
2296-
if (userIndexRelation->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
2296+
if (RELKIND_HAS_STORAGE(userIndexRelation->rd_rel->relkind))
22972297
RelationDropStorage(userIndexRelation);
22982298

22992299
/*

src/backend/commands/indexcmds.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2954,8 +2954,7 @@ reindex_error_callback(void *arg)
29542954
{
29552955
ReindexErrorInfo *errinfo = (ReindexErrorInfo *) arg;
29562956

2957-
Assert(errinfo->relkind == RELKIND_PARTITIONED_INDEX ||
2958-
errinfo->relkind == RELKIND_PARTITIONED_TABLE);
2957+
Assert(RELKIND_HAS_PARTITIONS(errinfo->relkind));
29592958

29602959
if (errinfo->relkind == RELKIND_PARTITIONED_TABLE)
29612960
errcontext("while reindexing partitioned table \"%s.%s\"",
@@ -2984,8 +2983,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
29842983
ErrorContextCallback errcallback;
29852984
ReindexErrorInfo errinfo;
29862985

2987-
Assert(relkind == RELKIND_PARTITIONED_INDEX ||
2988-
relkind == RELKIND_PARTITIONED_TABLE);
2986+
Assert(RELKIND_HAS_PARTITIONS(relkind));
29892987

29902988
/*
29912989
* Check if this runs in a transaction block, with an error callback to
@@ -3118,8 +3116,7 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
31183116
* Partitioned tables and indexes can never be processed directly, and
31193117
* a list of their leaves should be built first.
31203118
*/
3121-
Assert(relkind != RELKIND_PARTITIONED_INDEX &&
3122-
relkind != RELKIND_PARTITIONED_TABLE);
3119+
Assert(!RELKIND_HAS_PARTITIONS(relkind));
31233120

31243121
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
31253122
relpersistence != RELPERSISTENCE_TEMP)

src/backend/commands/tablecmds.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -916,9 +916,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
916916
errmsg("specifying a table access method is not supported on a partitioned table")));
917917

918918
}
919-
else if (relkind == RELKIND_RELATION ||
920-
relkind == RELKIND_TOASTVALUE ||
921-
relkind == RELKIND_MATVIEW)
919+
else if (RELKIND_HAS_TABLE_AM(relkind))
922920
accessMethod = default_table_access_method;
923921

924922
/* look up the access method, verify it is for a table */
@@ -13995,9 +13993,7 @@ ATExecSetTableSpace(Oid tableOid, Oid newTableSpace, LOCKMODE lockmode)
1399513993
}
1399613994
else
1399713995
{
13998-
Assert(rel->rd_rel->relkind == RELKIND_RELATION ||
13999-
rel->rd_rel->relkind == RELKIND_MATVIEW ||
14000-
rel->rd_rel->relkind == RELKIND_TOASTVALUE);
13996+
Assert(RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind));
1400113997
table_relation_copy_data(rel, &newrnode);
1400213998
}
1400313999

src/backend/optimizer/util/plancat.c

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -965,17 +965,13 @@ estimate_rel_size(Relation rel, int32 *attr_widths,
965965
BlockNumber relallvisible;
966966
double density;
967967

968-
switch (rel->rd_rel->relkind)
968+
if (RELKIND_HAS_TABLE_AM(rel->rd_rel->relkind))
969969
{
970-
case RELKIND_RELATION:
971-
case RELKIND_MATVIEW:
972-
case RELKIND_TOASTVALUE:
973970
table_relation_estimate_size(rel, attr_widths, pages, tuples,
974971
allvisfrac);
975-
break;
976-
977-
case RELKIND_INDEX:
978-
972+
}
973+
else if (rel->rd_rel->relkind == RELKIND_INDEX)
974+
{
979975
/*
980976
* XXX: It'd probably be good to move this into a callback,
981977
* individual index types e.g. know if they have a metapage.
@@ -991,7 +987,7 @@ estimate_rel_size(Relation rel, int32 *attr_widths,
991987
{
992988
*tuples = 0;
993989
*allvisfrac = 0;
994-
break;
990+
return;
995991
}
996992

997993
/* coerce values in pg_class to more desirable types */
@@ -1055,27 +1051,18 @@ estimate_rel_size(Relation rel, int32 *attr_widths,
10551051
*allvisfrac = 1;
10561052
else
10571053
*allvisfrac = (double) relallvisible / curpages;
1058-
break;
1059-
1060-
case RELKIND_SEQUENCE:
1061-
/* Sequences always have a known size */
1062-
*pages = 1;
1063-
*tuples = 1;
1064-
*allvisfrac = 0;
1065-
break;
1066-
case RELKIND_FOREIGN_TABLE:
1067-
/* Just use whatever's in pg_class */
1068-
/* Note that FDW must cope if reltuples is -1! */
1054+
}
1055+
else
1056+
{
1057+
/*
1058+
* Just use whatever's in pg_class. This covers foreign tables,
1059+
* sequences, and also relkinds without storage (shouldn't get
1060+
* here?); see initializations in AddNewRelationTuple(). Note
1061+
* that FDW must cope if reltuples is -1!
1062+
*/
10691063
*pages = rel->rd_rel->relpages;
10701064
*tuples = rel->rd_rel->reltuples;
10711065
*allvisfrac = 0;
1072-
break;
1073-
default:
1074-
/* else it has no disk storage; probably shouldn't get here? */
1075-
*pages = 0;
1076-
*tuples = 0;
1077-
*allvisfrac = 0;
1078-
break;
10791066
}
10801067
}
10811068

0 commit comments

Comments
 (0)