RE: Conflict detection for update_deleted in logical replication

From: "Zhijie Hou (Fujitsu)" <houzj(dot)fnst(at)fujitsu(dot)com>
To: "Hayato Kuroda (Fujitsu)" <kuroda(dot)hayato(at)fujitsu(dot)com>
Cc: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>, shveta malik <shveta(dot)malik(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Nisha Moond <nisha(dot)moond412(at)gmail(dot)com>, Peter Smith <smithpb2250(at)gmail(dot)com>
Subject: RE: Conflict detection for update_deleted in logical replication
Date: 2024-11-05 02:27:57
Message-ID: OS0PR01MB57167CE4F36ECE6512B958CF94522@OS0PR01MB5716.jpnprd01.prod.outlook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wednesday, October 30, 2024 4:34 PM Kuroda, Hayato/黒田 隼人 <kuroda(dot)hayato(at)fujitsu(dot)com> wrote:
>
> Thanks for updating the patch! Here are my comments.

Thanks for giving comments !

>
> 01. CreateSubscription
> ```
> + if (opts.detectupdatedeleted && !track_commit_timestamp)
> + ereport(ERROR,
> + errcode(ERRCODE_INVALID_PARAMETER_VALUE),
> + errmsg("detecting update_deleted conflicts requires \"%s\"
> to be enabled",
> + "track_commit_timestamp"));
> ```
>
> I don't think this guard is sufficient. I found two cases:
>
> * Creates a subscription with detect_update_deleted = false and
> track_commit_timestamp = false,
> then alters detect_update_deleted to true.

I think this is not allowed, as we already have a check in AlterSubscription().

> * Creates a subscription with detect_update_deleted = true and
> track_commit_timestamp = true,
> then update track_commit_timestamp to true and restart the instance.
>
> Based on that, how about detecting the inconsistency on the apply worker? It
> check
> the parameters and do error out when it starts or re-reads a catalog.

I agree to add the check in apply worker. But I would prefer to keep the check in
DDL as well, as I think It's better to prevent the bad configuration in the
beginning.

> If we want
> to detect in SQL commands, this can do in parse_subscription_options().

I personally feel parse_subscription_options() is not an appropriate place for this kind of
configuration error. So, I prefer to keep the check in CreateSubscription and
AlterSubscription.

> 03. build_index_column_bitmap()
> ```
> + for (int i = 0; i < indexinfo->ii_NumIndexAttrs; i++)
> + {
> + int keycol = indexinfo->ii_IndexAttrNumbers[i];
> +
> + index_bitmap = bms_add_member(index_bitmap, keycol);
> + }
> ```
>
> I feel we can assert the ii_IndexAttrNumbers is valid, because the passed index
> is a replica identity key.

I changed to directly get the replica identity index in the
FindMostRecentlyDeletedTupleInfo instead of passing by parameter. After that, I
think the assert is not necessary as the code is straightforward.

>
> 05. LogicalRepApplyLoop()
> ```
> + UpdateWorkerStats(last_received, timestamp,
> false);
> ```
>
> The statistics seems not correct. last_received is not sent at "timestamp", it
> had
> already been sent earlier. Do we really have to update here?

The timestamp updated here is used in the view
pg_stat_subscription.last_msg_send_time, which is a general timestamp that is
not directly related to the LSN received. See the desc in doc:

"""
last_msg_send_time

Send time of last message received from origin WAL sender; NULL for
parallel apply workers
"""

So, I think it's correct to update the timestamp here while keep last received lsn
unchanged.

>
> 06. ErrorOnReservedSlotName()
>
> I feel we should document that the slot name 'pg_conflict_detection' cannot be
> specified
> unconditionally.

I am a bit not sure if it's necessary, as the error message is clear that this
is a reserved name. The doc of the detect_update_delete also mentioned the name
of this slot.

When checking other options, we have reserved 'replication origin' name as well
"NONE,ANY" which are not explicitly mentioned in the doc. So, it seems fine.

>
> 07. General
>
> update_deleted can happen without DELETE commands. Should we rename
> the conflict
> reason, like 'update_target_modified'?

I am not sure if 'update_target_modified' is appropriate, as it's too similar to
the existing 'update_origin_change' conflict ("Updating a row that was
previously modified by another origin.").

Since the UPDATE can also be considered as DELETE + INSERT, so the current name
looks fine to me.

Attach the V7 patch set which address all other comments not mentioned.

Best Regards,
Hou zj

Attachment Content-Type Size
v7-0002-Maintain-the-replication-slot-in-logical-launcher.patch application/octet-stream 11.4 KB
v7-0001-Maintain-the-oldest-non-removeable-tranasction-ID.patch application/octet-stream 17.8 KB
v7-0003-Support-the-conflict-detection-for-update_deleted.patch application/octet-stream 21.3 KB
v7-0004-Add-a-detect_update_deleted-option-to-subscriptio.patch application/octet-stream 65.2 KB
v7-0005-Add-a-tap-test-to-verify-the-new-slot-xmin-mechan.patch application/octet-stream 6.9 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ryohei Takahashi (Fujitsu) 2024-11-05 02:34:20 COPY performance on Windows
Previous Message Zhijie Hou (Fujitsu) 2024-11-05 02:24:44 RE: Conflict detection for update_deleted in logical replication