Subject: Limitation of prepared statement name

From: Flavio Henrique Araque Gurgel <[email-masked]>
Date: Tue, 28 Aug 2018 16:26:30 +0200

Hello all

I just found a limitation in prepared statements names that I didn't know
before and can lead applications to be difficult to debug.

It seems that naming prepared statements is limited to 63 characters as per
https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

However, the documentation for PREPARE says nothing about it
https://www.postgresql.org/docs/10/static/sql-prepare.html

The dangerous situation is that PostgreSQL seems to ignore rigthmost
overflowed characters silently. If we try to prepare another statement with
the same 63 leftmost characters as the first one, we get an error of
duplicate prepared statement name.

Wouldn't it be good to include that information on the prepared statement
doc page? Maybe I should notify this as a general bug so we treat this at
the code level.

Best,
Flavio Gurgel

---

From: Tom Lane <[email-masked]>
Date: Tue, 28 Aug 2018 12:21:15 -0400

> https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

Yeah, just like every other SQL identifier in Postgres...

> overflowed characters silently.

It's hardly silent:

regression=# prepare foo1234567890123456789012345678901234567890123456789012345678901234567890 as select 1;
NOTICE:  identifier "foo1234567890123456789012345678901234567890123456789012345678901234567890" will be truncated to "foo123456789012345678901234567890123456789012345678901234567890"
PREPARE

> doc page?

You could as well argue that every single page that describes creating
any sort of object ought to repeat this information.  I doubt that people
would find that to be an improvement.

			regards, tom lane

---

From: Flavio Henrique Araque Gurgel <[email-masked]>
Date: Thu, 30 Aug 2018 16:23:14 +0200

Em ter, 28 de ago de 2018 às 18:21, Tom Lane <[email-masked]> escreveu:

> regression=# prepare
> foo1234567890123456789012345678901234567890123456789012345678901234567890
> as select 1;
> NOTICE:  identifier
> "foo1234567890123456789012345678901234567890123456789012345678901234567890"
> will be truncated to
> "foo123456789012345678901234567890123456789012345678901234567890"
> PREPARE
>

Sorry, when I said silently I should've say that the message level is too
low. A human would easily understand the NOTICE message but an application
code doesn't. And that was the case here.

I don't know how an application could follow what the NOTICE says here and
most NOTICE are harmless. I think most drivers just ignore NOTICE messages
at runtime or throw it on logs and that will never be caught by automated
alert systems since they're just NOTICE.

In my case, it goes down to the level of the library used to talk to
PostgreSQL, it's the pgx library largely used in Go projects. Developers in
my company will submit a patch there to avoid the situation but I would
like improve things at the PostgreSQL side since I'm a DBA.

I know this is not the right list to discuss this but maybe an ERROR level
would be better, completely aborting the command and the transaction would
completely avoid the situation and handling at the application side would
be complete.

> would find that to be an improvement.

I'll never argue repeating the same information everywhere in the docs but
a good solution would be to link the identifiers section everytime an
identifier is used in the docs. That would ease the task for developers
that are used to read PostgreSQL docs in a daily basis.

If that is a good thing, I can propose a doc patch at least for the SQL
language pages.

Flavio Gurgel

---

From: Tom Lane <[email-masked]>
Date: Thu, 30 Aug 2018 10:28:18 -0400

> identifier is used in the docs.

That seems like an impossible level of clutter, too.

			regards, tom lane