postgresql/src/backend/commands
Andrew Dunstan 16828d5c02 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
2018-03-28 10:43:52 +10:30
..
Makefile Implement multivariate n-distinct coefficients 2017-03-24 14:06:10 -03:00
aggregatecmds.c Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00
alter.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
amcmds.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
analyze.c Improve ANALYZE's strategy for finding MCVs. 2018-03-22 09:37:36 +00:00
async.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
cluster.c Fast ALTER TABLE ADD COLUMN with a non-NULL default 2018-03-28 10:43:52 +10:30
collationcmds.c Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00
comment.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
constraint.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
conversioncmds.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
copy.c Fix state reversal after partition tuple routing 2018-03-19 17:45:53 -03:00
createas.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
dbcommands.c Rename TransactionChain functions 2018-03-16 13:18:06 -04:00
define.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
discard.c Rename TransactionChain functions 2018-03-16 13:18:06 -04:00
dropcmds.c Add prokind column, replacing proisagg and proiswindow 2018-03-02 13:48:33 -05:00
event_trigger.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
explain.c Add 'unit' parameter to ExplainProperty{Integer,Float}. 2018-03-16 23:16:04 -07:00
extension.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
foreigncmds.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
functioncmds.c Fast ALTER TABLE ADD COLUMN with a non-NULL default 2018-03-28 10:43:52 +10:30
indexcmds.c Fast ALTER TABLE ADD COLUMN with a non-NULL default 2018-03-28 10:43:52 +10:30
lockcmds.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
matview.c Fix some corner-case issues in REFRESH MATERIALIZED VIEW CONCURRENTLY. 2018-03-19 18:50:05 -04:00
opclasscmds.c Support all SQL:2011 options for window frame clauses. 2018-02-07 00:06:56 -05:00
operatorcmds.c Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00
policy.c Allow memory contexts to have both fixed and variable ident strings. 2018-03-27 16:46:51 -04:00
portalcmds.c Rename TransactionChain functions 2018-03-16 13:18:06 -04:00
prepare.c Update portal-related memory context names and API 2018-01-09 13:47:56 -05:00
proclang.c Add prokind column, replacing proisagg and proiswindow 2018-03-02 13:48:33 -05:00
publicationcmds.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
schemacmds.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
seclabel.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
sequence.c Fix CREATE TABLE / LIKE with bigint identity column 2018-03-13 09:41:30 -04:00
statscmds.c Fix bogus Name assignment in CreateStatistics 2018-03-06 13:20:40 -03:00
subscriptioncmds.c Rename TransactionChain functions 2018-03-16 13:18:06 -04:00
tablecmds.c Fast ALTER TABLE ADD COLUMN with a non-NULL default 2018-03-28 10:43:52 +10:30
tablespace.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
trigger.c Allow FOR EACH ROW triggers on partitioned tables 2018-03-23 10:48:22 -03:00
tsearchcmds.c Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00
typecmds.c Fast ALTER TABLE ADD COLUMN with a non-NULL default 2018-03-28 10:43:52 +10:30
user.c Replace AclObjectKind with ObjectType 2018-01-19 14:01:15 -05:00
vacuum.c Sync up our various ways of estimating pg_class.reltuples. 2018-03-22 15:47:41 -04:00
vacuumlazy.c Sync up our various ways of estimating pg_class.reltuples. 2018-03-22 15:47:41 -04:00
variable.c Update copyright for 2018 2018-01-02 23:30:12 -05:00
view.c Avoid unnecessary use of pg_strcasecmp for already-downcased identifiers. 2018-01-26 18:25:14 -05:00