I created a postgres query that inserts a series of unique codes. If one code is a duplicate I run the following.
ON CONFLICT ON CONSTRAINT uniquecode DO NOTHING;
What I want to do instead of NOTHING
is "counter" = "counter" - 1;
because within my while loop "counter" is always incremented by 1. If there is a duplicate then I want it reduced so that if I pass in 10 as the number of generated codes, then it will always return 10 unique codes instead of 9 sometimes because one was a duplicate.
However, I receive the following error when I replaceDO NOTHING
or NOTHING
with the counter reduction.
syntax error at or near "count"
What is this syntax error. All examples I have seen online show table updates instead. Can I simple update a variable instead?
As per the link posted by @a_horse_with_no_name I tried to apply the below after the conflict.
ON CONFLICT ON CONSTRAINT uniquecode DO UPDATE SET "created_at" = NOW();
PERFORM * FROM (
SELECT c."created_at"
FROM "codes" as c
ORDER BY "created_at" DESC
LIMIT 1
) AS "newest";
IF ("newest"::xmax::text::int > 0) THEN
"counter" = "counter" - 1;
END IF;
The intention of the above is that I only reduce the counter if the xmax greater than zero, which means that it was updated not inserted. If updated then that means that "created_at" was changed in the conflict, else do nothing.
Currently, I am at a point where if I keep the PERFORM call then either "type" xmax does not exist
or "column" xmax does not exist
.