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 1uPV9e-009HBd-Jo for pgsql-bugs@arkaria.postgresql.org; Wed, 11 Jun 2025 23:46:50 +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 1uPV9c-007nK9-9g for pgsql-bugs@arkaria.postgresql.org; Wed, 11 Jun 2025 23:46:48 +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.94.2) (envelope-from ) id 1uPV9b-007nK1-B1 for pgsql-bugs@lists.postgresql.org; Wed, 11 Jun 2025 23:46:48 +0000 Received: from fout-b6-smtp.messagingengine.com ([202.12.124.149]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uPV9Z-001Xi6-0p for pgsql-bugs@lists.postgresql.org; Wed, 11 Jun 2025 23:46:47 +0000 Received: from phl-compute-03.internal (phl-compute-03.phl.internal [10.202.2.43]) by mailfout.stl.internal (Postfix) with ESMTP id 1D58D1140187; Wed, 11 Jun 2025 19:46:42 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-03.internal (MEProxy); Wed, 11 Jun 2025 19:46:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1749685601; x=1749772001; bh=w+WFAZuoSh vgc7SU7IjlnvmGjtbGDk1ZK3DUF1usqtw=; b=WpOB4RBVDPpsFOYckMrE83nzOd tQzu0XqIQIKMyvDUVeb/rYmX6Zuk9xp8FjgJR9gdAgGnN7UhaW2BkD+H6284mqu9 pWsaM+SkGzCf1evUb7ca5l9t8FVkGD38Dc/xA8PuXehraLyRgtSvhPcPJQTZGZm3 2OouQiLSgGJXzl02VM/ijalPQMq44tCVSw9vR/CHu1LY7ZpcUsD1OMVwDm6oS/0m l+jP/46h1V48yKWMT69Q1H3MS2IBvtWypVdI/buM0uOjQhskKOfjDrzPMHw0XWvP jm9CyKPY8avhkz45TQu8RO8MtdHRn7UydxU9UxkyE8wsXcT3om6Fy+G9FuCg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1749685601; x=1749772001; bh=w+WFAZuoShvgc7SU7IjlnvmGjtbGDk1ZK3D UF1usqtw=; b=ckeAJYnmIgaCBhzc8PoIh05r28uPbBHw3MVkSy7yVvdRhddsfrQ yTCnvJJKf+m3NGVFAMYekZroR7A8LCAGq5JN44LIfhdfuSpyfSuFupoXqL6dpiRD AElTltRaNOYN10pOJ3AjgaEKox8+FFI17w0Eu7jJq/fJ7k1glOrPpQLn1JRvtr+9 VWihiXLnlurZlkAWXYZiY6T/rW7XqD9KhVPkAjt1bUmls4ffz6D0uk40OlvMBW5J +jTcLPGKqM06Sxvt7DtMhjDQWcbmq1ZiR8cZasB7jHsin0fjKirBk1g8TFe7x8JC sIYPXZMVYDWsb9BqKz1WGE4ZBT4uZfXCAaQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtddugddufeehgecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenfghrlhcuvffnffculdejtddmnecujfgurhepfffhvfevuffk fhggtggujgesghdtreertddtvdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrh cuoehmihgthhgrvghlsehprghquhhivghrrdighiiiqeenucggtffrrghtthgvrhhnpeet leeifedufffhhfdtteelgeeggeffhfekueevteeigfduudevudetgfegiedvjeenucevlh hushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehmihgthhgrvghl sehprghquhhivghrrdighiiipdhnsggprhgtphhtthhopeehpdhmohguvgepshhmthhpoh huthdprhgtphhtthhopegrnhhthhhonhhinhdrsghonhhnvghfohihsegurghtrgguohhg hhhqrdgtohhmpdhrtghpthhtohepmhgrshgrohdrfhhujhhiihesohhsshdrnhhtthgurg htrgdrtghomhdprhgtphhtthhopeguihhlihhpsggrlhgruhhtsehgmhgrihhlrdgtohhm pdhrtghpthhtoheprgdrkhhoiihhvghmhigrkhhinhesphhoshhtghhrvghsphhrohdrrh hupdhrtghpthhtohepphhgshhqlhdqsghughhssehlihhsthhsrdhpohhsthhgrhgvshhq lhdrohhrgh X-ME-Proxy: Feedback-ID: i0fe9450f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 11 Jun 2025 19:46:38 -0400 (EDT) Date: Thu, 12 Jun 2025 08:46:26 +0900 From: Michael Paquier To: Anthonin Bonnefoy Cc: Fujii Masao , Dilip Kumar , a.kozhemyakin@postgrespro.ru, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18947: TRAP: failed Assert("len_to_wrt >= 0") in pg_stat_statements Message-ID: References: <9171ecda-e1f4-4390-ae54-98bc441b98e3@oss.nttdata.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="i1IFZ4qMitKXZXQq" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --i1IFZ4qMitKXZXQq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Jun 11, 2025 at 11:04:36AM +0200, Anthonin Bonnefoy wrote: > Here's a v2 for the attempted fix. This is using the mentioned approach: > - SelectStmt's location is set to Select's position when the node if built > - If there are surrounding parentheses, we include them and move the > start of the statement to the outermost '(' >=20 > The Statement length is only set for: > - COPY: We use the PreparableStatement's surrounding '()' to set both > location and length (setting location should be redundant here though) > - CTAS: we set the statement's length if there's a tailing 'WITH (NO) DAT= A'. > - Other than those, the statement length will fallback to use the top > RawStmt's remaining length. @@ -3417,9 +3418,9 @@ CopyStmt: COPY opt_binary qualified_name opt_column_l= ist=20 [...] - updatePreparableStmtEnd($3, @2, @4 - @2); + updatePreparableStmtEnd($3, @3, @4 - @3); For the case of COPY, it is possible to get a much better report by setting the location and length to not include the first parenthesis, like that. > I haven't done it for Views and JSON_ARRAY. Their nested queries > aren't currently tracked and reported by pgss so there's no way to > test those. I have to admit that it is inconsistent to set a location in their respective inner nodes while we don't use updateSelectStmtEnd() to adjust the length based on the location assigned by the parser. There's a risk of having a code path in the future looking at the location later on based on the parsed state and taking a wrong decision because the location and the length do not match. Such cases could be easier to reach outside of core, for example imagine an extension looking at a ViewStmt where we set the location but not a matching length. We would consider as inner query a string that begins at the beginning of the location until the end of the string because the length is not set to match with what the parser finds. > This could be done but there's no available way to test > this is correct AFAIK? Yes, perhaps we should think harder about this part. There are two patterns among a few others like UNION, which can also be used in views. Anyway, I am going to pull the plug on this one today, because we need to take a decision with beta2 approaching fast but it's mostly me being not fully confident that this is right as-is. If we're wrong in one path, that's an out-of-bound memory write waiting for us in PGSS for the query normalization, as this report proves, and that would be bad. The first step would be to improve the main regression test suite for all these grammar "parenthesis" patterns we have never had coverage for in core. I am wondering if it is actually possible to reach an inconsistent state even in older stable branches because we've never tested all these grammar patterns. Perhaps I'm wrong, but we would be more confident with all these extra patterns stressed first by 027_stream_regress.pl which forces queries to have some normalization applied. -- Michael --i1IFZ4qMitKXZXQq Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmhKFVIACgkQnvQgOdby QH0Vvw/+OT8d4BUZF5tS/l35RGwzK2jgKPHKW6crI257/k3WwvKv7l1u4VbM+lXy Dgx87KUEuw623jgFZLaxW5QHPbvTzoj0WnWHSPjneeWmL7ig1Yp+IOu+i+hPfuwT AeGKbBIKAJpS1LN6cC9lQ5/u5L8zFUd0sTV4M/dBpSF/iPZDE8elkwMvLruIAONn AsMf/8dOBleHj0aAdPQse/S2xYEqHFLUqlwjdxX5l3Urksh7bh1QDZwp3ORoQvFU oWE6LjTIw7YZrNrcaV5L5yYQekPSPqN7WC9UGNksHc8KoM00TwGMy7wSfInB+nMh ZCFX72gdefcPICa95zlQElqoDLUGe6xa66tUaXeWF7sMVCH21MZ5fJ0KUMGeEDDZ cWRgVF8Mo0dOJba+488352Fwzx+G+mnmLQaZcdvCLzgonJQtJJfOEFiUHPeSLv9+ 207sZwNlYjCVHgPWET7jmeDAvEI8jJ3cjS0z30I9eZp+V+J1/yQ5gLCZhV2eYMBv I/RfLlm8aueC0vrwMSy+pb24/KWbEs73unoCNrEy4rlHN/q3gwRG7SweKifktBhH 9E54myPptafAvQy11w17tBJvsuP+EkN53579qxJIPqbGjrfPhAhBLTj8zvsRqOgs qOe0T3lJmVWNF7HI20Q6oeeKzm4e+NHyWQ2hKEEPs/87GZCKm+E= =STrV -----END PGP SIGNATURE----- --i1IFZ4qMitKXZXQq--