Trying the following UPSERT in PG (nodejs):
WITH upsert_result AS(
INSERT INTO table1 (col1, col2)
VALUES($1,$2)
ON CONFLICT DO NOTHING
RETURNING *
)
SELECT * FROM upsert_result
UNION
SELECT * FROM table1 WHERE col1=$1
Why do I sometimes get empty results?
It is my understanding that the UNION
here was supposed to prevent this by fetching the conflicting row.
Here is the table definition:
CREATE EXTENSION IF NOT EXISTS "ltree";
CREATE TABLE IF NOT EXISTS table1(
col1 TEXT NOT NULL UNIQUE REFERENCES table2 (col1) ON DELETE CASCADE,
col2 ltree NOT NULL
);
CREATE INDEX IF NOT EXISTS tree_col2_idx ON table1 USING gist (col2);
I expected the query to return either the newly inserted row (if a row with same col1
doesn't already exist) or the existing row (if a row with the same col1
already exists). Indeed, this is what happens most of the time, but on some rare occasions, I get an empty result from the insert above.