Search code examples
sqlpostgresqldatabase-partitioning

Create partition for anything before a specific date


I am creating a date partitioned table mytable. I have 5 partitions for each year, but I want another for anything before the first partitions's year.

My code and the partitions look like so:

CREATE TABLE mytable (
    "id" INT,
    "data" TEXT, 
    "date" DATE
) PARTITION BY RANGE (date);

CREATE TABLE mytable_2010_2011 PARTITION OF mytable
FOR VALUES FROM ('2010-01-01') TO ('2011-01-01');
-- 3 more partitions
CREATE TABLE mytable_2014_2015 PARTITION OF mytable
FOR VALUES FROM ('2014-01-01') TO ('2015-01-01');

-- Everything before 2010
CREATE TABLE mytable_pre2010 PARTITION OF mytable
FOR VALUES FROM ('1500-01-01') TO ('2010-01-01');

Is there anyway to create that last partition to hold everything before 2010 without setting a hardcoded date. Something like FOR VALUES BEFORE ('2010-01-01');


Solution

  • You can use -infinity for that:

    -- Everything before 2010
    CREATE TABLE mytable_pre2010 PARTITION OF mytable
    FOR VALUES FROM ('-infinity') TO ('2010-01-01');