Re: Bug / Unintentional Feature: non-immutable functions can be used for generated columns. - Mailing list pgsql-bugs

From Shane Plesner
Subject Re: Bug / Unintentional Feature: non-immutable functions can be used for generated columns.
Date
Msg-id CAFBci=oVThkEVGZN9FezkMqYO+fTNb08mHCZTqQQydHZ72xs0Q@mail.gmail.com
Whole thread Raw
In response to Re: Bug / Unintentional Feature: non-immutable functions can be used for generated columns.  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: Bug / Unintentional Feature: non-immutable functions can be used for generated columns.
List pgsql-bugs
A lot of what makes postgres valuable and trustworthy is the way it guarantees data consistency.
One cannot apply a mutable function as the generator. The database throws an exception.
This replacement behavior is, at the very least, completely undocumented. It seems to me that any time a function is replaced, there could be a check to see if it was changed from immutable to mutable. If it was, check the schema for any references to that function that require immutable function calls, and raise an exception.
Is there something I'm missing that makes this unfeasible?

On Sat, Oct 1, 2022 at 1:54 PM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Sat, Oct 1, 2022 at 8:27 AM Shane Plesner <gebnar@gmail.com> wrote:
Important preface: This "bug" turns out to enable functionality that is extremely useful. Please carefully consider how the usefulness of this can be retained when "fixing" the bug.

No error or warning or behavioral problems occur if the function used to fill a generated column is replaced with a non-immutable version after it is assigned as the column generator.

 
Discovered by Shane Plesner, by accident, about 10 minutes ago...

Discovered (in various incarnations) by many people over the years...

The documentation clearly states this is not a supported setup.  In particular it tends to break during dump/restore (though I think that mostly happens for check constraints).  It is unfortunate that the system cannot simply prohibit the action and instead must rely upon documentation, but that is how things are.  Furthermore, if you lie to the system like in your example, about your function being immutable when it actually is not, the consequences are on you alone (i.e., we do not try to static analyze the function body).  That includes fixing any problems if you determine a truly immutable function is buggy and thus needs to be changed to produce the correct output which is different that prior output.

David J.

pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #17626: Permission denied errors should list role as well as user
Next
From: Jeff Janes
Date:
Subject: Re: pg ignores wal files in pg_wal, and instead tries to load them from archive/primary