Postgresql INSERT FROM SELECT RETURNING ID
In PostgreSQL, use an INSERT INTO...SELECT statement with a RETURNING clause inside a CTE (Common Table Expression) to quickly get back the id
:
This efficient little query inserts records from source_table
into target_table
and swiftly fetches the newly minted id
s. These id
s are now ready for your next mission - er, operation.
Deploying RETURNING within transaction blocks
In your transactions, you can directly catch the newly returned id
by assigning it to a local variable. This enables it to be immediately accessible for follow-up commands within the same transaction block:
By introducing the RETURNING
clause into the transaction and storing its result into a variable, your entire process becomes a sleek one-transaction operation. Efficiency level up: unlocked!
Suspending batch insertions with arrays
When multiple data rows need to be inserted and their new IDs captured, store them in an array or process them immediately:
With this tweak, you can perform bulk operations and grasp all the fresh identifiers in one swoop, improving overall efficiency and your chances for world domination.
Elevating with function and trigger
To automate data management, you can combine RETURNING id with database functions and triggers. Let PostgreSQL do the work for you!
This ensures each vehicle insert also creates a related dealer entry and autolinks both with the dealer_id
. Vehicle-dealer relationships have never been so meticulously managed.
Tricks, traps, and tips
- Beware NULLs: Ensure your SELECT statement doesn't introduce unexpected
NULL
values. Phantom data isn't as fun as it sounds. - Be matchmaker: Column types must match - or heartbreak ensues.
- Duplicates? Nope: Avoid unintentionally doubling your insertions. Use the DISTINCT keyword judiciously in your SELECT.
- Raw Speed: For larger data sets, optimise the
INSERT FROM SELECT RETURNING ID
pattern. Use batch operations or consider a clown-car approach, cramming loads of data into small units.
Was this article helpful?