Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vvdxI-00Bdf4-35 for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Feb 2026 16:11:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vvdxH-00Dusj-2H for pgsql-bugs@arkaria.postgresql.org; Thu, 26 Feb 2026 16:11:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vvdxH-00DusT-1S for pgsql-bugs@lists.postgresql.org; Thu, 26 Feb 2026 16:11:11 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vvdxA-00000001Py1-2u0s for pgsql-bugs@lists.postgresql.org; Thu, 26 Feb 2026 16:11:10 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 61QGAe1S643858; Thu, 26 Feb 2026 11:10:40 -0500 From: Tom Lane To: Swirl Smog Dowry cc: pgsql-bugs@lists.postgresql.org, Richard Guo Subject: Re: pg_get_viewdef() produces non-round-trippable SQL for views with USING join on mismatched integer types In-reply-to: References: Comments: In-reply-to Swirl Smog Dowry message dated "Thu, 26 Feb 2026 11:18:43 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <643856.1772122240.1@sss.pgh.pa.us> Date: Thu, 26 Feb 2026 11:10:40 -0500 Message-ID: <643857.1772122240@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Swirl Smog Dowry writes: > When a view uses a USING join on columns with different integer > types (integer vs bigint) and the SELECT clause contains an explicit > narrowing cast, pg_get_viewdef() produces SQL that PostgreSQL itself > rejects. This makes pg_dump produce dumps that fail on restore for > any such view. Hmm, yeah. This used to work as-expected, too. "git bisect" finds that it broke at 247dea89f7616fdf06b7272b74abafc29e8e5860 is the first bad commit commit 247dea89f7616fdf06b7272b74abafc29e8e5860 Author: Richard Guo Date: Tue Sep 10 12:35:34 2024 +0900 Introduce an RTE for the grouping step Looking at the parse tree for the problem query, I see {RANGETBLENTRY :alias <> :eref {ALIAS :aliasname *GROUP* :colnames ("?column?" "label") } :rtekind 9 :groupexprs ( {FUNCEXPR :funcid 481 :funcresulttype 20 :funcretset false :funcvariadic false :funcformat 2 :funccollid 0 :inputcollid 0 :args ( {VAR :varno 1 :varattno 1 :vartype 23 :vartypmod -1 :varcollid 0 :varnullingrels (b) :varlevelsup 0 :varreturningtype 0 :varnosyn 1 :varattnosyn 1 :location -1 } ) :location -1 } {VAR :varno 2 :varattno 2 :vartype 25 :vartypmod -1 :varcollid 100 :varnullingrels (b 3) :varlevelsup 0 :varreturningtype 0 :varnosyn 2 :varattnosyn 2 :location 32 } ) :lateral false :inFromCl false :securityQuals <> } The first groupexpr is the same as the joinaliasvars entry for that column in the JOIN RTE. This surprises me: I'd expect to see a reference to the join output column there, ie Var 3/1, because I'm pretty sure that's what parsing of "GROUP BY year" would have produced initially. If it were like that, I think ruleutils would produce the desired output. So I'd tentatively classify this as "join alias Vars are being flattened too soon". Richard, any thoughts? regards, tom lane