Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitc6d0b9b

Browse files
committed
Fix connection string handling in src/bin/scripts/ programs.
When told to process all databases, clusterdb, reindexdb, and vacuumdbwould reconnect by replacing their --maintenance-db parameter with thename of the target database. If that parameter is a connstring (whichhas been allowed for a long time, though we failed to document thatbefore this patch), we'd lose any other options it might specify, forexample SSL or GSS parameters, possibly resulting in failure to connect.Thus, this is the same bug as commita45bc8a fixed in pg_dump andpg_restore. We can fix it in the same way, by using libpq's rules forhandling multiple "dbname" parameters to add the target database nameseparately. I chose to apply the same refactoring approach as in thatpatch, with a struct to handle the command line parameters that need tobe passed through to connectDatabase. (Maybe someday we can unify thevery similar functions here and in pg_dump/pg_restore.)Per Peter Eisentraut's comments on bug #16604. Back-patch to allsupported branches.Discussion:https://postgr.es/m/16604-933f4b8791227b15@postgresql.org
1 parent7e175c5 commitc6d0b9b

File tree

14 files changed

+254
-196
lines changed

14 files changed

+254
-196
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ PostgreSQL documentation
9090
<term><option><optional>--dbname=</optional><replaceable class="parameter">dbname</replaceable></option></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 notused, 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 thisis notspecified, 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></option></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

Lines 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

Lines 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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ PostgreSQL documentation
134134
<term><option><optional>--dbname=</optional><replaceable class="parameter">dbname</replaceable></option></term>
135135
<listitem>
136136
<para>
137-
Specifies the name of the database to be reindexed.
138-
If this is not specified and<option>-a</option> (or
139-
<option>--all</option>)is notused, the database name is read
137+
Specifies the name of the database to be reindexed,
138+
when<option>-a</option>/<option>--all</option> is not used.
139+
If thisis notspecified, the database name is read
140140
from the environment variable <envar>PGDATABASE</envar>. If
141141
that is not set, the user name specified for the connection is
142142
used. The <replaceable>dbname</replaceable> can be a <link
@@ -328,10 +328,16 @@ PostgreSQL documentation
328328
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
329329
<listitem>
330330
<para>
331-
Specifies the name of the database to connect to discover what other
332-
databases should be reindexed. If not specified, the
333-
<literal>postgres</literal> database will be used,
334-
and if that does not exist, <literal>template1</literal> will be used.
331+
Specifies the name of the database to connect to to discover which
332+
databases should be reindexed,
333+
when <option>-a</option>/<option>--all</option> is used.
334+
If not specified, the <literal>postgres</literal> database will be used,
335+
or if that does not exist, <literal>template1</literal> will be used.
336+
This can be a <link linkend="libpq-connstring">connection
337+
string</link>. If so, connection string parameters will override any
338+
conflicting command line options. Also, connection string parameters
339+
other than the database name itself will be re-used when connecting
340+
to other databases.
335341
</para>
336342
</listitem>
337343
</varlistentry>

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ PostgreSQL documentation
9292
<term><option><optional>--dbname=</optional><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 notused, 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 thisis notspecified, 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
@@ -426,10 +426,16 @@ PostgreSQL documentation
426426
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
427427
<listitem>
428428
<para>
429-
Specifies the name of the database to connect to discover what other
430-
databases should be vacuumed. If not specified, the
431-
<literal>postgres</literal> database will be used,
432-
and if that does not exist, <literal>template1</literal> will be used.
429+
Specifies the name of the database to connect to to discover which
430+
databases should be vacuumed,
431+
when <option>-a</option>/<option>--all</option> is used.
432+
If not specified, the <literal>postgres</literal> database will be used,
433+
or if that does not exist, <literal>template1</literal> will be used.
434+
This can be a <link linkend="libpq-connstring">connection
435+
string</link>. If so, connection string parameters will override any
436+
conflicting command line options. Also, connection string parameters
437+
other than the database name itself will be re-used when connecting
438+
to other databases.
433439
</para>
434440
</listitem>
435441
</varlistentry>

‎src/bin/scripts/clusterdb.c

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@
1616
#include"fe_utils/string_utils.h"
1717

1818

19-
staticvoidcluster_one_database(constchar*dbname,boolverbose,constchar*table,
20-
constchar*host,constchar*port,
21-
constchar*username,enumtrivalueprompt_password,
22-
constchar*progname,boolecho);
23-
staticvoidcluster_all_databases(boolverbose,constchar*maintenance_db,
24-
constchar*host,constchar*port,
25-
constchar*username,enumtrivalueprompt_password,
26-
constchar*progname,boolecho,boolquiet);
27-
19+
staticvoidcluster_one_database(constConnParams*cparams,constchar*table,
20+
constchar*progname,boolverbose,boolecho);
21+
staticvoidcluster_all_databases(ConnParams*cparams,constchar*progname,
22+
boolverbose,boolecho,boolquiet);
2823
staticvoidhelp(constchar*progname);
2924

3025

@@ -57,6 +52,7 @@ main(int argc, char *argv[])
5752
char*port=NULL;
5853
char*username=NULL;
5954
enumtrivalueprompt_password=TRI_DEFAULT;
55+
ConnParamscparams;
6056
boolecho= false;
6157
boolquiet= false;
6258
boolalldb= false;
@@ -133,6 +129,13 @@ main(int argc, char *argv[])
133129
exit(1);
134130
}
135131

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

138141
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
staticvoid
189-
cluster_one_database(constchar*dbname,boolverbose,constchar*table,
190-
constchar*host,constchar*port,
191-
constchar*username,enumtrivalueprompt_password,
192-
constchar*progname,boolecho)
193+
cluster_one_database(constConnParams*cparams,constchar*table,
194+
constchar*progname,boolverbose,boolecho)
193195
{
194196
PQExpBufferDatasql;
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
staticvoid
230-
cluster_all_databases(boolverbose,constchar*maintenance_db,
231-
constchar*host,constchar*port,
232-
constchar*username,enumtrivalueprompt_password,
233-
constchar*progname,boolecho,boolquiet)
231+
cluster_all_databases(ConnParams*cparams,constchar*progname,
232+
boolverbose,boolecho,boolquiet)
234233
{
235234
PGconn*conn;
236235
PGresult*result;
237-
PQExpBufferDataconnstr;
238236
inti;
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)

[8]ページ先頭

©2009-2025 Movatter.jp