From: Andrew Dunstan <andrew@dunslane.net>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fast ALTER TABLE ADD COLUMN with a non-NULL default
Date: Wed, 28 Mar 2018 00:56:10 +0000
Message-ID: <E1f0zNq-00064z-Sn@gemulon.postgresql.org> (raw)
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(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: andrew@dunslane.net
Subject: Re: pgsql: Fast ALTER TABLE ADD COLUMN with a non-NULL default
In-Reply-To: <E1f0zNq-00064z-Sn@gemulon.postgresql.org>
References: <E1f0zNq-00064z-Sn@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox