pgsql: Fast ALTER TABLE ADD COLUMN with a non-NULL default

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fast ALTER TABLE ADD COLUMN with a non-NULL default
Date: 2018-03-28 00:56:10
Message-ID: E1f0zNq-00064z-Sn@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/16828d5c0273b4fe5f10f42588005f16b415b2d8

Modified Files
--------------
doc/src/sgml/catalogs.sgml | 27 ++
doc/src/sgml/ref/alter_table.sgml | 34 +-
src/backend/access/common/heaptuple.c | 629 ++++++++++++++++++++++------
src/backend/access/common/tupdesc.c | 60 +++
src/backend/catalog/aclchk.c | 2 +-
src/backend/catalog/heap.c | 171 +++++++-
src/backend/catalog/index.c | 4 +-
src/backend/commands/cluster.c | 12 +-
src/backend/commands/functioncmds.c | 2 +-
src/backend/commands/indexcmds.c | 4 +-
src/backend/commands/tablecmds.c | 51 ++-
src/backend/commands/typecmds.c | 2 +-
src/backend/executor/execExprInterp.c | 2 +-
src/backend/executor/execMain.c | 13 +-
src/backend/executor/execTuples.c | 28 +-
src/backend/executor/execUtils.c | 2 +
src/backend/optimizer/util/clauses.c | 4 +-
src/backend/optimizer/util/plancat.c | 8 +-
src/backend/rewrite/rewriteHandler.c | 3 +-
src/backend/statistics/extended_stats.c | 2 +-
src/backend/utils/adt/ri_triggers.c | 29 +-
src/backend/utils/adt/ruleutils.c | 6 +-
src/backend/utils/cache/relcache.c | 82 +++-
src/backend/utils/fmgr/fmgr.c | 4 +-
src/backend/utils/fmgr/funcapi.c | 8 +-
src/include/access/htup_details.h | 4 +-
src/include/access/tupdesc.h | 3 +
src/include/access/tupdesc_details.h | 29 ++
src/include/catalog/heap.h | 6 +-
src/include/catalog/pg_attribute.h | 30 +-
src/include/catalog/pg_class.h | 2 +-
src/test/regress/expected/event_trigger.out | 4 +-
src/test/regress/expected/fast_default.out | 515 +++++++++++++++++++++++
src/test/regress/parallel_schedule | 2 +-
src/test/regress/serial_schedule | 1 +
src/test/regress/sql/fast_default.sql | 357 ++++++++++++++++
36 files changed, 1898 insertions(+), 244 deletions(-)

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Andres Freund 2018-03-28 01:00:53 Re: pgsql: Fast ALTER TABLE ADD COLUMN with a non-NULL default
Previous Message Tom Lane 2018-03-27 22:15:44 pgsql: Update pgindent's typedefs blacklist, and make it easier to adju