Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 86322dc

Browse files
committed
Improve psql's behavior for \set and \unset of its control variables.
This commit improves on the results of commit 511ae62 in two ways: 1. It restores the historical behavior that "\set FOO" is interpreted as setting FOO to "on", if FOO is a boolean control variable. We already found one test script that was expecting that behavior, and the psql documentation certainly does nothing to discourage people from assuming that would work, since it often says just "if FOO is set" when describing the effects of a boolean variable. However, now this case will result in actually setting FOO to "on", not an empty string. 2. It arranges for an "\unset" of a control variable to set the value back to its default value, rather than becoming apparently undefined. The control variables are also initialized that way at psql startup. In combination, these things guarantee that a control variable always has a displayable value that reflects what psql is actually doing. That is a pretty substantial usability improvement. The implementation involves adding a second type of variable hook function that is able to replace a proposed new value (including NULL) with another one. We could alternatively have complicated the API of the assign hook, but this way seems better since many variables can share the same substitution hook function. Also document the actual behavior of these variables more fully, including covering assorted behaviors that were there before but never documented. This patch also includes some minor cleanup that should have been in 511ae62 but was missed. Patch by me, but it owes a lot to discussions with Daniel Vérité. Discussion: https://postgr.es/m/9572.1485821620@sss.pgh.pa.us
1 parent dbd6911 commit 86322dc

File tree

6 files changed

+319
-147
lines changed

6 files changed

+319
-147
lines changed

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

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ EOF
455455
any, by an equal sign on the command line. To unset a variable,
456456
leave off the equal sign. To set a variable with an empty value,
457457
use the equal sign but leave off the value. These assignments are
458-
done during a very early stage of start-up, so variables reserved
459-
for internal purposes might get overwritten later.
458+
done during command line processing, so variables that reflect
459+
connection state will get overwritten later.
460460
</para>
461461
</listitem>
462462
</varlistentry>
@@ -2692,7 +2692,7 @@ lo_import 152801
26922692
class="parameter">name</replaceable> to <replaceable
26932693
class="parameter">value</replaceable>, or if more than one value
26942694
is given, to the concatenation of all of them. If only one
2695-
argument is given, the variable is set with an empty value. To
2695+
argument is given, the variable is set to an empty-string value. To
26962696
unset a variable, use the <command>\unset</command> command.
26972697
</para>
26982698

@@ -2709,9 +2709,11 @@ lo_import 152801
27092709
</para>
27102710

27112711
<para>
2712-
Although you are welcome to set any variable to anything you
2713-
want, <application>psql</application> treats several variables
2714-
as special. They are documented in the section about variables.
2712+
Certain variables are special, in that they
2713+
control <application>psql</application>'s behavior or are
2714+
automatically set to reflect connection state. These variables are
2715+
documented in <xref linkend="APP-PSQL-variables"
2716+
endterm="APP-PSQL-variables-title">, below.
27152717
</para>
27162718

27172719
<note>
@@ -2835,6 +2837,14 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
28352837
Unsets (deletes) the <application>psql</> variable <replaceable
28362838
class="parameter">name</replaceable>.
28372839
</para>
2840+
2841+
<para>
2842+
Most variables that control <application>psql</application>'s behavior
2843+
cannot be unset; instead, an <literal>\unset</> command is interpreted
2844+
as setting them to their default values.
2845+
See <xref linkend="APP-PSQL-variables"
2846+
endterm="APP-PSQL-variables-title">, below.
2847+
</para>
28382848
</listitem>
28392849
</varlistentry>
28402850

@@ -3053,7 +3063,7 @@ bar
30533063

30543064
<para>
30553065
If you call <command>\set</command> without a second argument, the
3056-
variable is set, with an empty string as value. To unset (i.e., delete)
3066+
variable is set to an empty-string value. To unset (i.e., delete)
30573067
a variable, use the command <command>\unset</command>. To show the
30583068
values of all variables, call <command>\set</command> without any argument.
30593069
</para>
@@ -3082,8 +3092,23 @@ bar
30823092
By convention, all specially treated variables' names
30833093
consist of all upper-case ASCII letters (and possibly digits and
30843094
underscores). To ensure maximum compatibility in the future, avoid
3085-
using such variable names for your own purposes. A list of all specially
3086-
treated variables follows.
3095+
using such variable names for your own purposes.
3096+
</para>
3097+
3098+
<para>
3099+
Variables that control <application>psql</application>'s behavior
3100+
generally cannot be unset or set to invalid values. An <literal>\unset</>
3101+
command is allowed but is interpreted as setting the variable to its
3102+
default value. A <literal>\set</> command without a second argument is
3103+
interpreted as setting the variable to <literal>on</>, for control
3104+
variables that accept that value, and is rejected for others. Also,
3105+
control variables that accept the values <literal>on</>
3106+
and <literal>off</> will also accept other common spellings of Boolean
3107+
values, such as <literal>true</> and <literal>false</>.
3108+
</para>
3109+
3110+
<para>
3111+
The specially treated variables are:
30873112
</para>
30883113

30893114
<variablelist>
@@ -3153,7 +3178,7 @@ bar
31533178
<para>
31543179
The name of the database you are currently connected to. This is
31553180
set every time you connect to a database (including program
3156-
start-up), but can be unset.
3181+
start-up), but can be changed or unset.
31573182
</para>
31583183
</listitem>
31593184
</varlistentry>
@@ -3171,8 +3196,8 @@ bar
31713196
as it is sent to the server. The switch to select this behavior is
31723197
<option>-e</option>. If set to <literal>errors</literal>, then only
31733198
failed queries are displayed on standard error output. The switch
3174-
for this behavior is <option>-b</option>. If unset, or if set to
3175-
<literal>none</literal>, then no queries are displayed.
3199+
for this behavior is <option>-b</option>. If set to
3200+
<literal>none</literal> (the default), then no queries are displayed.
31763201
</para>
31773202
</listitem>
31783203
</varlistentry>
@@ -3187,8 +3212,9 @@ bar
31873212
<productname>PostgreSQL</productname> internals and provide
31883213
similar functionality in your own programs. (To select this behavior
31893214
on program start-up, use the switch <option>-E</option>.) If you set
3190-
the variable to the value <literal>noexec</literal>, the queries are
3215+
this variable to the value <literal>noexec</literal>, the queries are
31913216
just shown but are not actually sent to the server and executed.
3217+
The default value is <literal>off</>.
31923218
</para>
31933219
</listitem>
31943220
</varlistentry>
@@ -3200,7 +3226,7 @@ bar
32003226
The current client character set encoding.
32013227
This is set every time you connect to a database (including
32023228
program start-up), and when you change the encoding
3203-
with <literal>\encoding</>, but it can be unset.
3229+
with <literal>\encoding</>, but it can be changed or unset.
32043230
</para>
32053231
</listitem>
32063232
</varlistentry>
@@ -3209,7 +3235,7 @@ bar
32093235
<term><varname>FETCH_COUNT</varname></term>
32103236
<listitem>
32113237
<para>
3212-
If this variable is set to an integer value &gt; 0,
3238+
If this variable is set to an integer value greater than zero,
32133239
the results of <command>SELECT</command> queries are fetched
32143240
and displayed in groups of that many rows, rather than the
32153241
default behavior of collecting the entire result set before
@@ -3220,6 +3246,13 @@ bar
32203246
Keep in mind that when using this feature, a query might
32213247
fail after having already displayed some rows.
32223248
</para>
3249+
3250+
<para>
3251+
<varname>FETCH_COUNT</varname> is ignored if it is unset or does not
3252+
have a positive value. It cannot be set to a value that is not
3253+
syntactically an integer.
3254+
</para>
3255+
32233256
<tip>
32243257
<para>
32253258
Although you can use any output format with this feature,
@@ -3241,7 +3274,7 @@ bar
32413274
list. If set to a value of <literal>ignoredups</literal>, lines
32423275
matching the previous history line are not entered. A value of
32433276
<literal>ignoreboth</literal> combines the two options. If
3244-
unset, or if set to <literal>none</literal> (the default), all lines
3277+
set to <literal>none</literal> (the default), all lines
32453278
read in interactive mode are saved on the history list.
32463279
</para>
32473280
<note>
@@ -3257,8 +3290,12 @@ bar
32573290
<term><varname>HISTFILE</varname></term>
32583291
<listitem>
32593292
<para>
3260-
The file name that will be used to store the history list. The default
3261-
value is <filename>~/.psql_history</filename>. For example, putting:
3293+
The file name that will be used to store the history list. If unset,
3294+
the file name is taken from the <envar>PSQL_HISTORY</envar>
3295+
environment variable. If that is not set either, the default
3296+
is <filename>~/.psql_history</filename>,
3297+
or <filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
3298+
For example, putting:
32623299
<programlisting>
32633300
\set HISTFILE ~/.psql_history- :DBNAME
32643301
</programlisting>
@@ -3279,8 +3316,10 @@ bar
32793316
<term><varname>HISTSIZE</varname></term>
32803317
<listitem>
32813318
<para>
3282-
The number of commands to store in the command history. The
3283-
default value is 500.
3319+
The maximum number of commands to store in the command history.
3320+
If unset, at most 500 commands are stored by default.
3321+
If set to a value that is negative or not an integer, no limit is
3322+
applied.
32843323
</para>
32853324
<note>
32863325
<para>
@@ -3297,7 +3336,7 @@ bar
32973336
<para>
32983337
The database server host you are currently connected to. This is
32993338
set every time you connect to a database (including program
3300-
start-up), but can be unset.
3339+
start-up), but can be changed or unset.
33013340
</para>
33023341
</listitem>
33033342
</varlistentry>
@@ -3350,7 +3389,7 @@ bar
33503389
generates an error, the error is ignored and the transaction
33513390
continues. When set to <literal>interactive</>, such errors are only
33523391
ignored in interactive sessions, and not when reading script
3353-
files. When unset or set to <literal>off</>, a statement in a
3392+
files. When set to <literal>off</> (the default), a statement in a
33543393
transaction block that generates an error aborts the entire
33553394
transaction. The error rollback mode works by issuing an
33563395
implicit <command>SAVEPOINT</> for you, just before each command
@@ -3385,7 +3424,7 @@ bar
33853424
<para>
33863425
The database server port to which you are currently connected.
33873426
This is set every time you connect to a database (including
3388-
program start-up), but can be unset.
3427+
program start-up), but can be changed or unset.
33893428
</para>
33903429
</listitem>
33913430
</varlistentry>
@@ -3458,7 +3497,7 @@ bar
34583497
<para>
34593498
The database user you are currently connected as. This is set
34603499
every time you connect to a database (including program
3461-
start-up), but can be unset.
3500+
start-up), but can be changed or unset.
34623501
</para>
34633502
</listitem>
34643503
</varlistentry>
@@ -3481,7 +3520,7 @@ bar
34813520
<listitem>
34823521
<para>
34833522
This variable is set at program start-up to
3484-
reflect <application>psql</>'s version. It can be unset or changed.
3523+
reflect <application>psql</>'s version. It can be changed or unset.
34853524
</para>
34863525
</listitem>
34873526
</varlistentry>
@@ -4015,6 +4054,7 @@ PSQL_EDITOR_LINENUMBER_ARG='--line '
40154054
</para>
40164055
<para>
40174056
The location of the history file can be set explicitly via
4057+
the <varname>HISTFILE</varname> <application>psql</> variable or
40184058
the <envar>PSQL_HISTORY</envar> environment variable.
40194059
</para>
40204060
</listitem>

0 commit comments

Comments
 (0)