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 1ooPda-0005E3-Bt for pgsql-bugs@arkaria.postgresql.org; Fri, 28 Oct 2022 13:43:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ooPdZ-00070j-9X for pgsql-bugs@arkaria.postgresql.org; Fri, 28 Oct 2022 13:43:05 +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 1ooPdZ-0006zj-15 for pgsql-bugs@lists.postgresql.org; Fri, 28 Oct 2022 13:43:05 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ooPdX-0006CW-0I for pgsql-bugs@lists.postgresql.org; Fri, 28 Oct 2022 13:43:04 +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 29SDh0dC1195626; Fri, 28 Oct 2022 09:43:01 -0400 From: Tom Lane To: s.shinderuk@postgrespro.ru cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17669: Invalid TOAST pointer in PL/pgSQL variable In-reply-to: <17669-2238da15dce5c53c@postgresql.org> References: <17669-2238da15dce5c53c@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Thu, 27 Oct 2022 19:45:14 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1195624.1666964580.1@sss.pgh.pa.us> Date: Fri, 28 Oct 2022 09:43:00 -0400 Message-ID: <1195625.1666964580@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: > create function f() returns text as $$ > declare > s text; > begin > select b into s from t where a = 1; > truncate t; > return s; > end; > $$ language plpgsql; > postgres=# select f(); > ERROR: missing chunk number 0 for toast value 24727 in pg_toast_24722 To prevent that, every fetch into a plpgsql variable would have to immediately detoast the value, in case somebody did something as weird as dropping/truncating the table later in the function. That's an awfully expensive bit of protection. We do in fact do it like that in procedures (more specifically, in non-atomic contexts), so a possible workaround for you is to make this a procedure not a function. I'm disinclined to change it otherwise. regards, tom lane