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

Commit 2f0c19c

Browse files
committed
docs: remove use of escape strings and use bytea hex output
standard_conforming_strings defaulted to 'on' in PG 9.1. bytea_output defaulted to 'hex' in PG 9.0. Reported-by: André Hänsel Discussion: https://postgr.es/m/12e601d447ac$345994a0$9d0cbde0$@webkr.de Backpatch-through: 9.3
1 parent e553997 commit 2f0c19c

File tree

5 files changed

+72
-80
lines changed

5 files changed

+72
-80
lines changed

doc/src/sgml/array.sgml

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -767,9 +767,9 @@ SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
767767
For example, elements containing curly braces, commas (or the data type's
768768
delimiter character), double quotes, backslashes, or leading or trailing
769769
whitespace must be double-quoted. Empty strings and strings matching the
770-
word <literal>NULL</> must be quoted, too. To put a double quote or
771-
backslash in a quoted array element value, use escape string syntax
772-
and precede it with a backslash. Alternatively, you can avoid quotes and use
770+
word <literal>NULL</literal> must be quoted, too. To put a double
771+
quote or backslash in a quoted array element value, precede it
772+
with a backslash. Alternatively, you can avoid quotes and use
773773
backslash-escaping to protect all data characters that would otherwise
774774
be taken as array syntax.
775775
</para>
@@ -782,27 +782,6 @@ SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
782782
non-whitespace characters of an element, is not ignored.
783783
</para>
784784

785-
<note>
786-
<para>
787-
Remember that what you write in an SQL command will first be interpreted
788-
as a string literal, and then as an array. This doubles the number of
789-
backslashes you need. For example, to insert a <type>text</> array
790-
value containing a backslash and a double quote, you'd need to write:
791-
<programlisting>
792-
INSERT ... VALUES (E'{"\\\\","\\""}');
793-
</programlisting>
794-
The escape string processor removes one level of backslashes, so that
795-
what arrives at the array-value parser looks like <literal>{"\\","\""}</>.
796-
In turn, the strings fed to the <type>text</> data type's input routine
797-
become <literal>\</> and <literal>"</> respectively. (If we were working
798-
with a data type whose input routine also treated backslashes specially,
799-
<type>bytea</> for example, we might need as many as eight backslashes
800-
in the command to get one backslash into the stored array element.)
801-
Dollar quoting (see <xref linkend="sql-syntax-dollar-quoting">) can be
802-
used to avoid the need to double backslashes.
803-
</para>
804-
</note>
805-
806785
<tip>
807786
<para>
808787
The <literal>ARRAY</> constructor syntax (see

doc/src/sgml/datatype.sgml

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ SELECT b, char_length(b) FROM test2;
12821282
strings are distinguished from character strings in two
12831283
ways. First, binary strings specifically allow storing
12841284
octets of value zero and other <quote>non-printable</quote>
1285-
octets (usually, octets outside the range 32 to 126).
1285+
octets (usually, octets outside the decimal range 32 to 126).
12861286
Character strings disallow zero octets, and also disallow any
12871287
other octet values and sequences of octet values that are invalid
12881288
according to the database's selected character set encoding.
@@ -1294,9 +1294,10 @@ SELECT b, char_length(b) FROM test2;
12941294
</para>
12951295

12961296
<para>
1297-
The <type>bytea</type> type supports two external formats for
1298-
input and output: <productname>PostgreSQL</productname>'s historical
1299-
<quote>escape</quote> format, and <quote>hex</quote> format. Both
1297+
The <type>bytea</type> type supports two
1298+
formats for input and output: <quote>hex</quote> format
1299+
and <productname>PostgreSQL</productname>'s historical
1300+
<quote>escape</quote> format. Both
13001301
of these are always accepted on input. The output format depends
13011302
on the configuration parameter <xref linkend="guc-bytea-output">;
13021303
the default is hex. (Note that the hex format was introduced in
@@ -1334,7 +1335,7 @@ SELECT b, char_length(b) FROM test2;
13341335
<para>
13351336
Example:
13361337
<programlisting>
1337-
SELECT E'\\xDEADBEEF';
1338+
SELECT '\xDEADBEEF';
13381339
</programlisting>
13391340
</para>
13401341
</sect2>
@@ -1354,7 +1355,7 @@ SELECT E'\\xDEADBEEF';
13541355
convenient. But in practice it is usually confusing because it
13551356
fuzzes up the distinction between binary strings and character
13561357
strings, and also the particular escape mechanism that was chosen is
1357-
somewhat unwieldy. So this format should probably be avoided
1358+
somewhat unwieldy. Therefore, this format should probably be avoided
13581359
for most new applications.
13591360
</para>
13601361

@@ -1367,7 +1368,7 @@ SELECT E'\\xDEADBEEF';
13671368
octal value and precede it
13681369
by a backslash (or two backslashes, if writing the value as a
13691370
literal using escape string syntax).
1370-
Backslash itself (octet value 92) can alternatively be represented by
1371+
Backslash itself (octet decimal value 92) can alternatively be represented by
13711372
double backslashes.
13721373
<xref linkend="datatype-binary-sqlesc">
13731374
shows the characters that must be escaped, and gives the alternative
@@ -1391,33 +1392,33 @@ SELECT E'\\xDEADBEEF';
13911392
<row>
13921393
<entry>0</entry>
13931394
<entry>zero octet</entry>
1394-
<entry><literal>E'\\000'</literal></entry>
1395-
<entry><literal>SELECT E'\\000'::bytea;</literal></entry>
1396-
<entry><literal>\000</literal></entry>
1395+
<entry><literal>'\000'</literal></entry>
1396+
<entry><literal>SELECT '\000'::bytea;</literal></entry>
1397+
<entry><literal>\x00</literal></entry>
13971398
</row>
13981399

13991400
<row>
14001401
<entry>39</entry>
14011402
<entry>single quote</entry>
1402-
<entry><literal>''''</literal> or <literal>E'\\047'</literal></entry>
1403-
<entry><literal>SELECT E'\''::bytea;</literal></entry>
1404-
<entry><literal>'</literal></entry>
1403+
<entry><literal>''''</literal> or <literal>'\047'</literal></entry>
1404+
<entry><literal>SELECT ''''::bytea;</literal></entry>
1405+
<entry><literal>\x27</literal></entry>
14051406
</row>
14061407

14071408
<row>
14081409
<entry>92</entry>
14091410
<entry>backslash</entry>
1410-
<entry><literal>E'\\\\'</literal> or <literal>E'\\134'</literal></entry>
1411-
<entry><literal>SELECT E'\\\\'::bytea;</literal></entry>
1412-
<entry><literal>\\</literal></entry>
1411+
<entry><literal>'\'</literal> or <literal>'\\134'</literal></entry>
1412+
<entry><literal>SELECT '\\'::bytea;</literal></entry>
1413+
<entry><literal>\x5c</literal></entry>
14131414
</row>
14141415

14151416
<row>
14161417
<entry>0 to 31 and 127 to 255</entry>
14171418
<entry><quote>non-printable</quote> octets</entry>
1418-
<entry><literal>E'\\<replaceable>xxx'</></literal> (octal value)</entry>
1419-
<entry><literal>SELECT E'\\001'::bytea;</literal></entry>
1420-
<entry><literal>\001</literal></entry>
1419+
<entry><literal>'\<replaceable>xxx'</replaceable></literal> (octal value)</entry>
1420+
<entry><literal>SELECT '\001'::bytea;</literal></entry>
1421+
<entry><literal>\x01</literal></entry>
14211422
</row>
14221423

14231424
</tbody>
@@ -1445,7 +1446,7 @@ SELECT E'\\xDEADBEEF';
14451446
of escaping.) The remaining backslash is then recognized by the
14461447
<type>bytea</type> input function as starting either a three
14471448
digit octal value or escaping another backslash. For example,
1448-
a string literal passed to the server as <literal>E'\\001'</literal>
1449+
a string literal passed to the server as <literal>'\001'</literal>
14491450
becomes <literal>\001</literal> after passing through the
14501451
escape string parser. The <literal>\001</literal> is then sent
14511452
to the <type>bytea</type> input function, where it is converted
@@ -1456,12 +1457,24 @@ SELECT E'\\xDEADBEEF';
14561457
</para>
14571458

14581459
<para>
1459-
<type>Bytea</type> octets are sometimes escaped when output. In general, each
1460-
<quote>non-printable</quote> octet is converted into
1461-
its equivalent three-digit octal value and preceded by one backslash.
1462-
Most <quote>printable</quote> octets are represented by their standard
1463-
representation in the client character set. The octet with decimal
1464-
value 92 (backslash) is doubled in the output.
1460+
<type>Bytea</type> octets are output in <literal>hex</literal>
1461+
format by default. If you change <xref linkend="guc-bytea-output">
1462+
to <literal>escape</literal>,
1463+
<quote>non-printable</quote> octet are converted to
1464+
equivalent three-digit octal value and preceded by one backslash.
1465+
Most <quote>printable</quote> octets are output by their standard
1466+
representation in the client character set, e.g.:
1467+
1468+
<programlisting>
1469+
SET bytea_output = 'escape';
1470+
1471+
SELECT 'abc \153\154\155 \052\251\124'::bytea;
1472+
bytea
1473+
----------------
1474+
abc klm *\251T
1475+
</programlisting>
1476+
1477+
The octet with decimal value 92 (backslash) is doubled in the output.
14651478
Details are in <xref linkend="datatype-binary-resesc">.
14661479
</para>
14671480

@@ -1484,23 +1497,23 @@ SELECT E'\\xDEADBEEF';
14841497
<entry>92</entry>
14851498
<entry>backslash</entry>
14861499
<entry><literal>\\</literal></entry>
1487-
<entry><literal>SELECT E'\\134'::bytea;</literal></entry>
1500+
<entry><literal>SELECT '\134'::bytea;</literal></entry>
14881501
<entry><literal>\\</literal></entry>
14891502
</row>
14901503

14911504
<row>
14921505
<entry>0 to 31 and 127 to 255</entry>
14931506
<entry><quote>non-printable</quote> octets</entry>
14941507
<entry><literal>\<replaceable>xxx</></literal> (octal value)</entry>
1495-
<entry><literal>SELECT E'\\001'::bytea;</literal></entry>
1508+
<entry><literal>SELECT '\001'::bytea;</literal></entry>
14961509
<entry><literal>\001</literal></entry>
14971510
</row>
14981511

14991512
<row>
15001513
<entry>32 to 126</entry>
15011514
<entry><quote>printable</quote> octets</entry>
15021515
<entry>client character set representation</entry>
1503-
<entry><literal>SELECT E'\\176'::bytea;</literal></entry>
1516+
<entry><literal>SELECT '\176'::bytea;</literal></entry>
15041517
<entry><literal>~</literal></entry>
15051518
</row>
15061519

0 commit comments

Comments
 (0)