From: Bryn Llewellyn <bryn@yugabyte.com>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: Christophe Pettus <xof@thebuild.com>
Cc: Laurenz Albe <laurenz.albe@cybertec.at>
Cc: Pavel Stehule <pavel.stehule@gmail.com>
Cc: Tom Lane PostgreSQL <tgl@sss.pgh.pa.us>
Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
Subject: Re: User's responsibility when using a chain of "immutable" functions?
Date: Thu, 30 Jun 2022 11:33:42 -0700
Message-ID: <DF251B2D-B992-433A-9E61-2BF16BAB15DB@yugabyte.com> (raw)
In-Reply-To: <CAKFQuwbGwLGGYsirxfhT43-BnzQ6QynJZva9AEmcoy5fv8D53Q@mail.gmail.com>
References: <38FB5DA3-5940-43A9-8B09-5C856E2B18C4@yugabyte.com>
<80C40450-6C0C-46E2-AE82-E7D46120F7FF@thebuild.com>
<29ca255d477eaf9fb600890a6212a4d913ef0d31.camel@cybertec.at>
<F7C03996-CE0C-4CEB-927B-3201FEBE9248@thebuild.com>
<1DFED436-377A-4601-9AC3-13422DD60638@yugabyte.com>
<CAKFQuwaA8c6CD+c6q6LHtNedRZtmmq2yJNhArQ2+V5EXndBKyg@mail.gmail.com>
<5C7C7780-B7CD-45D1-BC2B-60E444EE2384@yugabyte.com>
<CAKFQuwbGwLGGYsirxfhT43-BnzQ6QynJZva9AEmcoy5fv8D53Q@mail.gmail.com>
> david.g.johnston@gmail.com wrote:
>
> bryn@yugabyte.com wrote:
>>
>> Meanwhile. I'll appeal for some pointers to what I should read...
>
> I tend not to search...or at least that isn't my first (or at least only) recourse. The pg/pgsql chapter has a subchapter named "Plan Caching":
>
> https://www.postgresql.org/docs/current/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING
>
> You really need to read the "see related" reference there to get the level of detail that you want:
>
> https://www.postgresql.org/docs/current/xfunc-volatility.html
>
> "This category allows the optimizer to pre-evaluate the function when a query calls it with constant arguments."
>
> The implication is that this operation is not session-scoped but query-scoped. Other parts of the page reinforce this. Not saying it is perfect wording but I came by my understanding pretty much exclusively from this documentation.
Thank you very much for the doc pointers, David. I believe that I have all I need, now. I understood already that "giving permission to cache" doesn't mean that PG will actually cache anything. I wanted only to find a compelling example of how lying when you mark a function "immutable" can bring wring results. I think that this is sufficient:
set x.a = '13';
create function dishonestly_marked_immutable(i in int)
returns int
immutable
language plpgsql
as $body$
begin
return i*(current_setting('x.a')::int);
end;
$body$;
prepare q as
select
dishonestly_marked_immutable(2) as "With actual '2'",
dishonestly_marked_immutable(3) as "With actual '3'";
execute q;
set x.a = '19';
execute q; ------------------<< Produces the stale "26 | 39".
discard plans;
execute q; ------------------<< Now produces the correct "38 | 57"
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: bryn@yugabyte.com, david.g.johnston@gmail.com, xof@thebuild.com, laurenz.albe@cybertec.at, pavel.stehule@gmail.com, tgl@sss.pgh.pa.us
Subject: Re: User's responsibility when using a chain of "immutable" functions?
In-Reply-To: <DF251B2D-B992-433A-9E61-2BF16BAB15DB@yugabyte.com>
References: <38FB5DA3-5940-43A9-8B09-5C856E2B18C4@yugabyte.com> <80C40450-6C0C-46E2-AE82-E7D46120F7FF@thebuild.com> <29ca255d477eaf9fb600890a6212a4d913ef0d31.camel@cybertec.at> <F7C03996-CE0C-4CEB-927B-3201FEBE9248@thebuild.com> <1DFED436-377A-4601-9AC3-13422DD60638@yugabyte.com> <CAKFQuwaA8c6CD+c6q6LHtNedRZtmmq2yJNhArQ2+V5EXndBKyg@mail.gmail.com> <5C7C7780-B7CD-45D1-BC2B-60E444EE2384@yugabyte.com> <CAKFQuwbGwLGGYsirxfhT43-BnzQ6QynJZva9AEmcoy5fv8D53Q@mail.gmail.com> <DF251B2D-B992-433A-9E61-2BF16BAB15DB@yugabyte.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox