summaryrefslogtreecommitdiff
path: root/src/fe_utils/recovery_gen.c
diff options
context:
space:
mode:
authorMasahiko Sawada <msawada@postgresql.org>2025-03-12 16:56:04 -0700
committerMasahiko Sawada <msawada@postgresql.org>2025-03-12 16:56:04 -0700
commit4ecdd4110d5cbaf107c0c85d16df78dffe0a9574 (patch)
tree7692f5f1a8d6387c06765fc05bb754980294b279 /src/fe_utils/recovery_gen.c
parentcdc1471cc7626ffd44416abe92d37de038a0a0ec (diff)
pg_rewind: Add dbname to primary_conninfo when using --write-recovery-conf.
This commit enhances pg_rewind's --write-recovery-conf option to include the dbname in the generated primary_conninfo value when specified in the --source-server option. With this modification, the rewound server can connect to the primary server without manual configuration file modifications when sync_replication_slots is enabled. Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com> Reviewed-by: Peter Smith <smithpb2250@gmail.com> Discussion: https://postgr.es/m/CAD21AoAkW=Ht0k9dVoBTCcqLiiZ2MXhVr+d=j2T_EZMerGrLWQ@mail.gmail.com
Diffstat (limited to 'src/fe_utils/recovery_gen.c')
-rw-r--r--src/fe_utils/recovery_gen.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/fe_utils/recovery_gen.c b/src/fe_utils/recovery_gen.c
index 7c172f65a10..e9023584768 100644
--- a/src/fe_utils/recovery_gen.c
+++ b/src/fe_utils/recovery_gen.c
@@ -14,6 +14,7 @@
#include "fe_utils/string_utils.h"
static char *escape_quotes(const char *src);
+static char *FindDbnameInConnOpts(PQconninfoOption *conn_opts);
/*
* Write recovery configuration contents into a fresh PQExpBuffer, and
@@ -168,3 +169,68 @@ escape_quotes(const char *src)
pg_fatal("out of memory");
return result;
}
+
+/*
+ * FindDbnameInConnOpts
+ *
+ * This is a helper function for GetDbnameFromConnectionOptions(). Extract
+ * the value of dbname from PQconninfoOption parameters, if it's present.
+ * Returns a strdup'd result or NULL.
+ */
+static char *
+FindDbnameInConnOpts(PQconninfoOption *conn_opts)
+{
+ for (PQconninfoOption *conn_opt = conn_opts;
+ conn_opt->keyword != NULL;
+ conn_opt++)
+ {
+ if (strcmp(conn_opt->keyword, "dbname") == 0 &&
+ conn_opt->val != NULL && conn_opt->val[0] != '\0')
+ return pg_strdup(conn_opt->val);
+ }
+ return NULL;
+}
+
+/*
+ * GetDbnameFromConnectionOptions
+ *
+ * This is a special purpose function to retrieve the dbname from either the
+ * 'connstr' specified by the caller or from the environment variables.
+ *
+ * Returns NULL, if dbname is not specified by the user in the given
+ * connection options.
+ */
+char *
+GetDbnameFromConnectionOptions(const char *connstr)
+{
+ PQconninfoOption *conn_opts;
+ char *err_msg = NULL;
+ char *dbname;
+
+ /* First try to get the dbname from connection string. */
+ if (connstr)
+ {
+ conn_opts = PQconninfoParse(connstr, &err_msg);
+ if (conn_opts == NULL)
+ pg_fatal("%s", err_msg);
+
+ dbname = FindDbnameInConnOpts(conn_opts);
+
+ PQconninfoFree(conn_opts);
+ if (dbname)
+ return dbname;
+ }
+
+ /*
+ * Next try to get the dbname from default values that are available from
+ * the environment.
+ */
+ conn_opts = PQconndefaults();
+ if (conn_opts == NULL)
+ pg_fatal("out of memory");
+
+ dbname = FindDbnameInConnOpts(conn_opts);
+
+ PQconninfoFree(conn_opts);
+ return dbname;
+}