I would like to partition a table in Postgres by previously unknown value. In my scenario that value would be device_id which is a string.
This is current situation:
Table 'device_data' - stores sensor data which is sent from devices, defined by DDL:
CREATE TABLE warehouse.device_data (
id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('device_data_id_seq'::regclass),
device_id TEXT NOT NULL,
device_data BYTEA NOT NULL,
-- contains additional fields which are omitted for brevity
received_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now()
);
Table currently holds millions of records and queries are taking huge amount of time. Most of queries contain WHERE device_id='something' clause.
Solution I have in mind is to create table partitions for each device_id.
Is it possible in Postgres to create table partitions for each device_id?
I went through Postgres documentation and couple of examples I found but all of them use fixed boundaries to create partitions. My solution would require:
- create new table partition on the fly when new
device_idis first encountered - store to an existing partition if the
device_idis already known and partition for thatdevice_idalready exist
I would like this to be done using table partitions as it would allow querying across multiple device_ids.
device_id? a simple index fordevice_idwill do the job. Performance questions should includeEXPLAIN ANALYZEand some information about table size, index, current time performance, desire time, etc.Slowis a relative term and we need a real value to compare.pg_pathman. It is a tool to simplify partition management under PostgreSQL and it specifically supports aHASHpartitioning strategy. The drawback is that the number of partitions is fixed, and chosen when you initialize your partitions set.