|
34 | 34 | int WalSegSz;
|
35 | 35 |
|
36 | 36 | static bool RetrieveDataDirCreatePerm(PGconn *conn);
|
| 37 | +static void FindDbnameInConnParams(PQconninfoOption *conn_opts, char **dbname); |
37 | 38 |
|
38 | 39 | /* SHOW command for replication connection was introduced in version 10 */
|
39 | 40 | #define MINIMUM_VERSION_FOR_SHOW_CMD 100000
|
@@ -267,6 +268,75 @@ GetConnection(void)
|
267 | 268 | return tmpconn;
|
268 | 269 | }
|
269 | 270 |
|
| 271 | +/* |
| 272 | + * FindDbnameInConnParams |
| 273 | + * |
| 274 | + * This is a helper function for GetDbnameFromConnectionOptions(). Extract |
| 275 | + * the value of dbname from PQconninfoOption parameters. |
| 276 | + */ |
| 277 | +static void |
| 278 | +FindDbnameInConnParams(PQconninfoOption *conn_opts, char **dbname) |
| 279 | +{ |
| 280 | + PQconninfoOption *conn_opt; |
| 281 | + |
| 282 | + Assert(dbname != NULL); |
| 283 | + |
| 284 | + for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++) |
| 285 | + { |
| 286 | + if ((strcmp(conn_opt->keyword, "dbname") == 0) && |
| 287 | + conn_opt->val != NULL && conn_opt->val[0] != '\0') |
| 288 | + *dbname = pg_strdup(conn_opt->val); |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | +/* |
| 293 | + * GetDbnameFromConnectionOptions |
| 294 | + * |
| 295 | + * This is a special purpose function to retrieve the dbname from either the |
| 296 | + * connection_string specified by the user or from the environment variables. |
| 297 | + * |
| 298 | + * We follow GetConnection() to fetch the dbname from various connection |
| 299 | + * options. |
| 300 | + * |
| 301 | + * Returns NULL, if dbname is not specified by the user in the above |
| 302 | + * mentioned connection options. |
| 303 | + */ |
| 304 | +char * |
| 305 | +GetDbnameFromConnectionOptions(void) |
| 306 | +{ |
| 307 | + PQconninfoOption *conn_opts = NULL; |
| 308 | + char *err_msg = NULL; |
| 309 | + char *dbname = NULL; |
| 310 | + |
| 311 | + /* First try to get the dbname from connection string. */ |
| 312 | + if (connection_string) |
| 313 | + { |
| 314 | + conn_opts = PQconninfoParse(connection_string, &err_msg); |
| 315 | + if (conn_opts == NULL) |
| 316 | + pg_fatal("%s", err_msg); |
| 317 | + |
| 318 | + FindDbnameInConnParams(conn_opts, &dbname); |
| 319 | + if (dbname) |
| 320 | + { |
| 321 | + PQconninfoFree(conn_opts); |
| 322 | + return dbname; |
| 323 | + } |
| 324 | + } |
| 325 | + |
| 326 | + /* |
| 327 | + * Next try to get the dbname from default values that are available from |
| 328 | + * the environment. |
| 329 | + */ |
| 330 | + conn_opts = PQconndefaults(); |
| 331 | + if (conn_opts == NULL) |
| 332 | + pg_fatal("out of memory"); |
| 333 | + |
| 334 | + FindDbnameInConnParams(conn_opts, &dbname); |
| 335 | + |
| 336 | + PQconninfoFree(conn_opts); |
| 337 | + return dbname; |
| 338 | +} |
| 339 | + |
270 | 340 | /*
|
271 | 341 | * From version 10, explicitly set wal segment size using SHOW wal_segment_size
|
272 | 342 | * since ControlFile is not accessible here.
|
|
0 commit comments