Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Bryn Llewellyn <bryn(at)yugabyte(dot)com>
Cc: Tom Lane PostgreSQL <tgl(at)sss(dot)pgh(dot)pa(dot)us>, John W Higgins <wishdev(at)gmail(dot)com>, pgsql-general list <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type
Date: 2021-08-10 05:23:11
Message-ID: CAFj8pRDwC=UN9=_jXuRDLFcqaAFdHP6YrOTgz_9Gxu4ZL9m8ew@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>
>
>
> It now seems to me to be odd, in the light of the explanations for why the
> naïve (PL/SQL-style) syntax doesn't work in PL/pgSQL, that assigning a
> scalar subquery to a variable of the composite type in question _does_
> work! But don't take that as a question. I'm going to regard this as "case
> closed".
>

This depends on how integration of PL/pgSQL and SQL is designed. PL/pgSQL
is a relatively small procedural interpretation over SQL engine. When you
evaluate a query, then you always get a composite value (named tuple)
always (in all cases).

SELECT 10, 20 INTO rec;

In this case you get composite (10,20) and it can be assigned to composite
without problems.

SELECT (10,20) INTO rec

returns composite ((10,20)), and that cannot be assigned to your composite.

Syntax rec := (SELECT 10,20) is not possible. Subquery can return only one
value always. More values are not allowed.

rec := (SELECT (10,20)) is working, because you can assign (in all cases)
the first field of returned composite value. This syntax cannot be
ambiguous.

If you work intensively with plpgsql, then it can be a very informative
look at plpgsql source code. Don't be afraid it is not too long, and you
will see. It is very simple. Then you can understand how it works.

https://github.com/postgres/postgres/tree/master/src/pl/plpgsql/src

Regards

Pavel

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Matthias Apitz 2021-08-10 07:23:34 Re: (13.1) pg_basebackups ./. pg_verifybackup
Previous Message Pavel Stehule 2021-08-10 05:05:57 Re: PL/pgSQL: Spurious 22P02 error on "select col into var" when col is user-defined type