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 1r4Jmv-008D2x-O9 for pgsql-performance@arkaria.postgresql.org; Sat, 18 Nov 2023 11:47:01 +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 1r4Jms-000d2m-1S for pgsql-performance@arkaria.postgresql.org; Sat, 18 Nov 2023 11:46:58 +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 1r4Jmr-000d2e-M5 for pgsql-performance@lists.postgresql.org; Sat, 18 Nov 2023 11:46:57 +0000 Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r4Jmk-0073OT-Uh for pgsql-performance@lists.postgresql.org; Sat, 18 Nov 2023 11:46:56 +0000 Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-53e2308198eso4140491a12.1 for ; Sat, 18 Nov 2023 03:46:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700308009; x=1700912809; darn=lists.postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=sVFIrWwHjXGX0n6bQFZMG0IJtvTsi81GTZ2OSKXHq6I=; b=UN96MuGVRTlcvMsh1NrqMRfDl2UDpaCak6cSBhSdo+gF7oGoCGahM3zsu9F3q3v8Me HPpHgmz1vviQwjMBx27dLHpARapIl9IWIBxtDpIxiDWGohf73tgCQ/oOJ/ZVvR20uEu2 4BZSXBlHRsaiVao+I/JuAnP4Ddnv9Bn6GAaW9nmCfN9AtfGDnMyYH1BasYmI/bp3jsqa EvEyryRLKOonWyctfB+43BHR5e+yE2Up231piBrPJR1ctNIfcnXaDtbT5s6/3EY94aoV u04U1Elot2f4aP0B+PjxmclcPQ62nHTGCshS+PrYV9ADJ4+QJXxVI42Vkf2kWMWtCxJh QKMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700308009; x=1700912809; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sVFIrWwHjXGX0n6bQFZMG0IJtvTsi81GTZ2OSKXHq6I=; b=rQvbd8LGW1M2uv1t7EHsb+f2TKaJcxX9hZuJuKaiyLIdmmq/t8QXEzyijitU018AfP tyMo+JFm3B/1q0+rBDSwMTrkA+41tV2OUe+5E7tVajLCB8pCuuTHu9XnQPtBsz33vDmk sV7IdRLWlAEpMVFKUziLkLM8WrpkXcOsQKaD2pu4TV0WWj1JtncUMjMvw5D4Qspoa/SF /phcZ6KtsoR6xyLZoaz1Bo5zbSRZ9x3/a5WZKpaNe9nr4zdiW8SdOeAN2bafLw1FTH3C a5DxMJXaoB4CiNQMBKAoBw4nrZx3SU2+mcCN0lvrAKwZUVZHk6OzwVscNuh10Tg2negh KIfA== X-Gm-Message-State: AOJu0YwBtmFdsFKusr0ZisDYnbsY0I/JJCU2Njg3lNog1szKQZX9ED2J BafQxkyboCoNGoS9zdqIeAQ= X-Google-Smtp-Source: AGHT+IFxjh+oOYrV6Zmzrtox+FfwA6Nc5+WjRqTMZ4bqb0/xuiMu4jmjOFwOBG978oZGIzhmVt+kfg== X-Received: by 2002:a17:907:a287:b0:9b2:89ec:d701 with SMTP id rd7-20020a170907a28700b009b289ecd701mr1821662ejc.27.1700308009392; Sat, 18 Nov 2023 03:46:49 -0800 (PST) Received: from smtpclient.apple (2001-1c04-3c07-1a00-f584-2b5a-2208-548f.cable.dynamic.v6.ziggo.nl. [2001:1c04:3c07:1a00:f584:2b5a:2208:548f]) by smtp.gmail.com with ESMTPSA id o26-20020a1709062e9a00b009e6a990a55esm1778554eji.158.2023.11.18.03.46.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 18 Nov 2023 03:46:48 -0800 (PST) From: Frits Hoogland Message-Id: <4226E7B1-8F5E-4CAF-B8BD-A1FE8F79F77F@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_B520A04D-DE00-429D-BA02-867EF2AD55DE" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.200.91.1.1\)) Subject: Re: simple query running long time within a long transaction. Date: Sat, 18 Nov 2023 12:46:45 +0100 In-Reply-To: Cc: Andreas Kretschmer , "pgsql-performance@lists.postgresql.org" To: "James Pang (chaolpan)" References: X-Mailer: Apple Mail (2.3774.200.91.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_B520A04D-DE00-429D-BA02-867EF2AD55DE Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Any statement will run using a custom plan at first. Only a prepared statement creates the memory area in the backend that = can hold the custom plan statistics, that is why generic plans only work = with prepared statements. A prepared statement has to run at least 5 times before the planner = looks at the plan statistics and determine whether a generic plan would = work (=3Dgeneric plan cost being equal or lower than the average custom = plan cost) That means that the values in the binds/filters also play a role (as far = as I know). Once a generic plan is selected, it doesn=E2=80=99t do the statistics = evaluation anymore, and thus the generic plan is fixed until the = prepared statement is closed or the session is terminated. It therefore also cannot choose a different plan based on the bind = values anymore. This means that if you want to manually replay your issue, just issuing = it using a prepared statement manually is not exactly what happens in a = session. Such a replay will always use a custom plan. You have to perform it at least 5 times for the generic plan to be = considered. And because that is evaluated cost based upon the cost of the previous = custom plans, the binds (filter values) have to entered correctly too = that have lead up to a potential generic plan having been chosen. Frits Hoogland > On 18 Nov 2023, at 12:13, James Pang (chaolpan) = wrote: >=20 > Looks like it's not sql issue, manually running still use prepared = statements and use same sql plan.=20 >=20 > -----Original Message----- > From: Andreas Kretschmer =20 > Sent: Friday, November 17, 2023 5:17 PM > To: pgsql-performance@lists.postgresql.org > Subject: Re: simple query running long time within a long transaction. >=20 >=20 >=20 > Am 17.11.23 um 09:10 schrieb James Pang (chaolpan): >>=20 >> Hi, >>=20 >> We found one simple query manually run very fast(finished in=20 >> several milliseconds), but there are 2 sessions within long=20 >> transaction to run same sql with same bind variables took tens of = seconds. >>=20 > you try to set plan_cache_mode to force_custom_plan, default is auto = and with that and bind variables pg will use a generic plan. >=20 >=20 > Regards, Andreas >=20 > --=20 > Andreas Kretschmer - currently still (garden leave) > Technical Account Manager (TAM) > www.enterprisedb.com >=20 >=20 >=20 --Apple-Mail=_B520A04D-DE00-429D-BA02-867EF2AD55DE Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Any statement = will run using a custom plan at first.
Only a prepared statement = creates the memory area in the backend that can hold the custom plan = statistics, that is why generic plans only work with prepared = statements.
A prepared statement has to run at least 5 times = before the planner looks at the plan statistics and determine whether a = generic plan would work (=3Dgeneric plan cost being equal or lower than = the average custom plan cost)
That means that the values in = the binds/filters also play a role (as far as I = know).

Once a generic plan is selected, it = doesn=E2=80=99t do the statistics evaluation anymore, and thus the = generic plan is fixed until the prepared statement is closed or the = session is terminated.
It therefore also cannot choose a = different plan based on the bind values = anymore.

This means that if you want to = manually replay your issue, just issuing it using a prepared statement = manually is not exactly what happens in a session.
Such a = replay will always use a custom plan.
You have to perform it = at least 5 times for the generic plan to be considered.
And = because that is evaluated cost based upon the cost of the previous = custom plans, the binds (filter values) have to entered correctly too = that have lead up to a potential generic plan having been = chosen.

Frits = Hoogland




On 18 Nov 2023, at 12:13, James = Pang (chaolpan) <chaolpan@cisco.com> wrote:

Looks like it's not sql = issue, manually running still use prepared statements and use same sql = plan.

-----Original Message-----
From: Andreas Kretschmer = <andreas@a-kretschmer.de>
Sent: Friday, November 17, 2023 5:17 = PM
To: pgsql-performance@lists.postgresql.org
Subject: Re: simple = query running long time within a long transaction.



Am = 17.11.23 um 09:10 schrieb James Pang (chaolpan):

Hi,

   We found one simple query = manually run very fast(finished in
several milliseconds), but there = are 2 sessions within long
transaction to run same sql with same = bind variables took tens of seconds.

you try to set = plan_cache_mode to force_custom_plan, default is auto and with that and = bind variables pg will use a generic plan.


Regards, = Andreas

--
Andreas Kretschmer - currently still (garden = leave)
Technical Account Manager = (TAM)
www.enterprisedb.com




= --Apple-Mail=_B520A04D-DE00-429D-BA02-867EF2AD55DE--