diff options
author | Amit Kapila <akapila@postgresql.org> | 2024-03-21 10:48:59 +0530 |
---|---|---|
committer | Amit Kapila <akapila@postgresql.org> | 2024-03-21 10:50:33 +0530 |
commit | a145f424d5248a09d766e8cb503b999290cb3b31 (patch) | |
tree | c78c1df755ced2b228c5d1c4a4b18fa67ba16c95 /src/bin/pg_basebackup/streamutil.c | |
parent | 30e144287a72529c9cd9fd6b07fe96eb8a1e270e (diff) |
Allow dbname to be written as part of connstring via pg_basebackup's -R option.
Commit cca97ce6a665 allowed dbname in pg_basebackup connstring and in this
commit we allow it to be written in postgresql.auto.conf when -R option is
used. The database name in the connection string will be used by the
logical replication slot synchronization on standby.
The dbname will be recorded only if specified explicitly in the connection
string or environment variable.
Masahiko Sawada hasn't reviewed the code in detail but endorsed the idea.
Author: Vignesh C, Kuroda Hayato
Reviewed-by: Amit Kapila
Discussion: https://postgr.es/m/CAB8KJ=hdKdg+UeXhReeHpHA6N6v3e0qFF+ZsPFHk9_ThWKf=2A@mail.gmail.com
Diffstat (limited to 'src/bin/pg_basebackup/streamutil.c')
-rw-r--r-- | src/bin/pg_basebackup/streamutil.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c index 56d1b15951f..9ffd5a6ebb5 100644 --- a/src/bin/pg_basebackup/streamutil.c +++ b/src/bin/pg_basebackup/streamutil.c @@ -34,6 +34,7 @@ int WalSegSz; static bool RetrieveDataDirCreatePerm(PGconn *conn); +static void FindDbnameInConnParams(PQconninfoOption *conn_opts, char **dbname); /* SHOW command for replication connection was introduced in version 10 */ #define MINIMUM_VERSION_FOR_SHOW_CMD 100000 @@ -268,6 +269,75 @@ GetConnection(void) } /* + * FindDbnameInConnParams + * + * This is a helper function for GetDbnameFromConnectionOptions(). Extract + * the value of dbname from PQconninfoOption parameters. + */ +static void +FindDbnameInConnParams(PQconninfoOption *conn_opts, char **dbname) +{ + PQconninfoOption *conn_opt; + + Assert(dbname != NULL); + + for (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') + *dbname = pg_strdup(conn_opt->val); + } +} + +/* + * GetDbnameFromConnectionOptions + * + * This is a special purpose function to retrieve the dbname from either the + * connection_string specified by the user or from the environment variables. + * + * We follow GetConnection() to fetch the dbname from various connection + * options. + * + * Returns NULL, if dbname is not specified by the user in the above + * mentioned connection options. + */ +char * +GetDbnameFromConnectionOptions(void) +{ + PQconninfoOption *conn_opts = NULL; + char *err_msg = NULL; + char *dbname = NULL; + + /* First try to get the dbname from connection string. */ + if (connection_string) + { + conn_opts = PQconninfoParse(connection_string, &err_msg); + if (conn_opts == NULL) + pg_fatal("%s", err_msg); + + FindDbnameInConnParams(conn_opts, &dbname); + if (dbname) + { + PQconninfoFree(conn_opts); + 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"); + + FindDbnameInConnParams(conn_opts, &dbname); + + PQconninfoFree(conn_opts); + return dbname; +} + +/* * From version 10, explicitly set wal segment size using SHOW wal_segment_size * since ControlFile is not accessible here. */ |