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 1o6dUM-00044E-AI for pgsql-general@arkaria.postgresql.org; Wed, 29 Jun 2022 19:36:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o6dUL-0006sR-69 for pgsql-general@arkaria.postgresql.org; Wed, 29 Jun 2022 19:36:37 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o6dUK-0006sE-GL for pgsql-general@lists.postgresql.org; Wed, 29 Jun 2022 19:36:36 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1o6dUH-0001Ph-Jp for pgsql-general@lists.postgresql.org; Wed, 29 Jun 2022 19:36:35 +0000 Received: by mail-pl1-x631.google.com with SMTP id x20so9415878plx.6 for ; Wed, 29 Jun 2022 12:36:33 -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=FC5J8oBhZ90xxeT1I07XhkbXxV74a7/h/t0vEcIZ5rw=; b=Brs2/Jp5IP1pjwJ3b5Y5qB9Nb7tv0ZF2altsQOWFWV3ztIrGYvtkzLk7cUxUt01Kz4 FUzEfrObuFaVB8aZEFIj9YG3B9h+BfCdnj6VeiK+ef7wtHrl084tylPOBFbDSIxNH5gJ 15s7DqIaRaL6dYZh7ZzwCmEAwepMzmWmGM+es= 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=FC5J8oBhZ90xxeT1I07XhkbXxV74a7/h/t0vEcIZ5rw=; b=UkDTg3aDqRfKYnCmHVwNqcI6JKpbD5XZkHi9PM8R9x5in0RPpPnsp6BdVY5Y7vem7B Au0jdj5RqI3qthGU59s60XK5JIVtW8CTVqqaedBpw2v1Sfcya6jCMINmvPI46IX0fyBM 3+iAVJYIyH3DB+rRUSLxvpvBBw39uZw5SngJi9PC1Z4d+g7uh8/D0gv+6Q6iVQR2+n9e x5lnjO373TZM9tg721iHQ8Ps4y67vS43JnzyqGO95cQKX9XAm91NSlv0sLldG9aTbqkb G6HmeQXbPn956MyKzKr6NNx8iaAGMgpFWrI4FxxCAXAvW7oPxLEmOOc5rHz4TOncTDlW 6xHw== X-Gm-Message-State: AJIora9gXzcynQvsSCUfGFGR6qJaZqv5qy0RxHxMTgmIwbQrlQrOuilJ KPKZVgi05KVa46YP5Y1GyfRI1Q== X-Google-Smtp-Source: AGRyM1v8udSoaNqV1RHdT20UsSnTYSzX9GLQomFIuF+PPLpgOa1IlbpF5zyatoNJUoi9f5vp1ZU3Rg== X-Received: by 2002:a17:90b:4cd0:b0:1ec:b260:db49 with SMTP id nd16-20020a17090b4cd000b001ecb260db49mr5523780pjb.193.1656531392056; Wed, 29 Jun 2022 12:36:32 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:550f:bbac:cee0:94af]) by smtp.gmail.com with ESMTPSA id m20-20020a170902c45400b0016a8010218bsm8087203plm.27.2022.06.29.12.36.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Jun 2022 12:36:31 -0700 (PDT) From: Bryn Llewellyn Message-Id: <1DFED436-377A-4601-9AC3-13422DD60638@yugabyte.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_6C8A7400-AFEC-4550-B150-F6A96D1C165A" 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: Wed, 29 Jun 2022 12:36:30 -0700 In-Reply-To: Cc: pgsql-general list To: Christophe Pettus , Laurenz Albe , "David G. Johnston" , Pavel Stehule , Tom Lane PostgreSQL References: <38FB5DA3-5940-43A9-8B09-5C856E2B18C4@yugabyte.com> <80C40450-6C0C-46E2-AE82-E7D46120F7FF@thebuild.com> <29ca255d477eaf9fb600890a6212a4d913ef0d31.camel@cybertec.at> 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=_6C8A7400-AFEC-4550-B150-F6A96D1C165A Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > xof@thebuild.com wrote: >=20 > I think "not allowed" is putting it too strongly. It would be a bit = much to ask that every single user-written immutable function be 100% = perfect when it is rolled out, and never have to fix any bugs in them. = However, you definitely *do* have to understand that there are = administrative consequences for doing so, like rebuilding indexes and = invalidating session caches. I think that the OP's statement that you = can't ever use user-defined functions from an immutable function is too = strong, too; you need to be aware of the consequences if you change an = immutable function in a way that alters the return result for a = previously-valid set of arguments. My email that started this discussion has prompted a lot of answers in a = few branches. This means that it's too hard for me to respond carefully = to everything that's been said. But it does seem that there are a few = different opinions about how safety might be ensured in the face of = wrong results risks and what, therefore, might define proper practice. I realize, now, that I didn't distinguish between: (1) What you might do (with some caution and attention to detail) in the = development shop; and (2) What you might do when patching the database backend of a deployed = production system. Case #2 is arguably clear cut=E2=80=94as long as you accept that there's = no such thing as safe hot patching (unless it's Oracle Database and you = have edition-based redefinition). So, accepting this, you have to = quiesce the system and make all your changes in a self-imposed = single-session fashion. Of course, you have to pay attention to = expression-based indexes. But anyone who adopts my maximally cautious = approach of allowing only hermetic "immutable" functions and who never = uses "create or replace" will find that the index safety risk looks = after itself. And case #1 is arguably less of a concern=E2=80=94unless it compromises = your regression testing. Anyway... PG has a very lightweight scheme for dependencies that tracks just a few = cases=E2=80=94like the dependence of an expression-based index that = references a user-defined function upon that function. But, = significantly, function-upon-function dependence (like my testcase = showed) is not tracked. This is a non-negotiable fundamental property of = PG. It's worth noting that (at least as I have understood it) the = "immutable" notion is simply a hint that gives PG permission to cache = results rather than to calculate them afresh on every access. And that = this is a device that seeks to improve performance. But significantly, = there's no robust cache invalidation scheme (and nor could there be) so = this leaves it to the user to promise safety. There's no possible discussion about the trade-off between performance = and correctness. So this argues for, at least, huge caution when you = think that you might mark a function "immutable". Having said this, I = noted this from pavel.stehule@gmail.com: > I know so many hard performance issues are related to missing STABLE = or IMMUTABLE flags of some functions. A caveat is needed because you're not allowed to reference a = user-defined function in an expression-based index unless it's marked = "immutable". But this ultimately is no more than a formal prick to the = user's conscience. Nothing stops you from lying through your teeth in = this scenario. It was all these considerations that led me to my proposal for *my own* = practice: (1) Never use "create or replace" to change an "immutable" = function=E2=80=94but, rather always use "drop" and a fresh bare = "create". (2) Never mark a function "immutable" unless its behavior is determined = entirely by its own source text. (I'll say this as "unless the function = is hermetic".) This notion accommodates use of some built-in functions = (but even there, caution is needed because of how session settings can = affect the behavior of some built-ins) but it most certainly prohibits = any reference to user-defined artifacts (including to other "immutable" = functions.) It also prohibits catalog queries. Both david.g.johnston@gmail.com and xof@thebuild.com have argued that my = stance is overly cautious. Fair enough. They can make their own minds = up. But what convinces me is the complete lack of static dependencies = and the infeasibility of discovering all dynamic dependencies by = exhaustive human analysis of source text when the only help you have is = a catalog query to identify all "immutable" functions in a database and = another to identify all indexes that reference a function. (But here, as = far as I can tell, you need human inspection to spot the subset that are = user-defined functions.) One more thing... I see now that I didn't clearly describe an outcome shown by the = testcase that I copied in my email that started this thread. Here's a = sufficient, shortened, version. The setup is what I showed you before. When I drop the first link, f1() in the chain of "immutable" functions, = I see that I cannot invoke f(2) because it now complains that f1() = doesn't exist. This surprises me because, after all, the result of f2() = is now cached (at least as I suppose) and its body isn't executed to = produce the result. This outcome almost suggests that there is, after = all, a dependency tracking scheme at work. Yet I can still invoke the third link, f(3), and it still does produce = the value that it had cached! This just tells me that it's stupid to try to deduce the intended = behavior of a software system by empirical testing. (It's also hard to = deduce what's intended by reading the source code.) = --------------------------------------------------------------------------= ------ create function f1() returns text immutable language plpgsql as $body$ begin return 'dog'; end; $body$; create function f2() returns text immutable language plpgsql as $body$ begin return f1(); end; $body$; create function f3() returns text immutable language plpgsql as $body$ begin return f2(); end; $body$; select f1(), f2(), f3(); drop function f1() cascade; select f2(); -- errors with "function f1() does not exist" select f3(); -- happily returns 'dog' --Apple-Mail=_6C8A7400-AFEC-4550-B150-F6A96D1C165A Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
xof@thebuild.com wrote:

I= think "not allowed" is putting it too strongly. It would be a bit much = to ask that every single user-written immutable function be 100% = perfect when it is rolled out, and never have to fix any bugs in them. = However, you definitely *do* have to understand that there are = administrative consequences for doing so, like rebuilding indexes and = invalidating session caches. I think that the OP's statement that = you can't ever use user-defined functions from an immutable function is = too strong, too; you need to be aware of the consequences if you = change an immutable function in a way that alters the return result for = a previously-valid set of arguments.

My email that started this discussion = has prompted a lot of answers in a few branches. This means that it's = too hard for me to respond carefully to everything that's been said. But = it does seem that there are a few different opinions about how safety = might be ensured in the face of wrong results risks and what, therefore, = might define proper practice.

I realize, now, that I didn't = distinguish  between:

(1) What you might do (with some caution and attention to = detail) in the development shop; and

(2) What you might do when patching the = database backend of a deployed production system.
Case #2 is arguably clear cut=E2=80=94as= long as you accept that there's no such thing as safe hot patching = (unless it's Oracle Database and you have edition-based redefinition). = So, accepting this, you have to quiesce the system and make all your = changes in a self-imposed single-session fashion. Of course, you have to = pay attention to expression-based indexes. But anyone who adopts my = maximally cautious approach of allowing only hermetic "immutable" = functions and who never uses "create or replace" will find that the = index safety risk looks after itself.

And case #1 is arguably less of a = concern=E2=80=94unless it compromises your regression testing.

Anyway...

PG has a very lightweight scheme for dependencies that tracks = just a few cases=E2=80=94like the dependence of an expression-based = index that references a user-defined function upon that function. But, = significantly, function-upon-function dependence (like my testcase = showed) is not tracked. This is a non-negotiable fundamental property of = PG.

It's worth = noting that (at least as I have understood it) the "immutable" notion is = simply a hint that gives PG permission to cache results rather than to = calculate them afresh on every access. And that this is a device that = seeks to improve performance. But significantly, there's no robust cache = invalidation scheme (and nor could there be) so this leaves it to the = user to promise safety.

There's no possible discussion about the trade-off between = performance and correctness. So this argues for, at least, huge caution = when you think that you might mark a function "immutable". Having said = this, I noted this from pavel.stehule@gmail.com:

I = know so many hard performance issues are related to missing STABLE or = IMMUTABLE flags of some functions.

A caveat is needed because you're not = allowed to reference a user-defined function in an expression-based = index unless it's marked "immutable". But this ultimately is no more = than a formal prick to the user's conscience. Nothing stops you from = lying through your teeth in this scenario.

It was all these considerations that = led me to my proposal for *my own* practice:

(1) Never use "create or replace" to = change an "immutable" function=E2=80=94but, rather always use "drop" and = a fresh bare "create".

(2) Never mark a function "immutable" unless its behavior is = determined entirely by its own source text. (I'll say this as "unless = the function is hermetic".) This notion accommodates use of some = built-in functions (but even there, caution is needed because of how = session settings can affect the behavior of some built-ins) but it most = certainly prohibits any reference to user-defined artifacts (including = to other "immutable" functions.) It also prohibits catalog = queries.

Both david.g.johnston@gmail.com and xof@thebuild.com have = argued that my stance is overly cautious. Fair enough. They can make = their own minds up. But what convinces me is the complete lack of static = dependencies and the infeasibility of discovering all dynamic = dependencies by exhaustive human analysis of source text when the only = help you have is a catalog query to identify all "immutable" functions = in a database and another to identify all indexes that reference a = function. (But here, as far as I can tell, you need human inspection to = spot the subset that are user-defined functions.)
One more = thing...

I = see now that I didn't clearly describe an outcome shown by the testcase = that I copied in my email that started this thread. Here's a sufficient, = shortened, version. The setup is what I showed you before.

When I drop the first = link, f1() in the chain of "immutable" functions, I see that I cannot = invoke f(2) because it now complains that f1() doesn't exist. This = surprises me because, after all, the result of f2() is now cached (at = least as I suppose) and its body isn't executed to produce the result. = This outcome almost suggests that there is, after all, a dependency = tracking scheme at work.

Yet I can still invoke the third link, f(3), and it still = does produce the value that it had cached!

This just tells me that it's stupid to = try to deduce the intended behavior of a software system by empirical = testing. (It's also hard to deduce what's intended by reading the source = code.)

---------------------------------------------------------------= -----------------

create function f1()
  returns = text
  immutable
  language plpgsql
as $body$
begin
  return 'dog';
end;
$body$;

create = function f2()
  returns text
  immutable
  language = plpgsql
as $body$
begin
  return f1();
end;
$body$;

create function f3()
  returns text
  immutable  language plpgsql
as $body$
begin
  return f2();
end;
$body$;

select = f1(), f2(), f3();

drop function f1() = cascade;

select f2(); -- errors with = "function f1() does not exist"

select f3(); = -- happily returns 'dog'

= --Apple-Mail=_6C8A7400-AFEC-4550-B150-F6A96D1C165A--