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

Commitd5563d7

Browse files
committed
psql: Support multiple -c and -f options, and allow mixing them.
To support this, we must reconcile some historical anomalies in thebehavior of -c. In particular, as a backward-incompatibility, -c nolonger implies --no-psqlrc.Pavel Stehule (code) and Catalin Iacob (documentation). Review byMichael Paquier and myself. Proposed behavior per Tom Lane.
1 parent385f337 commitd5563d7

File tree

5 files changed

+205
-135
lines changed

5 files changed

+205
-135
lines changed

‎doc/src/sgml/ref/psql-ref.sgml

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ PostgreSQL documentation
3838
<productname>PostgreSQL</productname>. It enables you to type in
3939
queries interactively, issue them to
4040
<productname>PostgreSQL</productname>, and see the query results.
41-
Alternatively, input can be from a file. In addition, it provides a
42-
number of meta-commands and various shell-like features to
43-
facilitate writing scripts and automating a wide variety of tasks.
41+
Alternatively, input can be from a file or from command line
42+
arguments. In addition, it provides a number of meta-commands and various
43+
shell-like features to facilitate writing scripts and automating a wide
44+
variety of tasks.
4445
</para>
4546
</refsect1>
4647

@@ -89,11 +90,10 @@ PostgreSQL documentation
8990
<term><option>--command=<replaceable class="parameter">command</replaceable></></term>
9091
<listitem>
9192
<para>
92-
Specifies that <application>psql</application> is to execute one
93-
command string, <replaceable class="parameter">command</replaceable>,
94-
and then exit. This is useful in shell scripts. Start-up files
95-
(<filename>psqlrc</filename> and <filename>~/.psqlrc</filename>) are
96-
ignored with this option.
93+
Specifies that <application>psql</application> is to execute the given
94+
command string, <replaceable class="parameter">command</replaceable>.
95+
This option can be repeated and combined in any order with
96+
the <option>-f</option> option.
9797
</para>
9898
<para>
9999
<replaceable class="parameter">command</replaceable> must be either
@@ -102,25 +102,34 @@ PostgreSQL documentation
102102
or a single backslash command. Thus you cannot mix
103103
<acronym>SQL</acronym> and <application>psql</application>
104104
meta-commands with this option. To achieve that, you could
105-
pipe the string into <application>psql</application>, for example:
105+
use repeated <option>-c</option> options or pipe the string
106+
into <application>psql</application>, for example:
107+
<literal>psql -c '\x' -c 'SELECT * FROM foo;'</literal> or
106108
<literal>echo '\x \\ SELECT * FROM foo;' | psql</literal>.
107109
(<literal>\\</> is the separator meta-command.)
108110
</para>
109111
<para>
110-
If the command string contains multiple SQL commands, they are
111-
processed in a single transaction, unless there are explicit
112-
<command>BEGIN</>/<command>COMMIT</> commands included in the
113-
string to divide it into multiple transactions. This is
114-
different from the behavior when the same string is fed to
115-
<application>psql</application>'s standard input. Also, only
116-
the result of the last SQL command is returned.
112+
Each command string passed to <option>-c</option> is sent to the server
113+
as a single query. Because of this, the server executes it as a single
114+
transaction, even if a command string contains
115+
multiple <acronym>SQL</acronym> commands, unless there are
116+
explicit <command>BEGIN</>/<command>COMMIT</> commands included in the
117+
string to divide it into multiple transactions. Also, the server only
118+
returns the result of the last <acronym>SQL</acronym> command to the
119+
client. This is different from the behavior when the same string with
120+
multiple <acronym>SQL</acronym> commands is fed
121+
to <application>psql</application>'s standard input because
122+
then <application>psql</application> sends each <acronym>SQL</acronym>
123+
command separately.
117124
</para>
118125
<para>
119-
Because of these legacy behaviors, putting more than one command in
120-
the <option>-c</option> string often has unexpected results. It's
121-
better to feed multiple commands to <application>psql</application>'s
122-
standard input, either using <application>echo</application> as
123-
illustrated above, or via a shell here-document, for example:
126+
Putting more than one command in the <option>-c</option> string often
127+
has unexpected results. This is a result of the fact that the whole
128+
string is sent to the server as a single query.
129+
It's better to use repeated <option>-c</option> commands or feed
130+
multiple commands to <application>psql</application>'s standard input,
131+
either using <application>echo</application> as illustrated above, or
132+
via a shell here-document, for example:
124133
<programlisting>
125134
psql &lt;&lt;EOF
126135
\x
@@ -185,9 +194,11 @@ EOF
185194
<para>
186195
Use the file <replaceable class="parameter">filename</replaceable>
187196
as the source of commands instead of reading commands interactively.
188-
After the file is processed, <application>psql</application>
189-
terminates. This is in many ways equivalent to the meta-command
190-
<command>\i</command>.
197+
This option can be repeated and combined in any order with
198+
the <option>-c</option> option. After the commands in
199+
every <option>-c</option> command string and <option>-f</option> file
200+
are processed, <application>psql</application> terminates. This option
201+
is in many ways equivalent to the meta-command <command>\i</command>.
191202
</para>
192203

193204
<para>
@@ -539,20 +550,21 @@ EOF
539550
<term><option>--single-transaction</option></term>
540551
<listitem>
541552
<para>
542-
When <application>psql</application> executes a script, adding
543-
this option wraps <command>BEGIN</>/<command>COMMIT</> around the
544-
script to execute it as a single transaction. This ensures that
545-
either all the commands complete successfully, or no changes are
546-
applied.
553+
When <application>psql</application> executes commands from a script
554+
and/or a <option>-c</option> option, adding this option
555+
wraps <command>BEGIN</>/<command>COMMIT</> around all of those
556+
commands as a whole to execute them as a single transaction. This
557+
ensures that either all the commands complete successfully, or no
558+
changes are applied.
547559
</para>
548560

549561
<para>
550-
If the script itself uses <command>BEGIN</>, <command>COMMIT</>,
562+
If the commands themselves
563+
contain <command>BEGIN</>, <command>COMMIT</>,
551564
or <command>ROLLBACK</>, this option will not have the desired
552-
effects.
553-
Also, if the script contains any command that cannot be executed
554-
inside a transaction block, specifying this option will cause that
555-
command (and hence the whole transaction) to fail.
565+
effects. Also, if an individual command cannot be executed inside a
566+
transaction block, specifying this option will cause the whole
567+
transaction to fail.
556568
</para>
557569
</listitem>
558570
</varlistentry>
@@ -3725,7 +3737,7 @@ PSQL_EDITOR_LINENUMBER_ARG='--line '
37253737
<term><filename>psqlrc</filename> and <filename>~/.psqlrc</filename></term>
37263738
<listitem>
37273739
<para>
3728-
Unless it is passed an <option>-X</option>or <option>-c</option>option,
3740+
Unless it is passed an <option>-X</option> option,
37293741
<application>psql</application> attempts to read and execute commands
37303742
from the system-wide startup file (<filename>psqlrc</filename>) and then
37313743
the user's personal startup file (<filename>~/.psqlrc</filename>), after
@@ -3819,6 +3831,12 @@ PSQL_EDITOR_LINENUMBER_ARG='--line '
38193831
</para>
38203832
</listitem>
38213833

3834+
<listitem>
3835+
<para>
3836+
Before <productname>PostgreSQL</productname> 9.6, <option>-c</option>
3837+
implied <option>-X</option>; this is no longer the case.
3838+
</para>
3839+
</listitem>
38223840
</itemizedlist>
38233841
</refsect1>
38243842

‎src/bin/psql/command.c

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ exec_command(const char *cmd,
916916
include_relative= (strcmp(cmd,"ir")==0
917917
||strcmp(cmd,"include_relative")==0);
918918
expand_tilde(&fname);
919-
success= (process_file(fname,false,include_relative)==EXIT_SUCCESS);
919+
success= (process_file(fname,include_relative)==EXIT_SUCCESS);
920920
free(fname);
921921
}
922922
}
@@ -2288,13 +2288,12 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
22882288
* the file from where the currently processed file (if any) is located.
22892289
*/
22902290
int
2291-
process_file(char*filename,boolsingle_txn,booluse_relative_path)
2291+
process_file(char*filename,booluse_relative_path)
22922292
{
22932293
FILE*fd;
22942294
intresult;
22952295
char*oldfilename;
22962296
charrelpath[MAXPGPATH];
2297-
PGresult*res;
22982297

22992298
if (!filename)
23002299
{
@@ -2339,37 +2338,8 @@ process_file(char *filename, bool single_txn, bool use_relative_path)
23392338
oldfilename=pset.inputfile;
23402339
pset.inputfile=filename;
23412340

2342-
if (single_txn)
2343-
{
2344-
if ((res=PSQLexec("BEGIN"))==NULL)
2345-
{
2346-
if (pset.on_error_stop)
2347-
{
2348-
result=EXIT_USER;
2349-
gotoerror;
2350-
}
2351-
}
2352-
else
2353-
PQclear(res);
2354-
}
2355-
23562341
result=MainLoop(fd);
23572342

2358-
if (single_txn)
2359-
{
2360-
if ((res=PSQLexec("COMMIT"))==NULL)
2361-
{
2362-
if (pset.on_error_stop)
2363-
{
2364-
result=EXIT_USER;
2365-
gotoerror;
2366-
}
2367-
}
2368-
else
2369-
PQclear(res);
2370-
}
2371-
2372-
error:
23732343
if (fd!=stdin)
23742344
fclose(fd);
23752345

‎src/bin/psql/command.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ typedef enum _backslashResult
2727
externbackslashResultHandleSlashCmds(PsqlScanStatescan_state,
2828
PQExpBufferquery_buf);
2929

30-
externintprocess_file(char*filename,boolsingle_txn,booluse_relative_path);
30+
externintprocess_file(char*filename,booluse_relative_path);
3131

3232
externbooldo_pset(constchar*param,
3333
constchar*value,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp