MVCC: Using function doing INSERT and returning PK in WHERE clause of SELECT statement

From: Dominique Devienne <ddevienne(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: MVCC: Using function doing INSERT and returning PK in WHERE clause of SELECT statement
Date: 2024-07-01 08:33:09
Message-ID: CAFCRh-_R8Q2_=jeWpe+RWt798hhwAQLoPF=YG+nUnJXTg80qhA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi. So for business rule reason,
INSERT'ing a new row is wrapped in a DEFINER function,
that returns the newly inserted row's OK (and integral ID).

And in the code calling that function, I was

SELECT * FROM table WHERE ID = insert_row_via_func(....)

to "save a round trip", combining the insertion and the select.
But of course, it didn't work, and in hindsight, the SCN for the
SELECT is chosen before the INSERT done inside the function,
so the new row is not seen, and the select returns nothing.

The work-around is easy, just make two statements.
But of course that's two round-trips.

I looked at DO blocks, to perform both statements in the same rtrip.
But DO block don't return anything. I guess I could have two functions,
the existing one just returning the ID, and another returning the table
row type, and the latter calling the former? That's just more code to
maintain.

Is my analysis correct? In terms of the problem?
And the solutions / work-arounds?

Thanks for any insights. --DD

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Laurenz Albe 2024-07-01 12:29:48 Re: MVCC: Using function doing INSERT and returning PK in WHERE clause of SELECT statement
Previous Message Shammat 2024-07-01 07:37:24 Re: Passing a dynamic interval to generate_series()