BUG #18647: INSERT statements execute functions twice. - Mailing list pgsql-bugs

From PG Bug reporting form
Subject BUG #18647: INSERT statements execute functions twice.
Date
Msg-id 18647-8ac606f2f23b1621@postgresql.org
Whole thread Raw
Responses Re: BUG #18647: INSERT statements execute functions twice.
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      18647
Logged by:          Todd Brandys
Email address:      brandystodd@gmail.com
PostgreSQL version: 16.1
Operating system:   Linux
Description:

I am writing a function uuid.nextval(...) that returns a UUID value from a
preallocated pool of values known to be unique.  I was seeing the pool
decrease by two per INSERT, and I found something interesting when I raised
a NOTICE in the nextval() function.  Here are three INSERTs, and only the
last will result in nextval() being called once.  Is this a known issue?
What would be the reason for calling the functions in the VALUES list twice?
 For the foreseeable future, this means that the most efficient method would
be to place all functions in a FROM list.

dchain=# INSERT INTO public.party
    VALUES ( uuid.nextval('party'), utc.get_timestamp(),
SESSION_USER::regrole, uuid.v4() );
NOTICE:  ***** uuid.nextval
DETAIL:
HINT:
NOTICE:  ***** uuid.nextval
DETAIL:
HINT:
INSERT 0 1

dchain=# INSERT INTO public.party
SELECT      uuid.nextval('party'::regclass) AS party,
            utc.get_timestamp() AS date_upated,
            SESSION_USER::regrole AS user_updated,
            uuid.v4() AS organization;
NOTICE:  ***** uuid.nextval
DETAIL:
HINT:
NOTICE:  ***** uuid.nextval
DETAIL:
HINT:
INSERT 0 1

dchain=# INSERT INTO public.party
SELECT      d_party AS party,
            utc.get_timestamp() AS date_upated,
            SESSION_USER::regrole AS user_updated,
            uuid.v4() AS organization
    FROM    uuid.nextval('party'::regclass) AS d_party;
NOTICE:  ***** uuid.nextval
DETAIL:
HINT:
INSERT 0 1


pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: pg_dump throwing segfault error during backup
Next
From: "David G. Johnston"
Date:
Subject: Re: BUG #18647: INSERT statements execute functions twice.