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 1vmPio-00BvR4-0o for pgsql-hackers@arkaria.postgresql.org; Sun, 01 Feb 2026 05:10:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vmPim-009VLe-0N for pgsql-hackers@arkaria.postgresql.org; Sun, 01 Feb 2026 05:10:04 +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 1vmPil-009VLV-22 for pgsql-hackers@lists.postgresql.org; Sun, 01 Feb 2026 05:10:04 +0000 Received: from mail-yw1-x112d.google.com ([2607:f8b0:4864:20::112d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vmPik-000R9O-0Z for pgsql-hackers@lists.postgresql.org; Sun, 01 Feb 2026 05:10:03 +0000 Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-79456d5dda4so30298847b3.2 for ; Sat, 31 Jan 2026 21:10:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1769922602; cv=none; d=google.com; s=arc-20240605; b=ISVsYEJVDqfQbm6dmwRwBqFD1bdIm6DmflgQIl/dDdem19nJ3npc21H3ut80FplI2o x/0YtCA/jMGp0qq6uyOg4qWGJtXSd5AvItsc6Ms6Asczo7YWQtQsNFz4d5wc36V0R3w+ uLgDPvuys5qOTKTP8cHIUkQI/etsvT2dGrOHe8xabWAbMRLg9Fnad/b1WpZ6FB45LrEt DRkjxeXe6ejtpHUO3YJwRemPTzQJtZPTJ6Lf7GGqPzUdoQuxzV9SMNPpu2J5OPe8pJls gRk/5lG7OtLEke9l5bjVrqWCZu6/6bTbqiZ3qpUHXDPgdzCJDxLfN4N4+f0C3Q7yKK2o ZOlg== 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=iaEwsYyXMeHtOgIFMkQf5HmjK02kIKT+3W6lCmBAjM8=; fh=YhmxPC0NYagsxVMppzYMgO0XMWsRSWzMegLjmIklu8U=; b=g7Yw+U3DJdIyjvYXuI3BSszrUmiXS9yIffZ77sMCblqQsYPiOCuLcmPKvUDWgCoM0A UuL1Bm8wIaEGrwAhvSxQLfp5Qa7hE6Iv1qA8O4G/WaS2qFUoZQrEcrE/DuqSG/uHyh1j 70H2v7DoqrrvuxycS+yWva1TuYqvk5wHg9gOyWgiPg6v8LhY0vYHjxS32stbBtG2a4Ri jhwqrg+116e81cdb3JP9F4oA7MErOkCD0aGnQJcjXH4XrulH4/1Np380FahzssXS1yQU GoE8VaTdPAHAS4Qla3FxRaennJbUXLNigb/0aB21YHkHRi8vp7t/CKmBNh+WYDYttqN0 wJ/g==; 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=1769922602; x=1770527402; 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=iaEwsYyXMeHtOgIFMkQf5HmjK02kIKT+3W6lCmBAjM8=; b=kycaK+c5ZkLoEpnvLnhCk8ptOP39KXIW/e+7ITU7f2hqlAieOGX7K99PSRA+b0/ulp Pm9474RCwbRw2HzUStUjyO5ezF1NNGBHMZzco5CBiuZ20VlKnaEqfRRgnqAlxJa7xiDE pEUP+mDShybP4v6Ti6jFKfXqv/ZyQypi75IlMAz955H7+yn6LKxKImrmGds0xt0dbGmC ZQEcRckceltM0XfNIs8XITJUidZnlIqjZT+i7CD84FCHXzkqDxcDzlJLB7QASyOt0gMo wX6ZN7HjTnaqXjNU5rcfGzMVy3aALq0dxTtSaA1cRnG1OmOxuIGV7fGouGyTH2BKDH9C DyqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769922602; x=1770527402; 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=iaEwsYyXMeHtOgIFMkQf5HmjK02kIKT+3W6lCmBAjM8=; b=FBVPzb+9vAlLjV128nzWT+8O2YcaeHbwxydfrXMANW5OlaqNkjC+8qZwGs7y+pte4k UHIqQzflIdJTUSSiGaZzRD+2X05GJIAOorh9OlkiGkiVJdIgsckKfugGZXU+3mvGzf/U FdWN50WMGgQHycKVUHSQNVKKR+xs6eT7vu6nAcnsDt49ihu8U8DFRbLu8zOIYHJt1Mpa fPRU9MXG4YIOEcPXDFEgZC0zpZ0Q8iBOG4m09PCxPiX1NO+fP2xs6QYRoFA4N89SDhVc M3ewhUA3zM6K3NW2t/g7nlmQnyupqZmpSqi7cdbmC8dZzxtrkoQ6Gb6970Gh8jv4BfPU kE/Q== X-Gm-Message-State: AOJu0YxeN80+QvFBHaisxG6YEETxcn4quaQ6wTp8OnvVkYcLXXbWfliZ OxFihTAW23lGTJ8/VbD4OxR60paDeMCW6afHSmHIu5GcantMv+6FgY/rTnPmpYiTemGLyjSbBdb 8iS3nc4A+q4ET6M0M2BcWESZbl9c/Q/Q= X-Gm-Gg: AZuq6aLW/Hcp69fa9PJlzhmsxdU8OF6hROuaKtsbhBqjAOa8Vc8BdMS163aaGjS2hiD nwHwNEP8lUXnOvEbkcaHoU/C5Ez0TujAzWzQ27RB8aFKR7ZdgWQyqIEB6jBa5/r9YL3gIapg00L iupE6E73j6xmM/f4ZlIffVWDoy2+WLbiIfiK7R1+97eH1OoZclAjJtU6OcX2BrEgGNaRxpWO2up xyMWG196e8iUmjvQtmwkqDtFxdQmKTT4j6+K4dKIZsvOQ85g2ci3UX6+70dt0zVW1cxFqivTnzS ooehaI9y62ml1vKY57eaARJr6gJtGwMMY6WA02ybVDnYFfEcTiZDpKQQMFjcCUAfteHRhj0UyQG Ffr6ScEfTFvuBmIf6YhoaIAVdFqjx5GPlcasNlVBsQBGCxQ== X-Received: by 2002:a05:690c:e3cd:b0:793:afc7:6b64 with SMTP id 00721157ae682-7949ded8b58mr153094397b3.3.1769922601954; Sat, 31 Jan 2026 21:10:01 -0800 (PST) MIME-Version: 1.0 References: <204196.1769893114@sss.pgh.pa.us> In-Reply-To: <204196.1769893114@sss.pgh.pa.us> From: Pavel Stehule Date: Sun, 1 Feb 2026 06:09:25 +0100 X-Gm-Features: AZwV_QiCGYj0aOiqpSaTuGIhnFBEkEFb5WOTYxJoDzWc-Ida1hUWoSsP8Fna6i4 Message-ID: Subject: Re: slow SELECT expr INTO var in plpgsql To: Tom Lane Cc: PostgreSQL Hackers Content-Type: multipart/alternative; boundary="000000000000f830b60649bc370a" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000f830b60649bc370a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable so 31. 1. 2026 v 21:58 odes=C3=ADlatel Tom Lane napsal: > Pavel Stehule writes: > > I remember the old discussion about this issue, and I thought that the > > performance of SELECT INTO and assignment should be almost the same. I > > repeated these tests on pg 9.4, 11 and master (asserts are disabled) wi= th > > interesting results > > > release, assign time, select into time > > 9.4, 2900 ms, 20800 ms > > 11, 2041 ms, 16243 ms > > master, 534ms, 15438 ms > > Yeah, we've sweated a good deal about optimizing plpgsql assignment, > but SELECT INTO is always done the hard way. > > 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. > > Probably no one would notice such details if it had been like that > all along, but would they complain about a change? I dunno. > This patch looks well. I can confirm massive speedup. I don't remember any report related to change of implementation of assign statement before, and I think it can be similar with this patch. In this specific case, I think so users suppose SELECT INTO is translated to assignment by default. And there are a lot of documents on the net that describe the transformation of the assignment statement to SELECT - so I think there is some grey zone where optimization can do some magic. More - the statistics for function execution can be covered by track_functions. Regards Pavel > > regards, tom lane > > --000000000000f830b60649bc370a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


so 31. 1. 2026 v=C2= =A021:58 odes=C3=ADlatel Tom Lane <tgl@sss.pgh.pa.us> napsal:
Pavel Stehule <pavel.stehule@gmail.com> wri= tes:
> I remember the old discussion about this issue, and I thought that the=
> performance of SELECT INTO and assignment should be almost the same. I=
> repeated these tests on pg 9.4, 11 and master (asserts are disabled) w= ith
> interesting results

> release, assign time, select into time
> 9.4, 2900 ms, 20800 ms
> 11, 2041 ms, 16243 ms
> master, 534ms, 15438 ms

Yeah, we've sweated a good deal about optimizing plpgsql assignment, but SELECT INTO is always done the hard way.

I experimented a little bit with converting simple-expression
SELECT INTO into an assignment, as attached.=C2=A0 It does reclaim
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.
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.

Probably no one would notice such details if it had been like that
all along, but would they complain about a change?=C2=A0 I dunno.

This patch looks well. I can confirm massive spe= edup.=C2=A0

I don't remember any report relate= d to change of implementation of assign statement before, and I think it ca= n be similar with this patch.=C2=A0

In this specif= ic case, I think so users suppose SELECT INTO is translated to assignment= =C2=A0by default. And there are a lot of documents on the net that describe= the transformation of the assignment statement to SELECT - so I think ther= e is some grey zone where optimization can do some magic. More - the statis= tics for function execution can be covered=C2=A0by track_functions.

Regards

Pavel

=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

--000000000000f830b60649bc370a--