From fcd778eb703c154c0fbba249e17c21b7ae4de19b Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 9 Mar 2017 22:42:16 -0500 Subject: [PATCH] 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 --- src/bin/initdb/initdb.c | 17 ++++++++++--- src/bin/pg_dump/pg_dump_sort.c | 14 +++++----- src/bin/pg_upgrade/info.c | 4 ++- src/bin/pg_upgrade/pg_upgrade.c | 11 ++++++-- src/bin/pg_upgrade/version.c | 7 ++++- src/bin/psql/command.c | 5 ++-- src/bin/psql/tab-complete.c | 45 +++++++++++++++++++++++---------- src/bin/scripts/vacuumdb.c | 10 ++++++-- 8 files changed, 81 insertions(+), 32 deletions(-) diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c index da40d7ab67..8bca635a3b 100644 --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -61,6 +61,7 @@ #include "catalog/catalog.h" #include "catalog/pg_authid.h" +#include "catalog/pg_class.h" #include "common/file_utils.h" #include "common/restricted_token.h" #include "common/username.h" @@ -1691,9 +1692,13 @@ setup_privileges(FILE *cmdfd) " SET relacl = (SELECT array_agg(a.acl) FROM " " (SELECT E'=r/\"$POSTGRES_SUPERUSERNAME\"' as acl " " UNION SELECT unnest(pg_catalog.acldefault(" - " CASE WHEN relkind = 'S' THEN 's' ELSE 'r' END::\"char\"," CppAsString2(BOOTSTRAP_SUPERUSERID) "::oid))" + " CASE WHEN relkind = " CppAsString2(RELKIND_SEQUENCE) " THEN 's' " + " ELSE 'r' END::\"char\"," CppAsString2(BOOTSTRAP_SUPERUSERID) "::oid))" " ) as a) " - " WHERE relkind IN ('r', 'v', 'm', 'S') AND relacl IS NULL;\n\n", + " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_SEQUENCE) ")" + " AND relacl IS NULL;\n\n", "GRANT USAGE ON SCHEMA pg_catalog TO PUBLIC;\n\n", "GRANT CREATE, USAGE ON SCHEMA public TO PUBLIC;\n\n", "REVOKE ALL ON pg_largeobject FROM PUBLIC;\n\n", @@ -1709,7 +1714,9 @@ setup_privileges(FILE *cmdfd) " pg_class" " WHERE" " relacl IS NOT NULL" - " AND relkind IN ('r', 'v', 'm', 'S');", + " AND relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_SEQUENCE) ");", "INSERT INTO pg_init_privs " " (objoid, classoid, objsubid, initprivs, privtype)" " SELECT" @@ -1723,7 +1730,9 @@ setup_privileges(FILE *cmdfd) " JOIN pg_attribute ON (pg_class.oid = pg_attribute.attrelid)" " WHERE" " pg_attribute.attacl IS NOT NULL" - " AND pg_class.relkind IN ('r', 'v', 'm', 'S');", + " AND pg_class.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_SEQUENCE) ");", "INSERT INTO pg_init_privs " " (objoid, classoid, objsubid, initprivs, privtype)" " SELECT" diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c index 708a47f3cb..e555de8857 100644 --- a/src/bin/pg_dump/pg_dump_sort.c +++ b/src/bin/pg_dump/pg_dump_sort.c @@ -19,6 +19,8 @@ #include "pg_backup_utils.h" #include "pg_dump.h" +#include "catalog/pg_class.h" + /* translator: this is a module name */ static const char *modulename = gettext_noop("sorter"); @@ -968,8 +970,8 @@ repairDependencyLoop(DumpableObject **loop, if (nLoop == 2 && loop[0]->objType == DO_TABLE && loop[1]->objType == DO_RULE && - (((TableInfo *) loop[0])->relkind == 'v' || /* RELKIND_VIEW */ - ((TableInfo *) loop[0])->relkind == 'm') && /* RELKIND_MATVIEW */ + (((TableInfo *) loop[0])->relkind == RELKIND_VIEW || + ((TableInfo *) loop[0])->relkind == RELKIND_MATVIEW) && ((RuleInfo *) loop[1])->ev_type == '1' && ((RuleInfo *) loop[1])->is_instead && ((RuleInfo *) loop[1])->ruletable == (TableInfo *) loop[0]) @@ -980,8 +982,8 @@ repairDependencyLoop(DumpableObject **loop, if (nLoop == 2 && loop[1]->objType == DO_TABLE && loop[0]->objType == DO_RULE && - (((TableInfo *) loop[1])->relkind == 'v' || /* RELKIND_VIEW */ - ((TableInfo *) loop[1])->relkind == 'm') && /* RELKIND_MATVIEW */ + (((TableInfo *) loop[1])->relkind == RELKIND_VIEW || + ((TableInfo *) loop[1])->relkind == RELKIND_MATVIEW) && ((RuleInfo *) loop[0])->ev_type == '1' && ((RuleInfo *) loop[0])->is_instead && ((RuleInfo *) loop[0])->ruletable == (TableInfo *) loop[1]) @@ -996,7 +998,7 @@ repairDependencyLoop(DumpableObject **loop, for (i = 0; i < nLoop; i++) { if (loop[i]->objType == DO_TABLE && - ((TableInfo *) loop[i])->relkind == 'v') /* RELKIND_VIEW */ + ((TableInfo *) loop[i])->relkind == RELKIND_VIEW) { for (j = 0; j < nLoop; j++) { @@ -1019,7 +1021,7 @@ repairDependencyLoop(DumpableObject **loop, for (i = 0; i < nLoop; i++) { if (loop[i]->objType == DO_TABLE && - ((TableInfo *) loop[i])->relkind == 'm') /* RELKIND_MATVIEW */ + ((TableInfo *) loop[i])->relkind == RELKIND_MATVIEW) { for (j = 0; j < nLoop; j++) { diff --git a/src/bin/pg_upgrade/info.c b/src/bin/pg_upgrade/info.c index 853d8990ff..6500302c3d 100644 --- a/src/bin/pg_upgrade/info.c +++ b/src/bin/pg_upgrade/info.c @@ -12,6 +12,7 @@ #include "pg_upgrade.h" #include "access/transam.h" +#include "catalog/pg_class.h" 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) " SELECT c.oid, 0::oid, 0::oid " " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n " " ON c.relnamespace = n.oid " - " WHERE relkind IN ('r', 'm') AND " + " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ") AND " /* exclude possible orphaned temp tables */ " ((n.nspname !~ '^pg_temp_' AND " " n.nspname !~ '^pg_toast_temp_' AND " diff --git a/src/bin/pg_upgrade/pg_upgrade.c b/src/bin/pg_upgrade/pg_upgrade.c index 145d2da69c..fc66d93dd6 100644 --- a/src/bin/pg_upgrade/pg_upgrade.c +++ b/src/bin/pg_upgrade/pg_upgrade.c @@ -37,6 +37,7 @@ #include "postgres_fe.h" #include "pg_upgrade.h" +#include "catalog/pg_class.h" #include "common/restricted_token.h" #include "fe_utils/string_utils.h" @@ -565,7 +566,10 @@ set_frozenxids(bool minmxid_only) "UPDATE pg_catalog.pg_class " "SET relfrozenxid = '%u' " /* only heap, materialized view, and TOAST are vacuumed */ - "WHERE relkind IN ('r', 'm', 't')", + "WHERE relkind IN (" + CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_TOASTVALUE) ")", old_cluster.controldata.chkpnt_nxtxid)); /* set pg_class.relminmxid */ @@ -573,7 +577,10 @@ set_frozenxids(bool minmxid_only) "UPDATE pg_catalog.pg_class " "SET relminmxid = '%u' " /* only heap, materialized view, and TOAST are vacuumed */ - "WHERE relkind IN ('r', 'm', 't')", + "WHERE relkind IN (" + CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_TOASTVALUE) ")", old_cluster.controldata.chkpnt_nxtmulti)); PQfinish(conn); diff --git a/src/bin/pg_upgrade/version.c b/src/bin/pg_upgrade/version.c index aa462daed3..a3651aaded 100644 --- a/src/bin/pg_upgrade/version.c +++ b/src/bin/pg_upgrade/version.c @@ -10,6 +10,8 @@ #include "postgres_fe.h" #include "pg_upgrade.h" + +#include "catalog/pg_class.h" #include "fe_utils/string_utils.h" @@ -234,7 +236,10 @@ old_9_6_check_for_unknown_data_type_usage(ClusterInfo *cluster) "WHERE c.oid = a.attrelid AND " " NOT a.attisdropped AND " " a.atttypid = 'pg_catalog.unknown'::pg_catalog.regtype AND " - " c.relkind IN ('r', 'c', 'm') AND " + " c.relkind IN (" + CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_COMPOSITE_TYPE) ", " + CppAsString2(RELKIND_MATVIEW) ") AND " " c.relnamespace = n.oid AND " /* exclude possible orphaned temp tables */ " n.nspname !~ '^pg_temp_' AND " diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 07efc27a69..4f4a0aa9bd 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -27,6 +27,7 @@ #include /* for stat() */ #endif +#include "catalog/pg_class.h" #include "portability/instr_time.h" #include "libpq-fe.h" @@ -3465,11 +3466,11 @@ get_create_object_cmd(EditableObjectType obj_type, Oid oid, switch (relkind[0]) { #ifdef NOT_USED - case 'm': + case RELKIND_MATVIEW: appendPQExpBufferStr(buf, "CREATE OR REPLACE MATERIALIZED VIEW "); break; #endif - case 'v': + case RELKIND_VIEW: appendPQExpBufferStr(buf, "CREATE OR REPLACE VIEW "); break; default: diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 121a492e6d..e8458e939e 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -40,6 +40,9 @@ #ifdef USE_READLINE #include + +#include "catalog/pg_class.h" + #include "libpq-fe.h" #include "pqexpbuffer.h" #include "common.h" @@ -85,8 +88,9 @@ typedef struct SchemaQuery /* * Selection condition --- only rows meeting this condition are candidates * to display. If catname mentions multiple tables, include the necessary - * join condition here. For example, "c.relkind = 'r'". Write NULL (not - * an empty string) if not needed. + * join condition here. For example, this might look like "c.relkind = " + * CppAsString2(RELKIND_RELATION). Write NULL (not an empty string) if + * not needed. */ const char *selcondition; @@ -361,7 +365,8 @@ static const SchemaQuery Query_for_list_of_datatypes = { "pg_catalog.pg_type t", /* selcondition --- ignore table rowtypes and array types */ "(t.typrelid = 0 " - " OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) " + " OR (SELECT c.relkind = " CppAsString2(RELKIND_COMPOSITE_TYPE) + " FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) " "AND t.typname !~ '^_'", /* viscondition */ "pg_catalog.pg_type_is_visible(t.oid)", @@ -407,7 +412,7 @@ static const SchemaQuery Query_for_list_of_indexes = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN ('i')", + "c.relkind IN (" CppAsString2(RELKIND_INDEX) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ @@ -422,7 +427,7 @@ static const SchemaQuery Query_for_list_of_sequences = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN ('S')", + "c.relkind IN (" CppAsString2(RELKIND_SEQUENCE) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ @@ -437,7 +442,7 @@ static const SchemaQuery Query_for_list_of_foreign_tables = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN ('f')", + "c.relkind IN (" CppAsString2(RELKIND_FOREIGN_TABLE) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ @@ -452,7 +457,8 @@ static const SchemaQuery Query_for_list_of_tables = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN ('r', 'P')", + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ @@ -467,7 +473,7 @@ static const SchemaQuery Query_for_list_of_partitioned_tables = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN ('P')", + "c.relkind IN (" CppAsString2(RELKIND_PARTITIONED_TABLE) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ @@ -498,7 +504,10 @@ static const SchemaQuery Query_for_list_of_updatables = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN ('r', 'f', 'v', 'P')", + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ", " + CppAsString2(RELKIND_VIEW) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ @@ -528,7 +537,12 @@ static const SchemaQuery Query_for_list_of_tsvmf = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN ('r', 'S', 'v', 'm', 'f', 'P')", + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_SEQUENCE) ", " + CppAsString2(RELKIND_VIEW) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ", " + CppAsString2(RELKIND_PARTITIONED_TABLE) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ @@ -543,7 +557,9 @@ static const SchemaQuery Query_for_list_of_tmf = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN ('r', 'm', 'f')", + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ", " + CppAsString2(RELKIND_FOREIGN_TABLE) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ @@ -558,7 +574,8 @@ static const SchemaQuery Query_for_list_of_tm = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN ('r', 'm')", + "c.relkind IN (" CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ @@ -573,7 +590,7 @@ static const SchemaQuery Query_for_list_of_views = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN ('v')", + "c.relkind IN (" CppAsString2(RELKIND_VIEW) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ @@ -588,7 +605,7 @@ static const SchemaQuery Query_for_list_of_matviews = { /* catname */ "pg_catalog.pg_class c", /* selcondition */ - "c.relkind IN ('m')", + "c.relkind IN (" CppAsString2(RELKIND_MATVIEW) ")", /* viscondition */ "pg_catalog.pg_table_is_visible(c.oid)", /* namespace */ diff --git a/src/bin/scripts/vacuumdb.c b/src/bin/scripts/vacuumdb.c index d3fa51cf1f..e2b187eab3 100644 --- a/src/bin/scripts/vacuumdb.c +++ b/src/bin/scripts/vacuumdb.c @@ -16,6 +16,8 @@ #include #endif +#include "catalog/pg_class.h" + #include "common.h" #include "fe_utils/simple_list.h" #include "fe_utils/string_utils.h" @@ -388,8 +390,12 @@ vacuum_one_database(const char *dbname, vacuumingOptions *vacopts, initPQExpBuffer(&buf); res = executeQuery(conn, - "SELECT c.relname, ns.nspname FROM pg_class c, pg_namespace ns\n" - " WHERE relkind IN (\'r\', \'m\') AND c.relnamespace = ns.oid\n" + "SELECT c.relname, ns.nspname" + " FROM pg_class c, pg_namespace ns\n" + " WHERE relkind IN (" + CppAsString2(RELKIND_RELATION) ", " + CppAsString2(RELKIND_MATVIEW) ")" + " AND c.relnamespace = ns.oid\n" " ORDER BY c.relpages DESC;", progname, echo); -- 2.40.0