Skip to content

Instantly share code, notes, and snippets.

@dinwwwh
Created October 13, 2023 03:59
Show Gist options
  • Save dinwwwh/8a54e354d553a667afaf3ea6f470babb to your computer and use it in GitHub Desktop.
Save dinwwwh/8a54e354d553a667afaf3ea6f470babb to your computer and use it in GitHub Desktop.
postgresql
DO $$
DECLARE
counter integer := 1;
batch_size integer := 100;
BEGIN
WHILE counter <= 10000 LOOP
-- Use the counter to generate unique table names
EXECUTE 'CREATE TABLE IF NOT EXISTS "customer_histories_' || counter || '" PARTITION OF "customer_histories" FOR VALUES WITH (MODULUS 10000, REMAINDER ' || (counter - 1)::int || ');';
counter := counter + 1;
-- WHILE NOT EXISTS (SELECT 1 FROM pg_indexes WHERE tablename = 'table_' || counter) LOOP
-- PERFORM pg_sleep(1); -- Wait for 1 second
-- END WHILE;
-- Commit every batch_size tables
IF counter % batch_size = 0 THEN
COMMIT;
END IF;
END LOOP;
-- Commit any remaining tables
COMMIT;
END $$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment