Created
June 20, 2020 16:44
-
-
Save jfischoff/b219ae45addd2f8c679e0b61bed22111 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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