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 1qY94P-000tqB-Ej for pgsql-general@arkaria.postgresql.org; Mon, 21 Aug 2023 17:52:06 +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 1qY94N-0096a0-6Q for pgsql-general@arkaria.postgresql.org; Mon, 21 Aug 2023 17:52:02 +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.94.2) (envelope-from ) id 1qY8W5-008ixt-Em for pgsql-general@lists.postgresql.org; Mon, 21 Aug 2023 17:16:37 +0000 Received: from mail-lj1-x231.google.com ([2a00:1450:4864:20::231]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qY8W0-000BlF-Q9 for pgsql-general@lists.postgresql.org; Mon, 21 Aug 2023 17:16:35 +0000 Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-2bcce32ccd4so84851fa.0 for ; Mon, 21 Aug 2023 10:16:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iisgubbio.edu.it; s=google; t=1692638190; x=1693242990; h=message-id:in-reply-to:to:references:date:subject:mime-version:from :from:to:cc:subject:date:message-id:reply-to; bh=depb6iIcX4BpQUbsw8fPJ3zGzh34oQtoigkfn5LRIts=; b=cR8BwfnxxQZXp46RcPhqrC6OOSoQzDj7LWKgoSsyLSWpgbO/8Ll2ui/82F6u5305mJ xXY/nAxUSv3mR70YRDWf7fSJch4KMaEsZXr0hjURy1mi85zmeOEJPFIY3bXnxZ0bamzb FSTTw9ZawyL2MSoiXDOLy8Fz6o20Tg/u7ez33fAAS3kACm/gNbgRLVW3Sb3I0q719qop 4pHpL/Kmo/khdqd6SFnzNlvymdoXqH5g+dsmuqL6LZuntM1scVra2UvLqxZKTucCRmg7 hQTeFz+DqeT2kW0rA01DfdmEeiEyMY9NLr4rRg+mP4dGOMz9XRA82fAgJAC03pjSqIxc C3zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692638190; x=1693242990; h=message-id:in-reply-to:to:references:date:subject:mime-version:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=depb6iIcX4BpQUbsw8fPJ3zGzh34oQtoigkfn5LRIts=; b=OdXQ8aC9NkqriBNQ9nvaf5u7j3P4cSdMDVRhsvPMcECIlJSWQdOsZ28js6r8famCK/ DS8IwmfyKxFgYt7IxQIKHGtZ767ebS2R8EYRDEKDVM+mqNLT04iAg49VbWNXGfcYt570 XOLT62BTwSgLZDdMOa7GFtStfJivvxULRQ7in6lLaNBJoTbyY46rY+Fpf9rciq2KsgYF 9SefIWkm4WoXuPpkE4kPQ0U54M5yz/u/RfCKLDgEb0sK2j20eIeRV22aq0nIkUYtat9a KkYaJQJBuh7HFHjGPRK4raO7vf0xYxg1RG+wHcRCMA5gHWqGZoQZenbKZMuUjO6cnAN/ hyvA== X-Gm-Message-State: AOJu0YxQBOv2II3h1E7nHh7jcW8xXWK6TZlOiPOZKJvixlb2QJB4mdQ5 U3V6IjlzH5pUUJUxfmmsgOOseOwHY5qOdLBVm5o= X-Google-Smtp-Source: AGHT+IGots5vfFRCgJAhrdzlHWLgwqXY5yxQMQAstM1cVdze62vHBuhMrzTZ7Zo3iaUFFmyUmHMpmg== X-Received: by 2002:a05:651c:1c6:b0:2b6:af68:6803 with SMTP id d6-20020a05651c01c600b002b6af686803mr5115970ljn.4.1692638190169; Mon, 21 Aug 2023 10:16:30 -0700 (PDT) Received: from smtpclient.apple (host-79-47-189-141.retail.telecomitalia.it. [79.47.189.141]) by smtp.gmail.com with ESMTPSA id x28-20020a05651c105c00b002b6ad323248sm2381963ljm.10.2023.08.21.10.16.28 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 21 Aug 2023 10:16:28 -0700 (PDT) From: Edoardo Panfili X-Google-Original-From: Edoardo Panfili Content-Type: multipart/alternative; boundary="Apple-Mail=_D6259FE7-67C7-4FB6-926F-A5EA0495404A" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.600.7\)) Subject: Re: The same prepared query yield "-1" the first six times and then "-1.0" Date: Mon, 21 Aug 2023 19:16:17 +0200 References: <5C40B9CC-40E4-4AC5-95A1-E5A521C282EF@gmail.com> <4e6d74b2-8beb-079d-4db1-f729fa76b7c2@aklaver.com> To: pgsql-general@lists.postgresql.org In-Reply-To: <4e6d74b2-8beb-079d-4db1-f729fa76b7c2@aklaver.com> Message-Id: <7C9C6D48-804A-44F7-836B-8B8AEE6D60CA@gmail.com> X-Mailer: Apple Mail (2.3731.600.7) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_D6259FE7-67C7-4FB6-926F-A5EA0495404A Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Il giorno 21 ago 2023, alle ore 17:45, Adrian Klaver = ha scritto: >=20 > On 8/21/23 08:27, Edoardo Panfili wrote: >> Hello, >> I am using >> postgresql version: 15.3 (Debian 15.3-0+deb12u1) >> org.postgresql.postgresql JDBC driver version: 42.6.0 >> via Java 17.0.7 >> I discovered an unattended (for me) situation: when I execute >> 10 times the same prepared query the result is not always the same. >=20 >> The attended result was a sequence of ten equal values but this is = the actual result: >> p: -1 >> p: -1 >> p: -1 >> p: -1 >> p: -1 >> p: -1.0 >> p: -1.0 >> p: -1.0 >> p: -1.0 >> p: -1.0 >=20 > They are equal values: >=20 > select -1 =3D -1.0; > ?column? > ---------- > t You are right, I know. But in a single occasion I have to use it as a = text value. Il comportamento attuale =C3=A8 sicuramente corretto but = seems to me not completely transparent, only my opinion. >=20 >=20 >> All works fine if I open and close the connection after every single = query >> but in production I am using pooled connections. >> This is what I can read in postgresql logs (it seems that after 4 = queries >> the statement becomes named and the result changes after the second = call to >> the named query): >> 2023-08-21 11:51:50.633 CEST [1511] user@testdb LOG: execute = : SET extra_float_digits =3D 3 >> 2023-08-21 11:51:50.634 CEST [1511] user@testdb LOG: execute = : SET application_name =3D 'PostgreSQL JDBC Driver' >> 2023-08-21 11:51:50.644 CEST [1511] user@testdb LOG: execute = : SELECT dim1 FROM number WHERE name=3D'first' >> 2023-08-21 11:51:50.648 CEST [1511] user@testdb LOG: execute = : SELECT dim1 FROM number WHERE name=3D'first' >> 2023-08-21 11:51:50.649 CEST [1511] user@testdb LOG: execute = : SELECT dim1 FROM number WHERE name=3D'first' >> 2023-08-21 11:51:50.650 CEST [1511] user@testdb LOG: execute = : SELECT dim1 FROM number WHERE name=3D'first' >> 2023-08-21 11:51:50.651 CEST [1511] user@testdb LOG: execute S_1: = SELECT dim1 FROM number WHERE name=3D'first' >> 2023-08-21 11:51:50.651 CEST [1511] user@testdb LOG: execute S_1: = SELECT dim1 FROM number WHERE name=3D'first' >> 2023-08-21 11:51:50.653 CEST [1511] user@testdb LOG: execute S_1: = SELECT dim1 FROM number WHERE name=3D'first' >> 2023-08-21 11:51:50.653 CEST [1511] user@testdb LOG: execute S_1: = SELECT dim1 FROM number WHERE name=3D'first' >> 2023-08-21 11:51:50.654 CEST [1511] user@testdb LOG: execute S_1: = SELECT dim1 FROM number WHERE name=3D'first' >> 2023-08-21 11:51:50.656 CEST [1511] user@testdb LOG: execute S_1: = SELECT dim1 FROM number WHERE name=3D=E2=80=98first' >> Can I do something to avoid this problem? >=20 > Read this: >=20 > https://www.postgresql.org/docs/current/sql-prepare.html >=20 > " > By default (that is, when plan_cache_mode is set to auto), the server = will automatically choose whether to use a generic or custom plan for a = prepared statement that has parameters. The current rule for this is = that the first five executions are done with custom plans and the = average estimated cost of those plans is calculated. Then a generic plan = is created and its estimated cost is compared to the average custom-plan = cost. Subsequent executions use the generic plan if its cost is not so = much higher than the average custom-plan cost as to make repeated = replanning seem preferable. >=20 > This heuristic can be overridden, forcing the server to use either = generic or custom plans, by setting plan_cache_mode to = force_generic_plan or force_custom_plan respectively. This setting is = primarily useful if the generic plan's cost estimate is badly off for = some reason, allowing it to be chosen even though its actual cost is = much more than that of a custom plan. > " Thank you for the link! I did a try setting =E2=80=9Cplan_cache_mode=E2=80= =9D but it seems nothing change, and my test query (not production one = obviously) has no parameter and in this occasion =E2=80=9Cif the = prepared statement has no parameters, then this is moot and a generic = plan is always used.=E2=80=9D=20 Also using variables in query nothing changes. I will work again on it. Thank you again. Edoardo= --Apple-Mail=_D6259FE7-67C7-4FB6-926F-A5EA0495404A Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

Il = giorno 21 ago 2023, alle ore 17:45, Adrian Klaver = <adrian.klaver@aklaver.com> ha scritto:

On 8/21/23 08:27, Edoardo Panfili = wrote:
Hello,
I am using
postgresql version: 15.3 = (Debian 15.3-0+deb12u1)
org.postgresql.postgresql JDBC driver = version: 42.6.0
via Java 17.0.7
I discovered an unattended (for = me) situation: when I execute
10 times the same prepared query the = result is not always the same.

The attended result was a sequence of ten equal = values but this is the actual result:
p: -1
p: -1
p: -1
p: = -1
p: -1
p: -1.0
p: -1.0
p: -1.0
p: -1.0
p: = -1.0

They = are equal values:

select = -1 =3D -1.0;
?column?
----------
t
You are right, I know. But in = a single occasion I have to use it as a text value. Il comportamento = attuale =C3=A8 sicuramente corretto but seems to me not completely = transparent, only my opinion.



All works fine if I open and close the = connection after every single query
but in production I am using = pooled connections.
This is what I can read in postgresql logs (it = seems that after 4 queries
the statement becomes named and the result = changes after the second call to
the named query):
2023-08-21 = 11:51:50.633 CEST [1511] user@testdb LOG: execute <unnamed>: SET = extra_float_digits =3D 3
2023-08-21 11:51:50.634 CEST [1511] = user@testdb LOG: execute <unnamed>: SET application_name =3D = 'PostgreSQL JDBC Driver'
2023-08-21 11:51:50.644 CEST [1511] = user@testdb LOG: execute <unnamed>: SELECT dim1 FROM number WHERE = name=3D'first'
2023-08-21 11:51:50.648 CEST [1511] user@testdb LOG: = execute <unnamed>: SELECT dim1 FROM number WHERE = name=3D'first'
2023-08-21 11:51:50.649 CEST [1511] user@testdb LOG: = execute <unnamed>: SELECT dim1 FROM number WHERE = name=3D'first'
2023-08-21 11:51:50.650 CEST [1511] user@testdb LOG: = execute <unnamed>: SELECT dim1 FROM number WHERE = name=3D'first'
2023-08-21 11:51:50.651 CEST [1511] user@testdb LOG: = execute S_1: SELECT dim1 FROM number WHERE name=3D'first'
2023-08-21 = 11:51:50.651 CEST [1511] user@testdb LOG: execute S_1: SELECT dim1 FROM = number WHERE name=3D'first'
2023-08-21 11:51:50.653 CEST [1511] = user@testdb LOG: execute S_1: SELECT dim1 FROM number WHERE = name=3D'first'
2023-08-21 11:51:50.653 CEST [1511] user@testdb LOG: = execute S_1: SELECT dim1 FROM number WHERE name=3D'first'
2023-08-21 = 11:51:50.654 CEST [1511] user@testdb LOG: execute S_1: SELECT dim1 FROM = number WHERE name=3D'first'
2023-08-21 11:51:50.656 CEST [1511] = user@testdb LOG: execute S_1: SELECT dim1 FROM number WHERE = name=3D=E2=80=98first'
Can I do something to avoid this = problem?

Read = this:

https://www.postgresql.org/docs/current/sql-prepare.html

"
By = default (that is, when plan_cache_mode is set to auto), the server will = automatically choose whether to use a generic or custom plan for a = prepared statement that has parameters. The current rule for this is = that the first five executions are done with custom plans and the = average estimated cost of those plans is calculated. Then a generic plan = is created and its estimated cost is compared to the average custom-plan = cost. Subsequent executions use the generic plan if its cost is not so = much higher than the average custom-plan cost as to make repeated = replanning seem preferable.

This = heuristic can be overridden, forcing the server to use either generic or = custom plans, by setting plan_cache_mode to force_generic_plan or = force_custom_plan respectively. This setting is primarily useful if the = generic plan's cost estimate is badly off for some reason, allowing it = to be chosen even though its actual cost is much more than that of a = custom plan.
"

Thank you for the link! I did a = try setting =E2=80=9Cplan_cache_mode=E2=80=9D but it seems nothing = change, and my test query (not production one obviously) has no = parameter and in this occasion =E2=80=9Cif the prepared statement has no = parameters, then this is moot and a generic plan is always = used.=E2=80=9D 
Also using variables in query = nothing changes. I will work again on it.

Thank = you again.
Edoardo
= --Apple-Mail=_D6259FE7-67C7-4FB6-926F-A5EA0495404A--