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 1qBeHU-0008LH-3Z for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Jun 2023 16:32:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qBeHS-0005Lp-V1 for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Jun 2023 16:32:34 +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 1qBeHS-0005Lf-NF for pgsql-bugs@lists.postgresql.org; Tue, 20 Jun 2023 16:32:34 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qBeHQ-003enI-As for pgsql-bugs@lists.postgresql.org; Tue, 20 Jun 2023 16:32:34 +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 35KGWRBO3505792; Tue, 20 Jun 2023 12:32:27 -0400 From: Tom Lane To: Japin Li cc: exclusion@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17983: Assert IsTransactionState() failed when empty string statement prepared in aborted transaction In-reply-to: References: <17983-da4569fcb878672e@postgresql.org> Comments: In-reply-to Japin Li message dated "Tue, 20 Jun 2023 16:17:33 +0800" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <3505697.1687278682.0@sss.pgh.pa.us> Date: Tue, 20 Jun 2023 12:32:27 -0400 Message-ID: <3505791.1687278747@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3505697.1687278682.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Japin Li writes: > On Tue, 20 Jun 2023 at 03:00, PG Bug reporting form wrote: >> The following psql script: >> BEGIN; >> ERROR; >> ; >> \gdesc >> triggers an assertion failure with the following stack trace: >> so maybe an empty input string is not legal for creating a cached plan >> when the transaction is aborted? > Yeah, SearchSysCache1() need in an transaction block, here is a patch > fixed it. I'm not sure if anyone out there is expecting that this case should work, but it probably did work at one time. Rather than throwing an error, it'd be better to fix plancache.c so it doesn't fail. I looked at the code and found that that's pretty much a one-line fix, because there are already code paths that avoid doing anything extra for transaction control commands (e.g ROLLBACK, which'd otherwise have this same issue). We just need to make a NULL parsetree use those paths. > Another question, why should we need to create a plan cache entry for > empty input? Well, we have to have something to support the wire protocol behavior for this case. No doubt we could hack up postgres.c to handle it without a plan cache entry, but it'd be far more invasive to do it there. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="dont-fail-on-preparing-empty-statement.patch"; charset="us-ascii" Content-ID: <3505697.1687278682.2@sss.pgh.pa.us> Content-Description: dont-fail-on-preparing-empty-statement.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/cache/plancache.c b/src/backend/utils/cache= /plancache.c index 87210fcf62..d4bfc55001 100644 --- a/src/backend/utils/cache/plancache.c +++ b/src/backend/utils/cache/plancache.c @@ -78,9 +78,10 @@ /* * We must skip "overhead" operations that involve database access when t= he * cached plan's subject statement is a transaction control command. + * For convenience, treat empty statements as control commands too. */ #define IsTransactionStmtPlan(plansource) \ - ((plansource)->raw_parse_tree && \ + ((plansource)->raw_parse_tree =3D=3D NULL || \ IsA((plansource)->raw_parse_tree->stmt, TransactionStmt)) = /* ------- =_aaaaaaaaaa0--