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

Commitcaa231b

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

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
Buffernewbuf,HeapTupleoldtup,
7777
HeapTuplenewtup,HeapTupleold_key_tuple,
7878
boolall_visible_cleared,boolnew_all_visible_cleared);
79-
staticBitmapset*HeapDetermineModifiedColumns(Relationrelation,
80-
Bitmapset*interesting_cols,
81-
HeapTupleoldtup,HeapTuplenewtup);
79+
staticBitmapset*HeapDetermineColumnsInfo(Relationrelation,
80+
Bitmapset*interesting_cols,
81+
Bitmapset*external_cols,
82+
HeapTupleoldtup,HeapTuplenewtup,
83+
bool*has_external);
8284
staticboolheap_acquire_tuplock(Relationrelation,ItemPointertid,
8385
LockTupleModemode,LockWaitPolicywait_policy,
8486
bool*have_tuple_lock);
@@ -102,7 +104,7 @@ static void MultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 in
102104
staticboolConditionalMultiXactIdWait(MultiXactIdmulti,MultiXactStatusstatus,
103105
uint16infomask,Relationrel,int*remaining);
104106
staticXLogRecPtrlog_heap_new_cid(Relationrelation,HeapTupletup);
105-
staticHeapTupleExtractReplicaIdentity(Relationrel,HeapTupletup,boolkey_changed,
107+
staticHeapTupleExtractReplicaIdentity(Relationrel,HeapTupletup,boolkey_required,
106108
bool*copy);
107109

108110

@@ -2910,6 +2912,7 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
29102912
boolall_visible_cleared_new= false;
29112913
boolchecked_lockers;
29122914
boollocker_remains;
2915+
boolid_has_external= false;
29132916
TransactionIdxmax_new_tuple,
29142917
xmax_old_tuple;
29152918
uint16infomask_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 forHeapDetermineModifiedColumns to work
3000+
* Fill in enough data in oldtup forHeapDetermineColumnsInfo 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'svalue issame in both given tuples.
3772-
*Subroutine for HeapDetermineModifiedColumns.
3784+
* Check if the specified attribute'svalues are thesame. Subroutine for
3785+
*HeapDetermineColumnsInfo.
37733786
*/
37743787
staticbool
3775-
heap_tuple_attr_equals(TupleDesctupdesc,intattrnum,
3776-
HeapTupletup1,HeapTupletup2)
3788+
heap_attr_equals(TupleDesctupdesc,intattrnum,Datumvalue1,Datumvalue2,
3789+
boolisnull1,boolisnull2)
37773790
{
3778-
Datumvalue1,
3779-
value2;
3780-
boolisnull1,
3781-
isnull2;
37823791
Form_pg_attributeatt;
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
staticBitmapset*
3857-
HeapDetermineModifiedColumns(Relationrelation,Bitmapset*interesting_cols,
3858-
HeapTupleoldtup,HeapTuplenewtup)
3842+
HeapDetermineColumnsInfo(Relationrelation,
3843+
Bitmapset*interesting_cols,
3844+
Bitmapset*external_cols,
3845+
HeapTupleoldtup,HeapTuplenewtup,
3846+
bool*has_external)
38593847
{
3860-
intattnum;
3848+
intattrnum;
38613849
Bitmapset*modified=NULL;
3850+
TupleDesctupdesc=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+
Datumvalue1,
3855+
value2;
3856+
boolisnull1,
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((structvarlena*)DatumGetPointer(value1))&&
3921+
bms_is_member(attrnum-FirstLowInvalidHeapAttributeNumber,
3922+
external_cols))
3923+
*has_external= true;
38713924
}
38723925

38733926
returnmodified;
@@ -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 falseifcaller knows that no replica identity
7624-
*columns changed value. It's always true in the DELETE case.
7676+
*Pass key_required trueifany 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
staticHeapTuple
7630-
ExtractReplicaIdentity(Relationrelation,HeapTupletp,boolkey_changed,
7683+
ExtractReplicaIdentity(Relationrelation,HeapTupletp,boolkey_required,
76317684
bool*copy)
76327685
{
76337686
TupleDescdesc=RelationGetDescr(relation);
@@ -7659,19 +7712,19 @@ ExtractReplicaIdentity(Relation relation, HeapTuple tp, bool key_changed,
76597712
returntp;
76607713
}
76617714

7662-
/* if the keyhasn'tchanged and we're only logging the key, we're done */
7663-
if (!key_changed)
7715+
/* if the keyisn'trequired and we're only logging the key, we're done */
7716+
if (!key_required)
76647717
returnNULL;
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-
* calculatekey_changed accurately. But heap_delete just passes constant
7674-
* true forkey_changed, so we can hit this case in deletes.)
7726+
* calculatekey_required accurately. But heap_delete just passes
7727+
*constanttrue forkey_required, so we can hit this case in deletes.)
76757728
*/
76767729
if (bms_is_empty(idattrs))
76777730
returnNULL;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp