Skip to content

Instantly share code, notes, and snippets.

@v0112358
Created February 25, 2020 14:17
Show Gist options
  • Save v0112358/fc09f7095d47d6c363e5b14d0bb5c6af to your computer and use it in GitHub Desktop.
Save v0112358/fc09f7095d47d6c363e5b14d0bb5c6af to your computer and use it in GitHub Desktop.

1/ On master

CREATE TABLE event_store (
id bigint DEFAULT nextval('public.eid_seq_1'::regclass) NOT NULL,
topic character varying(255),
created_at timestamp(6) without time zone DEFAULT now()
) PARTITION BY RANGE (created_at);
CREATE TABLE event_store_y2020m09 PARTITION of event_store
  FOR VALUES FROM ('2020-09-01') TO ('2020-10-01') ;

CREATE TABLE event_store_y2020m10 PARTITION of event_store
  FOR VALUES FROM ('2020-10-01') TO ('2020-11-01') ;

CREATE TABLE event_store_y2020m11 PARTITION of event_store
  FOR VALUES FROM ('2020-11-01') TO ('2020-12-01') ;
  
CREATE TABLE event_store_default PARTITION of event_store DEFAULT;
-- create publications for partitions
create publication event_store_y2020m09 for table event_store_y2020m09;
create publication event_store_y2020m10 for table event_store_y2020m10;
create publication event_store_y2020m11 for table event_store_y2020m11;

2/ On slave

CREATE TABLE event_store (
id bigint DEFAULT nextval('public.eid_seq_1'::regclass) NOT NULL,
topic character varying(255),
created_at timestamp(6) without time zone DEFAULT now()
) PARTITION BY RANGE (created_at);
CREATE TABLE event_store_y2020m09 PARTITION of event_store
  FOR VALUES FROM ('2020-09-01') TO ('2020-10-01') ;

CREATE TABLE event_store_y2020m10 PARTITION of event_store
  FOR VALUES FROM ('2020-10-01') TO ('2020-11-01') ;

CREATE TABLE event_store_y2020m11 PARTITION of event_store
  FOR VALUES FROM ('2020-11-01') TO ('2020-12-01') ;
  
CREATE TABLE event_store_default PARTITION of event_store DEFAULT;
-- create subscriptions for partitions
create subscription event_store_y2020m09 CONNECTION 'port=5432' publication event_store_y2020m09;
create subscription event_store_y2020m10 CONNECTION 'port=5432' publication event_store_y2020m10;
create subscription event_store_y2020m11 CONNECTION 'port=5432' publication event_store_y2020m11;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment