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 1vm4ql-0072Zd-2e for pgsql-hackers@arkaria.postgresql.org; Sat, 31 Jan 2026 06:52:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vm4qi-007XsO-0T for pgsql-hackers@arkaria.postgresql.org; Sat, 31 Jan 2026 06:52:52 +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.96) (envelope-from ) id 1vm4qh-007XsG-2S for pgsql-hackers@lists.postgresql.org; Sat, 31 Jan 2026 06:52:52 +0000 Received: from mail-yw1-x1131.google.com ([2607:f8b0:4864:20::1131]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vm4qf-00000000Jbm-41tf for pgsql-hackers@lists.postgresql.org; Sat, 31 Jan 2026 06:52:52 +0000 Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-790884840baso26534947b3.0 for ; Fri, 30 Jan 2026 22:52:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1769842367; cv=none; d=google.com; s=arc-20240605; b=AOZUlFJqTo2UkZoLplfdKTE1WYJdEyChjzToC1XN/rZZ52JlNMPHYUyE8JjwWYnVB0 dfLQQXq3tHD+GBDbCmeXF3U/ZO/a5zvvQ8HLjWotOQPC0XY2u6U+gCjCiiVoe8aOnQf+ 8inO7Jzqg+lLEV/v+JGT9NsWcJy+S1GUOruextseBYJYVogeDS25SbVfIJt4zhxmmBP3 +Xv8vUT3CbQfGLmvD6ROo9Z8Yv/J1Lv4rC5d6hKtl4QbIhI9/MzCS+JS5N2kwIGom0D2 EUXccTRkj+ArYdG7HkMhJHZpvgzesVhvGxMAiL9mYDP1nRJ5XJKONgvA34fC80eLTJAZ mFhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=to:subject:message-id:date:from:mime-version:dkim-signature; bh=uYoPJi6YfW9OTJ+o9dg1AbVEbxYtHSOHSpsPoF+gtBA=; fh=dxJXJbLzq9Nah1LUdsj4QTuQ3JoDScd0wp1YHY64NXM=; b=VamzFKWD4waqNlTueu030XAKMb0D8kVNvPpYu0qF5MeBgA9TZ2278Ze5YWl+LC0MUj 2eacSCoXkGB74d3VqnIuJSELa7eaf6mj8C3yPYlhUCLNVvFy1M9l+4Nfsu9hTbXdVP0q TT70xPt0DZE9qHRDd+qTmDFxn4VveTzgQXbLv/Ame/INDf3kk9mkQeiqdMa8CYPHjwCL kubQMKMv/mqogsoL9R15ip3e6d9QgsBHBNaHUn54GvzN4J9ezCR6Ni08CAItkeoF4SGL LL2xf0HZks6GfWhHrpD6bJC6FumLdyuyME8n1pBMsL+P0SWT/8i6pjMhNG30x2j0fvTz Fq4Q==; 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=1769842367; x=1770447167; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=uYoPJi6YfW9OTJ+o9dg1AbVEbxYtHSOHSpsPoF+gtBA=; b=HOtiysP5FCT/3iXknjER9hWwmKAkN8ZLZ7jmFybZBX9gnb1vNKO7R7fBioW3z9FUMB tv8Tdyr0q8p3FwzPFgKA6VveaS8loUq0Hwx9BLbayPhvRdMcmSZs/a8omewi6GgxSKU6 6MTUgmRF+o6JhzGJw7yCI6hYa2bdE4DqYe6bzx4DtenTxi68VcAH81YE+I3dtxMbsxVn eRSp0AFT7azyhkgOoe4jB9rJe/Xfo67rxsJCqz3mhPs5EOAMyPz0zHoOBWC0hTbIwEYW nzivMRsS6SGF6sNC0A/EQpEibesrd2Zvpc9vNzeT2A7sycbCaSPTzXshHbZ1qjyO20cp FSHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769842367; x=1770447167; h=to:subject:message-id:date:from:mime-version:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uYoPJi6YfW9OTJ+o9dg1AbVEbxYtHSOHSpsPoF+gtBA=; b=Cz9G86+18jE/Rrf1JHWqe1uAfVoTYceEkLPMgRrVJLWWBqJCCNs4V91lYXx2fN1aSh /s/pk35Rg7za5dNu4XQWoCR2IkbuldRnLH43Kv6nu4AdnlHzu870Ajqsfz0m5a1ujsg6 CeMi/qQrqMYsQFd4XoEvN9FKWJc6fA5ovvJUPJY8/FFQoFhogrT5MrIwkJwNK6FE45YX t5j7KGHoBQUMSitA+qE/Fvqhnat/KenFVSpW3kayuZWf6uHE8K4o/P6+rhvnxVTHm64h gN/YbspM24RFRq1cSoetI5iYBXM+qtYX4owY28C36UL8p41lciIXMxRALu46qNsLMfzg 53Uw== X-Gm-Message-State: AOJu0YyRnBnpP/5E3iNf3lIDMpy8D8TysKyTECMOL4zIc2WSZeE0LHCj qpT8zze/JHfzX8l05tpf0R5o3v/X9Z6L85gweAFjkEA564fJdwOfrp7W2Fza1fDDL3YbsN7ZtlL ZVBO0uqYjhjCa68lZU/oX89lxLt+8fWxA1HkE X-Gm-Gg: AZuq6aJQv+iN+QJeUvIclEUXGeVFgWftiQqozDkNuD0gsPZcN4r7ehlXlBDcMMbIrYZ mUQPsGBwLv30VgomqgvCcZGYcjX7qSiE0H53BNavB+IKd+okxkVHcBiGbfvya7LRE0jqwfyXLaW ZTyYCwCOurWGqGoIZFn092JgJm5V/ALyS86/vG8+Du9U/xO15m/Fl084KENoiHiUziqm0UGv+AD gEhlERuXlqIG+PpLSXCUJjC6YvV6hVWAvYByeICexEPV/SGHPVBUN1IXOqEVkhSLTf+CRmzHRWy 7yShy6PD438+0bS15i3Ysfq0oHZ9WoXjT82QZBKkunEcm+9OKLeavLGo48t6ngNL2AWbguapyhN osc4Tb3p2U2b1QYIqV+UTe9F9ZrY2NEB/CzUkb1gAzaw3eQ== X-Received: by 2002:a05:690c:6612:b0:794:3023:8596 with SMTP id 00721157ae682-7949dfbf60amr50867847b3.40.1769842367109; Fri, 30 Jan 2026 22:52:47 -0800 (PST) MIME-Version: 1.0 From: Pavel Stehule Date: Sat, 31 Jan 2026 07:52:10 +0100 X-Gm-Features: AZwV_QhuGOuE6kC3UWz2V8iD8bWTTpBDJoY0XbkR78QauIFNV3wHX2ZeVtxs32Y Message-ID: Subject: slow SELECT expr INTO var in plpgsql To: PostgreSQL Hackers Content-Type: multipart/alternative; boundary="000000000000999ffb0649a989c7" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000999ffb0649a989c7 Content-Type: text/plain; charset="UTF-8" Hi I found a article https://medium.com/google-cloud/postgresql-performance-the-context-switch-trap-that-slows-down-t-sql-migrations-7b8c6f518fd8 It compare T-SQL and PLpgSQL performance on some simple benchmark do $$ declare x int; begin for i in 1..10000000 loop x := 0; end loop; end $$; do $$ declare x int; begin for i in 1..10000000 loop select 0 into x; end loop; end $$; SELECT expr INTO var is syntax used on old sybase and mssql systems. The positive result in this article is fact, so Postgres in all tests are very well comparable. More - the assignment is really fast and significantly faster than on MSSQL. 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) with interesting results release, assign time, select into time 9.4, 2900 ms, 20800 ms 11, 2041 ms, 16243 ms master, 534ms, 15438 ms Originally, I used gcc with O0, and master is really slow without O2 optimization 9.4, 2600 ms, 20800 ms --<< 9.4 is faster with O0 11, 2177 ms, 19128 ms master, 1395 ms, 70060 ms -- << master is very slow with O0 Using SELECT expr INTO var is plpgsql's antipattern. plpgsql_check can detect it now. But it will still be nice if there will not be too big a difference like now. I didn't check the code yet, and I have no idea if there are some possibilities on how to execute this case better. Regards Pavel tested on Fedora 43 --000000000000999ffb0649a989c7 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi

I found a article=C2=A0https://medium.= com/google-cloud/postgresql-performance-the-context-switch-trap-that-slows-= down-t-sql-migrations-7b8c6f518fd8

It compare = T-SQL and PLpgSQL performance on some simple benchmark

=
do $$ declare x int; begin for i in 1..10000000 loop x :=3D 0; end loo= p; end $$;
do $$ declare x int; begin for i in 1..10000000 loop s= elect 0 into x; end loop; end $$;

SELECT expr INTO= var is syntax used on old sybase and mssql systems. The positive result in= this article is fact, so Postgres in all tests are very well comparable. M= ore - the assignment is really fast and significantly faster than on MSSQL.=

I remember the old discussion about this issue, a= nd I thought that the performance of SELECT INTO and assignment=C2=A0should= =C2=A0be almost the same. I repeated these tests on pg 9.4, 11 and master (= asserts are disabled) with interesting=C2=A0results

release, assign time, select into time
9.4, 2900 ms, 20800 ms
11, 2041 ms, 16243 ms
master, 534ms, 15438 ms
=
Originally, I used gcc with O0, and master is really slow wi= thout O2 optimization

9.4, 2600 ms, 20800 ms --<= ;< 9.4 is faster with O0
11, 2177 ms, 19128 ms
maste= r, 1395 ms, 70060 ms -- << master is very slow with O0

=
Using SELECT expr INTO var is plpgsql's antipattern. plpgsql= _check can detect it now. But it will still be nice if there will not be to= o big a difference like now. I didn't check the code yet, and I have no= idea if there are some possibilities on how to execute this case better.

Regards

Pavel
tested on Fedora 43


--000000000000999ffb0649a989c7--