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 1tiYcY-00HLHN-5h for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Feb 2025 12:47:10 +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 1tiYcW-006vW0-6i for pgsql-hackers@arkaria.postgresql.org; Thu, 13 Feb 2025 12:47:08 +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 1tiYcV-006vSQ-E1 for pgsql-hackers@lists.postgresql.org; Thu, 13 Feb 2025 12:47:08 +0000 Received: from fout-a1-smtp.messagingengine.com ([103.168.172.144]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1tiYcU-000Zzq-0U for pgsql-hackers@postgresql.org; Thu, 13 Feb 2025 12:47:07 +0000 Received: from phl-compute-09.internal (phl-compute-09.phl.internal [10.202.2.49]) by mailfout.phl.internal (Postfix) with ESMTP id CC1721380044; Thu, 13 Feb 2025 07:47:04 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-09.internal (MEProxy); Thu, 13 Feb 2025 07:47:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1739450824; x=1739537224; bh=c /bipPpddnGdEKCb1LYpQoUNwITOEIDUla2cZ4MIOYA=; b=GO/rSgk1xz3J5wW+9 LR7PCt+7KjfAFm0Ky9yUz9wAjDOUylDLPsVE5/SMXnGFgNQ9soH8euQrABIWEx/y v6hpR8NIClbZFhZ3jFcjeAGFqpg0R7cvar/P1c5Gdo23OT19MCOMvt9uyHFaylHp xJJd1SyRM5qy5qxDtIyxnGdyuAgYICOdh6i8q47cwlHDIAvlqsgXxhK8tzwq4rOO 5Se8Gd40AGXSlCnqmbTMF4iMS5R/CBgUbYkfpHScjOn9wbeQKGiZB3Zf8EwfBeHk ZycACnQDPEmOE9oO0+iWZZoK/WnYzIOTxSZ7WzQQ7izIm1XGN++QAt5ap8ItZkli J0l6g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdegieekudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpeffhffvvefukfggtggugfgjsehtkeertddttdej necuhfhrohhmpemllhhvrghrohcujfgvrhhrvghrrgcuoegrlhhvhhgvrhhrvgesrghlvh hhrdhnohdqihhprdhorhhgqeenucggtffrrghtthgvrhhnpedvheeuffehgfeukeeufeeh hefghfeugfefudekjeevudffgeffkeegffffveegheenucffohhmrghinhepvghnthgvrh hprhhishgvuggsrdgtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehm rghilhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrghdpnhgspg hrtghpthhtohepudekpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehkohhusegt lhgvrghrqdgtohguvgdrtghomhdprhgtphhtthhopehmrghrtghoshesfhdutddrtghomh drsghrpdhrtghpthhtoheplegvrhhthhgrlhhiohhnieesghhmrghilhdrtghomhdprhgt phhtthhopehgvghiuggrvhdrphhgsehgmhgrihhlrdgtohhmpdhrtghpthhtohepnhgrth hhrghnuggsohhsshgrrhhtsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphgrvhgvlhdr thhruhhkhhgrnhhovhesghhmrghilhdrtghomhdprhgtphhtthhopehrvghshhhkvghkih hrihhllhesghhmrghilhdrtghomhdprhgtphhtthhopehrohgsvghrthhmhhgrrghssehg mhgrihhlrdgtohhmpdhrtghpthhtohepshgrmhhimhhsvghihhesghhmrghilhdrtghomh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 13 Feb 2025 07:47:03 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1739450821; bh=KPaGNPF51pqcfF2yJ0dTHWHlsyQwOXVDvXw8vNMeHWY=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=T2nSadv4PztKG2WsPqJ7JNTn7H8D3uYh5l2EFZpdP5at2u6nCxiIu5s1SOj+bqOiN cV2RVMD31wyg1hL5v3q9wr91h/gxtJDovZWwJrAhYhz6B3BwEuS0reJ5Pv7j/ZrLFR HO9g7HN2gesRrSaDqfv4CSDplx9U2dCUgrY00cxlWTDB9ePgAeNcJ91WgTOE1do2kx TPbfde5BmgFv1PKg0r1YRqmNd3OS+7LJFDiPU0mPS8pkgJwVCaX2KRzivB7sH1OXmW iYoEPKKfupdodEGbsYMM1tJmuYFeeIll21roSzj4ncGJtlqYrFBvSl1rRKn13lDLXA /0SBCwa8h0vCw== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 183B36D; Thu, 13 Feb 2025 13:47:01 +0100 (CET) Date: Thu, 13 Feb 2025 13:47:01 +0100 From: =?utf-8?Q?=C3=81lvaro?= Herrera To: Dmitry Dolgov <9erthalion6@gmail.com> Cc: Sami Imseih , Kirill Reshke , Sergei Kornilov , yasuo.honda@gmail.com, tgl@sss.pgh.pa.us, smithpb2250@gmail.com, vignesh21@gmail.com, michael@paquier.xyz, nathandbossart@gmail.com, stark.cfm@gmail.com, geidav.pg@gmail.com, marcos@f10.com.br, robertmhaas@gmail.com, david@pgmasters.net, pgsql-hackers@postgresql.org, pavel.trukhanov@gmail.com, Sutou Kouhei Subject: Re: pg_stat_statements and "IN" conditions Message-ID: <202502131247.zls4jgcl2yqe@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2025-Feb-13, Dmitry Dolgov wrote: > Here is how it looks like (posting only the first patch, since we > concentrate on it). This version handles just a little more to cover > simpe cases like the implicit convertion above. The GUC is also moved > out from pgss and renamed to query_id_merge_values. On top I've added > more tests showing the impact, as well as sometimes awkward looking > normalized query I was talking about. I'm going to experiment how to > iron out the latter. Thanks! It's looking better. Some small comments -- please add the new GUC to postgresql.conf.sample. Also, how wed are you to "query_id_merge_values" as a name? It's not in any way obvious that this is about values in arrays. How about query_id_squash_arrays? Or are you thinking in having values in other types of structures squashed as well, and that this first patch does it for arrays only but you want the GUC to also control some future feature? (I think I prefer "squash" here as a verb to "merge"). > +static bool > +IsMergeableConst(Node *element) > +{ > + if (IsA(element, RelabelType)) > + element = (Node *) ((RelabelType *) element)->arg; > + > + if (IsA(element, CoerceViaIO)) > + element = (Node *) ((CoerceViaIO *) element)->arg; > + > + if(IsA(element, FuncExpr)) > + { > + FuncExpr *func = (FuncExpr *) element; > + char provolatile = func_volatile(func->funcid); I think calling func_volatile potentially once per array element is not good; this might cause dozens/thousands of identical syscache lookups. Maybe we can pass an initially NIL list from IsMergeableConstList (as List **), which IsMergeableConst fills with OIDs of functions that have been checked and found acceptable. Then the second time around we search the list first and only do func_volatile() after not finding a match. Another thing I didn't quite understand is why you did this rather baroque-looking list scan: > +static bool > +IsMergeableConstList(List *elements, Node **firstExpr, Node **lastExpr) > +{ > + ListCell *temp; > + Node *firstElem = NULL; > + > + if (elements == NIL) > + return false; > + > + if (!query_id_merge_values) > + { > + /* Merging is disabled, process everything one by one */ > + return false; > + } > + > + firstElem = linitial(elements); > + > + /* > + * If the first expression is a constant, verify if the following elements > + * are constants as well. If yes, the list is eligible for merging. > + */ > + if (IsMergeableConst(firstElem)) > + { > + foreach(temp, elements) > + { > + Node *element = lfirst(temp); > + > + if (!IsMergeableConst(element)) > + return false; > + } > + > + *firstExpr = firstElem; > + *lastExpr = llast(elements); > + return true; > + } Why not just scan the list in the straightforward way, that is foreach(temp, elements) { if (!IsMergeableConst(lfirst(temp))) return false; } *firstExpr = linitial(elements); *lastExpr = llast(elements); return true; Is there something being optimized here specifically for the first element? I don't see it. -- Álvaro Herrera PostgreSQL Developer — https://www.EnterpriseDB.com/