Partitioning a postgres database by both a tenant ID and time

849 Views Asked by At
CREATE SCHEMA plugin_work_queue;

CREATE TABLE IF NOT EXISTS plugin_work_queue.plugin_executions
(
    execution_key        bigserial   NOT NULL,
    tenant_id            uuid        NOT NULL,
    creation_time        timestamptz NOT NULL,
    message              bytea       NOT NULL
)
PARTITION BY RANGE (creation_time);


CREATE SCHEMA partman;
CREATE EXTENSION pg_partman WITH SCHEMA partman;


SELECT partman.create_parent(p_parent_table => 'plugin_work_queue.plugin_executions',
                             p_control => 'creation_time',
                             p_type => 'native',
                             p_interval=> 'daily',
                             p_premake => 30
           );

So I've got a database much like the one described above. This works and I can partition my data by creation_time.

But ideally I'd actually like to have two levels of partitioning. I'd like to first partition by tenant_id, and then sub-partition by creation_time.

Unfortunately I haven't found this to be straightforward.

  1. pg_partman doesn't support partitioning by uuid. Fine, I can extract the lower 4 bytes of the uuid and use that as a partition key.

  2. pg_partman doesn't seem to support partitioning by hash, which I believe would be the ideal way to manage this (but I'm unsure).

  3. The documentation for sub-partitions is a bit less than ideal in pg_partman.

I'd like to make operations like "drop this tenants partitions" or "drop partitions older than date X" easy, hence the desire for a tiered partitioning.

0

There are 0 best solutions below