Re: BUG #17137: Bug with datatypes definition in CTE

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: alexone07(at)mail(dot)ru, pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Subject: Re: BUG #17137: Bug with datatypes definition in CTE
Date: 2021-08-09 17:22:40
Message-ID: 20210809172240.GA31401@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Fri, Aug 6, 2021 at 06:12:28PM +0000, PG Bug reporting form wrote:
> SQL expression doesn't execute with error:
> SQL Error [42804]: ERROR: recursive query "t" column 1 has type character
> varying(20) in non-recursive term but type character varying overall
> Help: Cast the output of the non-recursive term to the correct type.
>
> *But it works with text datatype.
>
> Some repro's:
> First:
> WITH RECURSIVE t AS (
> SELECT a::varchar AS b
> FROM (
> SELECT 'a'::varchar(20) AS a
> ) AS x
> UNION ALL
> SELECT 'a'::varchar AS b
> FROM t
> )
> SELECT *
> FROM t;

Well, this is an interesting report. First, the failure goes back at
least as far as 9.6, and this isn't complained about often. However,
someone did complain about it on August 4:

https://www.postgresql.org/message-id/flat/CABNQVagu3bZGqiTjb31a8D5Od3fUMs7Oh3gmZMQZVHZ%3DuWWWfQ%40mail.gmail.com

and on August 6, Tom Lane patched PG 14 beta and PG master to fix this:

commit 5c056b0c25
Author: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Date: Fri Aug 6 17:32:54 2021 -0400

Don't elide casting to typmod -1.

Casting a value that's already of a type with a specific typmod
to an unspecified typmod doesn't do anything so far as run-time
behavior is concerned. However, it really ought to change the
exposed type of the expression to match. Up to now,
coerce_type_typmod hasn't bothered with that, which creates gotchas
in contexts such as recursive unions. If for example one side of
the union is numeric(18,3), but it needs to be plain numeric to
match the other side, there's no direct way to express that.

This is easy enough to fix, by inserting a RelabelType to update the
exposed type of the expression. However, it's a bit nervous-making
to change this behavior, because it's stood for a really long time.
(I strongly suspect that it's like this in part because the logic
pre-dates the introduction of RelabelType in 7.0. The commit log
message for 57b30e8e2 is interesting reading here.) As a compromise,
we'll sneak the change into 14beta3, and consider back-patching to
stable branches if no complaints emerge in the next three months.

Discussion: https://postgr.es/m/CABNQVagu3bZGqiTjb31a8D5Od3fUMs7Oh3gmZMQZVHZ=uWWWfQ@mail.gmail.com

You can read above that we might backpatch this into PG 13 and other
supported releases in the future if there are no complaints in the next
three months. If you want to, you can apply the patch yourself and test
it.

--
Bruce Momjian <bruce(at)momjian(dot)us> https://momjian.us
EDB https://enterprisedb.com

If only the physical world exists, free will is an illusion.

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Kacey Holston 2021-08-09 18:55:43 Re: Issue with logical replication
Previous Message Amit Kapila 2021-08-09 08:58:12 Re: Issue with logical replication