Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit171e041

Browse files
committed
Fix heap_getattr() handling of fast defaults.
Previously heap_getattr() returned NULL for attributes with a fastdefault value (c.f.16828d5), as it had no handling whatsoeverfor that case.A previous fix,7636e5c, attempted to fix issues caused by thisoversight, but just expanding OLD tuples for triggers doesn't actuallysolve the underlying issue.One known consequence of this bug is that the check for HOT updatescan return the wrong result, when a previously fast-default'ed columnis set to NULL. Which in turn means that an index over a column withfast default'ed columns might be corrupt if the underlying column(s)allow NULLs.Fix by handling fast default columns in heap_getattr(), remove nowsuperfluous expansion in GetTupleForTrigger().Author: Andres FreundDiscussion:https://postgr.es/m/20190201162404.onngi77f26baem4g@alap3.anarazel.deBackpatch: 11, where fast defaults were introduced
1 parentd07fb68 commit171e041

File tree

5 files changed

+52
-11
lines changed

5 files changed

+52
-11
lines changed

‎src/backend/access/common/heaptuple.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@
7171
#defineVARLENA_ATT_IS_PACKABLE(att) \
7272
((att)->attstorage != 'p')
7373

74-
staticDatumgetmissingattr(TupleDesctupleDesc,intattnum,bool*isnull);
75-
7674

7775
/* ----------------------------------------------------------------
7876
*misc support routines
@@ -82,7 +80,7 @@ static Datum getmissingattr(TupleDesc tupleDesc, int attnum, bool *isnull);
8280
/*
8381
* Return the missing value of an attribute, or NULL if there isn't one.
8482
*/
85-
staticDatum
83+
Datum
8684
getmissingattr(TupleDesctupleDesc,
8785
intattnum,bool*isnull)
8886
{

‎src/backend/commands/trigger.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3412,10 +3412,7 @@ ltrmark:;
34123412
LockBuffer(buffer,BUFFER_LOCK_UNLOCK);
34133413
}
34143414

3415-
if (HeapTupleHeaderGetNatts(tuple.t_data)<relation->rd_att->natts)
3416-
result=heap_expand_tuple(&tuple,relation->rd_att);
3417-
else
3418-
result=heap_copytuple(&tuple);
3415+
result=heap_copytuple(&tuple);
34193416
ReleaseBuffer(buffer);
34203417

34213418
returnresult;

‎src/include/access/htup_details.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -764,10 +764,7 @@ extern Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
764764
((attnum) > 0) ? \
765765
( \
766766
((attnum) > (int) HeapTupleHeaderGetNatts((tup)->t_data)) ? \
767-
( \
768-
(*(isnull) = true), \
769-
(Datum)NULL \
770-
) \
767+
getmissingattr((tupleDesc), (attnum), (isnull)) \
771768
: \
772769
fastgetattr((tup), (attnum), (tupleDesc), (isnull)) \
773770
) \
@@ -788,6 +785,8 @@ extern Datum nocachegetattr(HeapTuple tup, int attnum,
788785
TupleDescatt);
789786
externDatumheap_getsysattr(HeapTupletup,intattnum,TupleDesctupleDesc,
790787
bool*isnull);
788+
externDatumgetmissingattr(TupleDesctupleDesc,
789+
intattnum,bool*isnull);
791790
externHeapTupleheap_copytuple(HeapTupletuple);
792791
externvoidheap_copytuple_with_tuple(HeapTuplesrc,HeapTupledest);
793792
externDatumheap_copy_tuple_as_datum(HeapTupletuple,TupleDesctupleDesc);

‎src/test/regress/expected/fast_default.out

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,33 @@ SELECT * FROM vtype2;
770770
2 | yyy
771771
(2 rows)
772772

773+
-- Ensure that defaults are checked when evaluating whether HOT update
774+
-- is possible, this was broken for a while:
775+
-- https://postgr.es/m/20190202133521.ylauh3ckqa7colzj%40alap3.anarazel.de
776+
BEGIN;
777+
CREATE TABLE t();
778+
INSERT INTO t DEFAULT VALUES;
779+
ALTER TABLE t ADD COLUMN a int DEFAULT 1;
780+
CREATE INDEX ON t(a);
781+
-- set column with a default 1 to NULL, due to a bug that wasn't
782+
-- noticed has heap_getattr buggily returned NULL for default columns
783+
UPDATE t SET a = NULL;
784+
-- verify that index and non-index scans show the same result
785+
SET LOCAL enable_seqscan = true;
786+
SELECT * FROM t WHERE a IS NULL;
787+
a
788+
---
789+
790+
(1 row)
791+
792+
SET LOCAL enable_seqscan = false;
793+
SELECT * FROM t WHERE a IS NULL;
794+
a
795+
---
796+
797+
(1 row)
798+
799+
ROLLBACK;
773800
-- cleanup
774801
DROP TABLE vtype;
775802
DROP TABLE vtype2;

‎src/test/regress/sql/fast_default.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,26 @@ ALTER TABLE vtype2 ALTER COLUMN b TYPE varchar(20) USING b::varchar(20);
505505
SELECT*FROM vtype2;
506506

507507

508+
-- Ensure that defaults are checked when evaluating whether HOT update
509+
-- is possible, this was broken for a while:
510+
-- https://postgr.es/m/20190202133521.ylauh3ckqa7colzj%40alap3.anarazel.de
511+
BEGIN;
512+
CREATETABLEt();
513+
INSERT INTO t DEFAULTVALUES;
514+
ALTERTABLE t ADD COLUMN aint DEFAULT1;
515+
CREATEINDEXON t(a);
516+
-- set column with a default 1 to NULL, due to a bug that wasn't
517+
-- noticed has heap_getattr buggily returned NULL for default columns
518+
UPDATE tSET a=NULL;
519+
520+
-- verify that index and non-index scans show the same result
521+
SET LOCAL enable_seqscan= true;
522+
SELECT*FROM tWHERE a ISNULL;
523+
SET LOCAL enable_seqscan= false;
524+
SELECT*FROM tWHERE a ISNULL;
525+
ROLLBACK;
526+
527+
508528
-- cleanup
509529
DROPTABLE vtype;
510530
DROPTABLE vtype2;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp