Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hbnzN-00074w-Uo for pgsql-bugs@arkaria.postgresql.org; Fri, 14 Jun 2019 15:19:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hbnzM-00033e-Mn for pgsql-bugs@arkaria.postgresql.org; Fri, 14 Jun 2019 15:19:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hbnzM-00033F-CT for pgsql-bugs@lists.postgresql.org; Fri, 14 Jun 2019 15:19:36 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hbnzK-0004Zl-1f for pgsql-bugs@lists.postgresql.org; Fri, 14 Jun 2019 15:19:35 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x5EFJT2P027732; Fri, 14 Jun 2019 11:19:29 -0400 From: Tom Lane To: =?UTF-8?Q?S=c3=a9bastien_Lardi=c3=a8re?= cc: pgsql-bugs@lists.postgresql.org, fluca1978@gmail.com Subject: Re: Recursive CTE and collation In-reply-to: <4d73f7e5-a64d-2a9c-9148-09176d6261fb@lardiere.net> References: <97aa6131-7f79-a29b-1bb3-7e87b2f4f50c@lardiere.net> <28491.1560519572@sss.pgh.pa.us> <4d73f7e5-a64d-2a9c-9148-09176d6261fb@lardiere.net> Comments: In-reply-to =?UTF-8?Q?S=c3=a9bastien_Lardi=c3=a8re?= message dated "Fri, 14 Jun 2019 16:54:02 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27730.1560525569.1@sss.pgh.pa.us> Date: Fri, 14 Jun 2019 11:19:29 -0400 Message-ID: <27731.1560525569@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk =?UTF-8?Q?S=c3=a9bastien_Lardi=c3=a8re?= writes: > On 14/06/2019 15:39, Tom Lane wrote: >> I notice that it does work in a regular union: >> regression=# select null::text union select null::name collate "C"; >> but I believe that recursive union is intentionally stricter. I took a closer look at the code to refresh my memory about this, and the actual rule for recursive unions is that the output of the union has to have the same column types/collations that were inferred from the non-recursive (first) side alone. This is needed because when we do parse analysis of the recursive side, those types/collations are what we'll assume for any references to the recursive union's result. It's too late to change those decisions when we find out what the UNION actually produces. (You could imagine doing the parse analysis more than once in hopes of arriving at a stable result, but ugh. I don't think the SQL spec requires any such thing.) So what we have here is that in v11, you were union'ing text (collation "default") with name (no collation), and you got text with collation "default" because text is a preferred type over name. So it worked OK. In v12, you're union'ing text (collation "default") with name (collation "C"). You still get text output because text is still the preferred type, but the collation resolution rules consider "default" to not be preferred so the chosen output collation is "C". Ooops. Obviously there's more than one way you could fix the mismatch, but I think that changing the NULL to type "name" is the nicest. regards, tom lane