Skip to content

Instantly share code, notes, and snippets.

@jfischoff
Created June 20, 2020 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfischoff/b219ae45addd2f8c679e0b61bed22111 to your computer and use it in GitHub Desktop.
Save jfischoff/b219ae45addd2f8c679e0b61bed22111 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION enqueue(count int8, x anyarray) RETURNS void AS $$
DECLARE
startValue int8;
lastValue int8;
currentValue int8;
partition_name text;
doubleCount int8;
BEGIN
doubleCount := 2 * count;
SELECT nextval('modified_index') INTO currentValue ;
startValue := ((currentValue + 1) / doubleCount) * doubleCount;
lastValue := startValue + doubleCount - 1;
partition_name := 'payloads_' || startValue || '_' || lastValue ;
IF NOT EXISTS(SELECT relname FROM pg_class WHERE relname=partition_name) THEN
EXECUTE
format(E'CREATE TABLE %I partition of payloads for values from (%L) to (%L);',
partition_name, startValue, lastValue);
END IF;
-- EXECUTE 'INSERT INTO payloads (attempts, value) VALUES (0, $1)' USING x;
EXECUTE 'INSERT INTO payloads (attempts, value) SELECT 0, * FROM unnest($1)' USING x;
END;
$$ LANGUAGE plpgsql;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment