Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o6yzE-0002sc-9t for pgsql-general@arkaria.postgresql.org; Thu, 30 Jun 2022 18:33:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o6yzD-00087U-0d for pgsql-general@arkaria.postgresql.org; Thu, 30 Jun 2022 18:33:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o6yzC-00087K-JT for pgsql-general@lists.postgresql.org; Thu, 30 Jun 2022 18:33:54 +0000 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1o6yz5-0005C1-8K for pgsql-general@lists.postgresql.org; Thu, 30 Jun 2022 18:33:54 +0000 Received: by mail-pj1-x102e.google.com with SMTP id g16-20020a17090a7d1000b001ea9f820449so3980765pjl.5 for ; Thu, 30 Jun 2022 11:33:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=gIP2VO/3XMRfsFZ94A1falQS+jPtdMiVAzZXoU8TIH4=; b=ZccECn0JJL5vtmbAqj8WNwAd66PxGP2jHw906W4YMFV9oCb4XZ+CHkgcZpZl4rXrcy t0d0xYhSlgWeSwPPtIx8KnxxueMIOcTjnwUrHBNVlC42tz74HNxSpN5NRborkL7SeESj rdzBlpF/v16j2kc1FKEb5INHchPEqCLtvl5X4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=gIP2VO/3XMRfsFZ94A1falQS+jPtdMiVAzZXoU8TIH4=; b=vNMdteoneXNF/38lpG2sO2vkhf8PSPdvuzNwONehUNud49S37cWtIKKRQG+wmjMxRZ hBEeXyidTEdiu1m+CdNSOp/aK2NBAUZ7RHoSszOsK+1CM5UEOaioG8YNYbKTKZRg4lQ8 dcS51aW3jZvkm438kvjU8OpkO1ix0bLzJxXfz2XzMqSRg6cebOFrQ+xyQWXMxmBuAsNr dLnShdIb+OjRtwQsyzJVYZHRcHdOI8akM+Dj3xMajiIS/hfScu4WWPRzoxXWEei04kEQ X5RBugwQEE3EFBpH2LiS/E77VqLGyS6nAguPOSk0Oqu0GnfAn9b7TBZxDyHlc6R6qe9x Hp7Q== X-Gm-Message-State: AJIora9NoD1IfWo4smbr2HEm5N1991I/CuhcwZgDJhXeMDAvt0mS8Lht Muq+yEO9+Z3LyzNclqBTo09YgQ== X-Google-Smtp-Source: AGRyM1sRf2MFMAPG3jbQTPXu3IIhAwnuxpikNYGSRZXZm6vGoyaix4Xg1i2qCyzgONt8Ddf5GVAorA== X-Received: by 2002:a17:90a:c84:b0:1ef:3b1a:6077 with SMTP id v4-20020a17090a0c8400b001ef3b1a6077mr5098706pja.23.1656614024897; Thu, 30 Jun 2022 11:33:44 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:2919:2f75:5fdd:942f]) by smtp.gmail.com with ESMTPSA id a3-20020a1709027e4300b0016bb24f5d19sm1415805pln.209.2022.06.30.11.33.43 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 30 Jun 2022 11:33:44 -0700 (PDT) From: Bryn Llewellyn Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_78825C99-701A-4DE1-8F38-FF366579AF59" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: User's responsibility when using a chain of "immutable" functions? Date: Thu, 30 Jun 2022 11:33:42 -0700 In-Reply-To: Cc: Christophe Pettus , Laurenz Albe , Pavel Stehule , Tom Lane PostgreSQL , pgsql-general list To: "David G. Johnston" References: <38FB5DA3-5940-43A9-8B09-5C856E2B18C4@yugabyte.com> <80C40450-6C0C-46E2-AE82-E7D46120F7FF@thebuild.com> <29ca255d477eaf9fb600890a6212a4d913ef0d31.camel@cybertec.at> <1DFED436-377A-4601-9AC3-13422DD60638@yugabyte.com> <5C7C7780-B7CD-45D1-BC2B-60E444EE2384@yugabyte.com> X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_78825C99-701A-4DE1-8F38-FF366579AF59 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > david.g.johnston@gmail.com wrote: >=20 > bryn@yugabyte.com wrote: >>=20 >> Meanwhile. I'll appeal for some pointers to what I should read... >=20 > 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": >=20 > = https://www.postgresql.org/docs/current/plpgsql-implementation.html#PLPGSQ= L-PLAN-CACHING >=20 > You really need to read the "see related" reference there to get the = level of detail that you want: >=20 > https://www.postgresql.org/docs/current/xfunc-volatility.html >=20 > "This category allows the optimizer to pre-evaluate the function when = a query calls it with constant arguments." >=20 > 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 =3D '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 =3D '19'; execute q; ------------------<< Produces the stale "26 | 39". discard plans; execute q; ------------------<< Now produces the correct "38 | 57" --Apple-Mail=_78825C99-701A-4DE1-8F38-FF366579AF59 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
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 =3D '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 =3D '19';
execute q; = ------------------<< Produces the stale "26 | 39".
discard plans;
execute q; = ------------------<< Now produces the correct "38 | = 57"



= --Apple-Mail=_78825C99-701A-4DE1-8F38-FF366579AF59--