Skip to content

Commit caa231b

Browse files
author
Amit Kapila
committed
WAL log unchanged toasted replica identity key attributes.
Currently, during UPDATE, the unchanged replica identity key attributes are not logged separately because they are getting logged as part of the new tuple. But if they are stored externally then the untoasted values are not getting logged as part of the new tuple and logical replication won't be able to replicate such UPDATEs. So we need to log such attributes as part of the old_key_tuple during UPDATE. Reported-by: Haiying Tang Author: Dilip Kumar and Amit Kapila Reviewed-by: Alvaro Herrera, Haiying Tang, Andres Freund Backpatch-through: 10 Discussion: https://postgr.es/m/OS0PR01MB611342D0A92D4F4BF26C0F47FB229@OS0PR01MB6113.jpnprd01.prod.outlook.com
1 parent ac2303a commit caa231b

File tree

3 files changed

+123
-69
lines changed

3 files changed

+123
-69
lines changed

contrib/test_decoding/expected/toast.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ SELECT substr(data, 1, 200) FROM pg_logical_slot_get_changes('regression_slot',
7777
table public.toasted_key: INSERT: id[integer]:1 toasted_key[text]:'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123
7878
COMMIT
7979
BEGIN
80-
table public.toasted_key: UPDATE: id[integer]:1 toasted_key[text]:unchanged-toast-datum toasted_col1[text]:unchanged-toast-datum toasted_col2[text]:'987654321098765432109876543210987654321098765432109
80+
table public.toasted_key: UPDATE: old-key: toasted_key[text]:'123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
8181
COMMIT
8282
BEGIN
8383
table public.toasted_key: UPDATE: old-key: toasted_key[text]:'123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678

doc/src/sgml/ref/alter_table.sgml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,11 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
819819
<listitem>
820820
<para>
821821
This form changes the information which is written to the write-ahead log
822-
to identify rows which are updated or deleted. This option has no effect
823-
except when logical replication is in use.
824-
In all cases, no old values are logged unless at least one of the columns
825-
that would be logged differs between the old and new versions of the row.
822+
to identify rows which are updated or deleted.
823+
In most cases, the old value of each column is only logged if it differs
824+
from the new value; however, if the old value is stored externally, it is
825+
always logged regardless of whether it changed.
826+
This option has no effect except when logical replication is in use.
826827
<variablelist>
827828
<varlistentry>
828829
<term><literal>DEFAULT</literal></term>

src/backend/access/heap/heapam.c

Lines changed: 117 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ static XLogRecPtr log_heap_update(Relation reln, Buffer oldbuf,
7676
Buffer newbuf, HeapTuple oldtup,
7777
HeapTuple newtup, HeapTuple old_key_tuple,
7878
bool all_visible_cleared, bool new_all_visible_cleared);
79-
static Bitmapset *HeapDetermineModifiedColumns(Relation relation,
80-
Bitmapset *interesting_cols,
81-
HeapTuple oldtup, HeapTuple newtup);
79+
static Bitmapset *HeapDetermineColumnsInfo(Relation relation,
80+
Bitmapset *interesting_cols,
81+
Bitmapset *external_cols,
82+
HeapTuple oldtup, HeapTuple newtup,
83+
bool *has_external);
8284
static bool heap_acquire_tuplock(Relation relation, ItemPointer tid,
8385
LockTupleMode mode, LockWaitPolicy wait_policy,
8486
bool *have_tuple_lock);
@@ -102,7 +104,7 @@ static void MultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 in
102104
static bool ConditionalMultiXactIdWait(MultiXactId multi, MultiXactStatus status,
103105
uint16 infomask, Relation rel, int *remaining);
104106
static XLogRecPtr log_heap_new_cid(Relation relation, HeapTuple tup);
105-
static HeapTuple ExtractReplicaIdentity(Relation rel, HeapTuple tup, bool key_changed,
107+
static HeapTuple ExtractReplicaIdentity(Relation rel, HeapTuple tup, bool key_required,
106108
bool *copy);
107109

108110

@@ -2910,6 +2912,7 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
29102912
bool all_visible_cleared_new = false;
29112913
bool checked_lockers;
29122914
bool locker_remains;
2915+
bool id_has_external = false;
29132916
TransactionId xmax_new_tuple,
29142917
xmax_old_tuple;
29152918
uint16 infomask_old_tuple,
@@ -2994,7 +2997,7 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
29942997
Assert(ItemIdIsNormal(lp));
29952998

29962999
/*
2997-
* Fill in enough data in oldtup for HeapDetermineModifiedColumns to work
3000+
* Fill in enough data in oldtup for HeapDetermineColumnsInfo to work
29983001
* properly.
29993002
*/
30003003
oldtup.t_tableOid = RelationGetRelid(relation);
@@ -3005,9 +3008,17 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
30053008
/* the new tuple is ready, except for this: */
30063009
newtup->t_tableOid = RelationGetRelid(relation);
30073010

3008-
/* Determine columns modified by the update. */
3009-
modified_attrs = HeapDetermineModifiedColumns(relation, interesting_attrs,
3010-
&oldtup, newtup);
3011+
/*
3012+
* Determine columns modified by the update. Additionally, identify
3013+
* whether any of the unmodified replica identity key attributes in the
3014+
* old tuple is externally stored or not. This is required because for
3015+
* such attributes the flattened value won't be WAL logged as part of the
3016+
* new tuple so we must include it as part of the old_key_tuple. See
3017+
* ExtractReplicaIdentity.
3018+
*/
3019+
modified_attrs = HeapDetermineColumnsInfo(relation, interesting_attrs,
3020+
id_attrs, &oldtup,
3021+
newtup, &id_has_external);
30113022

30123023
/*
30133024
* If we're not updating any "key" column, we can grab a weaker lock type.
@@ -3609,10 +3620,12 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
36093620
* Compute replica identity tuple before entering the critical section so
36103621
* we don't PANIC upon a memory allocation failure.
36113622
* ExtractReplicaIdentity() will return NULL if nothing needs to be
3612-
* logged.
3623+
* logged. Pass old key required as true only if the replica identity key
3624+
* columns are modified or it has external data.
36133625
*/
36143626
old_key_tuple = ExtractReplicaIdentity(relation, &oldtup,
3615-
bms_overlap(modified_attrs, id_attrs),
3627+
bms_overlap(modified_attrs, id_attrs) ||
3628+
id_has_external,
36163629
&old_key_copied);
36173630

36183631
/* NO EREPORT(ERROR) from here till changes are logged */
@@ -3768,47 +3781,15 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
37683781
}
37693782

37703783
/*
3771-
* Check if the specified attribute's value is same in both given tuples.
3772-
* Subroutine for HeapDetermineModifiedColumns.
3784+
* Check if the specified attribute's values are the same. Subroutine for
3785+
* HeapDetermineColumnsInfo.
37733786
*/
37743787
static bool
3775-
heap_tuple_attr_equals(TupleDesc tupdesc, int attrnum,
3776-
HeapTuple tup1, HeapTuple tup2)
3788+
heap_attr_equals(TupleDesc tupdesc, int attrnum, Datum value1, Datum value2,
3789+
bool isnull1, bool isnull2)
37773790
{
3778-
Datum value1,
3779-
value2;
3780-
bool isnull1,
3781-
isnull2;
37823791
Form_pg_attribute att;
37833792

3784-
/*
3785-
* If it's a whole-tuple reference, say "not equal". It's not really
3786-
* worth supporting this case, since it could only succeed after a no-op
3787-
* update, which is hardly a case worth optimizing for.
3788-
*/
3789-
if (attrnum == 0)
3790-
return false;
3791-
3792-
/*
3793-
* Likewise, automatically say "not equal" for any system attribute other
3794-
* than tableOID; we cannot expect these to be consistent in a HOT chain,
3795-
* or even to be set correctly yet in the new tuple.
3796-
*/
3797-
if (attrnum < 0)
3798-
{
3799-
if (attrnum != TableOidAttributeNumber)
3800-
return false;
3801-
}
3802-
3803-
/*
3804-
* Extract the corresponding values. XXX this is pretty inefficient if
3805-
* there are many indexed columns. Should HeapDetermineModifiedColumns do
3806-
* a single heap_deform_tuple call on each tuple, instead? But that
3807-
* doesn't work for system columns ...
3808-
*/
3809-
value1 = heap_getattr(tup1, attrnum, tupdesc, &isnull1);
3810-
value2 = heap_getattr(tup2, attrnum, tupdesc, &isnull2);
3811-
38123793
/*
38133794
* If one value is NULL and other is not, then they are certainly not
38143795
* equal
@@ -3850,24 +3831,96 @@ heap_tuple_attr_equals(TupleDesc tupdesc, int attrnum,
38503831
* Given an updated tuple, determine (and return into the output bitmapset),
38513832
* from those listed as interesting, the set of columns that changed.
38523833
*
3853-
* The input bitmapset is destructively modified; that is OK since this is
3854-
* invoked at most once in heap_update.
3834+
* has_external indicates if any of the unmodified attributes (from those
3835+
* listed as interesting) of the old tuple is a member of external_cols and is
3836+
* stored externally.
3837+
*
3838+
* The input interesting_cols bitmapset is destructively modified; that is OK
3839+
* since this is invoked at most once in heap_update.
38553840
*/
38563841
static Bitmapset *
3857-
HeapDetermineModifiedColumns(Relation relation, Bitmapset *interesting_cols,
3858-
HeapTuple oldtup, HeapTuple newtup)
3842+
HeapDetermineColumnsInfo(Relation relation,
3843+
Bitmapset *interesting_cols,
3844+
Bitmapset *external_cols,
3845+
HeapTuple oldtup, HeapTuple newtup,
3846+
bool *has_external)
38593847
{
3860-
int attnum;
3848+
int attrnum;
38613849
Bitmapset *modified = NULL;
3850+
TupleDesc tupdesc = RelationGetDescr(relation);
38623851

3863-
while ((attnum = bms_first_member(interesting_cols)) >= 0)
3852+
while ((attrnum = bms_first_member(interesting_cols)) >= 0)
38643853
{
3865-
attnum += FirstLowInvalidHeapAttributeNumber;
3854+
Datum value1,
3855+
value2;
3856+
bool isnull1,
3857+
isnull2;
3858+
3859+
attrnum += FirstLowInvalidHeapAttributeNumber;
38663860

3867-
if (!heap_tuple_attr_equals(RelationGetDescr(relation),
3868-
attnum, oldtup, newtup))
3861+
/*
3862+
* If it's a whole-tuple reference, say "not equal". It's not really
3863+
* worth supporting this case, since it could only succeed after a
3864+
* no-op update, which is hardly a case worth optimizing for.
3865+
*/
3866+
if (attrnum == 0)
3867+
{
38693868
modified = bms_add_member(modified,
3870-
attnum - FirstLowInvalidHeapAttributeNumber);
3869+
attrnum -
3870+
FirstLowInvalidHeapAttributeNumber);
3871+
continue;
3872+
}
3873+
3874+
/*
3875+
* Likewise, automatically say "not equal" for any system attribute
3876+
* other than tableOID; we cannot expect these to be consistent in a
3877+
* HOT chain, or even to be set correctly yet in the new tuple.
3878+
*/
3879+
if (attrnum < 0)
3880+
{
3881+
if (attrnum != TableOidAttributeNumber)
3882+
{
3883+
modified = bms_add_member(modified,
3884+
attrnum -
3885+
FirstLowInvalidHeapAttributeNumber);
3886+
continue;
3887+
}
3888+
}
3889+
3890+
/*
3891+
* Extract the corresponding values. XXX this is pretty inefficient
3892+
* if there are many indexed columns. Should we do a single
3893+
* heap_deform_tuple call on each tuple, instead? But that doesn't
3894+
* work for system columns ...
3895+
*/
3896+
value1 = heap_getattr(oldtup, attrnum, tupdesc, &isnull1);
3897+
value2 = heap_getattr(newtup, attrnum, tupdesc, &isnull2);
3898+
3899+
if (!heap_attr_equals(tupdesc, attrnum, value1,
3900+
value2, isnull1, isnull2))
3901+
{
3902+
modified = bms_add_member(modified,
3903+
attrnum -
3904+
FirstLowInvalidHeapAttributeNumber);
3905+
continue;
3906+
}
3907+
3908+
/*
3909+
* No need to check attributes that can't be stored externally. Note
3910+
* that system attributes can't be stored externally.
3911+
*/
3912+
if (attrnum < 0 || isnull1 ||
3913+
TupleDescAttr(tupdesc, attrnum - 1)->attlen != -1)
3914+
continue;
3915+
3916+
/*
3917+
* Check if the old tuple's attribute is stored externally and is a
3918+
* member of external_cols.
3919+
*/
3920+
if (VARATT_IS_EXTERNAL((struct varlena *) DatumGetPointer(value1)) &&
3921+
bms_is_member(attrnum - FirstLowInvalidHeapAttributeNumber,
3922+
external_cols))
3923+
*has_external = true;
38713924
}
38723925

38733926
return modified;
@@ -7620,14 +7673,14 @@ log_heap_new_cid(Relation relation, HeapTuple tup)
76207673
* Returns NULL if there's no need to log an identity or if there's no suitable
76217674
* key defined.
76227675
*
7623-
* key_changed should be false if caller knows that no replica identity
7624-
* columns changed value. It's always true in the DELETE case.
7676+
* Pass key_required true if any replica identity columns changed value, or if
7677+
* any of them have any external data. Delete must always pass true.
76257678
*
76267679
* *copy is set to true if the returned tuple is a modified copy rather than
76277680
* the same tuple that was passed in.
76287681
*/
76297682
static HeapTuple
7630-
ExtractReplicaIdentity(Relation relation, HeapTuple tp, bool key_changed,
7683+
ExtractReplicaIdentity(Relation relation, HeapTuple tp, bool key_required,
76317684
bool *copy)
76327685
{
76337686
TupleDesc desc = RelationGetDescr(relation);
@@ -7659,19 +7712,19 @@ ExtractReplicaIdentity(Relation relation, HeapTuple tp, bool key_changed,
76597712
return tp;
76607713
}
76617714

7662-
/* if the key hasn't changed and we're only logging the key, we're done */
7663-
if (!key_changed)
7715+
/* if the key isn't required and we're only logging the key, we're done */
7716+
if (!key_required)
76647717
return NULL;
76657718

76667719
/* find out the replica identity columns */
76677720
idattrs = RelationGetIndexAttrBitmap(relation,
76687721
INDEX_ATTR_BITMAP_IDENTITY_KEY);
76697722

76707723
/*
7671-
* If there's no defined replica identity columns, treat as !key_changed.
7724+
* If there's no defined replica identity columns, treat as !key_required.
76727725
* (This case should not be reachable from heap_update, since that should
7673-
* calculate key_changed accurately. But heap_delete just passes constant
7674-
* true for key_changed, so we can hit this case in deletes.)
7726+
* calculate key_required accurately. But heap_delete just passes
7727+
* constant true for key_required, so we can hit this case in deletes.)
76757728
*/
76767729
if (bms_is_empty(idattrs))
76777730
return NULL;

0 commit comments

Comments
 (0)