Re: partitions vs indexes

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Enrico Thierbach <eno(at)open-lab(dot)org>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: partitions vs indexes
Date: 2019-10-03 11:34:18
Message-ID: 3f6a6a4fdc13d825c85f48e89e76757c972e1391.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 2019-10-02 at 22:09 +0200, Enrico Thierbach wrote:
> would like to convert a table with a primary key into a partitioned
> setup by a column which is not part of the primary key. Also, a
> column might hold a referenece to a parent row. So this is my current
> table setup, slimmed down:
>
> CREATE TYPE statuses AS ENUM ('ready', ‘processing’, ‘done’);
>
> CREATE TABLE mytable (
> id BIGSERIAL PRIMARY KEY NOT NULL,
> parent_id BIGINT REFERENCES mytable(id) ON DELETE CASCADE,
> status statuses DEFAULT 'ready'
> );
> Since entries in the table are often frequented when status is not
> ‘done’ I would like to partition by state.

Have you considered using partial indexes?

CREATE INDEX ..... WHERE status <> 'done'.

Such indexes will be smaller, and any query where "status" is in
the WHERE clause can use the index.

If partitoining is really what you need, you could create the primary
key on the partitions and not on the partitioned table.

That won't guarantee you global uniqueness, but you could rely on
a sequence to do that.

Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Laurenz Albe 2019-10-03 11:41:16 Re: Urgent :: Postgresql streaming replication issue - sync mode
Previous Message Laurenz Albe 2019-10-03 11:20:20 Re: Advice for geographically dispersed multi master