diff options
author | Andrew Dunstan | 2018-03-28 00:13:52 +0000 |
---|---|---|
committer | Andrew Dunstan | 2018-03-28 00:13:52 +0000 |
commit | 16828d5c0273b4fe5f10f42588005f16b415b2d8 (patch) | |
tree | bd7f858e309016473b7dac879f74ebe954fd8dad /src/include/access/htup_details.h | |
parent | ef1978d6ed1e4defe18d250226460409e6cd5447 (diff) |
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/[email protected]
Diffstat (limited to 'src/include/access/htup_details.h')
-rw-r--r-- | src/include/access/htup_details.h | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/src/include/access/htup_details.h b/src/include/access/htup_details.h index 67342ef63dc..cebaea097d1 100644 --- a/src/include/access/htup_details.h +++ b/src/include/access/htup_details.h @@ -799,7 +799,7 @@ extern void heap_fill_tuple(TupleDesc tupleDesc, Datum *values, bool *isnull, char *data, Size data_size, uint16 *infomask, bits8 *bit); -extern bool heap_attisnull(HeapTuple tup, int attnum); +extern bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc); extern Datum nocachegetattr(HeapTuple tup, int attnum, TupleDesc att); extern Datum heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, @@ -830,5 +830,7 @@ extern MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup); extern HeapTuple heap_tuple_from_minimal_tuple(MinimalTuple mtup); extern MinimalTuple minimal_tuple_from_heap_tuple(HeapTuple htup); extern size_t varsize_any(void *p); +extern HeapTuple heap_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc); +extern MinimalTuple minimal_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc); #endif /* HTUP_DETAILS_H */ |