From 1618a40ea46084112b2f7e29b0bfe2ba478721e6 Mon Sep 17 00:00:00 2001 From: Ajin Cherian Date: Fri, 18 Oct 2024 00:38:55 -0400 Subject: [PATCH v17 2/4] Conflict resolvers for insert, update, and delete This patch introduces support for handling conflicts with the following resolutions: - For INSERT conflicts: - insert_exists: apply_remote, keep_local, error - For UPDATE conflicts: - update_origin_differs: apply_remote, keep_local, error - update_missing: apply_or_skip, apply_or_error, skip, error - For DELETE conflicts: - delete_missing: skip, error - delete_origin_differs: apply_remote, keep_local, error --- src/backend/executor/execReplication.c | 97 ++- src/backend/replication/logical/conflict.c | 230 ++++-- src/backend/replication/logical/worker.c | 374 +++++++--- src/include/executor/executor.h | 5 +- src/include/replication/conflict.h | 12 +- src/test/subscription/meson.build | 1 + src/test/subscription/t/034_conflict_resolver.pl | 877 +++++++++++++++++++++++ 7 files changed, 1442 insertions(+), 154 deletions(-) create mode 100644 src/test/subscription/t/034_conflict_resolver.pl diff --git a/src/backend/executor/execReplication.c b/src/backend/executor/execReplication.c index 54025c9..27d6e98 100644 --- a/src/backend/executor/execReplication.c +++ b/src/backend/executor/execReplication.c @@ -550,14 +550,76 @@ CheckAndReportConflict(ResultRelInfo *resultRelInfo, EState *estate, TransactionId xmin; GetTupleTransactionInfo(conflictslot, &xmin, &origin, &committs); - ReportApplyConflict(estate, resultRelInfo, ERROR, type, + ReportApplyConflict(estate, resultRelInfo, type, CR_ERROR, searchslot, conflictslot, remoteslot, - uniqueidx, xmin, origin, committs); + uniqueidx, xmin, origin, committs, false); } } } /* + * Check the unique indexes for conflicts. Return true on finding the + * first conflict itself. + * If the configured resolver is in favour of apply, give the conflicted + * tuple information in conflictslot. + */ +static bool +has_conflicting_tuple(Oid subid, ConflictType type, + ResultRelInfo *resultRelInfo, EState *estate, + TupleTableSlot *slot, TupleTableSlot **conflictslot) +{ + ConflictResolver resolver; + bool apply_remote = false; + Relation rel = resultRelInfo->ri_RelationDesc; + List *conflictindexes = resultRelInfo->ri_onConflictArbiterIndexes; + + /* ASSERT if called for any conflict type other than insert_exists */ + Assert(type == CT_INSERT_EXISTS); + + /* + * Get the configured resolver and determine if remote changes should be + * applied. + */ + resolver = GetConflictResolver(subid, type, rel, NULL, &apply_remote); + + /* + * Proceed to find conflict if the resolver is set to a non-default value; + * if the resolver is 'ERROR' (default), the caller will handle it. + */ + if (resolver == CR_ERROR) + return false; + + /* Check all the unique indexes for a conflict */ + foreach_oid(uniqueidx, conflictindexes) + { + /* Return to caller for resolutions if any conflict is found */ + if (FindConflictTuple(resultRelInfo, estate, uniqueidx, slot, + &(*conflictslot))) + { + RepOriginId origin; + TimestampTz committs; + TransactionId xmin; + + GetTupleTransactionInfo(*conflictslot, &xmin, &origin, &committs); + ReportApplyConflict(estate, resultRelInfo, type, resolver, + NULL, *conflictslot, slot, uniqueidx, + xmin, origin, committs, apply_remote); + + /* Nothing to apply, free the resources */ + if (!apply_remote) + { + ExecDropSingleTupleTableSlot(*conflictslot); + *conflictslot = NULL; + } + + return true; + } + } + + return false; +} + +/* * Insert tuple represented in the slot to the relation, update the indexes, * and execute any constraints and per-row triggers. * @@ -565,7 +627,8 @@ CheckAndReportConflict(ResultRelInfo *resultRelInfo, EState *estate, */ void ExecSimpleRelationInsert(ResultRelInfo *resultRelInfo, - EState *estate, TupleTableSlot *slot) + EState *estate, TupleTableSlot *slot, + TupleTableSlot **conflictslot, Oid subid) { bool skip_tuple = false; Relation rel = resultRelInfo->ri_RelationDesc; @@ -601,6 +664,19 @@ ExecSimpleRelationInsert(ResultRelInfo *resultRelInfo, if (rel->rd_rel->relispartition) ExecPartitionCheck(resultRelInfo, slot, estate, true); + /* + * Check for conflict and return to caller for resolution, if found. + * + * XXX In case there are no conflicts, a non-default 'insert_exists' + * resolver adds overhead by performing an extra scan here. However, + * this approach avoids the extra work needed to rollback/delete the + * inserted tuple if a conflict is detected after insertion with a + * non-default resolution set. + */ + if (has_conflicting_tuple(subid, CT_INSERT_EXISTS, resultRelInfo, + estate, slot, &(*conflictslot))) + return; + /* OK, store the tuple and create index entries for it */ simple_table_tuple_insert(resultRelInfo->ri_RelationDesc, slot); @@ -615,13 +691,14 @@ ExecSimpleRelationInsert(ResultRelInfo *resultRelInfo, /* * Checks the conflict indexes to fetch the conflicting local tuple - * and reports the conflict. We perform this check here, instead of - * performing an additional index scan before the actual insertion and - * reporting the conflict if any conflicting tuples are found. This is - * to avoid the overhead of executing the extra scan for each INSERT - * operation, even when no conflict arises, which could introduce - * significant overhead to replication, particularly in cases where - * conflicts are rare. + * and reports the conflict. We perform this check here again to - + * + * a) optimize the default case where the resolution for + * 'insert_exists' is set to 'ERROR' by skipping the scan when there + * is no conflict. + * + * b) catch and report any conflict that might have been missed during + * the pre-insertion scan in has_conflicting_tuple(). * * XXX OTOH, this could lead to clean-up effort for dead tuples added * in heap and index in case of conflicts. But as conflicts shouldn't diff --git a/src/backend/replication/logical/conflict.c b/src/backend/replication/logical/conflict.c index cb6d50c..6bf9f0a 100644 --- a/src/backend/replication/logical/conflict.c +++ b/src/backend/replication/logical/conflict.c @@ -24,9 +24,9 @@ #include "parser/scansup.h" #include "pgstat.h" #include "replication/conflict.h" +#include "replication/worker_internal.h" #include "utils/builtins.h" #include "utils/fmgroids.h" -#include "replication/worker_internal.h" #include "storage/lmgr.h" #include "utils/lsyscache.h" #include "utils/syscache.h" @@ -117,12 +117,14 @@ static int errcode_apply_conflict(ConflictType type); static int errdetail_apply_conflict(EState *estate, ResultRelInfo *relinfo, ConflictType type, + ConflictResolver resolver, TupleTableSlot *searchslot, TupleTableSlot *localslot, TupleTableSlot *remoteslot, Oid indexoid, TransactionId localxmin, RepOriginId localorigin, - TimestampTz localts); + TimestampTz localts, + bool apply_remote); static char *build_tuple_value_details(EState *estate, ResultRelInfo *relinfo, ConflictType type, TupleTableSlot *searchslot, @@ -165,8 +167,8 @@ GetTupleTransactionInfo(TupleTableSlot *localslot, TransactionId *xmin, } /* - * This function is used to report a conflict while applying replication - * changes. + * This function is used to report a conflict and resolution applied while + * applying replication changes. * * 'searchslot' should contain the tuple used to search the local tuple to be * updated or deleted. @@ -185,13 +187,22 @@ GetTupleTransactionInfo(TupleTableSlot *localslot, TransactionId *xmin, * that we can fetch and display the conflicting key value. */ void -ReportApplyConflict(EState *estate, ResultRelInfo *relinfo, int elevel, - ConflictType type, TupleTableSlot *searchslot, - TupleTableSlot *localslot, TupleTableSlot *remoteslot, - Oid indexoid, TransactionId localxmin, - RepOriginId localorigin, TimestampTz localts) +ReportApplyConflict(EState *estate, ResultRelInfo *relinfo, + ConflictType type, ConflictResolver resolver, + TupleTableSlot *searchslot, TupleTableSlot *localslot, + TupleTableSlot *remoteslot, Oid indexoid, + TransactionId localxmin, RepOriginId localorigin, + TimestampTz localts, bool apply_remote) + { Relation localrel = relinfo->ri_RelationDesc; + int elevel; + + if (resolver == CR_ERROR || + (resolver == CR_APPLY_OR_ERROR && !apply_remote)) + elevel = ERROR; + else + elevel = LOG; Assert(!OidIsValid(indexoid) || CheckRelationOidLockedByMe(indexoid, RowExclusiveLock, true)); @@ -200,13 +211,14 @@ ReportApplyConflict(EState *estate, ResultRelInfo *relinfo, int elevel, ereport(elevel, errcode_apply_conflict(type), - errmsg("conflict detected on relation \"%s.%s\": conflict=%s", + errmsg("conflict detected on relation \"%s.%s\": conflict=%s, resolution=%s.", get_namespace_name(RelationGetNamespace(localrel)), RelationGetRelationName(localrel), - ConflictInfoMap[type].conflict_name), - errdetail_apply_conflict(estate, relinfo, type, searchslot, + ConflictInfoMap[type].conflict_name, + ConflictResolverNames[resolver]), + errdetail_apply_conflict(estate, relinfo, type, resolver, searchslot, localslot, remoteslot, indexoid, - localxmin, localorigin, localts)); + localxmin, localorigin, localts, apply_remote)); } /* @@ -274,17 +286,25 @@ errcode_apply_conflict(ConflictType type) */ static int errdetail_apply_conflict(EState *estate, ResultRelInfo *relinfo, - ConflictType type, TupleTableSlot *searchslot, - TupleTableSlot *localslot, TupleTableSlot *remoteslot, - Oid indexoid, TransactionId localxmin, - RepOriginId localorigin, TimestampTz localts) + ConflictType type, ConflictResolver resolver, + TupleTableSlot *searchslot, TupleTableSlot *localslot, + TupleTableSlot *remoteslot, Oid indexoid, + TransactionId localxmin, RepOriginId localorigin, + TimestampTz localts, bool apply_remote) { StringInfoData err_detail; char *val_desc; char *origin_name; + char *applymsg; + char *updmsg; initStringInfo(&err_detail); + if (apply_remote) + applymsg = "applying the remote changes."; + else + applymsg = "ignoring the remote changes."; + /* First, construct a detailed message describing the type of conflict */ switch (type) { @@ -295,13 +315,14 @@ errdetail_apply_conflict(EState *estate, ResultRelInfo *relinfo, if (localts) { if (localorigin == InvalidRepOriginId) - appendStringInfo(&err_detail, _("Key already exists in unique index \"%s\", modified locally in transaction %u at %s."), - get_rel_name(indexoid), - localxmin, timestamptz_to_str(localts)); + appendStringInfo(&err_detail, _("Key already exists in unique index \"%s\", modified locally in transaction %u at %s, %s"), + get_rel_name(indexoid), localxmin, + timestamptz_to_str(localts), applymsg); else if (replorigin_by_oid(localorigin, true, &origin_name)) - appendStringInfo(&err_detail, _("Key already exists in unique index \"%s\", modified by origin \"%s\" in transaction %u at %s."), + appendStringInfo(&err_detail, _("Key already exists in unique index \"%s\", modified by origin \"%s\" in transaction %u at %s, %s"), get_rel_name(indexoid), origin_name, - localxmin, timestamptz_to_str(localts)); + localxmin, timestamptz_to_str(localts), + applymsg); /* * The origin that modified this row has been removed. This @@ -311,47 +332,65 @@ errdetail_apply_conflict(EState *estate, ResultRelInfo *relinfo, * manually dropped by the user. */ else - appendStringInfo(&err_detail, _("Key already exists in unique index \"%s\", modified by a non-existent origin in transaction %u at %s."), - get_rel_name(indexoid), - localxmin, timestamptz_to_str(localts)); + appendStringInfo(&err_detail, _("Key already exists in unique index \"%s\", modified by a non-existent origin in transaction %u at %s, %s"), + get_rel_name(indexoid), localxmin, + timestamptz_to_str(localts), applymsg); } else - appendStringInfo(&err_detail, _("Key already exists in unique index \"%s\", modified in transaction %u."), - get_rel_name(indexoid), localxmin); + appendStringInfo(&err_detail, _("Key already exists in unique index \"%s\", modified in transaction %u, %s"), + get_rel_name(indexoid), localxmin, applymsg); break; case CT_UPDATE_ORIGIN_DIFFERS: if (localorigin == InvalidRepOriginId) - appendStringInfo(&err_detail, _("Updating the row that was modified locally in transaction %u at %s."), - localxmin, timestamptz_to_str(localts)); + appendStringInfo(&err_detail, _("Updating the row that was modified locally in transaction %u at %s, %s"), + localxmin, timestamptz_to_str(localts), + applymsg); else if (replorigin_by_oid(localorigin, true, &origin_name)) - appendStringInfo(&err_detail, _("Updating the row that was modified by a different origin \"%s\" in transaction %u at %s."), - origin_name, localxmin, timestamptz_to_str(localts)); + appendStringInfo(&err_detail, _("Updating the row that was modified by a different origin \"%s\" in transaction %u at %s, %s"), + origin_name, localxmin, + timestamptz_to_str(localts), applymsg); /* The origin that modified this row has been removed. */ else - appendStringInfo(&err_detail, _("Updating the row that was modified by a non-existent origin in transaction %u at %s."), - localxmin, timestamptz_to_str(localts)); + appendStringInfo(&err_detail, _("Updating the row that was modified by a non-existent origin in transaction %u at %s, %s"), + localxmin, timestamptz_to_str(localts), + applymsg); break; case CT_UPDATE_MISSING: - appendStringInfo(&err_detail, _("Could not find the row to be updated.")); + updmsg = "Could not find the row to be updated"; + if (resolver == CR_APPLY_OR_SKIP && !apply_remote) + appendStringInfo(&err_detail, _("%s, and the UPDATE cannot be converted to an INSERT, thus skipping the remote changes."), + updmsg); + else if (resolver == CR_APPLY_OR_ERROR && !apply_remote) + appendStringInfo(&err_detail, _("%s, and the UPDATE cannot be converted to an INSERT, thus raising the error."), + updmsg); + else if (apply_remote) + appendStringInfo(&err_detail, _("%s, thus converting the UPDATE to INSERT and %s"), + updmsg, applymsg); + else + appendStringInfo(&err_detail, _("%s, %s"), + updmsg, applymsg); break; case CT_DELETE_ORIGIN_DIFFERS: if (localorigin == InvalidRepOriginId) - appendStringInfo(&err_detail, _("Deleting the row that was modified locally in transaction %u at %s."), - localxmin, timestamptz_to_str(localts)); + appendStringInfo(&err_detail, _("Deleting the row that was modified locally in transaction %u at %s, %s"), + localxmin, timestamptz_to_str(localts), + applymsg); else if (replorigin_by_oid(localorigin, true, &origin_name)) - appendStringInfo(&err_detail, _("Deleting the row that was modified by a different origin \"%s\" in transaction %u at %s."), - origin_name, localxmin, timestamptz_to_str(localts)); + appendStringInfo(&err_detail, _("Deleting the row that was modified by a different origin \"%s\" in transaction %u at %s, %s"), + origin_name, localxmin, + timestamptz_to_str(localts), applymsg); /* The origin that modified this row has been removed. */ else - appendStringInfo(&err_detail, _("Deleting the row that was modified by a non-existent origin in transaction %u at %s."), - localxmin, timestamptz_to_str(localts)); + appendStringInfo(&err_detail, _("Deleting the row that was modified by a non-existent origin in transaction %u at %s, %s"), + localxmin, timestamptz_to_str(localts), + applymsg); break; @@ -658,6 +697,73 @@ ValidateConflictTypeAndResolver(const char *conflict_type, } /* + * Get the conflict resolver configured at subscription level for + * for the given conflict type. + */ +static ConflictResolver +get_conflict_resolver_internal(ConflictType type, Oid subid) +{ + + ConflictResolver resolver; + HeapTuple tuple; + Datum datum; + char *conflict_res; + + /* + * XXX: Currently, we fetch the conflict resolver from cache for each + * conflict detection. If needed, we can keep the info in global variable + * and fetch from cache only once after cache invalidation. + */ + tuple = SearchSysCache2(SUBSCRIPTIONCONFLMAP, + ObjectIdGetDatum(subid), + CStringGetTextDatum(ConflictInfoMap[type].conflict_name)); + + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for conflict type %u", type); + + datum = SysCacheGetAttrNotNull(SUBSCRIPTIONCONFLMAP, + tuple, Anum_pg_subscription_conflict_confres); + + conflict_res = TextDatumGetCString(datum); + + for (resolver = 0; resolver < CONFLICT_NUM_RESOLVERS; resolver++) + { + if (pg_strcasecmp(ConflictResolverNames[resolver], conflict_res) == 0) + break; + } + + ReleaseSysCache(tuple); + return resolver; +} + +/* + * Check if a full tuple can be created from the new tuple. + * Return true if yes, false otherwise. + */ +static bool +can_create_full_tuple(Relation localrel, + LogicalRepTupleData *newtup) +{ + int i; + int local_att = RelationGetNumberOfAttributes(localrel); + + if (newtup->ncols != local_att) + return false; + + /* + * A full tuple cannot be created if any column contains a toast value. + * Columns with toast values are marked as LOGICALREP_COLUMN_UNCHANGED. + */ + for (i = 0; i < newtup->ncols; i++) + { + if (newtup->colstatus[i] == LOGICALREP_COLUMN_UNCHANGED) + return false; + } + + return true; +} + +/* * Common 'CONFLICT RESOLVER' parsing function for CREATE and ALTER * SUBSCRIPTION commands. * @@ -922,3 +1028,47 @@ RemoveSubConflictResolvers(Oid subid) table_endscan(scan); table_close(rel, RowExclusiveLock); } + +/* + * Find the resolver for the given conflict type and subscription. + * + * Set 'apply_remote' to true if remote tuple should be applied, + * false otherwise. + */ +ConflictResolver +GetConflictResolver(Oid subid, ConflictType type, Relation localrel, + LogicalRepTupleData *newtup, bool *apply_remote) +{ + ConflictResolver resolver; + + resolver = get_conflict_resolver_internal(type, subid); + + switch (resolver) + { + case CR_APPLY_REMOTE: + *apply_remote = true; + break; + case CR_APPLY_OR_SKIP: + if (can_create_full_tuple(localrel, newtup)) + *apply_remote = true; + else + *apply_remote = false; + break; + case CR_APPLY_OR_ERROR: + if (can_create_full_tuple(localrel, newtup)) + *apply_remote = true; + else + *apply_remote = false; + break; + case CR_KEEP_LOCAL: + case CR_SKIP: + case CR_ERROR: + *apply_remote = false; + break; + default: + elog(ERROR, "Conflict %s is detected! Unrecogonized conflict resolution method", + ConflictInfoMap[type].conflict_name); + } + + return resolver; +} diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index 925dff9..abda71a 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -382,7 +382,9 @@ static void send_feedback(XLogRecPtr recvpos, bool force, bool requestReply); static void apply_handle_commit_internal(LogicalRepCommitData *commit_data); static void apply_handle_insert_internal(ApplyExecutionData *edata, ResultRelInfo *relinfo, - TupleTableSlot *remoteslot); + TupleTableSlot *remoteslot, + LogicalRepTupleData *newtup, + LogicalRepRelMapEntry *rel_entry); static void apply_handle_update_internal(ApplyExecutionData *edata, ResultRelInfo *relinfo, TupleTableSlot *remoteslot, @@ -2451,10 +2453,10 @@ apply_handle_insert(StringInfo s) /* For a partitioned table, insert the tuple into a partition. */ if (rel->localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) apply_handle_tuple_routing(edata, - remoteslot, NULL, CMD_INSERT); + remoteslot, &newtup, CMD_INSERT); else apply_handle_insert_internal(edata, edata->targetRelInfo, - remoteslot); + remoteslot, &newtup, rel); finish_edata(edata); @@ -2477,9 +2479,13 @@ apply_handle_insert(StringInfo s) static void apply_handle_insert_internal(ApplyExecutionData *edata, ResultRelInfo *relinfo, - TupleTableSlot *remoteslot) + TupleTableSlot *remoteslot, + LogicalRepTupleData *newtup, + LogicalRepRelMapEntry *rel_entry) { EState *estate = edata->estate; + MemoryContext oldctx; + TupleTableSlot *conflictslot = NULL; /* We must open indexes here. */ ExecOpenIndices(relinfo, true); @@ -2487,7 +2493,37 @@ apply_handle_insert_internal(ApplyExecutionData *edata, /* Do the insert. */ TargetPrivilegesCheck(relinfo->ri_RelationDesc, ACL_INSERT); - ExecSimpleRelationInsert(relinfo, estate, remoteslot); + + ExecSimpleRelationInsert(relinfo, estate, remoteslot, &conflictslot, + MySubscription->oid); + + /* + * If a conflict is detected, update the conflicting tuple by converting + * the remote INSERT to an UPDATE. Note that conflictslot will have the + * conflicting tuple only if the resolver is in favor of applying the + * changes, otherwise it will be NULL. + */ + if (conflictslot) + { + EPQState epqstate; + + EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1, NIL); + + /* Process and store remote tuple in the slot */ + oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); + slot_modify_data(remoteslot, conflictslot, rel_entry, newtup); + MemoryContextSwitchTo(oldctx); + + EvalPlanQualSetSlot(&epqstate, remoteslot); + + /* Do the update */ + TargetPrivilegesCheck(relinfo->ri_RelationDesc, ACL_UPDATE); + ExecSimpleRelationUpdate(relinfo, estate, &epqstate, conflictslot, + remoteslot); + + EvalPlanQualEnd(&epqstate); + ExecDropSingleTupleTableSlot(conflictslot); + } /* Cleanup. */ ExecCloseIndices(relinfo); @@ -2669,6 +2705,8 @@ apply_handle_update_internal(ApplyExecutionData *edata, TupleTableSlot *localslot; bool found; MemoryContext oldctx; + bool apply_remote = true; + ConflictResolver resolver; EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1, NIL); ExecOpenIndices(relinfo, true); @@ -2690,36 +2728,48 @@ apply_handle_update_internal(ApplyExecutionData *edata, TimestampTz localts; /* - * Report the conflict if the tuple was modified by a different - * origin. + * Report the conflict and configured resolver if the tuple was + * modified by a different origin. */ if (GetTupleTransactionInfo(localslot, &localxmin, &localorigin, &localts) && localorigin != replorigin_session_origin) { TupleTableSlot *newslot; + resolver = GetConflictResolver(MySubscription->oid, + CT_UPDATE_ORIGIN_DIFFERS, + localrel, NULL, + &apply_remote); + /* Store the new tuple for conflict reporting */ newslot = table_slot_create(localrel, &estate->es_tupleTable); slot_store_data(newslot, relmapentry, newtup); - ReportApplyConflict(estate, relinfo, LOG, CT_UPDATE_ORIGIN_DIFFERS, - remoteslot, localslot, newslot, - InvalidOid, localxmin, localorigin, localts); + ReportApplyConflict(estate, relinfo, CT_UPDATE_ORIGIN_DIFFERS, resolver, + remoteslot, localslot, newslot, InvalidOid, + localxmin, localorigin, localts, apply_remote); } - /* Process and store remote tuple in the slot */ - oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); - slot_modify_data(remoteslot, localslot, relmapentry, newtup); - MemoryContextSwitchTo(oldctx); + /* + * Apply the change if the configured resolver is in favor of that; + * otherwise, ignore the remote update. + */ + if (apply_remote) + { + /* Process and store remote tuple in the slot */ + oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); + slot_modify_data(remoteslot, localslot, relmapentry, newtup); + MemoryContextSwitchTo(oldctx); - EvalPlanQualSetSlot(&epqstate, remoteslot); + EvalPlanQualSetSlot(&epqstate, remoteslot); - InitConflictIndexes(relinfo); + InitConflictIndexes(relinfo); - /* Do the actual update. */ - TargetPrivilegesCheck(relinfo->ri_RelationDesc, ACL_UPDATE); - ExecSimpleRelationUpdate(relinfo, estate, &epqstate, localslot, - remoteslot); + /* Do the actual update. */ + TargetPrivilegesCheck(relinfo->ri_RelationDesc, ACL_UPDATE); + ExecSimpleRelationUpdate(relinfo, estate, &epqstate, localslot, + remoteslot); + } } else { @@ -2729,13 +2779,29 @@ apply_handle_update_internal(ApplyExecutionData *edata, slot_store_data(newslot, relmapentry, newtup); /* - * The tuple to be updated could not be found. Do nothing except for - * emitting a log message. + * The tuple to be updated could not be found. Report the conflict. If + * the configured resolver is in favor of applying the change, convert + * UPDATE to INSERT and apply the change. */ - ReportApplyConflict(estate, relinfo, LOG, CT_UPDATE_MISSING, - remoteslot, NULL, newslot, - InvalidOid, InvalidTransactionId, - InvalidRepOriginId, 0); + resolver = GetConflictResolver(MySubscription->oid, CT_UPDATE_MISSING, + localrel, newtup, &apply_remote); + + ReportApplyConflict(estate, relinfo, CT_UPDATE_MISSING, resolver, + remoteslot, NULL, newslot, InvalidOid, + InvalidTransactionId, InvalidRepOriginId, + 0, apply_remote); + + if (apply_remote) + { + /* Process and store remote tuple in the slot */ + oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); + slot_store_data(remoteslot, relmapentry, newtup); + slot_fill_defaults(relmapentry, estate, remoteslot); + MemoryContextSwitchTo(oldctx); + + apply_handle_insert_internal(edata, relinfo, remoteslot, newtup, + relmapentry); + } } /* Cleanup. */ @@ -2848,6 +2914,8 @@ apply_handle_delete_internal(ApplyExecutionData *edata, EPQState epqstate; TupleTableSlot *localslot; bool found; + bool apply_remote = true; + ConflictResolver resolver; EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1, NIL); ExecOpenIndices(relinfo, false); @@ -2863,31 +2931,50 @@ apply_handle_delete_internal(ApplyExecutionData *edata, TimestampTz localts; /* - * Report the conflict if the tuple was modified by a different - * origin. + * Report the conflict and configured resolver if the tuple was + * modified by a different origin. */ if (GetTupleTransactionInfo(localslot, &localxmin, &localorigin, &localts) && localorigin != replorigin_session_origin) - ReportApplyConflict(estate, relinfo, LOG, CT_DELETE_ORIGIN_DIFFERS, - remoteslot, localslot, NULL, - InvalidOid, localxmin, localorigin, localts); + { + resolver = GetConflictResolver(MySubscription->oid, + CT_DELETE_ORIGIN_DIFFERS, + localrel, NULL, &apply_remote); + + ReportApplyConflict(estate, relinfo, CT_DELETE_ORIGIN_DIFFERS, + resolver, remoteslot, localslot, NULL, + InvalidOid, localxmin, localorigin, localts, + apply_remote); + } - EvalPlanQualSetSlot(&epqstate, localslot); + /* + * Apply the change if configured resolver is in favor of that; + * otherwise, ignore the remote delete. + */ + if (apply_remote) + { + EvalPlanQualSetSlot(&epqstate, localslot); - /* Do the actual delete. */ - TargetPrivilegesCheck(relinfo->ri_RelationDesc, ACL_DELETE); - ExecSimpleRelationDelete(relinfo, estate, &epqstate, localslot); + /* Do the actual delete. */ + TargetPrivilegesCheck(relinfo->ri_RelationDesc, ACL_DELETE); + ExecSimpleRelationDelete(relinfo, estate, &epqstate, localslot); + } } else { /* - * The tuple to be deleted could not be found. Do nothing except for - * emitting a log message. + * The tuple to be deleted could not be found. Based on resolver + * configured, either skip and log a message or emit an error. */ - ReportApplyConflict(estate, relinfo, LOG, CT_DELETE_MISSING, - remoteslot, NULL, NULL, - InvalidOid, InvalidTransactionId, - InvalidRepOriginId, 0); + resolver = GetConflictResolver(MySubscription->oid, CT_DELETE_MISSING, + localrel, NULL, &apply_remote); + + /* Resolver is set to skip, thus report the conflict and skip */ + if (!apply_remote) + ReportApplyConflict(estate, relinfo, CT_DELETE_MISSING, + resolver, remoteslot, NULL, NULL, + InvalidOid, InvalidTransactionId, + InvalidRepOriginId, 0, apply_remote); } /* Cleanup. */ @@ -3021,19 +3108,21 @@ apply_handle_tuple_routing(ApplyExecutionData *edata, } MemoryContextSwitchTo(oldctx); - /* Check if we can do the update or delete on the leaf partition. */ - if (operation == CMD_UPDATE || operation == CMD_DELETE) + if (operation == CMD_INSERT || operation == CMD_UPDATE || + operation == CMD_DELETE) { - part_entry = logicalrep_partition_open(relmapentry, partrel, - attrmap); - check_relation_updatable(part_entry); + part_entry = logicalrep_partition_open(relmapentry, partrel, attrmap); + + /* Check if we can do the update or delete on the leaf partition */ + if (operation != CMD_INSERT) + check_relation_updatable(part_entry); } switch (operation) { case CMD_INSERT: apply_handle_insert_internal(edata, partrelinfo, - remoteslot_part); + remoteslot_part, newtup, part_entry); break; case CMD_DELETE: @@ -3059,6 +3148,9 @@ apply_handle_tuple_routing(ApplyExecutionData *edata, RepOriginId localorigin; TransactionId localxmin; TimestampTz localts; + LogicalRepRelMapEntry *part_entry_new = NULL; + ConflictResolver resolver; + bool apply_remote = true; /* Get the matching local tuple from the partition. */ found = FindReplTupleInLocalRel(edata, partrel, @@ -3071,47 +3163,87 @@ apply_handle_tuple_routing(ApplyExecutionData *edata, /* Store the new tuple for conflict reporting */ slot_store_data(newslot, part_entry, newtup); + resolver = GetConflictResolver(MySubscription->oid, + CT_UPDATE_MISSING, + partrel, newtup, + &apply_remote); /* - * The tuple to be updated could not be found. Do nothing - * except for emitting a log message. + * The tuple to be updated could not be found. Report the + * conflict and resolver. And take action based on the + * configured resolver. */ ReportApplyConflict(estate, partrelinfo, - LOG, CT_UPDATE_MISSING, + CT_UPDATE_MISSING, resolver, remoteslot_part, NULL, newslot, InvalidOid, InvalidTransactionId, - InvalidRepOriginId, 0); + InvalidRepOriginId, 0, apply_remote); - return; + /* + * Resolver is in favour of applying the remote changes. + * Prepare the slot for the INSERT. + */ + if (apply_remote) + { + oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); + slot_store_data(remoteslot_part, part_entry, newtup); + slot_fill_defaults(part_entry, estate, remoteslot_part); + MemoryContextSwitchTo(oldctx); + } } - - /* - * Report the conflict if the tuple was modified by a - * different origin. - */ - if (GetTupleTransactionInfo(localslot, &localxmin, &localorigin, &localts) && - localorigin != replorigin_session_origin) + else { - TupleTableSlot *newslot; + /* + * The tuple to be updated is found. Report and resolve + * the conflict if the tuple was modified by a different + * origin. + */ + if (GetTupleTransactionInfo(localslot, &localxmin, &localorigin, &localts) && + localorigin != replorigin_session_origin) + { + TupleTableSlot *newslot; - /* Store the new tuple for conflict reporting */ - newslot = table_slot_create(partrel, &estate->es_tupleTable); - slot_store_data(newslot, part_entry, newtup); + /* Store the new tuple for conflict reporting */ + newslot = table_slot_create(partrel, &estate->es_tupleTable); + slot_store_data(newslot, part_entry, newtup); - ReportApplyConflict(estate, partrelinfo, LOG, CT_UPDATE_ORIGIN_DIFFERS, - remoteslot_part, localslot, newslot, - InvalidOid, localxmin, localorigin, - localts); - } + resolver = GetConflictResolver(MySubscription->oid, + CT_UPDATE_ORIGIN_DIFFERS, + partrel, NULL, + &apply_remote); - /* - * Apply the update to the local tuple, putting the result in - * remoteslot_part. - */ - oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); - slot_modify_data(remoteslot_part, localslot, part_entry, - newtup); - MemoryContextSwitchTo(oldctx); + ReportApplyConflict(estate, partrelinfo, CT_UPDATE_ORIGIN_DIFFERS, + resolver, remoteslot_part, localslot, + newslot, InvalidOid, localxmin, + localorigin, localts, apply_remote); + } + if (apply_remote) + { + /* + * We can reach here in two cases: + * + * 1. If we found a tuple and no conflict is detected + * + * 2. If we found a tuple and conflict is detected, + * and the resolver is in favor of applying the change + * + * Putting the result in remoteslot_part. + */ + oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate)); + slot_modify_data(remoteslot_part, localslot, part_entry, + newtup); + MemoryContextSwitchTo(oldctx); + } + else + + /* + * apply_remote can be toggled if resolver for + * update_origin_differs is set to skip. Ignore remote + * update. + */ + return; + + } EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1, NIL); @@ -3123,23 +3255,52 @@ apply_handle_tuple_routing(ApplyExecutionData *edata, ExecPartitionCheck(partrelinfo, remoteslot_part, estate, false)) { - /* - * Yes, so simply UPDATE the partition. We don't call - * apply_handle_update_internal() here, which would - * normally do the following work, to avoid repeating some - * work already done above to find the local tuple in the - * partition. - */ - ExecOpenIndices(partrelinfo, true); - InitConflictIndexes(partrelinfo); - - EvalPlanQualSetSlot(&epqstate, remoteslot_part); - TargetPrivilegesCheck(partrelinfo->ri_RelationDesc, - ACL_UPDATE); - ExecSimpleRelationUpdate(partrelinfo, estate, &epqstate, - localslot, remoteslot_part); + if (found && apply_remote) + { + /* + * Yes, so simply UPDATE the partition. We don't call + * apply_handle_update_internal() here, which would + * normally do the following work, to avoid repeating + * some work already done above to find the local + * tuple in the partition. + * + * Do the update in cases - 1. found a tuple and no + * conflict is detected 2. update_origin_differs + * conflict is detected for the found tuple and the + * resolver is in favour of applying the update. + */ + ExecOpenIndices(partrelinfo, true); + InitConflictIndexes(partrelinfo); + + EvalPlanQualSetSlot(&epqstate, remoteslot_part); + TargetPrivilegesCheck(partrelinfo->ri_RelationDesc, + ACL_UPDATE); + ExecSimpleRelationUpdate(partrelinfo, estate, &epqstate, + localslot, remoteslot_part); + ExecCloseIndices(partrelinfo); + } + else if (apply_remote) + { + /* + * Tuple is not found but update_missing resolver is + * in favour of applying the change as INSERT. + */ + apply_handle_insert_internal(edata, partrelinfo, + remoteslot_part, newtup, + part_entry); + } } - else + + /* + * Updated tuple doesn't satisfy the current partition's + * constraint. + * + * Proceed by always applying the update (as 'apply_remote' is + * by default true). The 'apply_remote' can be OFF as well if + * the resolver for update_missing conflict conveys to skip + * the update. + */ + else if (apply_remote) { /* Move the tuple into the new partition. */ @@ -3180,12 +3341,20 @@ apply_handle_tuple_routing(ApplyExecutionData *edata, get_namespace_name(RelationGetNamespace(partrel_new)), RelationGetRelationName(partrel_new)); - ExecOpenIndices(partrelinfo, false); - - /* DELETE old tuple found in the old partition. */ - EvalPlanQualSetSlot(&epqstate, localslot); - TargetPrivilegesCheck(partrelinfo->ri_RelationDesc, ACL_DELETE); - ExecSimpleRelationDelete(partrelinfo, estate, &epqstate, localslot); + /* + * If tuple is found, delete it from old partition. We can + * reach this flow even for the case when the 'found' flag + * is false for 'update_missing' conflict and resolver is + * in favor of inserting the tuple. + */ + if (found) + { + ExecOpenIndices(partrelinfo, false); + EvalPlanQualSetSlot(&epqstate, localslot); + TargetPrivilegesCheck(partrelinfo->ri_RelationDesc, ACL_DELETE); + ExecSimpleRelationDelete(partrelinfo, estate, &epqstate, localslot); + ExecCloseIndices(partrelinfo); + } /* INSERT new tuple into the new partition. */ @@ -3201,22 +3370,27 @@ apply_handle_tuple_routing(ApplyExecutionData *edata, map = ExecGetRootToChildMap(partrelinfo_new, estate); if (map != NULL) { + attrmap = map->attrMap; remoteslot_part = execute_attr_map_slot(map->attrMap, remoteslot, remoteslot_part); } else { + attrmap = NULL; remoteslot_part = ExecCopySlot(remoteslot_part, remoteslot); slot_getallattrs(remoteslot); } MemoryContextSwitchTo(oldctx); + part_entry_new = logicalrep_partition_open(part_entry, + partrel_new, + attrmap); apply_handle_insert_internal(edata, partrelinfo_new, - remoteslot_part); + remoteslot_part, newtup, + part_entry_new); } - ExecCloseIndices(partrelinfo); EvalPlanQualEnd(&epqstate); } break; diff --git a/src/include/executor/executor.h b/src/include/executor/executor.h index 69c3ebf..f390975 100644 --- a/src/include/executor/executor.h +++ b/src/include/executor/executor.h @@ -18,6 +18,8 @@ #include "fmgr.h" #include "nodes/lockoptions.h" #include "nodes/parsenodes.h" +#include "replication/logicalproto.h" +#include "replication/logicalrelation.h" #include "utils/memutils.h" @@ -667,7 +669,8 @@ extern bool RelationFindReplTupleSeq(Relation rel, LockTupleMode lockmode, TupleTableSlot *searchslot, TupleTableSlot *outslot); extern void ExecSimpleRelationInsert(ResultRelInfo *resultRelInfo, - EState *estate, TupleTableSlot *slot); + EState *estate, TupleTableSlot *slot, + TupleTableSlot **conflictsloty, Oid subid); extern void ExecSimpleRelationUpdate(ResultRelInfo *resultRelInfo, EState *estate, EPQState *epqstate, TupleTableSlot *searchslot, TupleTableSlot *slot); diff --git a/src/include/replication/conflict.h b/src/include/replication/conflict.h index c5865a1..0680ef1 100644 --- a/src/include/replication/conflict.h +++ b/src/include/replication/conflict.h @@ -11,6 +11,7 @@ #include "nodes/execnodes.h" #include "parser/parse_node.h" +#include "replication/logicalrelation.h" #include "utils/timestamp.h" /* @@ -91,12 +92,14 @@ extern bool GetTupleTransactionInfo(TupleTableSlot *localslot, RepOriginId *localorigin, TimestampTz *localts); extern void ReportApplyConflict(EState *estate, ResultRelInfo *relinfo, - int elevel, ConflictType type, + ConflictType type, + ConflictResolver resolver, TupleTableSlot *searchslot, TupleTableSlot *localslot, TupleTableSlot *remoteslot, Oid indexoid, TransactionId localxmin, - RepOriginId localorigin, TimestampTz localts); + RepOriginId localorigin, TimestampTz localts, + bool apply_remote); extern void InitConflictIndexes(ResultRelInfo *relInfo); extern void SetSubConflictResolvers(Oid subId, List *resolvers); extern void RemoveSubConflictResolvers(Oid confid); @@ -109,5 +112,8 @@ extern ConflictType ValidateConflictTypeAndResolver(const char *conflict_type, const char *conflict_resolver); extern List *GetDefaultConflictResolvers(void); extern void ResetSubConflictResolver(Oid subid, char *conflict_type); - +extern ConflictResolver GetConflictResolver(Oid subid, ConflictType type, + Relation localrel, + LogicalRepTupleData *newtup, + bool *apply_remote); #endif diff --git a/src/test/subscription/meson.build b/src/test/subscription/meson.build index c591cd7..00ade29 100644 --- a/src/test/subscription/meson.build +++ b/src/test/subscription/meson.build @@ -40,6 +40,7 @@ tests += { 't/031_column_list.pl', 't/032_subscribe_use_index.pl', 't/033_run_as_table_owner.pl', + 't/034_conflict_resolver.pl', 't/100_bugs.pl', ], }, diff --git a/src/test/subscription/t/034_conflict_resolver.pl b/src/test/subscription/t/034_conflict_resolver.pl new file mode 100644 index 0000000..86e3ad1 --- /dev/null +++ b/src/test/subscription/t/034_conflict_resolver.pl @@ -0,0 +1,877 @@ + +# Copyright (c) 2024, PostgreSQL Global Development Group + +# Test the conflict detection and resolution in logical replication +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +############################### +# Setup +############################### + +# Initialize publisher node +my $node_publisher = PostgreSQL::Test::Cluster->new('publisher'); +$node_publisher->init(allows_streaming => 'logical'); +$node_publisher->append_conf('postgresql.conf', + qq(max_prepared_transactions = 10)); +$node_publisher->start; + +# Create subscriber node with track_commit_timestamp enabled +my $node_subscriber = PostgreSQL::Test::Cluster->new('subscriber'); +$node_subscriber->init; +$node_subscriber->append_conf('postgresql.conf', + qq(track_commit_timestamp = on)); +$node_subscriber->start; + +# Create table on publisher +$node_publisher->safe_psql( + 'postgres', + "CREATE TABLE conf_tab(a int PRIMARY key, data text, comments text); + ALTER TABLE conf_tab ALTER COLUMN comments SET STORAGE EXTERNAL;"); + +# Create similar table on subscriber +$node_subscriber->safe_psql( + 'postgres', + "CREATE TABLE conf_tab(a int PRIMARY key, data text, comments text); + ALTER TABLE conf_tab ALTER COLUMN comments SET STORAGE EXTERNAL;"); + +# Setup logical replication +my $publisher_connstr = $node_publisher->connstr . ' dbname=postgres'; +$node_publisher->safe_psql('postgres', + "CREATE PUBLICATION tap_pub FOR TABLE conf_tab"); + +# Create the subscription +my $appname = 'tap_sub'; +$node_subscriber->safe_psql( + 'postgres', + "CREATE SUBSCRIPTION tap_sub + CONNECTION '$publisher_connstr application_name=$appname' + PUBLICATION tap_pub;"); + +# Wait for initial table sync to finish +$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); + +my $result = $node_subscriber->safe_psql('postgres', + "SELECT conftype, confres FROM pg_subscription_conflict ORDER BY conftype" +); +is( $result, qq(delete_missing|skip +delete_origin_differs|apply_remote +insert_exists|error +update_exists|error +update_missing|skip +update_origin_differs|apply_remote), + "confirm that the default conflict resolvers are in place"); + +############################################ +# Test 'apply_remote' for 'insert_exists' +############################################ + +# Change CONFLICT RESOLVER of insert_exists to apply_remote +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (insert_exists = 'apply_remote');" +); + +# Create local data on the subscriber +$node_subscriber->safe_psql('postgres', + "INSERT INTO conf_tab(a, data) VALUES (1,'fromsub')"); + +# Create conflicting data on the publisher +my $log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "INSERT INTO conf_tab(a, data) VALUES (1,'frompub')"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab\": conflict=insert_exists, resolution=apply_remote/, + $log_offset); + +# Confirm that remote insert is converted to an update and the remote data is updated. +$result = $node_subscriber->safe_psql('postgres', + "SELECT data FROM conf_tab WHERE (a=1);"); + +is($result, 'frompub', "remote data is kept"); + + +######################################## +# Test 'keep_local' for 'insert_exists' +######################################## + +# Change CONFLICT RESOLVER of insert_exists to keep_local +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (insert_exists = 'keep_local');" +); + +# Create local data on the subscriber +$node_subscriber->safe_psql('postgres', + "INSERT INTO conf_tab(a, data) VALUES (2,'fromsub')"); + +# Confirm that row is updated +$result = $node_subscriber->safe_psql('postgres', + "SELECT data FROM conf_tab WHERE (a=2);"); + +is($result, 'fromsub', "data 2 from local is inserted"); + +$log_offset = -s $node_subscriber->logfile; + +# Create conflicting data on the publisher +$node_publisher->safe_psql('postgres', + "INSERT INTO conf_tab(a, data) VALUES (2,'frompub')"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab\": conflict=insert_exists, resolution=keep_local/, + $log_offset); + +# Confirm that remote insert is ignored and the local row is kept +$result = $node_subscriber->safe_psql('postgres', + "SELECT data FROM conf_tab WHERE (a=2);"); + +is($result, 'fromsub', "data from local is kept"); + +################################### +# Test 'error' for 'insert_exists' +################################### + +# Change CONFLICT RESOLVER of insert_exists to error +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (insert_exists = 'error');" +); + +# Create local data on the subscriber +$node_subscriber->safe_psql('postgres', + "INSERT INTO conf_tab(a, data) VALUES (3,'fromsub')"); + +# Create conflicting data on the publisher +$node_publisher->safe_psql('postgres', + "INSERT INTO conf_tab(a, data) VALUES (3,'frompub')"); + +$log_offset = -s $node_subscriber->logfile; + +# Confirm that this causes an error on the subscriber +$node_subscriber->wait_for_log( + qr/ERROR: conflict detected on relation \"public.conf_tab\": conflict=insert_exists, resolution=error/, + $log_offset); + +# Truncate table on subscriber to get rid of the error +$node_subscriber->safe_psql('postgres', "TRUNCATE conf_tab;"); + +################################### +# Test 'skip' for 'delete_missing' +################################### + +# Delete row on publisher that is not present on the subscriber and confirm that it is skipped +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', "DELETE FROM conf_tab WHERE (a=2);"); + +# Confirm that the missing row is skipped because 'delete_missing' is set to 'skip' +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab\": conflict=delete_missing, resolution=skip/, + $log_offset); + +#################################### +# Test 'error' for 'delete_missing' +#################################### + +# Change CONFLICT RESOLVER of delete_missing to error +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (delete_missing = 'error');" +); + +# Capture the log offset before performing the delete on the publisher +$log_offset = -s $node_subscriber->logfile; + +# Perform the delete on the publisher +$node_publisher->safe_psql('postgres', "DELETE FROM conf_tab WHERE (a=1);"); + +# Confirm that this causes an error on the subscriber +$node_subscriber->wait_for_log( + qr/ERROR: conflict detected on relation \"public.conf_tab\": conflict=delete_missing, resolution=error/, + $log_offset); + +# Drop the subscriber to remove error +$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub;"); + +# Truncate table on subscriber +$node_subscriber->safe_psql('postgres', "TRUNCATE conf_tab;"); + +# Truncate the table on the publisher +$node_publisher->safe_psql('postgres', "TRUNCATE conf_tab;"); + +# Create the subscription +$node_subscriber->safe_psql( + 'postgres', + "CREATE SUBSCRIPTION tap_sub + CONNECTION '$publisher_connstr application_name=$appname' + PUBLICATION tap_pub"); + +# Wait for initial table sync to finish +$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); + + +################################################# +# Test 'apply_remote' for 'delete_origin_differs' +################################################# + +# Insert data in the publisher +$node_publisher->safe_psql( + 'postgres', + "INSERT INTO conf_tab(a, data) VALUES (1,'frompub'); + INSERT INTO conf_tab(a, data) VALUES (2,'frompub'); + INSERT INTO conf_tab(a, data) VALUES (3,'frompub');"); + +# Modify data on the subscriber +$node_subscriber->safe_psql('postgres', + "UPDATE conf_tab SET data = 'fromsub' WHERE (a=1);"); + +# Create a conflicting delete on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', "DELETE FROM conf_tab WHERE (a=1);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab\": conflict=delete_origin_differs, resolution=apply_remote/, + $log_offset); + +# Confirm that the remote delete the local updated row +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab WHERE (a=1);"); + +is($result, '', "delete from remote is applied"); + +############################################### +# Test 'keep_local' for 'delete_origin_differs' +############################################### + +# Change CONFLICT RESOLVER of delete_origin_differs to keep_local +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (delete_origin_differs = 'keep_local');" +); + +# Modify data on the subscriber +$node_subscriber->safe_psql('postgres', + "UPDATE conf_tab SET data = 'fromsub' WHERE (a=2);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', "DELETE FROM conf_tab WHERE (a=2);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab\": conflict=delete_origin_differs, resolution=keep_local/, + $log_offset); + +# Confirm that the local data is untouched by the remote update +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab WHERE (a=2);"); + +is($result, 'fromsub', "update from local is kept"); + +########################################## +# Test 'error' for 'delete_origin_differs' +########################################## + +# Change CONFLICT RESOLVER of delete_origin_differs to error +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (delete_origin_differs = 'error');" +); + +# Modify data on the subscriber +$node_subscriber->safe_psql('postgres', + "UPDATE conf_tab SET data = 'fromsub' WHERE (a=3);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', "DELETE FROM conf_tab WHERE (a=3);"); + +$node_subscriber->wait_for_log( + qr/ERROR: conflict detected on relation \"public.conf_tab\": conflict=delete_origin_differs, resolution=error/, + $log_offset); + +# Drop the subscriber to remove error +$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub;"); + +# Truncate the table on the publisher +$node_publisher->safe_psql('postgres', "TRUNCATE conf_tab;"); + +# Truncate the table on the subscriber +$node_subscriber->safe_psql('postgres', "TRUNCATE conf_tab;"); + +# Create the subscription +$node_subscriber->safe_psql( + 'postgres', + "CREATE SUBSCRIPTION tap_sub + CONNECTION '$publisher_connstr application_name=$appname' + PUBLICATION tap_pub"); + +# Wait for initial table sync to finish +$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); + +################################################# +# Test 'apply_remote' for 'update_origin_differs' +################################################# + +# Insert data in the publisher +$node_publisher->safe_psql( + 'postgres', + "INSERT INTO conf_tab(a, data) VALUES (1,'frompub'); + INSERT INTO conf_tab(a, data) VALUES (2,'frompub'); + INSERT INTO conf_tab(a, data) VALUES (3,'frompub');"); + +# Modify data on the subscriber +$node_subscriber->safe_psql('postgres', + "UPDATE conf_tab SET data = 'fromsub' WHERE (a=1);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab SET data = 'frompubnew' WHERE (a=1);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab\": conflict=update_origin_differs, resolution=apply_remote/, + $log_offset); + +# Confirm that the remote update overrides the local update +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab WHERE (a=1);"); + +is($result, 'frompubnew', "update from remote is kept"); + +############################################### +# Test 'keep_local' for 'update_origin_differs' +############################################### + +# Change CONFLICT RESOLVER of update_origin_differs to keep_local +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (update_origin_differs = 'keep_local');" +); + +# Modify data on the subscriber +$node_subscriber->safe_psql('postgres', + "UPDATE conf_tab SET data = 'fromsub' WHERE (a=2);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab SET data = 'frompubnew' WHERE (a=2);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab\": conflict=update_origin_differs, resolution=keep_local/, + $log_offset); + +# Confirm that the local data is untouched by the remote update +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab WHERE (a=2);"); + +is($result, 'fromsub', "update from local is kept"); + +########################################## +# Test 'error' for 'update_origin_differs' +########################################## + +# Change CONFLICT RESOLVER of update_origin_differs to error +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (update_origin_differs = 'error');" +); + +# Modify data on the subscriber +$node_subscriber->safe_psql('postgres', + "UPDATE conf_tab SET data = 'fromsub' WHERE (a=3);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab SET data = 'frompubnew' WHERE (a=3);"); + +$node_subscriber->wait_for_log( + qr/ERROR: conflict detected on relation \"public.conf_tab\": conflict=update_origin_differs, resolution=error/, + $log_offset); + +# Drop the subscriber to remove error +$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub;"); + +# Truncate the table on the publisher +$node_publisher->safe_psql('postgres', "TRUNCATE conf_tab;"); + +# Truncate the table on the subscriber +$node_subscriber->safe_psql('postgres', "TRUNCATE conf_tab;"); + +# Create the subscription +$node_subscriber->safe_psql( + 'postgres', + "CREATE SUBSCRIPTION tap_sub + CONNECTION '$publisher_connstr application_name=$appname' + PUBLICATION tap_pub"); + +# Wait for initial table sync to finish +$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); + +# Insert data in the publisher +$node_publisher->safe_psql( + 'postgres', + "INSERT INTO conf_tab(a, data) VALUES (1,'frompub'); + INSERT INTO conf_tab(a, data) VALUES (2,'frompub'); + INSERT INTO conf_tab(a, data) VALUES (3,'frompub');"); + +########################################### +# Test 'apply_or_skip' for 'update_missing' +########################################### + +# Change CONFLICT RESOLVER of update_missing to apply_or_skip +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (update_missing = 'apply_or_skip');" +); + +# Test the apply part + +# Delete the row on the subscriber +$node_subscriber->safe_psql('postgres', "DELETE FROM conf_tab WHERE (a=3);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab SET data = 'frompubnew' WHERE (a=3);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab\": conflict=update_missing, resolution=apply_or_skip/, + $log_offset); + +# Confirm that the remote update is converted to an insert and new row applied +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab WHERE (a=3);"); + +is($result, 'frompubnew', "update from remote is converted to insert"); + +# Test the skip part + +# Create new row on publisher with toast data +$node_publisher->safe_psql('postgres', + "INSERT INTO conf_tab (a, data, comments) VALUES(4,'frompub',repeat('abcdefghij', 200));" +); + +# Delete the row on the subscriber +$node_subscriber->safe_psql('postgres', "DELETE FROM conf_tab WHERE (a=4);"); + +# Update the row on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab SET data = 'frompubnew' WHERE (a=4);"); + +$node_subscriber->wait_for_log( + qr/DETAIL: Could not find the row to be updated, and the UPDATE cannot be converted to an INSERT, thus skipping the remote changes./, + $log_offset); + +########################################### +# Test 'apply_or_error' for 'update_missing' +########################################### + +# Test the apply part + +# Change CONFLICT RESOLVER of update_missing to apply_or_error +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (update_missing = 'apply_or_error');" +); + +# Create new row on the publisher +$node_publisher->safe_psql('postgres', + "INSERT INTO conf_tab(a, data) VALUES (5,'frompub');"); + +# Delete the row on the subscriber +$node_subscriber->safe_psql('postgres', "DELETE FROM conf_tab WHERE (a=5);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab SET data = 'frompubnew' WHERE (a=5);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab\": conflict=update_missing, resolution=apply_or_error/, + $log_offset); + +# Confirm that the remote update is converted to an insert and new row applied +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab WHERE (a=5);"); + +is($result, 'frompubnew', "update from remote is converted to insert"); + +# Test the error part + +# Create new row on publisher with toast data +$node_publisher->safe_psql('postgres', + "INSERT INTO conf_tab (a, data, comments) VALUES(6,'frompub',repeat('abcdefghij', 200));" +); + +# Delete the row on the subscriber +$node_subscriber->safe_psql('postgres', "DELETE FROM conf_tab WHERE (a=6);"); + +# Update the row on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab SET data = 'frompubnew' WHERE (a=6);"); + +$node_subscriber->wait_for_log( + qr/ERROR: conflict detected on relation \"public.conf_tab\": conflict=update_missing, resolution=apply_or_error/, + $log_offset); + +# Drop the subscriber to remove error +$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub;"); + +# Truncate the table on the publisher +$node_publisher->safe_psql('postgres', "TRUNCATE conf_tab;"); + +# Truncate the table on the subscriber +$node_subscriber->safe_psql('postgres', "TRUNCATE conf_tab;"); + +# Create the subscription +$node_subscriber->safe_psql( + 'postgres', + "CREATE SUBSCRIPTION tap_sub + CONNECTION '$publisher_connstr application_name=$appname' + PUBLICATION tap_pub"); + +# Wait for initial table sync to finish +$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); + +# Insert data in the publisher +$node_publisher->safe_psql( + 'postgres', + "INSERT INTO conf_tab(a, data) VALUES (1,'frompub'); + INSERT INTO conf_tab(a, data) VALUES (2,'frompub'); + INSERT INTO conf_tab(a, data) VALUES (3,'frompub');"); + +################################### +# Test 'skip' for 'update_missing' +################################### + +# Change CONFLICT RESOLVER of update_missing to skip +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (update_missing = 'skip');" +); + +# Delete the row on the subscriber +$node_subscriber->safe_psql('postgres', "DELETE FROM conf_tab WHERE (a=2);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab SET data = 'frompubnew' WHERE (a=2);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab\": conflict=update_missing, resolution=skip/, + $log_offset); + +# Confirm that the update does not change anything on the subscriber +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab WHERE (a=2);"); + +is($result, '', "update from remote is skipped on the subscriber"); + +################################### +# Test 'error' for 'update_missing' +################################### + +# Change CONFLICT RESOLVER of update_missing to error +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION tap_sub CONFLICT RESOLVER (update_missing = 'error');" +); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab SET data = 'frompubnewer' WHERE (a=2);"); + +$node_subscriber->wait_for_log( + qr/ERROR: conflict detected on relation \"public.conf_tab\": conflict=update_missing, resolution=error/, + $log_offset); + +# Drop the subscriber to remove error +$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub;"); + +################################################# +# Partition table tests for UPDATE conflicts +################################################# + +# Create partitioned table on publisher +$node_publisher->safe_psql( + 'postgres', + "CREATE TABLE conf_tab_part (a int not null, b int not null, data text); + ALTER TABLE conf_tab_part ADD CONSTRAINT conf_tab_part_pk primary key (a,b);" +); + +# Create similar table on subscriber but with partitions +$node_subscriber->safe_psql( + 'postgres', + "CREATE TABLE conf_tab_part (a int not null, b int not null, data text) partition by range (b); + ALTER TABLE conf_tab_part ADD CONSTRAINT conf_tab_part_pk primary key (a,b); + CREATE TABLE conf_tab_part_1 PARTITION OF conf_tab_part FOR VALUES FROM (MINVALUE) TO (100); + CREATE TABLE conf_tab_part_2 PARTITION OF conf_tab_part FOR VALUES FROM (101) TO (MAXVALUE);" +); + +# Setup logical replication +$node_publisher->safe_psql('postgres', + "CREATE PUBLICATION pub_part FOR TABLE conf_tab_part with (publish_via_partition_root=true);" +); + +# Create the subscription +$appname = 'sub_part'; +$node_subscriber->safe_psql( + 'postgres', + "CREATE SUBSCRIPTION sub_part + CONNECTION '$publisher_connstr application_name=$appname' + PUBLICATION pub_part;"); + +# Wait for initial table sync to finish +$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); + +################################################# +# Test 'apply_remote' for 'update_origin_differs' +################################################# + +# Insert data in the publisher +$node_publisher->safe_psql( + 'postgres', + "INSERT INTO conf_tab_part VALUES (1,1,'frompub'); + INSERT INTO conf_tab_part VALUES (2,1,'frompub'); + INSERT INTO conf_tab_part VALUES (3,1,'frompub');"); + +# Modify data on the subscriber +$node_subscriber->safe_psql('postgres', + "UPDATE conf_tab_part SET data = 'fromsub' WHERE (a=1);"); + +# Create a conflicting update on the same partition +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab_part SET data = 'frompubnew_p1' WHERE (a=1);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab_part_1\": conflict=update_origin_differs, resolution=apply_remote/, + $log_offset); + +# Confirm that the remote update overrides the local update +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab_part WHERE (a=1);"); + +is($result, 'frompubnew_p1', + "update from remote is applied to the first partition"); + +# Create a conflicting update which also changes the partition +$node_subscriber->safe_psql('postgres', + "UPDATE conf_tab_part SET data = 'fromsub' WHERE (a=1);"); +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab_part SET b=101, data='frompubnew_p2' WHERE (a=1);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab_part_1\": conflict=update_origin_differs, resolution=apply_remote/, + $log_offset); + +# Confirm that the remote update overrides the local update +$result = $node_subscriber->safe_psql('postgres', + "SELECT b, data from conf_tab_part WHERE (a=1);"); + +is($result, qq(101|frompubnew_p2), + "update from remote is the second partition"); + +############################################### +# Test 'keep_local' for 'update_origin_differs' +############################################### + +# Change CONFLICT RESOLVER of update_origin_differs to keep_local +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION sub_part CONFLICT RESOLVER (update_origin_differs = 'keep_local');" +); + +# Modify data on the subscriber +$node_subscriber->safe_psql('postgres', + "UPDATE conf_tab_part SET data = 'fromsub' WHERE (a=2);"); + +# Create a conflicting update on the same partition +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab_part SET data = 'frompubnew_p1' WHERE (a=2);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab_part_1\": conflict=update_origin_differs, resolution=keep_local/, + $log_offset); + +# Confirm that the local data is untouched by the remote update +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab_part WHERE (a=2);"); + +is($result, 'fromsub', "update from local is kept on the first partition"); + +# Create a conflicting update which also changes the partition +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab_part SET b=102, data='frompubnew_p2' WHERE (a=2);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab_part_1\": conflict=update_origin_differs, resolution=keep_local/, + $log_offset); + +# Confirm that the local data is untouched by the remote update +$result = $node_subscriber->safe_psql('postgres', + "SELECT b, data from conf_tab_part WHERE (a=2);"); + +is($result, qq(1|fromsub), + "update from local is kept on the first partition"); + +########################################## +# Test 'error' for 'update_origin_differs' +########################################## + +# Change CONFLICT RESOLVER of update_origin_differs to error +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION sub_part CONFLICT RESOLVER (update_origin_differs = 'error');" +); + +# Modify data on the subscriber +$node_subscriber->safe_psql('postgres', + "UPDATE conf_tab_part SET data = 'fromsub' WHERE (a=3);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab_part SET data = 'frompubnew' WHERE (a=3);"); + +$node_subscriber->wait_for_log( + qr/ERROR: conflict detected on relation \"public.conf_tab_part_1\": conflict=update_origin_differs, resolution=error/, + $log_offset); + +# Drop the subscriber to remove error +$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION sub_part;"); + +# Truncate the table on the publisher +$node_publisher->safe_psql('postgres', "TRUNCATE conf_tab_part;"); + +# Truncate the table on the subscriber +$node_subscriber->safe_psql('postgres', "TRUNCATE conf_tab_part;"); + +$node_subscriber->safe_psql( + 'postgres', + "CREATE SUBSCRIPTION sub_part + CONNECTION '$publisher_connstr application_name=$appname' + PUBLICATION pub_part;"); + +# Wait for initial table sync to finish +$node_subscriber->wait_for_subscription_sync($node_publisher, $appname); + +# Insert data in the publisher +$node_publisher->safe_psql( + 'postgres', + "INSERT INTO conf_tab_part VALUES (1,1,'frompub'); + INSERT INTO conf_tab_part VALUES (2,1,'frompub'); + INSERT INTO conf_tab_part VALUES (3,1,'frompub');"); + +########################################### +# Test 'apply_or_skip' for 'update_missing' +########################################### + +# Change CONFLICT RESOLVER of update_missing to apply_or_skip +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION sub_part CONFLICT RESOLVER (update_missing = 'apply_or_skip');" +); + +# Delete the row on the subscriber +$node_subscriber->safe_psql('postgres', + "DELETE FROM conf_tab_part WHERE (a=3);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab_part SET data='frompubnew_p1' WHERE (a=3);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab_part_1\": conflict=update_missing, resolution=apply_or_skip/, + $log_offset); + +# Confirm that the remote update is converted to an insert and new row applied +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab_part WHERE (a=3);"); + +is($result, 'frompubnew_p1', + "update from remote is converted to insert in the first partition"); + +# Test the update which also changes the partition +# Delete the row on the subscriber +$node_subscriber->safe_psql('postgres', + "DELETE FROM conf_tab_part WHERE (a=3);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab_part SET b=103, data='frompubnew_p2' WHERE (a=3);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab_part_1\": conflict=update_missing, resolution=apply_or_skip/, + $log_offset); + +# Confirm that the remote update is converted to an insert and new row applied +$result = $node_subscriber->safe_psql('postgres', + "SELECT b,data from conf_tab_part WHERE (a=3);"); + +is($result, '103|frompubnew_p2', + "update from remote is converted to insert in the second partition"); + +################################### +# Test 'skip' for 'update_missing' +################################### + +# Change CONFLICT RESOLVER of update_missing to skip +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION sub_part CONFLICT RESOLVER (update_missing = 'skip');" +); + +# Delete the row on the subscriber +$node_subscriber->safe_psql('postgres', + "DELETE FROM conf_tab_part WHERE (a=2);"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab_part SET data='frompubnew_p1' WHERE (a=2);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab_part_1\": conflict=update_missing, resolution=skip/, + $log_offset); + +# Confirm that the update does not change anything on the subscriber +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab_part WHERE (a=2);"); + +is($result, '', + "update from remote on first partition is skipped on the subscriber"); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab_part SET b=102, data='frompubnew_p2' WHERE (a=2);"); + +$node_subscriber->wait_for_log( + qr/LOG: conflict detected on relation \"public.conf_tab_part_1\": conflict=update_missing, resolution=skip/, + $log_offset); + +# Confirm that the update does not change anything on the subscriber +$result = $node_subscriber->safe_psql('postgres', + "SELECT data from conf_tab_part WHERE (a=2);"); + +is($result, '', + "update from remote on second partition is skipped on the subscriber"); + +################################### +# Test 'error' for 'update_missing' +################################### + +# Change CONFLICT RESOLVER of update_missing to error +$node_subscriber->safe_psql('postgres', + "ALTER SUBSCRIPTION sub_part CONFLICT RESOLVER (update_missing = 'error');" +); + +# Create a conflicting update on the publisher +$log_offset = -s $node_subscriber->logfile; +$node_publisher->safe_psql('postgres', + "UPDATE conf_tab_part SET data = 'frompubnewer' WHERE (a=2);"); + +$node_subscriber->wait_for_log( + qr/ERROR: conflict detected on relation \"public.conf_tab_part_2\": conflict=update_missing, resolution=error/, + $log_offset); + +done_testing(); -- 1.8.3.1