Re: escape vs. bytea in config

From: "Peter J(dot) Holzer" <hjp-pgsql(at)hjp(dot)at>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: escape vs. bytea in config
Date: 2023-03-07 12:53:03
Message-ID: 20230307125303.aazyrkzdp6fyw53h@hjp.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2023-03-07 11:53:29 +0100, Mathias Mayrhofer wrote:
> I came across the "bytea_output" configuration variable inside postgresql.conf.
> It seems to be affecting the "digest" function, but in a way I cannot
> understand.

No, it just affects the way bytea values are displayed.

The return value of the digest() function is a 32 byte bytea value in
both cases. But you can choose wether you want to display bytea values
as 2 hex digits per byte or as a mixture of printable ascii characters
and octal escapes.

> When my postgresql 14 server executes the digest function, it considers the
> "bytea_output" variable, but the output of the function is *NOT* the same as
> the "encode" function. It differs only slightly.

I don't think the docs say anywhere that these are the same.

In particular,
https://www.postgresql.org/docs/15/datatype-binary.html#id-1.5.7.12.9
says that "the entire string is preceded by the sequence \x (to
distinguish it from the escape format".

> -- /etc/postgresql/14/main/postgresql.conf
> -- bytea_output = 'escape'
>
> db=> select encode(digest('mtmayr', 'sha256'), 'escape');
> ┌────────────────────────────────────────────────────────────────────────────────────────────┐
> │ encode │
> ├────────────────────────────────────────────────────────────────────────────────────────────┤
> │ \277\x19\225\335g\307\224\337-\347Y\203\254\324\x1Ff\307Ph*\267\\\243\2515S\213WX\204\375G │
> └────────────────────────────────────────────────────────────────────────────────────────────┘

This is a bit weirder. Why are there two bytes encoded as hex instead of
octal? It turns out this is again an artefact of displaying the value.

https://www.postgresql.org/docs/14/functions-binarystring.html says:

| The escape format converts zero bytes and bytes with the high bit set
| into octal escape sequences (\nnn), and it doubles backslashes. Other
| byte values are represented literally.

So the byte 0x19 is converted to a single character U+0019 (EM) which is
then displayed as '\x19', while bytes >= 0x80 are converted to
four-character escape sequences.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp(at)hjp(dot)at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ron 2023-03-07 17:58:45 psql \conninfo in tabular form?
Previous Message Laurenz Albe 2023-03-07 12:35:09 Re: escape vs. bytea in config