質問

My PostgreSQL database has table with entities which can be active and inactive - it's determined by isActive column value. Inactive entities are accessed very rarely, and, as database grows, "inactive to active" rate becomes very high for the database. So I expect partitioning based on simple isActive check to bring huge performance outcome.

The problem is, the table is referenced by foreign key constraint from many other tables. As specified in the last bullet of Caveats section of PostgreSQL Inheritance doc, there is no good workaround for this case.

So, is it true that currently partitioning in PostgreSQL is only suitable for the simple cases when the table partitioned is not referenced from anywhere? Are there any other ways to go and optimize performance of queries to the table I described above? I'm pretty sure my use case is common and there should be good solution for that.

Example of queries to create the tables:

CREATE TABLE resources
(
  id uuid NOT NULL,
  isActive integer NOT NULL, -- 0 means false, anything else is true, I intentionally do not use boolean type
  PRIMARY KEY (id)
);

CREATE TABLE resource_attributes
(
  id uuid NOT NULL,
  resourceId uuid NOT NULL,
  name character varying(128) NOT NULL,
  value character varying(1024) DEFAULT NULL,
  PRIMARY KEY (id),
  CONSTRAINT fk_resource_attributes_resourceid_resources_id FOREIGN KEY (resourceId) REFERENCES resources (id)
);

In this case, I'd like to partition resources table.

役に立ちましたか?

解決

If the inactive to active ratio is very high a partial index is a good choice

create index index_name on resources (isActive) where isActive = 1

他のヒント

The only known workaround (i can think of) to create a foreign key for a table which has multiple child tables is to create another table to hold just the primary keys (but all of them, maintained by triggers) and point all foreign key references to it.

like:

+-----------+     +----------------+     +---------------------+
| resources |     | resource_uuids |     |   resource_part_n   |
+===========+ 0 1 +================+ 1 0 +=====================+
|    id     | --> |       id       | <-- | (id from resources) |
+-----------+     +----------------+     +---------------------+
|    ...    |              ↑ 1           |     CHECK(...)      |
+-----------+              +--------+    +---------------------+
                                    |    | INHERITS(resources) |
          +---------------------+   |    +---------------------+
          | resource_attributes |   |
          +---------------------+   |
          |     resourceId      | --+ *
          +---------------------+
          |         ...         |
          +---------------------+

But you still can't partition that table (resource_uuids), so i don't think partitioning will help you in this case.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top