PostgreSQL 19 Get or Create: Choose the Conflict Result

PostgreSQL 19 ON CONFLICT DO SELECT lets an insert return an existing conflicting row instead of changing it. That is useful when a duplicate means “give me the stored record.” If a duplicate means “change the label,” choose an explicit update instead. The business rule should come before the syntax.

Our local PostgreSQL 19.0 exercise ran six synthetic branches. Each finished with one stored row, but two branches returned no row. One separate two-session case waited for the first transaction to commit, then returned its original label. The downloadable results distinguish returned data from persisted data.

PostgreSQL 19 ON CONFLICT DO SELECT: the new choice

The 19 November release announcement introduces the feature for get-or-create patterns. The INSERT reference requires a conflict target and RETURNING for this action. A condition can exclude a conflict from the returned result. Optional row locks and the required permissions also matter.

Here is the central statement from our disposable fixture. It proposes a different ID and label for a key that already exists. That makes it easy to see whether the conflict action returns the original row or changes its contents.

INSERT INTO lab_signups
VALUES (20, 'synthetic-signup', 'Candidate')
ON CONFLICT (signup_key) DO SELECT
RETURNING id, label;

The existing row is ID 10 with label Original. In the conflict branch above, our observed result was 10 and Original. The stored label stayed Original. The candidate’s different label therefore did not silently become an update request.

Compare the returned answer with the stored answer

Six observed PostgreSQL 19 branches each leave one stored row;DO NOTHING and a false DO SELECT filter return zero rows,while the other four return one
Returned-row count and stored-row count measure different things. Each branch used a reset synthetic table, one unique key and Read Committed isolation.
BranchReturned ID / labelStored labelStored rows
Fresh insert20 / CandidateCandidate1
DO SELECT conflict10 / OriginalOriginal1
DO NOTHING conflictNo rowOriginal1
DO UPDATE conflict10 / CandidateCandidate1
False conflict filterNo rowOriginal1
Two sessions; first commits10 / OriginalOriginal1
Actual local results from PostgreSQL 19.0. The two-session branch additionally observed a lock wait before the holder committed.

DO NOTHING RETURNING does not return the conflicting row in this example. It returns no row because the candidate was not inserted. That differs from a fresh insert, where RETURNING can provide the inserted data. Your application must distinguish those two paths rather than assuming an empty result means that the key is absent.

For the update branch, we deliberately set label to EXCLUDED.label. The existing ID stayed 10 while its label became Candidate. This is an intentional data change. A no-op update used only to obtain an ID still deserves a separate design review; this lab does not measure its trigger, sequence or storage effects.

A false condition creates another empty-result path

We also tested a DO SELECT condition that does not match Original. The statement returned no row and left the existing record unchanged. The missing return was therefore the expected filtered outcome, not evidence that the database had lost the record.

Put the condition and the expected empty-result handling in your test ledger. For a real application, permissions, other constraints and independent errors need their own checks. A unique-key collision is only one part of the operation’s possible outcomes.

The two-session check: waiting is not failure

The Read Committed documentation explains that this action can wait for a conflicting transaction and return its row after commit, even when that row was not visible at statement start. We tested a narrow version of that sequence, rather than treating the documentation as a measurement.

Session A inserted the synthetic key and kept the transaction open. Session B proposed the same key with Candidate and used DO SELECT RETURNING. A separate observer saw B waiting on a lock. After A committed, B returned ID 10 and Original. The final count for that key was one.

We did not time the wait as a performance result. We also did not test rollback, every isolation level, triggers, notifications, client retries or a whole request lifecycle. In a production system, an atomic database statement does not automatically make payments, messages or other external effects idempotent.

Choose a statement that matches your business rule

Comparison distinguishes returning the stored Original row,skipping the conflict with no returned row,and explicitly updating its label to Candidate
The same key can lead to three different application answers. Choose the intended collision meaning before interpreting RETURNING or writing retry logic.

Use the first branch when the stored identity is the answer and its current label should remain authoritative. Choose the skip branch when the caller is prepared to handle no returned conflict row. Select the update branch only when changing the stored fields is the intended operation.

Then verify both outputs: the returned fields and a separate query of final state. Our PostgreSQL restore-verification report applies the same evidence habit to a different problem. Equal counts alone do not establish equal answers, and a successful command does not settle every business rule.

Method, reproducible files and limits

We built the official PostgreSQL 19.0 source after checking its published SHA-256, then ran the exercise on 23 November 2026. The disposable server used a private local socket with no TCP listener. The fixture held invented values only. Each single-session branch reset the table before its candidate insert.

Tech Consulting Tailored to Your Coding Journey

Get expert guidance in coding with a personalized consultation. Receive unique, actionable insights delivered in 1-3 business days.

Get Started

The source pack contains the Python driver, setup description and full observed results. It requires a new disposable PostgreSQL 19 database and the pinned Python adapter. It creates and removes only its named lab table there. Do not point teaching scripts at an existing application database. No real account, signup or customer data are needed.

Our six observations explain semantics, not scalability or a deployment recommendation. The blank sheet separates expected and observed outcomes so your own run can retain an error or unresolved result honestly. Keep the server version, isolation level, conflict target and condition alongside any cited result.

Suggested citation: Learn Coding USA, “PostgreSQL 19 Get or Create: Choose the Conflict Result”, 23 November 2026. Identify the synthetic fixture and Read Committed scope when reusing its observations.