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 1mH5GV-000225-Hr for pgsql-novice@arkaria.postgresql.org; Fri, 20 Aug 2021 14:12:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mH5GT-0005hx-R6 for pgsql-novice@arkaria.postgresql.org; Fri, 20 Aug 2021 14:12:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mH5GT-0005hn-Jm for pgsql-novice@lists.postgresql.org; Fri, 20 Aug 2021 14:12:57 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mH5GO-0006Qu-1C for pgsql-novice@postgresql.org; Fri, 20 Aug 2021 14:12:56 +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 17KECmY31853892; Fri, 20 Aug 2021 10:12:48 -0400 From: Tom Lane To: Roger Mason cc: pgsql-novice@postgresql.org Subject: Re: plpgsql select into In-reply-to: References: Comments: In-reply-to Roger Mason message dated "Fri, 20 Aug 2021 08:08:07 -0230" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1853890.1629468768.1@sss.pgh.pa.us> Date: Fri, 20 Aug 2021 10:12:48 -0400 Message-ID: <1853891.1629468768@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Roger Mason writes: > CREATE OR REPLACE FUNCTION get_info (id text) > RETURNS TABLE ( > tabular_info text > ) > AS $function$ > BEGIN > RETURN query WITH a AS ( > SELECT > regexp_split_to_table(info_out, '\n') AS info > FROM > public.results > WHERE > public.results.jid = id > ) > SELECT > * INTO tabular_info > FROM > a RETURN; > END; > $function$ > LANGUAGE plpgsql; You need to drop the "INTO tabular_info" bit, as the RETURN QUERY context already dictates where the results should go. Possibly we could improve the error message. It's already been changed somewhat in v14/HEAD: I get ERROR: query "WITH a AS ( SELECT regexp_split_to_table(info_out, '\n') AS info FROM public.results WHERE public.results.jid = id ) SELECT * INTO tabular_info FROM a RETURN" is not a SELECT CONTEXT: PL/pgSQL function get_info(text) line 3 at RETURN QUERY Looking at this, though, I'm pretty unhappy with it. It would be more readable to put the query text last, or maybe even as a CONTEXT line. But the real issue is that it's still not making the point that SELECT INTO is different from plain SELECT. Perhaps we should special-case that, with say "query is SELECT INTO, but it should be a plain SELECT". regards, tom lane