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

Commit8e5793a

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 parentc8ab970 commit8e5793a

16 files changed

+260
-206
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
@@ -284,6 +284,9 @@ PostgreSQL documentation
284284
database will be used; if that does not exist (or if it is the name
285285
of the new database being created), <literal>template1</literal> will
286286
be used.
287+
This can be a <link linkend="libpq-connstring">connection
288+
string</link>. If so, connection string parameters will override any
289+
conflicting command line options.
287290
</para>
288291
</listitem>
289292
</varlistentry>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ PostgreSQL documentation
217217
target database. If not specified, the <literal>postgres</literal>
218218
database will be used; if that does not exist (or is the database
219219
being dropped), <literal>template1</literal> will be used.
220+
This can be a <link linkend="libpq-connstring">connection
221+
string</link>. If so, connection string parameters will override any
222+
conflicting command line options.
220223
</para>
221224
</listitem>
222225
</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
@@ -351,10 +351,16 @@ PostgreSQL documentation
351351
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
352352
<listitem>
353353
<para>
354-
Specifies the name of the database to connect to discover what other
355-
databases should be reindexed. If not specified, the
356-
<literal>postgres</literal> database will be used,
357-
and if that does not exist, <literal>template1</literal> will be used.
354+
Specifies the name of the database to connect to to discover which
355+
databases should be reindexed,
356+
when <option>-a</option>/<option>--all</option> is used.
357+
If not specified, the <literal>postgres</literal> database will be used,
358+
or if that does not exist, <literal>template1</literal> will be used.
359+
This can be a <link linkend="libpq-connstring">connection
360+
string</link>. If so, connection string parameters will override any
361+
conflicting command line options. Also, connection string parameters
362+
other than the database name itself will be re-used when connecting
363+
to other databases.
358364
</para>
359365
</listitem>
360366
</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
@@ -474,10 +474,16 @@ PostgreSQL documentation
474474
<term><option>--maintenance-db=<replaceable class="parameter">dbname</replaceable></option></term>
475475
<listitem>
476476
<para>
477-
Specifies the name of the database to connect to discover what other
478-
databases should be vacuumed. If not specified, the
479-
<literal>postgres</literal> database will be used,
480-
and if that does not exist, <literal>template1</literal> will be used.
477+
Specifies the name of the database to connect to to discover which
478+
databases should be vacuumed,
479+
when <option>-a</option>/<option>--all</option> is used.
480+
If not specified, the <literal>postgres</literal> database will be used,
481+
or if that does not exist, <literal>template1</literal> will be used.
482+
This can be a <link linkend="libpq-connstring">connection
483+
string</link>. If so, connection string parameters will override any
484+
conflicting command line options. Also, connection string parameters
485+
other than the database name itself will be re-used when connecting
486+
to other databases.
481487
</para>
482488
</listitem>
483489
</varlistentry>

‎src/bin/scripts/clusterdb.c

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

1919

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

3126

@@ -58,6 +53,7 @@ main(int argc, char *argv[])
5853
char*port=NULL;
5954
char*username=NULL;
6055
enumtrivalueprompt_password=TRI_DEFAULT;
56+
ConnParamscparams;
6157
boolecho= false;
6258
boolquiet= false;
6359
boolalldb= false;
@@ -134,6 +130,13 @@ main(int argc, char *argv[])
134130
exit(1);
135131
}
136132

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

139142
if (alldb)
@@ -150,8 +153,9 @@ main(int argc, char *argv[])
150153
exit(1);
151154
}
152155

153-
cluster_all_databases(verbose,maintenance_db,host,port,username,prompt_password,
154-
progname,echo,quiet);
156+
cparams.dbname=maintenance_db;
157+
158+
cluster_all_databases(&cparams,progname,verbose,echo,quiet);
155159
}
156160
else
157161
{
@@ -165,39 +169,36 @@ main(int argc, char *argv[])
165169
dbname=get_user_name_or_exit(progname);
166170
}
167171

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

172178
for (cell=tables.head;cell;cell=cell->next)
173179
{
174-
cluster_one_database(dbname,verbose,cell->val,
175-
host,port,username,prompt_password,
176-
progname,echo);
180+
cluster_one_database(&cparams,cell->val,
181+
progname,verbose,echo);
177182
}
178183
}
179184
else
180-
cluster_one_database(dbname,verbose,NULL,
181-
host,port,username,prompt_password,
182-
progname,echo);
185+
cluster_one_database(&cparams,NULL,
186+
progname,verbose,echo);
183187
}
184188

185189
exit(0);
186190
}
187191

188192

189193
staticvoid
190-
cluster_one_database(constchar*dbname,boolverbose,constchar*table,
191-
constchar*host,constchar*port,
192-
constchar*username,enumtrivalueprompt_password,
193-
constchar*progname,boolecho)
194+
cluster_one_database(constConnParams*cparams,constchar*table,
195+
constchar*progname,boolverbose,boolecho)
194196
{
195197
PQExpBufferDatasql;
196198

197199
PGconn*conn;
198200

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

202203
initPQExpBuffer(&sql);
203204

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

229230

230231
staticvoid
231-
cluster_all_databases(boolverbose,constchar*maintenance_db,
232-
constchar*host,constchar*port,
233-
constchar*username,enumtrivalueprompt_password,
234-
constchar*progname,boolecho,boolquiet)
232+
cluster_all_databases(ConnParams*cparams,constchar*progname,
233+
boolverbose,boolecho,boolquiet)
235234
{
236235
PGconn*conn;
237236
PGresult*result;
238-
PQExpBufferDataconnstr;
239237
inti;
240238

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

246-
initPQExpBuffer(&connstr);
247243
for (i=0;i<PQntuples(result);i++)
248244
{
249245
char*dbname=PQgetvalue(result,i,0);
@@ -254,15 +250,10 @@ cluster_all_databases(bool verbose, const char *maintenance_db,
254250
fflush(stdout);
255251
}
256252

257-
resetPQExpBuffer(&connstr);
258-
appendPQExpBufferStr(&connstr,"dbname=");
259-
appendConnStrVal(&connstr,dbname);
253+
cparams->override_dbname=dbname;
260254

261-
cluster_one_database(connstr.data,verbose,NULL,
262-
host,port,username,prompt_password,
263-
progname,echo);
255+
cluster_one_database(cparams,NULL,progname,verbose,echo);
264256
}
265-
termPQExpBuffer(&connstr);
266257

267258
PQclear(result);
268259
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp