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.96) (envelope-from ) id 1w3ocf-001ZRj-24 for pgsql-hackers@arkaria.postgresql.org; Sat, 21 Mar 2026 05:11:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w3oce-008vql-0P for pgsql-hackers@arkaria.postgresql.org; Sat, 21 Mar 2026 05:11:40 +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.96) (envelope-from ) id 1w3ocd-008vqc-2G for pgsql-hackers@lists.postgresql.org; Sat, 21 Mar 2026 05:11:40 +0000 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w3ocX-00000000H9z-3GfY for pgsql-hackers@lists.postgresql.org; Sat, 21 Mar 2026 05:11:39 +0000 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-4870206f73bso5401715e9.3 for ; Fri, 20 Mar 2026 22:11:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1774069891; cv=none; d=google.com; s=arc-20240605; b=WCT0nGuch4WhZ/3rr8wMkQ7XCZ1eL6XZeqSl6oPHfc309uwQWdgp4VQgkVbRmY9TMk QD+JjI39CB8VHRiCmeUa6ixhr6t0WxDeOB3lYBJKYUMh5bLczi1E7p3IQNM2temOcyvI BdrGaOYxEErGdFUgOsMPaZ14tiFU5NzPBEgs1GTrYTxZytowzCQjK2l/SqVDMvJn0g8S X/vXo8SlQ6ap+ATSVUSUf3uUG1tor4LZQiTmZRZ/3TZJgBpc/K+0WI6p5M4/rwKek5lb H/gheA9UVpL51FNnQVcK8LkAOHVQUOXeL6G7egA3eABqmDJCVaVPg8x7w1zlbCtui+h5 V/zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=VPjpOhS4Wh3sbtS2KC1ZatyPGUrvt7EQFulbiEOcOW4=; fh=YhmxPC0NYagsxVMppzYMgO0XMWsRSWzMegLjmIklu8U=; b=DBTh++vW4bVd7cuUOH9heTZg5LSbSgpOAIOjPX9UgtQk47VKidLnudQmsYVZxNzh0g ejPeLpfdZgvdY0c42x/H3NbEQfM5AEmBPZSq9uDRBvbmRZSBpBFfK8e9+YcuZv2yemPu JX6AAd80zZ8zs8VwFrKVJx1PzoHv+7gSIZdKl5+90NLPFNFqegVsmjavJs5Vqg+CYDAR gzVDJGhsnWs0jDSMGOOgB630R8MX587ds7Ql7AfEpe93tQkMh1E8f4U/tNsuhb4BwYYB +zYWty3AtscXonVfR3bDCtKsYNAS3EWUMekR4LQBC8Dz6H0u6vX6DX9jI9hdlcprRgI2 JZog==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1774069891; x=1774674691; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=VPjpOhS4Wh3sbtS2KC1ZatyPGUrvt7EQFulbiEOcOW4=; b=gORtFvDGX+ERek7ARhmkNo9OFLQHiIxll1z9zoqHC9uscvhDImg1WYwT6EERm8KpHw sPJoU3misep+jL0DtG0r3tMb7V8B1cmxsnLcTq3I7Fc8hnt69Go7Z9oAgUK2VZrdAusw H1Sp9Xm01GCjwzc2NRwXGZSzouZQggiD9W8Us+nfDjyVFoLciHyGIDzqbSNMf3aq07WK FXLMj3S3KgEgqiPdln9QyhMqSl8YIvFIJN7DuWcpkiRYfJqfLWG3fbIOfZ/jOYNbOH7m PR4E0iMcwPEX9wk5IPyZTrcgPI8NGltgDx8DbKf2LtBFfyl2ng4E3POqJQgoI4RrK+i7 QyFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774069891; x=1774674691; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=VPjpOhS4Wh3sbtS2KC1ZatyPGUrvt7EQFulbiEOcOW4=; b=UUHnZ10PjU5Nq8VXnr77Tz+uwrfHJKgaJpcQPhGItw9+/UPNumDTmvUoNrdNjKjBhz pYPWvANa313wH31hMmhCI58OcckWkYYUvLyoh6eVtDjsl++J5mlArMJ2xIdQU+XEcoPl gfFxk5YU6oxAhs/P93dYdFjYEOiknreCtST/dzhSPYpMbAc8rgsZidD3ZSCxZKYxF6+N Ee5nBVfi51hN8uGO2BiGn/FEgI2stHVRY8ImdTIJmHRaWLZ0uOEu5jNVEX931myD761e FhQ7GjgAcpnHWGW4Dw+p7BrOJltNNQW8Vp9woNnCR0qhhU1JwX4cV89SJoMylPSBH1eP +l7Q== X-Gm-Message-State: AOJu0YygKWp2Qrciy29YRj0jAmtVPwU5j+OOomgtEJQRD7G09pER/n4Q toooim9xZfeXc+6YsaIBiF12r6sPNUxT9RH5nROFkDkOYFsfhTC4EO3/NQeAotN4Pjb/ETTHVEk pTpJ4GKD2VLStcJIEb+qoswBxcqUzj7nsAQ== X-Gm-Gg: ATEYQzyC70FKAUHw9PrXNAKY4PEoxJxhPQE6flGwbivcjomqIhetxjPRrgHLRbj0ZBI aWppFmH4XPIQ+vNxibZlwNbQR3WVu9Hupkuox63N74UlDPYqqXeU7Y+S1KnylzmU71HqQE+YQIk z1Kdie2c9+LeEP7OI2UtpcAvsh/b8hwWXB7heQZLdkzHfrzm/N+eNez1wgAU9CBtyJwhfUNyS5j ZW3CVleqW2u5AZhhAGpr6l6ORAD5/PtDOOkyJa4z+UwwGnooKWAFZ0z1a2LH6dWAlrfM1EgI88X 92QJWaEAmJVBLTgzJQR8UeFnXfzBIGqlYWfTvmLRFes848/gh9+Kz7dbybkHoI9mbzreG9Q4ktZ q87oHFE/CzvsvzDXOr6K2q1AZxGpi5q/1dRv81OSopGhGKsVJBd/5vdXNkg== X-Received: by 2002:a05:600c:8908:b0:486:fc95:1a91 with SMTP id 5b1f17b1804b1-486fedb9003mr55529615e9.12.1774069890790; Fri, 20 Mar 2026 22:11:30 -0700 (PDT) MIME-Version: 1.0 References: <204196.1769893114@sss.pgh.pa.us> <2034619.1774045988@sss.pgh.pa.us> In-Reply-To: <2034619.1774045988@sss.pgh.pa.us> From: Pavel Stehule Date: Sat, 21 Mar 2026 06:10:52 +0100 X-Gm-Features: AaiRm52PNjo94Eel4YPjTtOjUKF9cBLH0whO-MHArWtTP6ONx9gEdQQeoRG-FGA Message-ID: Subject: Re: slow SELECT expr INTO var in plpgsql To: Tom Lane Cc: PostgreSQL Hackers Content-Type: multipart/alternative; boundary="000000000000a5bae6064d81d512" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000a5bae6064d81d512 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable p=C3=A1 20. 3. 2026 v 23:33 odes=C3=ADlatel Tom Lane na= psal: > Pavel Stehule writes: > >> so 31. 1. 2026 v 21:58 odes=C3=ADlatel Tom Lane na= psal: > >>> I experimented a little bit with converting simple-expression > >>> SELECT INTO into an assignment, as attached. It does reclaim > >>> nearly all of the performance difference: for me, these two > >>> test cases now take about 276 vs 337 ms. However, I'm concerned > >>> about the side-effects of substituting this other code path; > >>> there's a lot of potential minor differences in behavior. > >>> Two that you can see in the regression test changes are: > >>> > >>> * SELECT INTO is tracked by pg_stat_statements, assignments aren't. > >>> > >>> * The context report for an error can be different, because > >>> _SPI_error_callback() doesn't get used. > >>> > >>> We could probably eliminate the context-report difference by setting > >>> up a custom error context callback in this new code path, but the > >>> difference in pg_stat_statements output would be hard to mask. > >>> There may be other discrepancies as well, such as variations in > >>> error message wording. > > > Do you plan to push this patch? Unfortunately there is not any discussi= on > > about side effects. > > Yeah, general radio silence out there. After thinking about it for > awhile, I've decided to go ahead with the patch. It'll be easy enough > to revert if people are unhappy. > > > I wrote a version with dedicated error context callback, > > Thanks for doing that. I found though that it wasn't quite enough, > because the existing code path applies _SPI_error_callback() during > evaluation of the expression but not during assignment to the target > variable. So for example, errors during type conversion to match > the target variable don't get a context line claiming they happened > during evaluation of the expression, which seems correct to me. > I was able to fix it by not using exec_assign_expr() but instead > copying that code in-line, so that we can pop the error context stack > at the right point. (See added tests in the committed patch, > ce8d5fe0e2802158b65699aeae1551d489948167.) > > > ... so there will be > > differences only in pg_stat_statements. It is true, so this should be > hard > > to mask. Maybe this difference can be just documented - like "`SELECT > expr > > INTO variable` can be optimized and executed by a direct expression > > executor, and then this query will not be visible in pg_stat_statement.= " > > We don't document that "var :=3D expression" isn't captured, so I don't > think this needs to be either. Possibly Bruce will pick up the change > as a release-note item. > Thank you very much Regards Pavel > > regards, tom lane > --000000000000a5bae6064d81d512 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


p=C3=A1 20. 3. = 2026 v=C2=A023:33 odes=C3=ADlatel Tom Lane <tgl@sss.pgh.pa.us> napsal:
Pavel Stehule <pavel.stehule@gmail.com> writes:
>> so 31. 1. 2026 v 21:58 odes=C3=ADlatel Tom Lane <tgl@sss.pgh.pa.us> napsal:<= br> >>> I experimented a little bit with converting simple-expression<= br> >>> SELECT INTO into an assignment, as attached.=C2=A0 It does rec= laim
>>> nearly all of the performance difference: for me, these two >>> test cases now take about 276 vs 337 ms.=C2=A0 However, I'= m concerned
>>> about the side-effects of substituting this other code path; >>> there's a lot of potential minor differences in behavior.<= br> >>> Two that you can see in the regression test changes are:
>>>
>>> * SELECT INTO is tracked by pg_stat_statements, assignments ar= en't.
>>>
>>> * The context report for an error can be different, because >>> _SPI_error_callback() doesn't get used.
>>>
>>> We could probably eliminate the context-report difference by s= etting
>>> up a custom error context callback in this new code path, but = the
>>> difference in pg_stat_statements output would be hard to mask.=
>>> There may be other discrepancies as well, such as variations i= n
>>> error message wording.

> Do you plan to push this patch? Unfortunately there is not any discuss= ion
> about side effects.

Yeah, general radio silence out there.=C2=A0 After thinking about it for awhile, I've decided to go ahead with the patch.=C2=A0 It'll be eas= y enough
to revert if people are unhappy.

> I wrote a version with dedicated error context callback,

Thanks for doing that.=C2=A0 I found though that it wasn't quite enough= ,
because the existing code path applies _SPI_error_callback() during
evaluation of the expression but not during assignment to the target
variable.=C2=A0 So for example, errors during type conversion to match
the target variable don't get a context line claiming they happened
during evaluation of the expression, which seems correct to me.
I was able to fix it by not using exec_assign_expr() but instead
copying that code in-line, so that we can pop the error context stack
at the right point.=C2=A0 (See added tests in the committed patch,
ce8d5fe0e2802158b65699aeae1551d489948167.)

> ... so there will be
> differences only in pg_stat_statements. It is true, so this should be = hard
> to mask. Maybe this difference can be just documented - like "`SE= LECT expr
> INTO variable` can be optimized and executed by a direct expression > executor, and then this query will not be visible in pg_stat_statement= ."

We don't document that "var :=3D expression" isn't captur= ed, so I don't
think this needs to be either.=C2=A0 Possibly Bruce will pick up the change=
as a release-note item.

Thank you very = much

Regards

Pavel
<= div>=C2=A0

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane
--000000000000a5bae6064d81d512--