Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mD8zg-0006UK-Bh for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Aug 2021 17:23:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mD8z9-0001hQ-PA for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Aug 2021 17:22:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mD8z9-0001hH-IU for pgsql-bugs@lists.postgresql.org; Mon, 09 Aug 2021 17:22:47 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mD8z4-0001eK-1O for pgsql-bugs@lists.postgresql.org; Mon, 09 Aug 2021 17:22:46 +0000 Received: from bruce by momjian.us with local (Exim 4.92) (envelope-from ) id 1mD8z2-00030m-3Y; Mon, 09 Aug 2021 13:22:40 -0400 Date: Mon, 9 Aug 2021 13:22:40 -0400 From: Bruce Momjian To: alexone07@mail.ru, pgsql-bugs@lists.postgresql.org Cc: Tom Lane Subject: Re: BUG #17137: Bug with datatypes definition in CTE Message-ID: <20210809172240.GA31401@momjian.us> References: <17137-3d3732d5a259612c@postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <17137-3d3732d5a259612c@postgresql.org> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 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 https://momjian.us EDB https://enterprisedb.com If only the physical world exists, free will is an illusion.