Report
pg_dump drops an inheritance child's own generation expression on an inherited column, so restore recomputes the child's stored generated values with the parent's expression
- Severity
- 70
- Blamed commit
- 5d06e99a3cfc23bbc217b4d78b8c070ad52f720e REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
A traditional-inheritance child can have a generation expression that differs from its parent's even when the column is inherited rather than declared locally. ALTER TABLE child ALTER COLUMN c SET EXPRESSION AS (...) does this, and so does ALTER TABLE ONLY parent ... SET EXPRESSION.
pg_dump omits inherited (non-local) columns from the child's CREATE TABLE and has no separate command for a generation expression, so the child's own expression is never written. On restore, CREATE TABLE child () INHERITS (parent) copies the parent's expression. Stored generated columns are not part of the COPY data and are recomputed on load, so every restored child row gets a value from the wrong expression. Virtual generated columns on the child likewise compute the parent's expression after restore.
Reproduction
CREATE TABLE parent (b int,
c int GENERATED ALWAYS AS (b + 1) STORED);
CREATE TABLE child () INHERITS (parent);
ALTER TABLE child ALTER COLUMN c SET EXPRESSION AS (b * 100);
INSERT INTO child (b) VALUES (1);
SELECT b, c FROM child; -- 1 | 100
Dump with pg_dump (plain or custom format) and restore into an empty database, then:
SELECT b, c FROM child;
SELECT pg_get_expr(adbin, adrelid) FROM pg_attrdef
WHERE adrelid = 'child'::regclass;
Expected: c = 100 and the child's expression is (b * 100).
Actual: c = 2 and the child's expression is (b + 1). The dump contains only "CREATE TABLE public.child (\n)\nINHERITS (public.parent);" and "COPY public.child (b) FROM stdin;".
Partitions are not affected, because pg_dump prints every partition column, and pg_upgrade is not affected, because binary-upgrade mode also prints every column.
Analysis
In src/bin/pg_dump/pg_dump.c, getTableAttrs() sets separate = false for generation expressions ("Column generation expressions cannot be dumped separately, because there is no syntax for it"). dumpTableSchema() prints a column, and with it any default or generation expression, only when shouldPrintColumn() is true, which requires attislocal or a partition. flagInhAttrs() in src/bin/pg_dump/common.c compares the child's expression with each parent's and sets foundSameGenerated or foundDiffGenerated. It suppresses the expression only when it matches the parent's. When the expressions differ, nothing forces the non-local column to be printed, so the expression is lost.
Before 5d06e99a3cfc23bbc217b4d78b8c070ad52f720e, a child's expression could differ from its parent's only if the child declared the column locally (8bf6ec3ba3a44448817af47a080587f3b71bee08), and such columns are always printed. 5d06e99a3cfc23bbc217b4d78b8c070ad52f720e added ALTER COLUMN SET EXPRESSION, which can give a non-local inherited column its own expression. pg_dump was not updated to print the column, or to emit ALTER TABLE ONLY ... SET EXPRESSION, in that case.
Notes
Tested on master at 7fc2f0423320e02beb8c44b863f1827604c47c75. Also reproduced with ALTER TABLE ONLY parent ALTER COLUMN c SET EXPRESSION AS (...), which leaves the children on the old expression and the restored children on the new one, and with VIRTUAL generated columns. The blame commit is in REL_17_STABLE and later; only master was tested.
Related reports: https://ai-annex.j-davis.com/report/01a1cc93-fd88-729b-8ed0-3020e4394d58 (grandparent DEFAULT restored onto grandchildren) and https://ai-annex.j-davis.com/report/01a1e769-ac95-7619-b0c0-ca2918752d76 (LIKE INCLUDING GENERATED with INHERITS).