mirror of
https://github.com/postgres/postgres.git
synced 2025-04-25 21:42:33 +03:00
Fix heap_getattr() handling of fast defaults.
Previously heap_getattr() returned NULL for attributes with a fast default value (c.f. 16828d5c0273), as it had no handling whatsoever for that case. A previous fix, 7636e5c60f, attempted to fix issues caused by this oversight, but just expanding OLD tuples for triggers doesn't actually solve the underlying issue. One known consequence of this bug is that the check for HOT updates can return the wrong result, when a previously fast-default'ed column is set to NULL. Which in turn means that an index over a column with fast default'ed columns might be corrupt if the underlying column(s) allow NULLs. Fix by handling fast default columns in heap_getattr(), remove now superfluous expansion in GetTupleForTrigger(). Author: Andres Freund Discussion: https://postgr.es/m/20190201162404.onngi77f26baem4g@alap3.anarazel.de Backpatch: 11, where fast defaults were introduced
This commit is contained in:
parent
d07fb6810e
commit
171e0418b0
@ -71,8 +71,6 @@
|
|||||||
#define VARLENA_ATT_IS_PACKABLE(att) \
|
#define VARLENA_ATT_IS_PACKABLE(att) \
|
||||||
((att)->attstorage != 'p')
|
((att)->attstorage != 'p')
|
||||||
|
|
||||||
static Datum getmissingattr(TupleDesc tupleDesc, int attnum, bool *isnull);
|
|
||||||
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------
|
/* ----------------------------------------------------------------
|
||||||
* misc support routines
|
* misc support routines
|
||||||
@ -82,7 +80,7 @@ static Datum getmissingattr(TupleDesc tupleDesc, int attnum, bool *isnull);
|
|||||||
/*
|
/*
|
||||||
* Return the missing value of an attribute, or NULL if there isn't one.
|
* Return the missing value of an attribute, or NULL if there isn't one.
|
||||||
*/
|
*/
|
||||||
static Datum
|
Datum
|
||||||
getmissingattr(TupleDesc tupleDesc,
|
getmissingattr(TupleDesc tupleDesc,
|
||||||
int attnum, bool *isnull)
|
int attnum, bool *isnull)
|
||||||
{
|
{
|
||||||
|
@ -3412,10 +3412,7 @@ ltrmark:;
|
|||||||
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (HeapTupleHeaderGetNatts(tuple.t_data) < relation->rd_att->natts)
|
result = heap_copytuple(&tuple);
|
||||||
result = heap_expand_tuple(&tuple, relation->rd_att);
|
|
||||||
else
|
|
||||||
result = heap_copytuple(&tuple);
|
|
||||||
ReleaseBuffer(buffer);
|
ReleaseBuffer(buffer);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -764,10 +764,7 @@ extern Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
|
|||||||
((attnum) > 0) ? \
|
((attnum) > 0) ? \
|
||||||
( \
|
( \
|
||||||
((attnum) > (int) HeapTupleHeaderGetNatts((tup)->t_data)) ? \
|
((attnum) > (int) HeapTupleHeaderGetNatts((tup)->t_data)) ? \
|
||||||
( \
|
getmissingattr((tupleDesc), (attnum), (isnull)) \
|
||||||
(*(isnull) = true), \
|
|
||||||
(Datum)NULL \
|
|
||||||
) \
|
|
||||||
: \
|
: \
|
||||||
fastgetattr((tup), (attnum), (tupleDesc), (isnull)) \
|
fastgetattr((tup), (attnum), (tupleDesc), (isnull)) \
|
||||||
) \
|
) \
|
||||||
@ -788,6 +785,8 @@ extern Datum nocachegetattr(HeapTuple tup, int attnum,
|
|||||||
TupleDesc att);
|
TupleDesc att);
|
||||||
extern Datum heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
|
extern Datum heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
|
||||||
bool *isnull);
|
bool *isnull);
|
||||||
|
extern Datum getmissingattr(TupleDesc tupleDesc,
|
||||||
|
int attnum, bool *isnull);
|
||||||
extern HeapTuple heap_copytuple(HeapTuple tuple);
|
extern HeapTuple heap_copytuple(HeapTuple tuple);
|
||||||
extern void heap_copytuple_with_tuple(HeapTuple src, HeapTuple dest);
|
extern void heap_copytuple_with_tuple(HeapTuple src, HeapTuple dest);
|
||||||
extern Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc);
|
extern Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc);
|
||||||
|
@ -770,6 +770,33 @@ SELECT * FROM vtype2;
|
|||||||
2 | yyy
|
2 | yyy
|
||||||
(2 rows)
|
(2 rows)
|
||||||
|
|
||||||
|
-- Ensure that defaults are checked when evaluating whether HOT update
|
||||||
|
-- is possible, this was broken for a while:
|
||||||
|
-- https://postgr.es/m/20190202133521.ylauh3ckqa7colzj%40alap3.anarazel.de
|
||||||
|
BEGIN;
|
||||||
|
CREATE TABLE t();
|
||||||
|
INSERT INTO t DEFAULT VALUES;
|
||||||
|
ALTER TABLE t ADD COLUMN a int DEFAULT 1;
|
||||||
|
CREATE INDEX ON t(a);
|
||||||
|
-- set column with a default 1 to NULL, due to a bug that wasn't
|
||||||
|
-- noticed has heap_getattr buggily returned NULL for default columns
|
||||||
|
UPDATE t SET a = NULL;
|
||||||
|
-- verify that index and non-index scans show the same result
|
||||||
|
SET LOCAL enable_seqscan = true;
|
||||||
|
SELECT * FROM t WHERE a IS NULL;
|
||||||
|
a
|
||||||
|
---
|
||||||
|
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
SET LOCAL enable_seqscan = false;
|
||||||
|
SELECT * FROM t WHERE a IS NULL;
|
||||||
|
a
|
||||||
|
---
|
||||||
|
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
ROLLBACK;
|
||||||
-- cleanup
|
-- cleanup
|
||||||
DROP TABLE vtype;
|
DROP TABLE vtype;
|
||||||
DROP TABLE vtype2;
|
DROP TABLE vtype2;
|
||||||
|
@ -505,6 +505,26 @@ ALTER TABLE vtype2 ALTER COLUMN b TYPE varchar(20) USING b::varchar(20);
|
|||||||
SELECT * FROM vtype2;
|
SELECT * FROM vtype2;
|
||||||
|
|
||||||
|
|
||||||
|
-- Ensure that defaults are checked when evaluating whether HOT update
|
||||||
|
-- is possible, this was broken for a while:
|
||||||
|
-- https://postgr.es/m/20190202133521.ylauh3ckqa7colzj%40alap3.anarazel.de
|
||||||
|
BEGIN;
|
||||||
|
CREATE TABLE t();
|
||||||
|
INSERT INTO t DEFAULT VALUES;
|
||||||
|
ALTER TABLE t ADD COLUMN a int DEFAULT 1;
|
||||||
|
CREATE INDEX ON t(a);
|
||||||
|
-- set column with a default 1 to NULL, due to a bug that wasn't
|
||||||
|
-- noticed has heap_getattr buggily returned NULL for default columns
|
||||||
|
UPDATE t SET a = NULL;
|
||||||
|
|
||||||
|
-- verify that index and non-index scans show the same result
|
||||||
|
SET LOCAL enable_seqscan = true;
|
||||||
|
SELECT * FROM t WHERE a IS NULL;
|
||||||
|
SET LOCAL enable_seqscan = false;
|
||||||
|
SELECT * FROM t WHERE a IS NULL;
|
||||||
|
ROLLBACK;
|
||||||
|
|
||||||
|
|
||||||
-- cleanup
|
-- cleanup
|
||||||
DROP TABLE vtype;
|
DROP TABLE vtype;
|
||||||
DROP TABLE vtype2;
|
DROP TABLE vtype2;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user