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

Commit68f2369

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 parent463bef2 commit68f2369

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=</><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 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></></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
@@ -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 notused, 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 thisis notspecified, 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

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=</><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
@@ -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

Lines 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-
staticvoidcluster_one_database(constchar*dbname,boolverbose,constchar*table,
19-
constchar*host,constchar*port,
20-
constchar*username,enumtrivalueprompt_password,
21-
constchar*progname,boolecho);
22-
staticvoidcluster_all_databases(boolverbose,constchar*maintenance_db,
23-
constchar*host,constchar*port,
24-
constchar*username,enumtrivalueprompt_password,
25-
constchar*progname,boolecho,boolquiet);
26-
18+
staticvoidcluster_one_database(constConnParams*cparams,constchar*table,
19+
constchar*progname,boolverbose,boolecho);
20+
staticvoidcluster_all_databases(ConnParams*cparams,constchar*progname,
21+
boolverbose,boolecho,boolquiet);
2722
staticvoidhelp(constchar*progname);
2823

2924

@@ -56,6 +51,7 @@ main(int argc, char *argv[])
5651
char*port=NULL;
5752
char*username=NULL;
5853
enumtrivalueprompt_password=TRI_DEFAULT;
54+
ConnParamscparams;
5955
boolecho= false;
6056
boolquiet= false;
6157
boolalldb= 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
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