From: | Shane Plesner <gebnar(at)gmail(dot)com> |
---|---|
To: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
Subject: | Bug / Unintentional Feature: non-immutable functions can be used for generated columns. |
Date: | 2022-10-01 15:18:41 |
Message-ID: | CAFBci=qAOea1SCwkJgLdR6OwLOBVTuHqmqDGz8jKMFUzZd1b5g@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-bugs |
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.
SQL which reproduces this issue, as well as showing one valid (useful)
use-case for why this isn't a bad thing in itself:
create function gen_tmp() returns text
> language plpgsql
> immutable
> as $$
> declare
> _tmp text;
> begin
> loop
> select 'C-'||array_to_string(array(select
> substr('ABCDEFGHJKLMNPQRSTUVWXYZ0123456789',((random()*(33)+1)::integer),1)
> from generate_series(1,8)),'') into _tmp;
> exit when not exists(select 1 from tmp where k = _tmp);
> end loop;
> return _tmp;
> end
> $$;
>
> create table tmp(
> k text unique primary key generated always as (gen_tmp()) stored,
> v text
> );
>
> create or replace function gen_tmp() returns text
> language plpgsql
> as $$
> declare
> _tmp text;
> begin
> loop
> select 'C-'||array_to_string(array(select
> substr('ABCDEFGHJKLMNPQRSTUVWXYZ0123456789',((random()*(33)+1)::integer),1)
> from generate_series(1,8)),'') into _tmp;
> exit when not exists(select 1 from tmp where k = _tmp);
> end loop;
> return _tmp;
> end
> $$;
>
> insert into tmp (v) select 'test' from generate_series(1,10);
>
> select * from tmp;
>
Discovered by Shane Plesner, by accident, about 10 minutes ago...
From | Date | Subject | |
---|---|---|---|
Next Message | David G. Johnston | 2022-10-01 18:54:36 | Re: Bug / Unintentional Feature: non-immutable functions can be used for generated columns. |
Previous Message | Tom Lane | 2022-09-30 14:29:47 | Re: BUG #17625: In PG15 PQsslAttribute returns different values than PG14 when SSL is not in use for the connection |