Underscore in positional parameters?

From: Erik Wienhold <ewie(at)ewie(dot)name>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Underscore in positional parameters?
Date: 2024-05-14 03:18:24
Message-ID: 5d216d1c-91f6-4cbe-95e2-b4cbd930520c@ewie.name
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I noticed that we (kind of) accept underscores in positional parameters.
For example, this works:

=> PREPARE p1 AS SELECT $1_2;
PREPARE
=> EXECUTE p1 (123);
?column?
----------
123
(1 row)

Parameter $1_2 is taken as $1 because in rule {param} in scan.l we get
the parameter number with atol which stops at the underscore. That's a
regression in faff8f8e47f. Before that commit, $1_2 resulted in
"ERROR: trailing junk after parameter".

I can't tell which fix is the way to go: (1) accept underscores without
using atol, or (2) just forbid underscores. Any ideas?

atol can be replaced with pg_strtoint32_safe to handle the underscores.
This also avoids atol's undefined behavior on overflows. AFAICT,
positional parameters are not part of the SQL standard, so nothing
prevents us from accepting underscores here as well. The attached patch
does that and also adds a test case.

But reverting {param} to its old form to forbid underscores also makes
sense. That is:

param \${decdigit}+
param_junk \${decdigit}+{ident_start}

It seems very unlikely that anybody uses that many parameters and still
cares about readability to use underscores. But maybe users simply
expect that underscores are valid here as well.

--
Erik

Attachment Content-Type Size
0001-Fix-parsing-of-positional-parameters-with-underscore.patch text/x-diff 2.1 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andy Fan 2024-05-14 03:38:08 Re: UniqueKey v2
Previous Message Andy Fan 2024-05-14 03:15:58 Re: UniqueKey v2