I stumbled upon a rather uncommon feature of PostgreSQL recently, namely, clause RETURNING
. Let's say I have a database which contains a table products
. I needed to copy some products directly in SQL, and then to invoke a function with the ids of both the old and the new products (for some side-effects irrelevant here).
At first I felt quite confident that a simple INSERT
would do the job, but how to get the new ids in the most elegant way? As the doc states:
INSERT conforms to the SQL standard, except that the RETURNING clause is a PostgreSQL extension
So here it is:
INSERT INTO products (name,