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.94.2) (envelope-from ) id 1t8oXR-0065XP-HQ for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Nov 2024 22:30:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1t8oXO-009m32-Cb for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Nov 2024 22:30:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t8oXO-009m2u-2z for pgsql-bugs@lists.postgresql.org; Wed, 06 Nov 2024 22:30:06 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t8oXL-000XK4-W8 for pgsql-bugs@lists.postgresql.org; Wed, 06 Nov 2024 22:30:05 +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 4A6MU2rv3305168; Wed, 6 Nov 2024 17:30:03 -0500 From: Tom Lane To: jbe-mlist@magnetkern.de cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18693: Column names not set when using SELECT STRICT INTO with RECORD type In-reply-to: <18693-65968418890877b4@postgresql.org> References: <18693-65968418890877b4@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Wed, 06 Nov 2024 18:05:53 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3305166.1730932202.1@sss.pgh.pa.us> Date: Wed, 06 Nov 2024 17:30:02 -0500 Message-ID: <3305167.1730932202@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > I used the following SQL code: > CREATE TABLE tbl (a TEXT, b TEXT); > CREATE FUNCTION "buggy"() RETURNS VOID LANGUAGE plpgsql AS $$ > DECLARE > t tbl%ROWTYPE; > r RECORD; > BEGIN > SELECT a, b INTO t FROM (SELECT 'A' AS a, 'B' AS b) AS q; > RAISE NOTICE 'non-strict t = %', t; > RAISE NOTICE 'non-strict t.a = %', t.a; > RAISE NOTICE 'non-strict t.b = %', t.b; > SELECT a, b STRICT INTO t FROM (SELECT 'A' AS a, 'B' AS b) AS q; > RAISE NOTICE 'STRICT t = %', t; > RAISE NOTICE 'STRICT t.a = %', t.a; > RAISE NOTICE 'STRICT t.b = %', t.b; > SELECT a, b INTO r FROM (SELECT 'A' AS a, 'B' AS b) AS q; > RAISE NOTICE 'non-strict r = %', r; > RAISE NOTICE 'non-strict r.a = %', r.a; > RAISE NOTICE 'non-strict r.b = %', r.b; > SELECT a, b STRICT INTO r FROM (SELECT 'A' AS a, 'B' AS b) AS q; > RAISE NOTICE 'STRICT r = %', r; > RAISE NOTICE 'STRICT r.a = %', r.a; > RAISE NOTICE 'STRICT r.b = %', r.b; > END; > $$; You have the syntax wrong: what you intended (I presume) is SELECT a, b INTO STRICT r FROM (SELECT 'A' AS a, 'B' AS b) AS q; With what you wrote, the STRICT is taken as an output column alias of the outer SELECT, as if the command were SELECT a, b AS "strict" INTO r FROM (SELECT 'A' AS a, 'B' AS b) AS q; The alias makes no difference when assigning to "t", since its column names are predetermined. But when assigning to "r", the second column winds up being named "strict" not "b". You can demonstrate this with SELECT a, b STRICT INTO r FROM (SELECT 'A' AS a, 'B' AS b) AS q; RAISE NOTICE 'STRICT r = %', r; RAISE NOTICE 'STRICT r.a = %', r.a; RAISE NOTICE 'STRICT r.strict = %', r."strict"; (It's kind of annoying that "strict" has to be double-quoted in the RAISE NOTICE, especially since you get a rather misleading error if it isn't. But that seems like a different discussion.) regards, tom lane