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

Commit04645bb

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 parentc76665e commit04645bb

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
@@ -850,10 +850,11 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
850850
<listitem>
851851
<para>
852852
This form changes the information which is written to the write-ahead log
853-
to identify rows which are updated or deleted. This option has no effect
854-
except when logical replication is in use.
855-
In all cases, no old values are logged unless at least one of the columns
856-
that would be logged differs between the old and new versions of the row.
853+
to identify rows which are updated or deleted.
854+
In most cases, the old value of each column is only logged if it differs
855+
from the new value; however, if the old value is stored externally, it is
856+
always logged regardless of whether it changed.
857+
This option has no effect except when logical replication is in use.
857858
<variablelist>
858859
<varlistentry>
859860
<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
@@ -78,9 +78,11 @@ static XLogRecPtr log_heap_update(Relation reln, Buffer oldbuf,
7878
Buffernewbuf,HeapTupleoldtup,
7979
HeapTuplenewtup,HeapTupleold_key_tuple,
8080
boolall_visible_cleared,boolnew_all_visible_cleared);
81-
staticBitmapset*HeapDetermineModifiedColumns(Relationrelation,
82-
Bitmapset*interesting_cols,
83-
HeapTupleoldtup,HeapTuplenewtup);
81+
staticBitmapset*HeapDetermineColumnsInfo(Relationrelation,
82+
Bitmapset*interesting_cols,
83+
Bitmapset*external_cols,
84+
HeapTupleoldtup,HeapTuplenewtup,
85+
bool*has_external);
8486
staticboolheap_acquire_tuplock(Relationrelation,ItemPointertid,
8587
LockTupleModemode,LockWaitPolicywait_policy,
8688
bool*have_tuple_lock);
@@ -106,7 +108,7 @@ static bool ConditionalMultiXactIdWait(MultiXactId multi, MultiXactStatus status
106108
staticvoidindex_delete_sort(TM_IndexDeleteOp*delstate);
107109
staticintbottomup_sort_and_shrink(TM_IndexDeleteOp*delstate);
108110
staticXLogRecPtrlog_heap_new_cid(Relationrelation,HeapTupletup);
109-
staticHeapTupleExtractReplicaIdentity(Relationrel,HeapTupletup,boolkey_changed,
111+
staticHeapTupleExtractReplicaIdentity(Relationrel,HeapTupletup,boolkey_required,
110112
bool*copy);
111113

112114

@@ -3185,6 +3187,7 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
31853187
boolall_visible_cleared_new= false;
31863188
boolchecked_lockers;
31873189
boollocker_remains;
3190+
boolid_has_external= false;
31883191
TransactionIdxmax_new_tuple,
31893192
xmax_old_tuple;
31903193
uint16infomask_old_tuple,
@@ -3269,7 +3272,7 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
32693272
Assert(ItemIdIsNormal(lp));
32703273

32713274
/*
3272-
* Fill in enough data in oldtup forHeapDetermineModifiedColumns to work
3275+
* Fill in enough data in oldtup forHeapDetermineColumnsInfo to work
32733276
* properly.
32743277
*/
32753278
oldtup.t_tableOid=RelationGetRelid(relation);
@@ -3280,9 +3283,17 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
32803283
/* the new tuple is ready, except for this: */
32813284
newtup->t_tableOid=RelationGetRelid(relation);
32823285

3283-
/* Determine columns modified by the update. */
3284-
modified_attrs=HeapDetermineModifiedColumns(relation,interesting_attrs,
3285-
&oldtup,newtup);
3286+
/*
3287+
* Determine columns modified by the update. Additionally, identify
3288+
* whether any of the unmodified replica identity key attributes in the
3289+
* old tuple is externally stored or not. This is required because for
3290+
* such attributes the flattened value won't be WAL logged as part of the
3291+
* new tuple so we must include it as part of the old_key_tuple. See
3292+
* ExtractReplicaIdentity.
3293+
*/
3294+
modified_attrs=HeapDetermineColumnsInfo(relation,interesting_attrs,
3295+
id_attrs,&oldtup,
3296+
newtup,&id_has_external);
32863297

32873298
/*
32883299
* If we're not updating any "key" column, we can grab a weaker lock type.
@@ -3883,10 +3894,12 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
38833894
* Compute replica identity tuple before entering the critical section so
38843895
* we don't PANIC upon a memory allocation failure.
38853896
* ExtractReplicaIdentity() will return NULL if nothing needs to be
3886-
* logged.
3897+
* logged. Pass old key required as true only if the replica identity key
3898+
* columns are modified or it has external data.
38873899
*/
38883900
old_key_tuple=ExtractReplicaIdentity(relation,&oldtup,
3889-
bms_overlap(modified_attrs,id_attrs),
3901+
bms_overlap(modified_attrs,id_attrs)||
3902+
id_has_external,
38903903
&old_key_copied);
38913904

38923905
/* NO EREPORT(ERROR) from here till changes are logged */
@@ -4042,47 +4055,15 @@ heap_update(Relation relation, ItemPointer otid, HeapTuple newtup,
40424055
}
40434056

40444057
/*
4045-
* Check if the specified attribute'svalue issame in both given tuples.
4046-
*Subroutine for HeapDetermineModifiedColumns.
4058+
* Check if the specified attribute'svalues are thesame. Subroutine for
4059+
*HeapDetermineColumnsInfo.
40474060
*/
40484061
staticbool
4049-
heap_tuple_attr_equals(TupleDesctupdesc,intattrnum,
4050-
HeapTupletup1,HeapTupletup2)
4062+
heap_attr_equals(TupleDesctupdesc,intattrnum,Datumvalue1,Datumvalue2,
4063+
boolisnull1,boolisnull2)
40514064
{
4052-
Datumvalue1,
4053-
value2;
4054-
boolisnull1,
4055-
isnull2;
40564065
Form_pg_attributeatt;
40574066

4058-
/*
4059-
* If it's a whole-tuple reference, say "not equal". It's not really
4060-
* worth supporting this case, since it could only succeed after a no-op
4061-
* update, which is hardly a case worth optimizing for.
4062-
*/
4063-
if (attrnum==0)
4064-
return false;
4065-
4066-
/*
4067-
* Likewise, automatically say "not equal" for any system attribute other
4068-
* than tableOID; we cannot expect these to be consistent in a HOT chain,
4069-
* or even to be set correctly yet in the new tuple.
4070-
*/
4071-
if (attrnum<0)
4072-
{
4073-
if (attrnum!=TableOidAttributeNumber)
4074-
return false;
4075-
}
4076-
4077-
/*
4078-
* Extract the corresponding values. XXX this is pretty inefficient if
4079-
* there are many indexed columns. Should HeapDetermineModifiedColumns do
4080-
* a single heap_deform_tuple call on each tuple, instead?But that
4081-
* doesn't work for system columns ...
4082-
*/
4083-
value1=heap_getattr(tup1,attrnum,tupdesc,&isnull1);
4084-
value2=heap_getattr(tup2,attrnum,tupdesc,&isnull2);
4085-
40864067
/*
40874068
* If one value is NULL and other is not, then they are certainly not
40884069
* equal
@@ -4124,24 +4105,96 @@ heap_tuple_attr_equals(TupleDesc tupdesc, int attrnum,
41244105
* Given an updated tuple, determine (and return into the output bitmapset),
41254106
* from those listed as interesting, the set of columns that changed.
41264107
*
4127-
* The input bitmapset is destructively modified; that is OK since this is
4128-
* invoked at most once in heap_update.
4108+
* has_external indicates if any of the unmodified attributes (from those
4109+
* listed as interesting) of the old tuple is a member of external_cols and is
4110+
* stored externally.
4111+
*
4112+
* The input interesting_cols bitmapset is destructively modified; that is OK
4113+
* since this is invoked at most once in heap_update.
41294114
*/
41304115
staticBitmapset*
4131-
HeapDetermineModifiedColumns(Relationrelation,Bitmapset*interesting_cols,
4132-
HeapTupleoldtup,HeapTuplenewtup)
4116+
HeapDetermineColumnsInfo(Relationrelation,
4117+
Bitmapset*interesting_cols,
4118+
Bitmapset*external_cols,
4119+
HeapTupleoldtup,HeapTuplenewtup,
4120+
bool*has_external)
41334121
{
4134-
intattnum;
4122+
intattrnum;
41354123
Bitmapset*modified=NULL;
4124+
TupleDesctupdesc=RelationGetDescr(relation);
41364125

4137-
while ((attnum=bms_first_member(interesting_cols)) >=0)
4126+
while ((attrnum=bms_first_member(interesting_cols)) >=0)
41384127
{
4139-
attnum+=FirstLowInvalidHeapAttributeNumber;
4128+
Datumvalue1,
4129+
value2;
4130+
boolisnull1,
4131+
isnull2;
4132+
4133+
attrnum+=FirstLowInvalidHeapAttributeNumber;
41404134

4141-
if (!heap_tuple_attr_equals(RelationGetDescr(relation),
4142-
attnum,oldtup,newtup))
4135+
/*
4136+
* If it's a whole-tuple reference, say "not equal". It's not really
4137+
* worth supporting this case, since it could only succeed after a
4138+
* no-op update, which is hardly a case worth optimizing for.
4139+
*/
4140+
if (attrnum==0)
4141+
{
41434142
modified=bms_add_member(modified,
4144-
attnum-FirstLowInvalidHeapAttributeNumber);
4143+
attrnum-
4144+
FirstLowInvalidHeapAttributeNumber);
4145+
continue;
4146+
}
4147+
4148+
/*
4149+
* Likewise, automatically say "not equal" for any system attribute
4150+
* other than tableOID; we cannot expect these to be consistent in a
4151+
* HOT chain, or even to be set correctly yet in the new tuple.
4152+
*/
4153+
if (attrnum<0)
4154+
{
4155+
if (attrnum!=TableOidAttributeNumber)
4156+
{
4157+
modified=bms_add_member(modified,
4158+
attrnum-
4159+
FirstLowInvalidHeapAttributeNumber);
4160+
continue;
4161+
}
4162+
}
4163+
4164+
/*
4165+
* Extract the corresponding values. XXX this is pretty inefficient
4166+
* if there are many indexed columns. Should we do a single
4167+
* heap_deform_tuple call on each tuple, instead?But that doesn't
4168+
* work for system columns ...
4169+
*/
4170+
value1=heap_getattr(oldtup,attrnum,tupdesc,&isnull1);
4171+
value2=heap_getattr(newtup,attrnum,tupdesc,&isnull2);
4172+
4173+
if (!heap_attr_equals(tupdesc,attrnum,value1,
4174+
value2,isnull1,isnull2))
4175+
{
4176+
modified=bms_add_member(modified,
4177+
attrnum-
4178+
FirstLowInvalidHeapAttributeNumber);
4179+
continue;
4180+
}
4181+
4182+
/*
4183+
* No need to check attributes that can't be stored externally. Note
4184+
* that system attributes can't be stored externally.
4185+
*/
4186+
if (attrnum<0||isnull1||
4187+
TupleDescAttr(tupdesc,attrnum-1)->attlen!=-1)
4188+
continue;
4189+
4190+
/*
4191+
* Check if the old tuple's attribute is stored externally and is a
4192+
* member of external_cols.
4193+
*/
4194+
if (VARATT_IS_EXTERNAL((structvarlena*)DatumGetPointer(value1))&&
4195+
bms_is_member(attrnum-FirstLowInvalidHeapAttributeNumber,
4196+
external_cols))
4197+
*has_external= true;
41454198
}
41464199

41474200
returnmodified;
@@ -8306,14 +8359,14 @@ log_heap_new_cid(Relation relation, HeapTuple tup)
83068359
* Returns NULL if there's no need to log an identity or if there's no suitable
83078360
* key defined.
83088361
*
8309-
*key_changed should be falseifcaller knows that no replica identity
8310-
*columns changed value. It's always true in the DELETE case.
8362+
*Pass key_required trueifany replica identity columns changed value, or if
8363+
*any of them have any external data. Delete must always pass true.
83118364
*
83128365
* *copy is set to true if the returned tuple is a modified copy rather than
83138366
* the same tuple that was passed in.
83148367
*/
83158368
staticHeapTuple
8316-
ExtractReplicaIdentity(Relationrelation,HeapTupletp,boolkey_changed,
8369+
ExtractReplicaIdentity(Relationrelation,HeapTupletp,boolkey_required,
83178370
bool*copy)
83188371
{
83198372
TupleDescdesc=RelationGetDescr(relation);
@@ -8345,19 +8398,19 @@ ExtractReplicaIdentity(Relation relation, HeapTuple tp, bool key_changed,
83458398
returntp;
83468399
}
83478400

8348-
/* if the keyhasn'tchanged and we're only logging the key, we're done */
8349-
if (!key_changed)
8401+
/* if the keyisn'trequired and we're only logging the key, we're done */
8402+
if (!key_required)
83508403
returnNULL;
83518404

83528405
/* find out the replica identity columns */
83538406
idattrs=RelationGetIndexAttrBitmap(relation,
83548407
INDEX_ATTR_BITMAP_IDENTITY_KEY);
83558408

83568409
/*
8357-
* If there's no defined replica identity columns, treat as !key_changed.
8410+
* If there's no defined replica identity columns, treat as !key_required.
83588411
* (This case should not be reachable from heap_update, since that should
8359-
* calculatekey_changed accurately. But heap_delete just passes constant
8360-
* true forkey_changed, so we can hit this case in deletes.)
8412+
* calculatekey_required accurately. But heap_delete just passes
8413+
*constanttrue forkey_required, so we can hit this case in deletes.)
83618414
*/
83628415
if (bms_is_empty(idattrs))
83638416
returnNULL;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp