Skip to content

Instantly share code, notes, and snippets.

@dtseiler
Last active June 20, 2020 20:40
Show Gist options
  • Select an option

  • Save dtseiler/febe7971723a21d8998c171776d3b140 to your computer and use it in GitHub Desktop.

Select an option

Save dtseiler/febe7971723a21d8998c171776d3b140 to your computer and use it in GitHub Desktop.
pg_partman with PG12 native partitioning
-- Setup users table and populate
CREATE TABLE users (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email VARCHAR(40) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL
);
INSERT INTO users (email, created_at)
SELECT
'user_' || seq || '@' || (
CASE (RANDOM() * 2)::INT
WHEN 0 THEN 'gmail'
WHEN 1 THEN 'hotmail'
WHEN 2 THEN 'yahoo'
END
) || '.com' AS email,
timestamp '2016-01-10 20:00:00' +
random() * (timestamp '2020-01-20 20:00:00' -
timestamp '2016-01-10 20:00:00')
FROM GENERATE_SERIES(1, 500000) seq;
CREATE INDEX users_created_at_idx ON users(created_at);
VACUUM ANALYZE users;
-- Use template table so we can have local unique/PK on each partition
CREATE TABLE users_part_template
(LIKE users INCLUDING ALL);
-- Downtime would effectively start now
ALTER TABLE users RENAME TO users_source;
CREATE TABLE users
(LIKE users_part_template INCLUDING DEFAULTS INCLUDING CONSTRAINTS)
PARTITION BY RANGE (created_at);
SELECT partman.create_parent('public.users', 'created_at', 'native', 'monthly', p_premake := 2, p_template_table := 'public.users_part_template');
DROP TABLE users_default;
ALTER TABLE users_source RENAME TO users_default;
ALTER TABLE users ATTACH PARTITION users_default DEFAULT;
-- Downtime could effectively end now?
-- Move the data from the default partition out to the monthly partitions
CALL partman.partition_data_proc('public.users', '1 month');
VACUUM ANALYZE users;
-- Reindex the original table's indexes to resize them to 0
REINDEX TABLE users_default;
@davidalejandroaguilar
Copy link

Hey Don, how did things go? Were you able to partition your data with reduced downtime as you planned?

@dtseiler
Copy link
Author

I haven't done this with any actual app databases yet. Thanks for asking!

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