pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
plpgsql: ambiguous column reference in ON CONFLICT clause
4+ messages / 4 participants
[nested] [flat]

* plpgsql: ambiguous column reference in ON CONFLICT clause
@ 2023-02-06 17:46  Torsten Förtsch <tfoertsch123@gmail.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Torsten Förtsch @ 2023-02-06 17:46 UTC (permalink / raw)
  To: pgsql-general

Is there a way to prevent this from happening? I know I can use the PK
constraint name or rename the OUT variable i. The question is can this be
resolved while keeping the arbiter inference and the variable name.

CREATE TABLE x.x (
    i INT PRIMARY KEY
);

CREATE OR REPLACE FUNCTION x.ins(p_i INT, OUT i INT)
LANGUAGE plpgsql AS $$
  BEGIN
    INSERT INTO x.x(i)
    SELECT p_i
        ON CONFLICT (i) DO NOTHING;
  END
$$;

postgres=# select * from x.ins(1);
ERROR:  column reference "i" is ambiguous
LINE 3:         ON CONFLICT (i) DO NOTHING
                            ^
DETAIL:  It could refer to either a PL/pgSQL variable or a table column.
QUERY:  INSERT INTO x.x(i)
    SELECT p_i
        ON CONFLICT (i) DO NOTHING
CONTEXT:  PL/pgSQL function x.ins(integer) line 3 at SQL statement

The conflicting variable is the OUT parameter of the function.

Normally, I'd suggest to fully qualify the name but the following or
similar is a syntax error:

    INSERT INTO x.x(i) AS t
    SELECT p_i
        ON CONFLICT (t.i) DO NOTHING;

According to the documentation in
https://www.postgresql.org/docs/current/plpgsql-implementation.html:

> Query parameters will only be substituted in places where they are
syntactically permissible.

and

> Another way to understand this is that variable substitution can only
insert data values into an SQL command; it cannot dynamically change which
database objects are referenced by the command.

After reading this I am wondering if the current behavior is actually a bug.

Thanks,
Torsten

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: plpgsql: ambiguous column reference in ON CONFLICT clause
@ 2023-02-06 17:50  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Torsten Förtsch <tfoertsch123@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Adrian Klaver @ 2023-02-06 17:50 UTC (permalink / raw)
  To: Torsten Förtsch <tfoertsch123@gmail.com>; pgsql-general

On 2/6/23 09:46, Torsten Förtsch wrote:
> Is there a way to prevent this from happening? I know I can use the PK 
> constraint name or rename the OUT variable i. The question is can this 
> be resolved while keeping the arbiter inference and the variable name.
> 
> CREATE TABLE x.x (
>      i INT PRIMARY KEY
> );
> 
> CREATE OR REPLACE FUNCTION x.ins(p_i INT, OUT i INT)
> LANGUAGE plpgsql AS $$
>    BEGIN
>      INSERT INTO x.x(i)
>      SELECT p_i
>          ON CONFLICT (i) DO NOTHING;
>    END
> $$;
> 
> postgres=# select * from x.ins(1);
> ERROR:  column reference "i" is ambiguous
> LINE 3:         ON CONFLICT (i) DO NOTHING
>                              ^
> DETAIL:  It could refer to either a PL/pgSQL variable or a table column.
> QUERY:  INSERT INTO x.x(i)
>      SELECT p_i
>          ON CONFLICT (i) DO NOTHING
> CONTEXT:  PL/pgSQL function x.ins(integer) line 3 at SQL statement
> 
> The conflicting variable is the OUT parameter of the function.
> 
> Normally, I'd suggest to fully qualify the name but the following or 
> similar is a syntax error:
> 
>      INSERT INTO x.x(i) AS t
>      SELECT p_i
>          ON CONFLICT (t.i) DO NOTHING;
> 
> According to the documentation in 
> https://www.postgresql.org/docs/current/plpgsql-implementation.html 
> <https://www.postgresql.org/docs/current/plpgsql-implementation.html;:
> 
>  > Query parameters will only be substituted in places where they are 
> syntactically permissible.


See:

https://www.postgresql.org/docs/current/plpgsql-implementation.html#PLPGSQL-VAR-SUBST

for your choices.

> 
> and
> 
>  > Another way to understand this is that variable substitution can only 
> insert data values into an SQL command; it cannot dynamically change 
> which database objects are referenced by the command.
> 
> After reading this I am wondering if the current behavior is actually a bug.
> 
> Thanks,
> Torsten

-- 
Adrian Klaver
adrian.klaver@aklaver.com






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: plpgsql: ambiguous column reference in ON CONFLICT clause
@ 2023-02-06 18:02  David G. Johnston <david.g.johnston@gmail.com>
  parent: Torsten Förtsch <tfoertsch123@gmail.com>
  1 sibling, 1 reply; 4+ messages in thread

From: David G. Johnston @ 2023-02-06 18:02 UTC (permalink / raw)
  To: Torsten Förtsch <tfoertsch123@gmail.com>; +Cc: pgsql-general

On Monday, February 6, 2023, Torsten Förtsch <tfoertsch123@gmail.com> wrote:

>
> After reading this I am wondering if the current behavior is actually a
> bug.
>

Arguably it is a bug, and a known one at that if you want some light
reading, but regardless there is presently no proposal to get rid of the
POLA violation and little chance for any solution to be back-patched since
the issue is lack of good existing technical options.

https://www.postgresql.org/message-id/1990141.1638388378%40sss.pgh.pa.us

https://www.postgresql.org/message-id/flat/15533-ac9506ee433a0d18%40postgresql.org

David J.

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: plpgsql: ambiguous column reference in ON CONFLICT clause
@ 2023-02-06 18:26  Pavel Stehule <pavel.stehule@gmail.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Pavel Stehule @ 2023-02-06 18:26 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Torsten Förtsch <tfoertsch123@gmail.com>; pgsql-general

po 6. 2. 2023 v 19:02 odesílatel David G. Johnston <
david.g.johnston@gmail.com> napsal:

> On Monday, February 6, 2023, Torsten Förtsch <tfoertsch123@gmail.com>
> wrote:
>
>>
>> After reading this I am wondering if the current behavior is actually a
>> bug.
>>
>
> Arguably it is a bug, and a known one at that if you want some light
> reading, but regardless there is presently no proposal to get rid of the
> POLA violation and little chance for any solution to be back-patched since
> the issue is lack of good existing technical options.
>
> https://www.postgresql.org/message-id/1990141.1638388378%40sss.pgh.pa.us
>
>
> https://www.postgresql.org/message-id/flat/15533-ac9506ee433a0d18%40postgresql.org
>

This is an interesting issue. I fully agree, so it is very pretty
unintuitive, but when you start calculating with possibility of expression
indexes, it is probably the best that is possible. I don't think it is a
bug. Unfortunately, at this moment we have no information about single or
double braces usage. So there is not any possibility to say so there should
not be an expression's index. And if there can be a functional index, then
using a possible variable can be really confusing without unique meaning.

This is probably only one case, where you can specify an index in a query,
so there are not any other similar cases, and this special case should be
documented and explained in plpgsql doc.

Regards

Pavel



>
> David J.
>
>

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2023-02-06 18:26 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-02-06 17:46 plpgsql: ambiguous column reference in ON CONFLICT clause Torsten Förtsch <tfoertsch123@gmail.com>
2023-02-06 17:50 ` Adrian Klaver <adrian.klaver@aklaver.com>
2023-02-06 18:02 ` David G. Johnston <david.g.johnston@gmail.com>
2023-02-06 18:26   ` Pavel Stehule <pavel.stehule@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox