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 1uBuvK-008CWS-OY for pgsql-general@arkaria.postgresql.org; Mon, 05 May 2025 12:27:55 +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 1uBuvI-0037N7-53 for pgsql-general@arkaria.postgresql.org; Mon, 05 May 2025 12:27: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.94.2) (envelope-from ) id 1uBuvH-0037Mz-Mm for pgsql-general@lists.postgresql.org; Mon, 05 May 2025 12:27:51 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uBuvF-000H0A-05 for pgsql-general@lists.postgresql.org; Mon, 05 May 2025 12:27:51 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-22e033a3a07so42941055ad.0 for ; Mon, 05 May 2025 05:27:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kset.org; s=google; t=1746448067; x=1747052867; 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=p82XHmpwwKYnPPAFSvZc7DGuGvihG2KIeWiKYJkjTOM=; b=kYnlEDdmvez+tqbero1M2I1YmzBAOY5uI1fJfwfILt4NMtwXdqCaiSg5yMQa8q7p1t ru6Xpt7aZhWUxqe8TgDFP01rUiAPkDp+L6hdI019uZVp1i6UsaYXDuNBf0aFAtS3HYAi HNbnY0bu7S/dOWS6iy4GqVM4gkEcuFXk/dLIokFaZgswOFBK+kJ9zf5L5UXApF9m2gjA VYrUFNQFt5A0jcbqVIWdIDJw2THUTJs97Lkuk6tXiJPFJ6bz5bbM0dDadT2kiRlXkaBD Xo+8TXH0ijF624T2IxWf9p618/SnVoH4v+bM+RxqcWZ/DzestRdPfd6LrY2yxHUnB7ar Eqpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746448067; x=1747052867; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=p82XHmpwwKYnPPAFSvZc7DGuGvihG2KIeWiKYJkjTOM=; b=E+e6SodwqQd2kYHuAsFlT+Rc+V74Im/GxQXINZZd/zHwbwrvMkpsIU+A6A1+7c64u7 AQ9m9eN3CUsKNhYFxklejTt+mB3kyhLjroQg0ci4aZrnnh0diSWMLOOLXjdD5l+nkRVk Pj2Q8JZ1dE+AN4DjJC0YsCkF9tAJNSfkimuyDN8SxEQ0mmzoqlzzrZt1MA67FIUikrpC PXoyFAHO5vdIYXoa5t1MbqiUScghMRt0ocOWXfYl+YFfCk8VhKlB97UZ9XiLTgzwqJon w2OEXV1wAAbI9HQzD3PvFOOhPEyQ50v5Mtk6HymV+zsCMTE0ixUXE6iHqj32D1zCe1J0 iYKw== X-Gm-Message-State: AOJu0YwKZWTJqQp36WqqzX0/2xrRfEjVzHJMcFA6mLvkFg3X0xHABrGi 5/bFZruMucgrwU4634cyVJhS/Vi39S6Vd062zuoUZ/84Atb8FbksiDpQbxqUlbZfkncFb7JVVVQ pRhPeWXPwRRO/krS/fJNGpqsnKrRR03zrTsrpN1HWu2Glu/aowC4= X-Gm-Gg: ASbGncuYz6K3d0ITVsM6xBqycgVb1yC3k4kMzWHn87VLUQ9M2rfix3mK0r9kd58ZsA4 609yahl7FzhY8TPjOfKDe7p4TUmCyqpfmKRJKyrnsgSsK/Mdb2zGmleRss9U8GATjdH0yZbxmC4 whmbUgj77gwAdqYW1EJU4ZAg== X-Google-Smtp-Source: AGHT+IFkJ0owQawTF3FQymCfHFRsMx3eWr1kcSdDS/sSOkrW3IArgDedY3YdYEstY+FjDSnoqKFfLRXV3HMJZQDMoPs= X-Received: by 2002:a17:902:f60e:b0:223:62f5:fd44 with SMTP id d9443c01a7336-22e18c398bcmr139965375ad.40.1746448067393; Mon, 05 May 2025 05:27:47 -0700 (PDT) MIME-Version: 1.0 References: <4ec38b6d-bd92-4055-8d2f-7efa583a2b9f@cloud.gatewaynet.com> In-Reply-To: From: =?UTF-8?Q?Mladen_Marinovi=C4=87?= Date: Mon, 5 May 2025 14:27:36 +0200 X-Gm-Features: ATxdqUHKtSVlw4Rvan33l20tOlALgbhBHNdZBXipUCIZuXZg2tibH5TxQTrrNE8 Message-ID: Subject: Re: Different execution plans in PG17 and pgBouncer... To: Achilleas Mantzios Cc: "pgsql-general@lists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000ad0e24063462a0a2" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ad0e24063462a0a2 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, May 5, 2025 at 12:07=E2=80=AFPM Achilleas Mantzios < a.mantzios@cloud.gatewaynet.com> wrote: > > On 5/5/25 11:00, Mladen Marinovi=C4=87 wrote: > > > > On Mon, May 5, 2025 at 11:24=E2=80=AFAM Achilleas Mantzios < > a.mantzios@cloud.gatewaynet.com> wrote: > >> >> On 5/5/25 09:52, Mladen Marinovi=C4=87 wrote: >> >> Hi, >> >> We recently migrated our production instances from PG11 to PG17. While >> doing so we upgraded our pgBouncer instances from 1.12 to 1.24. As >> everything worked on the test servers we pushed this to production a few >> weeks ago. We did not notice any problems until a few days ago (but the >> problems were here from the start). The main manifestation of the proble= ms >> is a service that runs a fixed query to get a backlog of unprocessed dat= a >> (limited to a 1000 rows). When testing the query using pgAdmin connected >> directly to the database we get a result in cca. 20 seconds. The same qu= ery >> runs for 2 hours when using pgBouncer to connect to the same database. >> >> >> That's a huge jump, I hope you guys did extensive testing of your app. I= n >> which language is your app written? If java, then define prepareThreshol= d=3D0 >> in your jdbc and set max_prepared_statements =3D 0 in pgbouncer. >> > Mainly python, but the problem was noticed in a java service. > Prepare treshold was already set to 0. We changed the max_prepared_state= ments > to 0 from the default (200) but no change was noticed. > >> How about search paths ? any difference on those between the two runs ? >> Do you set search_path in pgbouncer ? what is "cca." btw ? >> >> >> The more interesting part is that when we issue an explain of the same >> query we get different plans. We did this a few seconds apart so there >> should be no difference in collected statistics. We ruled out prepared >> statements, as we suspected the generic plan might be the problem, but i= t >> is not. Is there any pgBouncer or PG17 parameter that might be the cause= of >> this? >> >> >> Does this spawn any connections (such as dblink) ? are there limits per >> user/db pool_size in pgbouncer ? >> > No additional connection nor dbling. Just plain SQL (CTE, SELECT, INSERT, > UPDATE, DELETE,...) There are limits, but they are not hit. The query jus= t > uses a different plan and runs slower because of that. > >> Pgbouncer, in contrast to its old friend PgPool-II is completely passive= , >> just passes through SQL to the server as fast as possible as it can. But= I >> am sure you know that. Good luck, keep us posted! >> > Yes, that is what puzzles me. > > What is the pgbouncer's timeout in the server connections ? > > How about "idle in transaction" ? do you get any of those? What's the > isolation level ? > > How about the user ? is this the same user doing pgadmin queries VS via > the app ? > > Can you identify the user under which the problem is manifested and : > > ALTER user "unlucky_user" SET log_statement =3D 'all'; > > ALTER user "unlucky_user" SET log_min_duration_statement =3D 0; -- to hel= p > you debug the prepared statements .. just in case , and other stuff not > printed by log_statement =3D all. > > None of those parameters should affect the fact that when issuing the explain select query (the statement is not prepared) from psql directly gives a different result than issuing it over the pgbouncer connection. The result is repeatable. We have rolled back pgbouncer to 1.12. and it seems the problem persists. This is one of the weirdest things I have ever seen with PostgreSQL. > > Regards, > Mladen Marinovi=C4=87 > > --000000000000ad0e24063462a0a2 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Mon, May 5, = 2025 at 12:07=E2=80=AFPM Achilleas Mantzios <a.mantzios@cloud.gatewaynet.com> wrote:
<= /div>
=20 =20 =20


On 5/5/25 11:00, Mladen Marinovi=C4=87 wrote:
=20


On Mon, May 5, 2025 at 11:24=E2=80=AFAM Achilleas Mantzios <a.mantzios@cloud.gatewaynet.c= om> wrote:


On 5/5/25 09:52, Mladen Marinovi=C4=87 wrote:
Hi,

We recently migrated our production instances from PG11 to PG17. While doing so we upgraded our pgBouncer instances from 1.12 to 1.24. As everything worked on the test servers we pushed this to production a few weeks ago. We did not notice any problems until a few days ago (but the problems were here from the start). The main manifestation of the problems is a service that runs a fixed query to get a backlog of unprocessed data (limited to a 1000 rows). When testing the query using pgAdmin connected directly to the database we get a result in cca. 20 seconds. The same query runs for 2 hours when using pgBouncer to connect to the same database.


That's a huge jump, I hope you guys did extensive testing of your app. In which language is your app written? If java, then define prepareThreshold=3D0 in your jdbc and set max_prepared_state= ments =3D 0 in pgbouncer.

Mainly python, but the problem was noticed in a java service.
Prepare treshold was already set to 0. We changed the=C2=A0 = max_prepared_statements to 0 from the default (200) but no change was noticed.

How about search paths ? any difference on those between the two runs ? Do you set search_path in pgbouncer ? what is "cca." btw ?


The more interesting part is that when we issue an explain of the same query we get different plans. We did this a few seconds apart so there should be no difference in collected statistics. We ruled out prepared statements, as we suspected the generic plan might be the problem, but it is not. Is there any pgBouncer or PG17 parameter that might be the cause of this?


Does this spawn any connections (such as dblink) ? are there limits per user/db pool_size in pgbouncer ?

No additional connection nor dbling. Just plain SQL (CTE, SELECT, INSERT, UPDATE, DELETE,...) There are limits, but they are not hit. The query just uses a different plan and runs slower because of that.

Pgbouncer, in contrast to its old friend PgPool-II is completely passive, just passes through SQL to the server as fast as possible as it can. But I am sure you know that. Good luck, keep us posted!

Yes, that is what puzzles me.

What is the pgbouncer's timeout in the server connections ?

How about "idle in transaction" ? do you get any of those?= What's the isolation level ?

How about the user ? is this the same user doing pgadmin queries VS via the app ?

Can you identify the user under which the problem is manifested and :

ALTER user "unlucky_user" SET log_statement =3D 'all&#= 39;;

ALTER user "unlucky_user" SET log_min_duration_statement = =3D 0; -- to help you debug the prepared statements .. just in case , and other stuff not printed by log_statement =3D all.

None of = those parameters should affect the fact that when issuing the explain selec= t query (the statement is not prepared) from psql directly gives a differen= t result than issuing it over the pgbouncer connection. The result is repea= table.

We have rolled back pgbouncer to 1.12.= and it seems the problem persists. This is one of the weirdest things I ha= ve ever seen with PostgreSQL.
=C2=A0
Regards,
Mladen Marinovi=C4=87
--000000000000ad0e24063462a0a2--