Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5c78f79

Browse filesBrowse files
committed
Fix connection string handling in src/bin/scripts/ programs.
When told to process all databases, clusterdb, reindexdb, and vacuumdb would reconnect by replacing their --maintenance-db parameter with the name of the target database. If that parameter is a connstring (which has been allowed for a long time, though we failed to document that before this patch), we'd lose any other options it might specify, for example SSL or GSS parameters, possibly resulting in failure to connect. Thus, this is the same bug as commit a45bc8a fixed in pg_dump and pg_restore. We can fix it in the same way, by using libpq's rules for handling multiple "dbname" parameters to add the target database name separately. I chose to apply the same refactoring approach as in that patch, with a struct to handle the command line parameters that need to be passed through to connectDatabase. (Maybe someday we can unify the very similar functions here and in pg_dump/pg_restore.) Per Peter Eisentraut's comments on bug #16604. Back-patch to all supported branches. Discussion: https://postgr.es/m/16604-933f4b8791227b15@postgresql.org
1 parent 67a6af6 commit 5c78f79
Copy full SHA for 5c78f79

File tree

Expand file treeCollapse file tree

16 files changed

+274
-200
lines changed
Filter options
Expand file treeCollapse file tree

16 files changed

+274
-200
lines changed

‎doc/src/sgml/ref/clusterdb.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/clusterdb.sgml
+13-7Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ PostgreSQL documentation
9090
<term><option><optional>--dbname=</><replaceable class="parameter">dbname</replaceable></></term>
9191
<listitem>
9292
<para>
93-
Specifies the name of the database to be clustered.
94-
If this is not specified and <option>-a</option> (or
95-
<option>--all</option>) is not used, the database name is read
93+
Specifies the name of the database to be clustered,
94+
when <option>-a</option>/<option>--all</option> is not used.
95+
If this is not specified, the database name is read
9696
from the environment variable <envar>PGDATABASE</envar>. If
9797
that is not set, the user name specified for the connection is
9898
used. The <replaceable>dbname</replaceable> can be a <link
@@ -249,10 +249,16 @@ PostgreSQL documentation
249249
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></></term>
250250
<listitem>
251251
<para>
252-
Specifies the name of the database to connect to discover what other
253-
databases should be clustered. If not specified, the
254-
<literal>postgres</literal> database will be used,
255-
and if that does not exist, <literal>template1</literal> will be used.
252+
Specifies the name of the database to connect to to discover which
253+
databases should be clustered,
254+
when <option>-a</option>/<option>--all</option> is used.
255+
If not specified, the <literal>postgres</literal> database will be used,
256+
or if that does not exist, <literal>template1</literal> will be used.
257+
This can be a <link linkend="libpq-connstring">connection
258+
string</link>. If so, connection string parameters will override any
259+
conflicting command line options. Also, connection string parameters
260+
other than the database name itself will be re-used when connecting
261+
to other databases.
256262
</para>
257263
</listitem>
258264
</varlistentry>

‎doc/src/sgml/ref/createdb.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/createdb.sgml
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ PostgreSQL documentation
286286
database will be used; if that does not exist (or if it is the name
287287
of the new database being created), <literal>template1</literal> will
288288
be used.
289+
This can be a <link linkend="libpq-connstring">connection
290+
string</link>. If so, connection string parameters will override any
291+
conflicting command line options.
289292
</para>
290293
</listitem>
291294
</varlistentry>

‎doc/src/sgml/ref/dropdb.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/dropdb.sgml
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ PostgreSQL documentation
205205
target database. If not specified, the <literal>postgres</literal>
206206
database will be used; if that does not exist (or is the database
207207
being dropped), <literal>template1</literal> will be used.
208+
This can be a <link linkend="libpq-connstring">connection
209+
string</link>. If so, connection string parameters will override any
210+
conflicting command line options.
208211
</para>
209212
</listitem>
210213
</varlistentry>

‎doc/src/sgml/ref/reindexdb.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/reindexdb.sgml
+13-7Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ PostgreSQL documentation
123123
<term><option><optional>--dbname=</><replaceable class="parameter">dbname</replaceable></></term>
124124
<listitem>
125125
<para>
126-
Specifies the name of the database to be reindexed.
127-
If this is not specified and <option>-a</option> (or
128-
<option>--all</option>) is not used, the database name is read
126+
Specifies the name of the database to be reindexed,
127+
when <option>-a</option>/<option>--all</option> is not used.
128+
If this is not specified, the database name is read
129129
from the environment variable <envar>PGDATABASE</envar>. If
130130
that is not set, the user name specified for the connection is
131131
used. The <replaceable>dbname</replaceable> can be a <link
@@ -317,10 +317,16 @@ PostgreSQL documentation
317317
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></></term>
318318
<listitem>
319319
<para>
320-
Specifies the name of the database to connect to discover what other
321-
databases should be reindexed. If not specified, the
322-
<literal>postgres</literal> database will be used,
323-
and if that does not exist, <literal>template1</literal> will be used.
320+
Specifies the name of the database to connect to to discover which
321+
databases should be reindexed,
322+
when <option>-a</option>/<option>--all</option> is used.
323+
If not specified, the <literal>postgres</literal> database will be used,
324+
or if that does not exist, <literal>template1</literal> will be used.
325+
This can be a <link linkend="libpq-connstring">connection
326+
string</link>. If so, connection string parameters will override any
327+
conflicting command line options. Also, connection string parameters
328+
other than the database name itself will be re-used when connecting
329+
to other databases.
324330
</para>
325331
</listitem>
326332
</varlistentry>

‎doc/src/sgml/ref/vacuumdb.sgml

Copy file name to clipboardExpand all lines: doc/src/sgml/ref/vacuumdb.sgml
+13-7Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ PostgreSQL documentation
9292
<term><option><optional>--dbname=</><replaceable class="parameter">dbname</replaceable></option></term>
9393
<listitem>
9494
<para>
95-
Specifies the name of the database to be cleaned or analyzed.
96-
If this is not specified and <option>-a</option> (or
97-
<option>--all</option>) is not used, the database name is read
95+
Specifies the name of the database to be cleaned or analyzed,
96+
when <option>-a</option>/<option>--all</option> is not used.
97+
If this is not specified, the database name is read
9898
from the environment variable <envar>PGDATABASE</envar>. If
9999
that is not set, the user name specified for the connection is
100100
used. The <replaceable>dbname</replaceable> can be a <link
@@ -342,10 +342,16 @@ PostgreSQL documentation
342342
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></></term>
343343
<listitem>
344344
<para>
345-
Specifies the name of the database to connect to discover what other
346-
databases should be vacuumed. If not specified, the
347-
<literal>postgres</literal> database will be used,
348-
and if that does not exist, <literal>template1</literal> will be used.
345+
Specifies the name of the database to connect to to discover which
346+
databases should be vacuumed,
347+
when <option>-a</option>/<option>--all</option> is used.
348+
If not specified, the <literal>postgres</literal> database will be used,
349+
or if that does not exist, <literal>template1</literal> will be used.
350+
This can be a <link linkend="libpq-connstring">connection
351+
string</link>. If so, connection string parameters will override any
352+
conflicting command line options. Also, connection string parameters
353+
other than the database name itself will be re-used when connecting
354+
to other databases.
349355
</para>
350356
</listitem>
351357
</varlistentry>

‎src/bin/scripts/clusterdb.c

Copy file name to clipboardExpand all lines: src/bin/scripts/clusterdb.c
+29-38Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@
1515
#include "fe_utils/string_utils.h"
1616

1717

18-
static void cluster_one_database(const char *dbname, bool verbose, const char *table,
19-
const char *host, const char *port,
20-
const char *username, enum trivalue prompt_password,
21-
const char *progname, bool echo);
22-
static void cluster_all_databases(bool verbose, const char *maintenance_db,
23-
const char *host, const char *port,
24-
const char *username, enum trivalue prompt_password,
25-
const char *progname, bool echo, bool quiet);
26-
18+
static void cluster_one_database(const ConnParams *cparams, const char *table,
19+
const char *progname, bool verbose, bool echo);
20+
static void cluster_all_databases(ConnParams *cparams, const char *progname,
21+
bool verbose, bool echo, bool quiet);
2722
static void help(const char *progname);
2823

2924

@@ -56,6 +51,7 @@ main(int argc, char *argv[])
5651
char *port = NULL;
5752
char *username = NULL;
5853
enum trivalue prompt_password = TRI_DEFAULT;
54+
ConnParams cparams;
5955
bool echo = false;
6056
bool quiet = false;
6157
bool alldb = false;
@@ -131,6 +127,13 @@ main(int argc, char *argv[])
131127
exit(1);
132128
}
133129

130+
/* fill cparams except for dbname, which is set below */
131+
cparams.pghost = host;
132+
cparams.pgport = port;
133+
cparams.pguser = username;
134+
cparams.prompt_password = prompt_password;
135+
cparams.override_dbname = NULL;
136+
134137
setup_cancel_handler();
135138

136139
if (alldb)
@@ -149,8 +152,9 @@ main(int argc, char *argv[])
149152
exit(1);
150153
}
151154

152-
cluster_all_databases(verbose, maintenance_db, host, port, username, prompt_password,
153-
progname, echo, quiet);
155+
cparams.dbname = maintenance_db;
156+
157+
cluster_all_databases(&cparams, progname, verbose, echo, quiet);
154158
}
155159
else
156160
{
@@ -164,39 +168,36 @@ main(int argc, char *argv[])
164168
dbname = get_user_name_or_exit(progname);
165169
}
166170

171+
cparams.dbname = dbname;
172+
167173
if (tables.head != NULL)
168174
{
169175
SimpleStringListCell *cell;
170176

171177
for (cell = tables.head; cell; cell = cell->next)
172178
{
173-
cluster_one_database(dbname, verbose, cell->val,
174-
host, port, username, prompt_password,
175-
progname, echo);
179+
cluster_one_database(&cparams, cell->val,
180+
progname, verbose, echo);
176181
}
177182
}
178183
else
179-
cluster_one_database(dbname, verbose, NULL,
180-
host, port, username, prompt_password,
181-
progname, echo);
184+
cluster_one_database(&cparams, NULL,
185+
progname, verbose, echo);
182186
}
183187

184188
exit(0);
185189
}
186190

187191

188192
static void
189-
cluster_one_database(const char *dbname, bool verbose, const char *table,
190-
const char *host, const char *port,
191-
const char *username, enum trivalue prompt_password,
192-
const char *progname, bool echo)
193+
cluster_one_database(const ConnParams *cparams, const char *table,
194+
const char *progname, bool verbose, bool echo)
193195
{
194196
PQExpBufferData sql;
195197

196198
PGconn *conn;
197199

198-
conn = connectDatabase(dbname, host, port, username, prompt_password,
199-
progname, echo, false, false);
200+
conn = connectDatabase(cparams, progname, echo, false, false);
200201

201202
initPQExpBuffer(&sql);
202203

@@ -227,22 +228,17 @@ cluster_one_database(const char *dbname, bool verbose, const char *table,
227228

228229

229230
static void
230-
cluster_all_databases(bool verbose, const char *maintenance_db,
231-
const char *host, const char *port,
232-
const char *username, enum trivalue prompt_password,
233-
const char *progname, bool echo, bool quiet)
231+
cluster_all_databases(ConnParams *cparams, const char *progname,
232+
bool verbose, bool echo, bool quiet)
234233
{
235234
PGconn *conn;
236235
PGresult *result;
237-
PQExpBufferData connstr;
238236
int i;
239237

240-
conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
241-
prompt_password, progname, echo);
238+
conn = connectMaintenanceDatabase(cparams, progname, echo);
242239
result = executeQuery(conn, "SELECT datname FROM pg_database WHERE datallowconn ORDER BY 1;", progname, echo);
243240
PQfinish(conn);
244241

245-
initPQExpBuffer(&connstr);
246242
for (i = 0; i < PQntuples(result); i++)
247243
{
248244
char *dbname = PQgetvalue(result, i, 0);
@@ -253,15 +249,10 @@ cluster_all_databases(bool verbose, const char *maintenance_db,
253249
fflush(stdout);
254250
}
255251

256-
resetPQExpBuffer(&connstr);
257-
appendPQExpBuffer(&connstr, "dbname=");
258-
appendConnStrVal(&connstr, dbname);
252+
cparams->override_dbname = dbname;
259253

260-
cluster_one_database(connstr.data, verbose, NULL,
261-
host, port, username, prompt_password,
262-
progname, echo);
254+
cluster_one_database(cparams, NULL, progname, verbose, echo);
263255
}
264-
termPQExpBuffer(&connstr);
265256

266257
PQclear(result);
267258
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.