postgresql postgresql-12 add tag
Jack Douglas
I have two tables that have deferrable foreign key constraints that refer to each other. I'd like to insert into both of these tables via a function, and it is straightforward to do this with `plpgsql`:

<>https://dbfiddle.uk/?rdbms=postgres_12&fiddle=17387ab8b8ce39c82b055dbc7b25288c

Is there any way of doing the same with a SQL function?
Top Answer
Jack Douglas
I'm not sure this is a sensible general solution as I suspect it may cause concurrency issues, but if you choose a dummy value that will never be present in the table you can insert that, and then update it away again, in a pure `sql` function:

<>https://dbfiddle.uk/?rdbms=postgres_12&fiddle=88f7da708d2eef734400c13accc2e3fc&hide=7

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.