pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Christophe Pettus <xof@thebuild.com>
To: Bryn Llewellyn <bryn@yugabyte.com>
Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
Subject: Re: User's responsibility when using a chain of "immutable" functions?
Date: Wed, 29 Jun 2022 08:42:36 +0200
Message-ID: <29ca255d477eaf9fb600890a6212a4d913ef0d31.camel@cybertec.at> (raw)
In-Reply-To: <80C40450-6C0C-46E2-AE82-E7D46120F7FF@thebuild.com>
References: <38FB5DA3-5940-43A9-8B09-5C856E2B18C4@yugabyte.com>
	<80C40450-6C0C-46E2-AE82-E7D46120F7FF@thebuild.com>

On Tue, 2022-06-28 at 19:02 -0700, Christophe Pettus wrote:
> > On Jun 28, 2022, at 18:41, Bryn Llewellyn <bryn@yugabyte.com> wrote:
> > Should I simply understand that when I have such a dynamic dependency
> > chain of "immutable" functions, and should I drop and re-create the
> > function at the start of the chain, then all bets are off until I drop
> > and re-create every function along the rest of the chain?
> 
> Yes.

That is not enough in the general case.  You are not allowed to redefine
an IMMUTABLE function in a way that changes its behavior:

CREATE FUNCTION const(integer) RETURNS integer LANGUAGE plpgsql IMMUTABLE AS 'BEGIN RETURN $1; END;';

CREATE TABLE t (x integer);
INSERT INTO t VALUES (1);

CREATE INDEX ON t (const(x));

SET enable_seqscan = off;

SELECT * FROM t WHERE const(x) = 1; -- returns a correct result
 x 
═══
 1
(1 row)

CREATE OR REPLACE FUNCTION const(integer) RETURNS integer LANGUAGE plpgsql IMMUTABLE AS 'BEGIN RETURN $1 + 1; END;';

SELECT * FROM t WHERE const(x) = 1; -- returns a bad result
 x 
═══
 1
(1 row)

Of course, you are allowed to cheat if you know what you are doing.
But any problem you encounter that way is your own problem entirely.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com





view thread (17+ messages)  latest in thread

Message-ID: <29ca255d477eaf9fb600890a6212a4d913ef0d31.camel@cybertec.at>
Permalink:  ../29ca255d477eaf9fb600890a6212a4d913ef0d31.camel@cybertec.at/
Also on:    postgresql.org/message-id/29ca255d477eaf9fb600890a6212a4d913ef0d31.camel@cybertec.at

 · 

reply

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: laurenz.albe@cybertec.at, xof@thebuild.com, bryn@yugabyte.com
  Subject: Re: User's responsibility when using a chain of "immutable" functions?
  In-Reply-To: <29ca255d477eaf9fb600890a6212a4d913ef0d31.camel@cybertec.at>
  References: <38FB5DA3-5940-43A9-8B09-5C856E2B18C4@yugabyte.com> <80C40450-6C0C-46E2-AE82-E7D46120F7FF@thebuild.com> <29ca255d477eaf9fb600890a6212a4d913ef0d31.camel@cybertec.at>

* 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