diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2017-04-01 21:44:54 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2017-04-01 21:44:54 -0400 |
commit | f833c847b8fa4782efab45c8371d3cee64292d9b (patch) | |
tree | 85f93d92d7f7ab45bab157bb24ff482c6f47b97b /doc/src | |
parent | 41bd155dd656e7f17c02855be7aff234843347cd (diff) | |
download | postgresql-f833c847b8fa4782efab45c8371d3cee64292d9b.tar.gz postgresql-f833c847b8fa4782efab45c8371d3cee64292d9b.zip |
Allow psql variable substitution to occur in backtick command strings.
Previously, text between backquotes in a psql metacommand's arguments
was always passed to the shell literally. That considerably hobbles
the usefulness of the feature for scripting, so we'd foreseen for a long
time that we'd someday want to allow substitution of psql variables into
the shell command. IMO the addition of \if metacommands has brought us to
that point, since \if can greatly benefit from some sort of client-side
expression evaluation capability, and psql itself is not going to grow any
such thing in time for v10. Hence, this patch. It allows :VARIABLE to be
replaced by the exact contents of the named variable, while :'VARIABLE'
is replaced by the variable's contents suitably quoted to become a single
shell-command argument. (The quoting rules for that are different from
those for SQL literals, so this is a bit of an abuse of the :'VARIABLE'
notation, but I doubt anyone will be confused.)
As with other situations in psql, no substitution occurs if the word
following a colon is not a known variable name. That limits the risk of
compatibility problems for existing psql scripts; but the risk isn't zero,
so this needs to be called out in the v10 release notes.
Discussion: https://postgr.es/m/9561.1490895211@sss.pgh.pa.us
Diffstat (limited to 'doc/src')
-rw-r--r-- | doc/src/sgml/ref/psql-ref.sgml | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index b51b11baa35..ad463e71c10 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -770,17 +770,32 @@ testdb=> </para> <para> - Within an argument, text that is enclosed in backquotes - (<literal>`</literal>) is taken as a command line that is passed to the - shell. The output of the command (with any trailing newline removed) - replaces the backquoted text. - </para> - - <para> If an unquoted colon (<literal>:</literal>) followed by a <application>psql</> variable name appears within an argument, it is replaced by the variable's value, as described in <xref linkend="APP-PSQL-interpolation" endterm="APP-PSQL-interpolation-title">. + The forms <literal>:'<replaceable>variable_name</>'</literal> and + <literal>:"<replaceable>variable_name</>"</literal> described there + work as well. + </para> + + <para> + Within an argument, text that is enclosed in backquotes + (<literal>`</literal>) is taken as a command line that is passed to the + shell. The output of the command (with any trailing newline removed) + replaces the backquoted text. Within the text enclosed in backquotes, + no special quoting or other processing occurs, except that appearances + of <literal>:<replaceable>variable_name</></literal> where + <replaceable>variable_name</> is a <application>psql</> variable name + are replaced by the variable's value. Also, appearances of + <literal>:'<replaceable>variable_name</>'</literal> are replaced by the + variable's value suitably quoted to become a single shell command + argument. (The latter form is almost always preferable, unless you are + very sure of what is in the variable.) Because carriage return and line + feed characters cannot be safely quoted on all platforms, the + <literal>:'<replaceable>variable_name</>'</literal> form prints an + error message and does not substitute the variable value when such + characters appear in the value. </para> <para> |