Skip to content

Instantly share code, notes, and snippets.

@CHERTS
Created April 29, 2025 10:59
Show Gist options
  • Save CHERTS/d8badf2e1ca8de245bc607bc22611540 to your computer and use it in GitHub Desktop.
Save CHERTS/d8badf2e1ca8de245bc607bc22611540 to your computer and use it in GitHub Desktop.
CREATE TABLE test (
id BIGINT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY,
data_field VARCHAR(255) NOT NULL
);
INSERT INTO test(data_field) SELECT(i::text) FROM generate_series(1,10000) as t(i);
-- repeat several times
WITH candidate_rows AS (
SELECT id
FROM test
WHERE data_field <> 'updated'
LIMIT 1200
FOR UPDATE NOWAIT
), update_rows AS (
UPDATE test
SET data_field = 'updated'
FROM candidate_rows
WHERE candidate_rows.id = test.id
RETURNING test.id
)
SELECT count(1) FROM update_rows;
DROP TABLE test;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment