Last active
May 26, 2017 02:23
-
-
Save aanari/ddd7d6285373ff3abbc9 to your computer and use it in GitHub Desktop.
PG Batch Update
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you Please show a example the usage of the function with the arguments to be passed ..?