Keeping the SQL commands that initdb runs in string arrays before
feeding them to PG_CMD_PUTS() seems unnecessarily verbose and
inflexible. In some cases, the array only has one member. In other
cases, one might want to use PG_CMD_PRINTF() instead, to parametrize a
command, but that would require breaking up the loop or using
workarounds like replace_token(). This patch unwinds all that; it's
much simpler that way.From e177a142a9a5412ff8aeb271330005ef518b32d1 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut
Date: Thu, 1 Dec 2022 09:49:48 +0100
Subject: [PATCH] initdb: Refactor PG_CMD_PUTS loops
Keeping the SQL commands that initdb runs in string arrays before
feeding them to PG_CMD_PUTS() seems unnecessarily verbose and
inflexible. In some cases, the array only has one member. In other
cases, one might want to use PG_CMD_PRINTF() instead, to parametrize a
command, but that would require breaking up the loop or using
workarounds like replace_token(). Unwind all that; it's much simpler
that way.
---
src/bin/initdb/initdb.c | 379 ++--
1 file changed, 170 insertions(+), 209 deletions(-)
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index f61a04305590..7c391aaf0b13 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -1350,18 +1350,11 @@ bootstrap_template1(void)
static void
setup_auth(FILE *cmdfd)
{
- const char *const *line;
- static const char *const pg_authid_setup[] = {
- /*
-* The authid table shouldn't be readable except through views,
to
-* ensure passwords are not publicly visible.
-*/
- "REVOKE ALL ON pg_authid FROM public;\n\n",
- NULL
- };
-
- for (line = pg_authid_setup; *line != NULL; line++)
- PG_CMD_PUTS(*line);
+ /*
+* The authid table shouldn't be readable except through views, to
+* ensure passwords are not publicly visible.
+*/
+ PG_CMD_PUTS("REVOKE ALL ON pg_authid FROM public;\n\n");
if (superuser_password)
PG_CMD_PRINTF("ALTER USER \"%s\" WITH PASSWORD E'%s';\n\n",
@@ -1433,18 +1426,11 @@ get_su_pwd(void)
static void
setup_depend(FILE *cmdfd)
{
- const char *const *line;
- static const char *const pg_depend_setup[] = {
- /*
-* Advance the OID counter so that subsequently-created objects
aren't
-* pinned.
-*/
- "SELECT pg_stop_making_pinned_objects();\n\n",
- NULL
- };
-
- for (line = pg_depend_setup; *line != NULL; line++)
- PG_CMD_PUTS(*line);
+ /*
+* Advance the OID counter so that subsequently-created objects aren't
+* pinned.
+*/
+ PG_CMD_PUTS("SELECT pg_stop_making_pinned_objects();\n\n");
}
/*
@@ -1530,147 +1516,138 @@ setup_collation(FILE *cmdfd)
static void
setup_privileges(FILE *cmdfd)
{
- char **line;
- char **priv_lines;
- static char *privileges_setup[] = {
- "UPDATE pg_class "
- " SET relacl = (SELECT array_agg(a.acl) FROM "
- " (SELECT E'=r/\"$POSTGRES_SUPERUSERNAME\"' as acl "
- " UNION SELECT unnest(pg_catalog.acldefault("
- "CASE WHEN relkind = " CppAsString2(RELKIND_SEQUENCE) "
THEN 's' "
- " ELSE 'r' END::\"char\","
CppAsString2(BOOTSTRAP_SUPERUSERID) "::oid))"
- " ) as a) "
- " 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, public TO PUBLIC;\n\n",
- "REVOKE ALL ON pg_largeobject FROM PUBLIC;\n\n",
- "INSERT INTO pg_init_privs "
- " (objoid, classoid, objsubid, initprivs, privtype)"
- "SELECT"
- "oid,"
- "(SELECT oid FROM pg_class WHERE relname = 'pg_class'),"
- "0,"
- "relacl,"
- "'i'"
- "FROM"
- "pg_class"
- "WHERE"
- "relacl IS NOT NULL"
- "AND relkind IN (" CppAsString2(RELKIND_RELATION) ", "
- CppAsString2(RELKIND_VIEW) ", " Cp