2

With Postgres native partitioning, can you add extra columns to a partition of the base table? Like:

CREATE TABLE item (
  tenant_id bigint NOT NULL,
  item_id   bigint NOT NULL,
  kind      text NOT NULL CHECK ( kind IN ('product', 'ingredient') ),
  PRIMARY KEY (tenant_id, item_id)
) PARTITION BY LIST (kind);

CREATE TABLE item_product PARTITION OF item (
  extra_sku TEXT
) FOR VALUES IN ('product');

CREATE TABLE item_ingredient PARTITION OF item
FOR VALUES IN ('ingredient');

The CREATE TABLE syntax doesn't seem to support adding columns on child tables, only adding constraints to existing columns, so I'm guessing it's not allowed.

1 Answer 1

1

After reading the docs in the course of putting together this question, the answer is it's not possible to add columns to partitions that don't exactly match the columns of the parent table. From the docs on DDL Partitioning Limitations:

Partitions cannot have columns that are not present in the parent. It is neither possible to specify columns when creating partitions with CREATE TABLE nor is it possible to add columns to partitions after-the-fact using ALTER TABLE. Tables may be added as a partition with ALTER TABLE ... ATTACH PARTITION only if their columns exactly match the parent, including any oid column.

However, it's possible to use table inheritance to accomplish this as the same doc notes:

However, it is not possible to use all of the generic features of inheritance with declaratively partitioned tables or their partitions, as discussed below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.