Predict the output
A batch of 3 inserts uses one statement. psql runs with ON_ERROR_STOP, so the script stops at the first error. What happens?
CREATE TABLE items (sku text PRIMARY KEY, qty int CHECK (qty > 0));
INSERT INTO items VALUES ('a', 1), ('b', 0), ('c', 2)
ON CONFLICT DO NOTHING;
SELECT count(*) FROM items;Hint 1 of 1
Which errors does ON CONFLICT handle?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A The INSERT fails with a CHECK violation, so nothing is inserted
Correct. ON CONFLICT only covers unique conflicts; the bad row aborts the whole statement.
A single INSERT is atomic: a CHECK violation on one row aborts the whole statement, and ON CONFLICT only handles unique or exclusion conflicts. With ON_ERROR_STOP, psql exits before the SELECT runs.
Why not the others?
- B
2. One statement is atomic: a CHECK failure rejects all rows. - C
3. qty 0 violates the CHECK. - D
0. The script stops at the error before the SELECT runs.