Skip to content

Commit fcd778e

Browse files
committed
Fix hard-coded relkind constants in assorted src/bin files.
Although it's reasonable to expect that most of these constants will never change, that does not make it good programming style to hard-code the value rather than using the RELKIND_FOO macros. Discussion: https://postgr.es/m/11145.1488931324@sss.pgh.pa.us
1 parent 15bb93e commit fcd778e

File tree

8 files changed

+81
-32
lines changed

8 files changed

+81
-32
lines changed

src/bin/initdb/initdb.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
#include "catalog/catalog.h"
6363
#include "catalog/pg_authid.h"
64+
#include "catalog/pg_class.h"
6465
#include "common/file_utils.h"
6566
#include "common/restricted_token.h"
6667
#include "common/username.h"
@@ -1691,9 +1692,13 @@ setup_privileges(FILE *cmdfd)
16911692
" SET relacl = (SELECT array_agg(a.acl) FROM "
16921693
" (SELECT E'=r/\"$POSTGRES_SUPERUSERNAME\"' as acl "
16931694
" UNION SELECT unnest(pg_catalog.acldefault("
1694-
" CASE WHEN relkind = 'S' THEN 's' ELSE 'r' END::\"char\"," CppAsString2(BOOTSTRAP_SUPERUSERID) "::oid))"
1695+
" CASE WHEN relkind = " CppAsString2(RELKIND_SEQUENCE) " THEN 's' "
1696+
" ELSE 'r' END::\"char\"," CppAsString2(BOOTSTRAP_SUPERUSERID) "::oid))"
16951697
" ) as a) "
1696-
" WHERE relkind IN ('r', 'v', 'm', 'S') AND relacl IS NULL;\n\n",
1698+
" WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1699+
CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1700+
CppAsString2(RELKIND_SEQUENCE) ")"
1701+
" AND relacl IS NULL;\n\n",
16971702
"GRANT USAGE ON SCHEMA pg_catalog TO PUBLIC;\n\n",
16981703
"GRANT CREATE, USAGE ON SCHEMA public TO PUBLIC;\n\n",
16991704
"REVOKE ALL ON pg_largeobject FROM PUBLIC;\n\n",
@@ -1709,7 +1714,9 @@ setup_privileges(FILE *cmdfd)
17091714
" pg_class"
17101715
" WHERE"
17111716
" relacl IS NOT NULL"
1712-
" AND relkind IN ('r', 'v', 'm', 'S');",
1717+
" AND relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1718+
CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1719+
CppAsString2(RELKIND_SEQUENCE) ");",
17131720
"INSERT INTO pg_init_privs "
17141721
" (objoid, classoid, objsubid, initprivs, privtype)"
17151722
" SELECT"
@@ -1723,7 +1730,9 @@ setup_privileges(FILE *cmdfd)
17231730
" JOIN pg_attribute ON (pg_class.oid = pg_attribute.attrelid)"
17241731
" WHERE"
17251732
" pg_attribute.attacl IS NOT NULL"
1726-
" AND pg_class.relkind IN ('r', 'v', 'm', 'S');",
1733+
" AND pg_class.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1734+
CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1735+
CppAsString2(RELKIND_SEQUENCE) ");",
17271736
"INSERT INTO pg_init_privs "
17281737
" (objoid, classoid, objsubid, initprivs, privtype)"
17291738
" SELECT"

src/bin/pg_dump/pg_dump_sort.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "pg_backup_utils.h"
2020
#include "pg_dump.h"
2121

22+
#include "catalog/pg_class.h"
23+
2224
/* translator: this is a module name */
2325
static const char *modulename = gettext_noop("sorter");
2426

@@ -968,8 +970,8 @@ repairDependencyLoop(DumpableObject **loop,
968970
if (nLoop == 2 &&
969971
loop[0]->objType == DO_TABLE &&
970972
loop[1]->objType == DO_RULE &&
971-
(((TableInfo *) loop[0])->relkind == 'v' || /* RELKIND_VIEW */
972-
((TableInfo *) loop[0])->relkind == 'm') && /* RELKIND_MATVIEW */
973+
(((TableInfo *) loop[0])->relkind == RELKIND_VIEW ||
974+
((TableInfo *) loop[0])->relkind == RELKIND_MATVIEW) &&
973975
((RuleInfo *) loop[1])->ev_type == '1' &&
974976
((RuleInfo *) loop[1])->is_instead &&
975977
((RuleInfo *) loop[1])->ruletable == (TableInfo *) loop[0])
@@ -980,8 +982,8 @@ repairDependencyLoop(DumpableObject **loop,
980982
if (nLoop == 2 &&
981983
loop[1]->objType == DO_TABLE &&
982984
loop[0]->objType == DO_RULE &&
983-
(((TableInfo *) loop[1])->relkind == 'v' || /* RELKIND_VIEW */
984-
((TableInfo *) loop[1])->relkind == 'm') && /* RELKIND_MATVIEW */
985+
(((TableInfo *) loop[1])->relkind == RELKIND_VIEW ||
986+
((TableInfo *) loop[1])->relkind == RELKIND_MATVIEW) &&
985987
((RuleInfo *) loop[0])->ev_type == '1' &&
986988
((RuleInfo *) loop[0])->is_instead &&
987989
((RuleInfo *) loop[0])->ruletable == (TableInfo *) loop[1])
@@ -996,7 +998,7 @@ repairDependencyLoop(DumpableObject **loop,
996998
for (i = 0; i < nLoop; i++)
997999
{
9981000
if (loop[i]->objType == DO_TABLE &&
999-
((TableInfo *) loop[i])->relkind == 'v') /* RELKIND_VIEW */
1001+
((TableInfo *) loop[i])->relkind == RELKIND_VIEW)
10001002
{
10011003
for (j = 0; j < nLoop; j++)
10021004
{
@@ -1019,7 +1021,7 @@ repairDependencyLoop(DumpableObject **loop,
10191021
for (i = 0; i < nLoop; i++)
10201022
{
10211023
if (loop[i]->objType == DO_TABLE &&
1022-
((TableInfo *) loop[i])->relkind == 'm') /* RELKIND_MATVIEW */
1024+
((TableInfo *) loop[i])->relkind == RELKIND_MATVIEW)
10231025
{
10241026
for (j = 0; j < nLoop; j++)
10251027
{

src/bin/pg_upgrade/info.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "pg_upgrade.h"
1313

1414
#include "access/transam.h"
15+
#include "catalog/pg_class.h"
1516

1617

1718
static void create_rel_filename_map(const char *old_data, const char *new_data,
@@ -444,7 +445,8 @@ get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
444445
" SELECT c.oid, 0::oid, 0::oid "
445446
" FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
446447
" ON c.relnamespace = n.oid "
447-
" WHERE relkind IN ('r', 'm') AND "
448+
" WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
449+
CppAsString2(RELKIND_MATVIEW) ") AND "
448450
/* exclude possible orphaned temp tables */
449451
" ((n.nspname !~ '^pg_temp_' AND "
450452
" n.nspname !~ '^pg_toast_temp_' AND "

src/bin/pg_upgrade/pg_upgrade.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "postgres_fe.h"
3838

3939
#include "pg_upgrade.h"
40+
#include "catalog/pg_class.h"
4041
#include "common/restricted_token.h"
4142
#include "fe_utils/string_utils.h"
4243

@@ -565,15 +566,21 @@ set_frozenxids(bool minmxid_only)
565566
"UPDATE pg_catalog.pg_class "
566567
"SET relfrozenxid = '%u' "
567568
/* only heap, materialized view, and TOAST are vacuumed */
568-
"WHERE relkind IN ('r', 'm', 't')",
569+
"WHERE relkind IN ("
570+
CppAsString2(RELKIND_RELATION) ", "
571+
CppAsString2(RELKIND_MATVIEW) ", "
572+
CppAsString2(RELKIND_TOASTVALUE) ")",
569573
old_cluster.controldata.chkpnt_nxtxid));
570574

571575
/* set pg_class.relminmxid */
572576
PQclear(executeQueryOrDie(conn,
573577
"UPDATE pg_catalog.pg_class "
574578
"SET relminmxid = '%u' "
575579
/* only heap, materialized view, and TOAST are vacuumed */
576-
"WHERE relkind IN ('r', 'm', 't')",
580+
"WHERE relkind IN ("
581+
CppAsString2(RELKIND_RELATION) ", "
582+
CppAsString2(RELKIND_MATVIEW) ", "
583+
CppAsString2(RELKIND_TOASTVALUE) ")",
577584
old_cluster.controldata.chkpnt_nxtmulti));
578585
PQfinish(conn);
579586

src/bin/pg_upgrade/version.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include "postgres_fe.h"
1111

1212
#include "pg_upgrade.h"
13+
14+
#include "catalog/pg_class.h"
1315
#include "fe_utils/string_utils.h"
1416

1517

@@ -234,7 +236,10 @@ old_9_6_check_for_unknown_data_type_usage(ClusterInfo *cluster)
234236
"WHERE c.oid = a.attrelid AND "
235237
" NOT a.attisdropped AND "
236238
" a.atttypid = 'pg_catalog.unknown'::pg_catalog.regtype AND "
237-
" c.relkind IN ('r', 'c', 'm') AND "
239+
" c.relkind IN ("
240+
CppAsString2(RELKIND_RELATION) ", "
241+
CppAsString2(RELKIND_COMPOSITE_TYPE) ", "
242+
CppAsString2(RELKIND_MATVIEW) ") AND "
238243
" c.relnamespace = n.oid AND "
239244
/* exclude possible orphaned temp tables */
240245
" n.nspname !~ '^pg_temp_' AND "

src/bin/psql/command.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <sys/stat.h> /* for stat() */
2828
#endif
2929

30+
#include "catalog/pg_class.h"
3031
#include "portability/instr_time.h"
3132

3233
#include "libpq-fe.h"
@@ -3465,11 +3466,11 @@ get_create_object_cmd(EditableObjectType obj_type, Oid oid,
34653466
switch (relkind[0])
34663467
{
34673468
#ifdef NOT_USED
3468-
case 'm':
3469+
case RELKIND_MATVIEW:
34693470
appendPQExpBufferStr(buf, "CREATE OR REPLACE MATERIALIZED VIEW ");
34703471
break;
34713472
#endif
3472-
case 'v':
3473+
case RELKIND_VIEW:
34733474
appendPQExpBufferStr(buf, "CREATE OR REPLACE VIEW ");
34743475
break;
34753476
default:

src/bin/psql/tab-complete.c

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
#ifdef USE_READLINE
4141

4242
#include <ctype.h>
43+
44+
#include "catalog/pg_class.h"
45+
4346
#include "libpq-fe.h"
4447
#include "pqexpbuffer.h"
4548
#include "common.h"
@@ -85,8 +88,9 @@ typedef struct SchemaQuery
8588
/*
8689
* Selection condition --- only rows meeting this condition are candidates
8790
* to display. If catname mentions multiple tables, include the necessary
88-
* join condition here. For example, "c.relkind = 'r'". Write NULL (not
89-
* an empty string) if not needed.
91+
* join condition here. For example, this might look like "c.relkind = "
92+
* CppAsString2(RELKIND_RELATION). Write NULL (not an empty string) if
93+
* not needed.
9094
*/
9195
const char *selcondition;
9296

@@ -361,7 +365,8 @@ static const SchemaQuery Query_for_list_of_datatypes = {
361365
"pg_catalog.pg_type t",
362366
/* selcondition --- ignore table rowtypes and array types */
363367
"(t.typrelid = 0 "
364-
" OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) "
368+
" OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE)
369+
" FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) "
365370
"AND t.typname !~ '^_'",
366371
/* viscondition */
367372
"pg_catalog.pg_type_is_visible(t.oid)",
@@ -407,7 +412,7 @@ static const SchemaQuery Query_for_list_of_indexes = {
407412
/* catname */
408413
"pg_catalog.pg_class c",
409414
/* selcondition */
410-
"c.relkind IN ('i')",
415+
"c.relkind IN (" CppAsString2(RELKIND_INDEX) ")",
411416
/* viscondition */
412417
"pg_catalog.pg_table_is_visible(c.oid)",
413418
/* namespace */
@@ -422,7 +427,7 @@ static const SchemaQuery Query_for_list_of_sequences = {
422427
/* catname */
423428
"pg_catalog.pg_class c",
424429
/* selcondition */
425-
"c.relkind IN ('S')",
430+
"c.relkind IN (" CppAsString2(RELKIND_SEQUENCE) ")",
426431
/* viscondition */
427432
"pg_catalog.pg_table_is_visible(c.oid)",
428433
/* namespace */
@@ -437,7 +442,7 @@ static const SchemaQuery Query_for_list_of_foreign_tables = {
437442
/* catname */
438443
"pg_catalog.pg_class c",
439444
/* selcondition */
440-
"c.relkind IN ('f')",
445+
"c.relkind IN (" CppAsString2(RELKIND_FOREIGN_TABLE) ")",
441446
/* viscondition */
442447
"pg_catalog.pg_table_is_visible(c.oid)",
443448
/* namespace */
@@ -452,7 +457,8 @@ static const SchemaQuery Query_for_list_of_tables = {
452457
/* catname */
453458
"pg_catalog.pg_class c",
454459
/* selcondition */
455-
"c.relkind IN ('r', 'P')",
460+
"c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
461+
CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
456462
/* viscondition */
457463
"pg_catalog.pg_table_is_visible(c.oid)",
458464
/* namespace */
@@ -467,7 +473,7 @@ static const SchemaQuery Query_for_list_of_partitioned_tables = {
467473
/* catname */
468474
"pg_catalog.pg_class c",
469475
/* selcondition */
470-
"c.relkind IN ('P')",
476+
"c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
471477
/* viscondition */
472478
"pg_catalog.pg_table_is_visible(c.oid)",
473479
/* namespace */
@@ -498,7 +504,10 @@ static const SchemaQuery Query_for_list_of_updatables = {
498504
/* catname */
499505
"pg_catalog.pg_class c",
500506
/* selcondition */
501-
"c.relkind IN ('r', 'f', 'v', 'P')",
507+
"c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
508+
CppAsString2(RELKIND_FOREIGN_TABLE) ", "
509+
CppAsString2(RELKIND_VIEW) ", "
510+
CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
502511
/* viscondition */
503512
"pg_catalog.pg_table_is_visible(c.oid)",
504513
/* namespace */
@@ -528,7 +537,12 @@ static const SchemaQuery Query_for_list_of_tsvmf = {
528537
/* catname */
529538
"pg_catalog.pg_class c",
530539
/* selcondition */
531-
"c.relkind IN ('r', 'S', 'v', 'm', 'f', 'P')",
540+
"c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
541+
CppAsString2(RELKIND_SEQUENCE) ", "
542+
CppAsString2(RELKIND_VIEW) ", "
543+
CppAsString2(RELKIND_MATVIEW) ", "
544+
CppAsString2(RELKIND_FOREIGN_TABLE) ", "
545+
CppAsString2(RELKIND_PARTITIONED_TABLE) ")",
532546
/* viscondition */
533547
"pg_catalog.pg_table_is_visible(c.oid)",
534548
/* namespace */
@@ -543,7 +557,9 @@ static const SchemaQuery Query_for_list_of_tmf = {
543557
/* catname */
544558
"pg_catalog.pg_class c",
545559
/* selcondition */
546-
"c.relkind IN ('r', 'm', 'f')",
560+
"c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
561+
CppAsString2(RELKIND_MATVIEW) ", "
562+
CppAsString2(RELKIND_FOREIGN_TABLE) ")",
547563
/* viscondition */
548564
"pg_catalog.pg_table_is_visible(c.oid)",
549565
/* namespace */
@@ -558,7 +574,8 @@ static const SchemaQuery Query_for_list_of_tm = {
558574
/* catname */
559575
"pg_catalog.pg_class c",
560576
/* selcondition */
561-
"c.relkind IN ('r', 'm')",
577+
"c.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
578+
CppAsString2(RELKIND_MATVIEW) ")",
562579
/* viscondition */
563580
"pg_catalog.pg_table_is_visible(c.oid)",
564581
/* namespace */
@@ -573,7 +590,7 @@ static const SchemaQuery Query_for_list_of_views = {
573590
/* catname */
574591
"pg_catalog.pg_class c",
575592
/* selcondition */
576-
"c.relkind IN ('v')",
593+
"c.relkind IN (" CppAsString2(RELKIND_VIEW) ")",
577594
/* viscondition */
578595
"pg_catalog.pg_table_is_visible(c.oid)",
579596
/* namespace */
@@ -588,7 +605,7 @@ static const SchemaQuery Query_for_list_of_matviews = {
588605
/* catname */
589606
"pg_catalog.pg_class c",
590607
/* selcondition */
591-
"c.relkind IN ('m')",
608+
"c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")",
592609
/* viscondition */
593610
"pg_catalog.pg_table_is_visible(c.oid)",
594611
/* namespace */

src/bin/scripts/vacuumdb.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <sys/select.h>
1717
#endif
1818

19+
#include "catalog/pg_class.h"
20+
1921
#include "common.h"
2022
#include "fe_utils/simple_list.h"
2123
#include "fe_utils/string_utils.h"
@@ -388,8 +390,12 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts,
388390
initPQExpBuffer(&buf);
389391

390392
res = executeQuery(conn,
391-
"SELECT c.relname, ns.nspname FROM pg_class c, pg_namespace ns\n"
392-
" WHERE relkind IN (\'r\', \'m\') AND c.relnamespace = ns.oid\n"
393+
"SELECT c.relname, ns.nspname"
394+
" FROM pg_class c, pg_namespace ns\n"
395+
" WHERE relkind IN ("
396+
CppAsString2(RELKIND_RELATION) ", "
397+
CppAsString2(RELKIND_MATVIEW) ")"
398+
" AND c.relnamespace = ns.oid\n"
393399
" ORDER BY c.relpages DESC;",
394400
progname, echo);
395401

0 commit comments

Comments
 (0)