]> granicus.if.org Git - postgresql/commit
Fast ALTER TABLE ADD COLUMN with a non-NULL default
authorAndrew Dunstan <andrew@dunslane.net>
Wed, 28 Mar 2018 00:13:52 +0000 (10:43 +1030)
committerAndrew Dunstan <andrew@dunslane.net>
Wed, 28 Mar 2018 00:13:52 +0000 (10:43 +1030)
commit16828d5c0273b4fe5f10f42588005f16b415b2d8
treebd7f858e309016473b7dac879f74ebe954fd8dad
parentef1978d6ed1e4defe18d250226460409e6cd5447
Fast ALTER TABLE ADD COLUMN with a non-NULL default

Currently adding a column to a table with a non-NULL default results in
a rewrite of the table. For large tables this can be both expensive and
disruptive. This patch removes the need for the rewrite as long as the
default value is not volatile. The default expression is evaluated at
the time of the ALTER TABLE and the result stored in a new column
(attmissingval) in pg_attribute, and a new column (atthasmissing) is set
to true. Any existing row when fetched will be supplied with the
attmissingval. New rows will have the supplied value or the default and
so will never need the attmissingval.

Any time the table is rewritten all the atthasmissing and attmissingval
settings for the attributes are cleared, as they are no longer needed.

The most visible code change from this is in heap_attisnull, which
acquires a third TupleDesc argument, allowing it to detect a missing
value if there is one. In many cases where it is known that there will
not be any (e.g.  catalog relations) NULL can be passed for this
argument.

Andrew Dunstan, heavily modified from an original patch from Serge
Rielau.
Reviewed by Tom Lane, Andres Freund, Tomas Vondra and David Rowley.

Discussion: https://postgr.es/m/31e2e921-7002-4c27-59f5-51f08404c858@2ndQuadrant.com
36 files changed:
doc/src/sgml/catalogs.sgml
doc/src/sgml/ref/alter_table.sgml
src/backend/access/common/heaptuple.c
src/backend/access/common/tupdesc.c
src/backend/catalog/aclchk.c
src/backend/catalog/heap.c
src/backend/catalog/index.c
src/backend/commands/cluster.c
src/backend/commands/functioncmds.c
src/backend/commands/indexcmds.c
src/backend/commands/tablecmds.c
src/backend/commands/typecmds.c
src/backend/executor/execExprInterp.c
src/backend/executor/execMain.c
src/backend/executor/execTuples.c
src/backend/executor/execUtils.c
src/backend/optimizer/util/clauses.c
src/backend/optimizer/util/plancat.c
src/backend/rewrite/rewriteHandler.c
src/backend/statistics/extended_stats.c
src/backend/utils/adt/ri_triggers.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/cache/relcache.c
src/backend/utils/fmgr/fmgr.c
src/backend/utils/fmgr/funcapi.c
src/include/access/htup_details.h
src/include/access/tupdesc.h
src/include/access/tupdesc_details.h [new file with mode: 0644]
src/include/catalog/heap.h
src/include/catalog/pg_attribute.h
src/include/catalog/pg_class.h
src/test/regress/expected/event_trigger.out
src/test/regress/expected/fast_default.out [new file with mode: 0644]
src/test/regress/parallel_schedule
src/test/regress/serial_schedule
src/test/regress/sql/fast_default.sql [new file with mode: 0644]