Skip to content

Instantly share code, notes, and snippets.

@aanari
Last active May 26, 2017 02:23
Show Gist options
  • Save aanari/ddd7d6285373ff3abbc9 to your computer and use it in GitHub Desktop.
Save aanari/ddd7d6285373ff3abbc9 to your computer and use it in GitHub Desktop.
PG Batch Update
CREATE EXTENSION IF NOT EXISTS "dblink";
CREATE OR REPLACE FUNCTION batch_update(TEXT) RETURNS INTEGER LANGUAGE plpgsql AS $$
DECLARE
batch_query TEXT := '';
row_limit INTEGER := (REGEXP_MATCHES($1, 'LIMIT (\d+)', 'i'))[1];
row_count INTEGER := 1;
row_offset INTEGER := 0;
total_rows INTEGER := 0;
connection_str TEXT := 'dbname=' || CURRENT_DATABASE();
BEGIN
WHILE row_count > 0 LOOP
batch_query = REGEXP_REPLACE(
$1,
('(LIMIT ' || row_limit || ')'),
('\1 OFFSET ' || row_offset)
);
RAISE NOTICE 'Batching rows % - %', row_offset, row_offset + row_limit;
SELECT COUNT(*)
FROM DBLINK(connection_str, batch_query)
AS t(id INTEGER)
INTO row_count;
total_rows = total_rows + row_count;
row_offset = row_offset + row_count;
END LOOP;
RETURN total_rows;
END$$;
@Harrykris
Copy link

Can you Please show a example the usage of the function with the arguments to be passed ..?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment