RefLib

Reference library for AI

Back

postgresql_16:3.5.4.2 bytea Escape Format

PostgreSQL 16 > The SQL Language > Data Types > Binary Data Types > bytea Escape Format

The “escape” format is the traditional PostgreSQL format for the bytea type. It takes the approach of representing a binary string as a sequence of ASCII characters, while converting those bytes that cannot be represented as an ASCII character into special escape sequences. If, from the point of view of the application, representing bytes as characters makes sense, then this representation can be convenient. But in practice it is usually confusing because it fuzzes up the distinction between binary strings and character strings, and also the particular escape mechanism that was chosen is somewhat unwieldy. Therefore, this format should probably be avoided for most new applications.

When entering bytea values in escape format, octets of certain values *must* be escaped, while all octet values *can* be escaped. In general, to escape an octet, convert it into its three-digit octal value and precede it by a backslash. Backslash itself (octet decimal value 92) can alternatively be represented by double backslashes. (see datatype-binary-sqlesc) shows the characters that must be escaped, and gives the alternative escape sequences where applicable.

| Decimal Octet Value | Description | Escaped Input Representation | Example | Hex Representation | | --- | --- | --- | --- | --- | | 0 | zero octet | '\000' | '\000'::bytea | \x00 | | 39 | single quote | '''' or '\047' | ''''::bytea | \x27 | | 92 | backslash | '\\' or '\134' | '\\'::bytea | \x5c | | 0 to 31 and 127 to 255 | “non-printable” octets | '\*xxx'* (octal value) | '\001'::bytea | \x01 |

The requirement to escape *non-printable* octets varies depending on locale settings. In some instances you can get away with leaving them unescaped.

The reason that single quotes must be doubled, as shown in (see datatype-binary-sqlesc), is that this is true for any string literal in an SQL command. The generic string-literal parser consumes the outermost single quotes and reduces any pair of single quotes to one data character. What the bytea input function sees is just one single quote, which it treats as a plain data character. However, the bytea input function treats backslashes as special, and the other behaviors shown in (see datatype-binary-sqlesc) are implemented by that function.

In some contexts, backslashes must be doubled compared to what is shown above, because the generic string-literal parser will also reduce pairs of backslashes to one data character; see (see postgresql_16:3.1.1.2).

Bytea octets are output in hex format by default. If you change (see guc-bytea-output) to escape, “non-printable” octets are converted to their equivalent three-digit octal value and preceded by one backslash. Most “printable” octets are output by their standard representation in the client character set, e.g.: SET bytea_output = 'escape'; SELECT 'abc \153\154\155 \052\251\124'::bytea; bytea ---------------- abc klm *\251T The octet with decimal value 92 (backslash) is doubled in the output. Details are in (see datatype-binary-resesc).

| Decimal Octet Value | Description | Escaped Output Representation | Example | Output Result | | --- | --- | --- | --- | --- | | 92 | backslash | \\ | '\134'::bytea | \\ | | 0 to 31 and 127 to 255 | “non-printable” octets | \*xxx* (octal value) | '\001'::bytea | \001 | | 32 to 126 | “printable” octets | client character set representation | '\176'::bytea | ~ |

Depending on the front end to PostgreSQL you use, you might have additional work to do in terms of escaping and unescaping bytea strings. For example, you might also have to escape line feeds and carriage returns if your interface automatically translates these.

Back