aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2024-11-26 09:45:34 +0900
committerMichael Paquier <michael@paquier.xyz>2024-11-26 09:45:34 +0900
commit91f5a4a000ea4979e5490e0a111c24f4486d7361 (patch)
tree004f89ae72bfc829bbc02fd223122f5fa740000b /src
parentcc4c90cef918fc0cbd13af8b51838c5de5da73bb (diff)
downloadpostgresql-91f5a4a000ea4979e5490e0a111c24f4486d7361.tar.gz
postgresql-91f5a4a000ea4979e5490e0a111c24f4486d7361.zip
pg_amcheck: Use CppAsString2() for relkind and relpersistence in queries
This utility has been using hardcoded values for relkind and relpersistence in its queries generated. These queries are switched to use CppAsString2() instead, with the values fetched directly from the header of pg_class. This has the advantage of making the code more self-documented, as it becomes unnecessary to look at a header for the meaning of a value. There should be no functional changes; the queries are generated the same way as before this commit. Reviewed-by: Nathan Bossart, Daniel Gustafsson, Álvaro Herrera, Karina Litskevich Discussion: https://postgr.es/m/ZxIvemDk0Ob1RGwh@paquier.xyz
Diffstat (limited to 'src')
-rw-r--r--src/bin/pg_amcheck/pg_amcheck.c42
1 files changed, 29 insertions, 13 deletions
diff --git a/src/bin/pg_amcheck/pg_amcheck.c b/src/bin/pg_amcheck/pg_amcheck.c
index 0c05cf58bce..27a7d5e925e 100644
--- a/src/bin/pg_amcheck/pg_amcheck.c
+++ b/src/bin/pg_amcheck/pg_amcheck.c
@@ -16,6 +16,7 @@
#include <time.h>
#include "catalog/pg_am_d.h"
+#include "catalog/pg_class_d.h"
#include "catalog/pg_namespace_d.h"
#include "common/logging.h"
#include "common/username.h"
@@ -857,7 +858,7 @@ prepare_heap_command(PQExpBuffer sql, RelationInfo *rel, PGconn *conn)
appendPQExpBuffer(sql,
"\n) v WHERE c.oid = %u "
- "AND c.relpersistence != 't'",
+ "AND c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP),
rel->reloid);
}
@@ -890,7 +891,7 @@ prepare_btree_command(PQExpBuffer sql, RelationInfo *rel, PGconn *conn)
"\nFROM pg_catalog.pg_class c, pg_catalog.pg_index i "
"WHERE c.oid = %u "
"AND c.oid = i.indexrelid "
- "AND c.relpersistence != 't' "
+ "AND c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP) " "
"AND i.indisready AND i.indisvalid AND i.indislive",
rel->datinfo->amcheck_schema,
(opts.heapallindexed ? "true" : "false"),
@@ -905,7 +906,7 @@ prepare_btree_command(PQExpBuffer sql, RelationInfo *rel, PGconn *conn)
"\nFROM pg_catalog.pg_class c, pg_catalog.pg_index i "
"WHERE c.oid = %u "
"AND c.oid = i.indexrelid "
- "AND c.relpersistence != 't' "
+ "AND c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP) " "
"AND i.indisready AND i.indisvalid AND i.indislive",
rel->datinfo->amcheck_schema,
(opts.heapallindexed ? "true" : "false"),
@@ -1952,7 +1953,8 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
* until firing off the amcheck command, as the state of an index may
* change by then.
*/
- appendPQExpBufferStr(&sql, "\nWHERE c.relpersistence != 't'");
+ appendPQExpBufferStr(&sql, "\nWHERE c.relpersistence != "
+ CppAsString2(RELPERSISTENCE_TEMP));
if (opts.excludetbl || opts.excludeidx || opts.excludensp)
appendPQExpBufferStr(&sql, "\nAND ep.pattern_id IS NULL");
@@ -1972,15 +1974,29 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
if (opts.allrel)
appendPQExpBuffer(&sql,
" AND c.relam = %u "
- "AND c.relkind IN ('r', 'S', 'm', 't') "
+ "AND c.relkind IN ("
+ CppAsString2(RELKIND_RELATION) ", "
+ CppAsString2(RELKIND_SEQUENCE) ", "
+ CppAsString2(RELKIND_MATVIEW) ", "
+ CppAsString2(RELKIND_TOASTVALUE) ") "
"AND c.relnamespace != %u",
HEAP_TABLE_AM_OID, PG_TOAST_NAMESPACE);
else
appendPQExpBuffer(&sql,
" AND c.relam IN (%u, %u)"
- "AND c.relkind IN ('r', 'S', 'm', 't', 'i') "
- "AND ((c.relam = %u AND c.relkind IN ('r', 'S', 'm', 't')) OR "
- "(c.relam = %u AND c.relkind = 'i'))",
+ "AND c.relkind IN ("
+ CppAsString2(RELKIND_RELATION) ", "
+ CppAsString2(RELKIND_SEQUENCE) ", "
+ CppAsString2(RELKIND_MATVIEW) ", "
+ CppAsString2(RELKIND_TOASTVALUE) ", "
+ CppAsString2(RELKIND_INDEX) ") "
+ "AND ((c.relam = %u AND c.relkind IN ("
+ CppAsString2(RELKIND_RELATION) ", "
+ CppAsString2(RELKIND_SEQUENCE) ", "
+ CppAsString2(RELKIND_MATVIEW) ", "
+ CppAsString2(RELKIND_TOASTVALUE) ")) OR "
+ "(c.relam = %u AND c.relkind = "
+ CppAsString2(RELKIND_INDEX) "))",
HEAP_TABLE_AM_OID, BTREE_AM_OID,
HEAP_TABLE_AM_OID, BTREE_AM_OID);
@@ -2007,7 +2023,7 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
"\nAND (t.relname ~ ep.rel_regex OR ep.rel_regex IS NULL)"
"\nAND ep.heap_only"
"\nWHERE ep.pattern_id IS NULL"
- "\nAND t.relpersistence != 't'");
+ "\nAND t.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP));
appendPQExpBufferStr(&sql,
"\n)");
}
@@ -2026,7 +2042,7 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
"ON r.oid = i.indrelid "
"INNER JOIN pg_catalog.pg_class c "
"ON i.indexrelid = c.oid "
- "AND c.relpersistence != 't'");
+ "AND c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP));
if (opts.excludeidx || opts.excludensp)
appendPQExpBufferStr(&sql,
"\nINNER JOIN pg_catalog.pg_namespace n "
@@ -2041,7 +2057,7 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
"\nWHERE true");
appendPQExpBuffer(&sql,
" AND c.relam = %u "
- "AND c.relkind = 'i'",
+ "AND c.relkind = " CppAsString2(RELKIND_INDEX),
BTREE_AM_OID);
if (opts.no_toast_expansion)
appendPQExpBuffer(&sql,
@@ -2065,7 +2081,7 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
"ON t.oid = i.indrelid"
"\nINNER JOIN pg_catalog.pg_class c "
"ON i.indexrelid = c.oid "
- "AND c.relpersistence != 't'");
+ "AND c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP));
if (opts.excludeidx)
appendPQExpBufferStr(&sql,
"\nLEFT OUTER JOIN exclude_pat ep "
@@ -2078,7 +2094,7 @@ compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
"\nWHERE true");
appendPQExpBuffer(&sql,
" AND c.relam = %u"
- " AND c.relkind = 'i')",
+ " AND c.relkind = " CppAsString2(RELKIND_INDEX) ")",
BTREE_AM_OID);
}