forked from 2ndQuadrant/pglogical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpglogical_conflict.c
550 lines (478 loc) · 14.1 KB
/
pglogical_conflict.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*-------------------------------------------------------------------------
*
* pglogical_conflict.c
* Functions for detecting and handling conflicts
*
* Copyright (c) 2015, PostgreSQL Global Development Group
*
* IDENTIFICATION
* pglogical_conflict.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "libpq-fe.h"
#include "miscadmin.h"
#include "access/commit_ts.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/transam.h"
#include "access/xact.h"
#include "executor/executor.h"
#include "parser/parse_relation.h"
#include "replication/origin.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
#include "pglogical_proto.h"
#include "pglogical_conflict.h"
int pglogical_conflict_resolver = PGLOGICAL_RESOLVE_APPLY_REMOTE;
/*
* Setup a ScanKey for a search in the relation 'rel' for a tuple 'key' that
* is setup to match 'rel' (*NOT* idxrel!).
*
* Returns whether any column contains NULLs.
*/
static bool
build_index_scan_key(ScanKey skey, Relation rel, Relation idxrel, PGLogicalTupleData *tup)
{
int attoff;
Datum indclassDatum;
Datum indkeyDatum;
bool isnull;
oidvector *opclass;
int2vector *indkey;
bool hasnulls = false;
indclassDatum = SysCacheGetAttr(INDEXRELID, idxrel->rd_indextuple,
Anum_pg_index_indclass, &isnull);
Assert(!isnull);
opclass = (oidvector *) DatumGetPointer(indclassDatum);
indkeyDatum = SysCacheGetAttr(INDEXRELID, idxrel->rd_indextuple,
Anum_pg_index_indkey, &isnull);
Assert(!isnull);
indkey = (int2vector *) DatumGetPointer(indkeyDatum);
for (attoff = 0; attoff < RelationGetNumberOfAttributes(idxrel); attoff++)
{
Oid operator;
Oid opfamily;
RegProcedure regop;
int pkattno = attoff + 1;
int mainattno = indkey->values[attoff];
Oid atttype = attnumTypeId(rel, mainattno);
Oid optype = get_opclass_input_type(opclass->values[attoff]);
opfamily = get_opclass_family(opclass->values[attoff]);
operator = get_opfamily_member(opfamily, optype,
optype,
BTEqualStrategyNumber);
if (!OidIsValid(operator))
elog(ERROR,
"could not lookup equality operator for type %u, optype %u in opfamily %u",
atttype, optype, opfamily);
regop = get_opcode(operator);
/* FIXME: convert type? */
ScanKeyInit(&skey[attoff],
pkattno,
BTEqualStrategyNumber,
regop,
tup->values[mainattno - 1]);
if (tup->nulls[mainattno - 1])
{
hasnulls = true;
skey[attoff].sk_flags |= SK_ISNULL;
}
}
return hasnulls;
}
/*
* Search the index 'idxrel' for a tuple identified by 'skey' in 'rel'.
*
* If a matching tuple is found lock it with lockmode, fill the slot with its
* contents and return true, return false is returned otherwise.
*/
static bool
find_index_tuple(ScanKey skey, Relation rel, Relation idxrel,
LockTupleMode lockmode, TupleTableSlot *slot)
{
HeapTuple scantuple;
bool found;
IndexScanDesc scan;
SnapshotData snap;
TransactionId xwait;
InitDirtySnapshot(snap);
scan = index_beginscan(rel, idxrel, &snap,
RelationGetNumberOfAttributes(idxrel),
0);
retry:
found = false;
index_rescan(scan, skey, RelationGetNumberOfAttributes(idxrel), NULL, 0);
if ((scantuple = index_getnext(scan, ForwardScanDirection)) != NULL)
{
found = true;
/* FIXME: Improve TupleSlot to not require copying the whole tuple */
ExecStoreTuple(scantuple, slot, InvalidBuffer, false);
ExecMaterializeSlot(slot);
xwait = TransactionIdIsValid(snap.xmin) ?
snap.xmin : snap.xmax;
if (TransactionIdIsValid(xwait))
{
XactLockTableWait(xwait, NULL, NULL, XLTW_None);
goto retry;
}
}
if (found)
{
Buffer buf;
HeapUpdateFailureData hufd;
HTSU_Result res;
HeapTupleData locktup;
ItemPointerCopy(&slot->tts_tuple->t_self, &locktup.t_self);
PushActiveSnapshot(GetLatestSnapshot());
res = heap_lock_tuple(rel, &locktup, GetCurrentCommandId(false),
lockmode,
false /* wait */,
false /* don't follow updates */,
&buf, &hufd);
/* the tuple slot already has the buffer pinned */
ReleaseBuffer(buf);
PopActiveSnapshot();
switch (res)
{
case HeapTupleMayBeUpdated:
break;
case HeapTupleUpdated:
/* XXX: Improve handling here */
ereport(LOG,
(errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
errmsg("concurrent update, retrying")));
goto retry;
default:
elog(ERROR, "unexpected HTSU_Result after locking: %u", res);
break;
}
}
index_endscan(scan);
return found;
}
/*
* Find tuple using REPLICA IDENTITY index.
*/
bool
pglogical_tuple_find_replidx(EState *estate, PGLogicalTupleData *tuple,
TupleTableSlot *oldslot)
{
ResultRelInfo *relinfo = estate->es_result_relation_info;
Oid idxoid;
Relation idxrel;
ScanKeyData index_key[INDEX_MAX_KEYS];
bool found;
/* Open REPLICA IDENTITY index.*/
idxoid = RelationGetReplicaIndex(relinfo->ri_RelationDesc);
if (!OidIsValid(idxoid))
{
elog(ERROR, "could not find primary key for table with oid %u",
RelationGetRelid(relinfo->ri_RelationDesc));
}
idxrel = index_open(idxoid, RowExclusiveLock);
/* Buold scan key for just opened index*/
build_index_scan_key(index_key, relinfo->ri_RelationDesc, idxrel, tuple);
/* Try to find the row. */
found = find_index_tuple(index_key, relinfo->ri_RelationDesc, idxrel,
LockTupleExclusive, oldslot);
/* Don't release lock until commit. */
index_close(idxrel, NoLock);
return found;
}
/*
* Find the tuple in a table using any index.
*/
Oid
pglogical_tuple_find_conflict(EState *estate, PGLogicalTupleData *tuple,
TupleTableSlot *oldslot)
{
Oid conflict_idx = InvalidOid;
ScanKeyData index_key[INDEX_MAX_KEYS];
int i;
ResultRelInfo *relinfo;
ItemPointerData conflicting_tid;
ItemPointerSetInvalid(&conflicting_tid);
relinfo = estate->es_result_relation_info;
/* Do a SnapshotDirty search for conflicting tuples. */
for (i = 0; i < relinfo->ri_NumIndices; i++)
{
IndexInfo *ii = relinfo->ri_IndexRelationInfo[i];
Relation idxrel;
bool found = false;
/*
* Only unique indexes are of interest here, and we can't deal with
* expression indexes so far. FIXME: predicates should be handled
* better.
*/
if (!ii->ii_Unique || ii->ii_Expressions != NIL)
continue;
idxrel = relinfo->ri_IndexRelationDescs[i];
if (build_index_scan_key(index_key, relinfo->ri_RelationDesc,
idxrel, tuple))
continue;
/* Try to find conflicting row. */
found = find_index_tuple(index_key, relinfo->ri_RelationDesc,
idxrel, LockTupleExclusive, oldslot);
/* Alert if there's more than one conflicting unique key, we can't
* currently handle that situation. */
if (found &&
ItemPointerIsValid(&conflicting_tid) &&
!ItemPointerEquals(&oldslot->tts_tuple->t_self,
&conflicting_tid))
{
/* TODO: Report tuple identity in log */
ereport(ERROR,
(errcode(ERRCODE_UNIQUE_VIOLATION),
errmsg("multiple unique constraints violated by remote tuple"),
errdetail("cannot apply transaction because remotely tuple "
"conflicts with a local tuple on more than one "
"UNIQUE constraint and/or PRIMARY KEY"),
errhint("Resolve the conflict by removing or changing the "
"conflicting local tuple")));
}
else if (found)
{
ItemPointerCopy(&oldslot->tts_tuple->t_self, &conflicting_tid);
conflict_idx = RelationGetRelid(idxrel);
break;
}
CHECK_FOR_INTERRUPTS();
}
return conflict_idx;
}
/*
* Resolve conflict based on commit timestamp.
*/
static bool
conflict_resolve_by_timestamp(RepOriginId local_origin_id,
RepOriginId remote_origin_id,
TimestampTz local_ts,
TimestampTz remote_ts,
bool last_update_wins,
PGLogicalConflictResolution *resolution)
{
int cmp;
cmp = timestamptz_cmp_internal(remote_ts, local_ts);
/*
* The logic bellow assumes last update wins, we invert the logic by
* inverting result of timestamp comparison if first update wins was
* requested.
*/
if (!last_update_wins)
cmp = -cmp;
if (cmp > 0)
{
/* The remote row wins, update the local one. */
*resolution = PGLogicalResolution_ApplyRemote;
return true;
}
else if (cmp < 0)
{
/* The local row wins, retain it */
*resolution = PGLogicalResolution_KeepLocal;
return false;
}
else
{
/*
* The timestamps were equal, break the tie in a manner that is
* consistent across all nodes.
*
* XXX: TODO, for now we just always apply remote change.
*/
*resolution = PGLogicalResolution_ApplyRemote;
return true;
}
}
/*
* Get the origin of the local tuple.
*
* If the track_commit_timestamp is off, we return remote origin info since
* there is no way to get any meaningful info locally. This means that
* the caller will assume that all the local tuples came from remote site when
* track_commit_timestamp is off.
*
* This function is used by UPDATE conflict detection so the above means that
* UPDATEs will not be recognized as conflict even if they change locally
* modified row.
*
* Returns true if local origin data was found, false if not.
*/
bool
get_tuple_origin(HeapTuple local_tuple, TransactionId *xmin,
RepOriginId *local_origin, TimestampTz *local_ts)
{
if (!track_commit_timestamp)
{
*xmin = HeapTupleHeaderGetXmin(local_tuple->t_data);
*local_origin = replorigin_session_origin;
*local_ts = replorigin_session_origin_timestamp;
return false;
}
else
{
*xmin = HeapTupleHeaderGetXmin(local_tuple->t_data);
if (TransactionIdIsValid(*xmin) && !TransactionIdIsNormal(*xmin))
{
/*
* Pg emits an ERROR if you try to pass FrozenTransactionId (2)
* or BootstrapTransactionId (1) to TransactionIdGetCommitTsData,
* per RT#46983 . This seems like an oversight in the core function,
* but we can work around it here by setting it to the same thing
* we'd get if the xid's commit timestamp was trimmed already.
*/
*local_origin = InvalidRepOriginId;
*local_ts = 0;
return false;
}
else
return TransactionIdGetCommitTsData(*xmin, local_ts, local_origin);
}
}
/*
* Try resolving the conflict resolution.
*
* Returns true when remote tuple should be applied.
*/
bool
try_resolve_conflict(Relation rel, HeapTuple localtuple, HeapTuple remotetuple,
HeapTuple *resulttuple,
PGLogicalConflictResolution *resolution)
{
TransactionId xmin;
TimestampTz local_ts;
RepOriginId local_origin;
bool apply = false;
switch (pglogical_conflict_resolver)
{
case PGLOGICAL_RESOLVE_ERROR:
/* TODO: proper error message */
elog(ERROR, "cannot apply conflicting row");
break;
case PGLOGICAL_RESOLVE_APPLY_REMOTE:
apply = true;
*resolution = PGLogicalResolution_ApplyRemote;
break;
case PGLOGICAL_RESOLVE_KEEP_LOCAL:
apply = false;
*resolution = PGLogicalResolution_KeepLocal;
break;
case PGLOGICAL_RESOLVE_LAST_UPDATE_WINS:
get_tuple_origin(localtuple, &xmin, &local_origin, &local_ts);
apply = conflict_resolve_by_timestamp(local_origin,
replorigin_session_origin,
local_ts,
replorigin_session_origin_timestamp,
true, resolution);
break;
case PGLOGICAL_RESOLVE_FIRST_UPDATE_WINS:
get_tuple_origin(localtuple, &xmin, &local_origin, &local_ts);
apply = conflict_resolve_by_timestamp(local_origin,
replorigin_session_origin,
local_ts,
replorigin_session_origin_timestamp,
false, resolution);
break;
default:
elog(ERROR, "unrecognized pglogical_conflict_resolver setting %d",
pglogical_conflict_resolver);
}
if (apply)
*resulttuple = remotetuple;
else
*resulttuple = localtuple;
return apply;
}
static char *
conflict_type_to_string(PGLogicalConflictType conflict_type)
{
switch (conflict_type)
{
case CONFLICT_INSERT_INSERT:
return "insert_insert";
case CONFLICT_UPDATE_UPDATE:
return "update_update";
case CONFLICT_UPDATE_DELETE:
return "update_delete";
case CONFLICT_DELETE_DELETE:
return "delete_delete";
}
/* Unreachable */
return NULL;
}
static char *
conflict_resolution_to_string(PGLogicalConflictResolution resolution)
{
switch (resolution)
{
case PGLogicalResolution_ApplyRemote:
return "apply_remote";
case PGLogicalResolution_KeepLocal:
return "keep_local";
case PGLogicalResolution_Skip:
return "skip";
}
/* Unreachable */
return NULL;
}
/*
* Log the conflict to server log.
*
* TODO: provide more detail.
*/
void
pglogical_report_conflict(PGLogicalConflictType conflict_type, Relation rel,
HeapTuple localtuple, HeapTuple remotetuple,
HeapTuple applytuple,
PGLogicalConflictResolution resolution)
{
switch (conflict_type)
{
case CONFLICT_INSERT_INSERT:
case CONFLICT_UPDATE_UPDATE:
ereport(LOG,
(errcode(ERRCODE_INTEGRITY_CONSTRAINT_VIOLATION),
errmsg("CONFLICT: remote %s on relation %s. Resolution: %s.",
CONFLICT_INSERT_INSERT ? "INSERT" : "UPDATE",
quote_qualified_identifier(get_namespace_name(RelationGetNamespace(rel)),
RelationGetRelationName(rel)),
conflict_resolution_to_string(resolution))));
break;
case CONFLICT_UPDATE_DELETE:
case CONFLICT_DELETE_DELETE:
ereport(LOG,
(errcode(ERRCODE_INTEGRITY_CONSTRAINT_VIOLATION),
errmsg("CONFLICT: remote %s on relation %s (tuple not found). Resolution: %s.",
CONFLICT_UPDATE_DELETE ? "UPDATE" : "DELETE",
quote_qualified_identifier(get_namespace_name(RelationGetNamespace(rel)),
RelationGetRelationName(rel)),
conflict_resolution_to_string(resolution))));
break;
}
}
/* Checks validity of pglogical_conflict_resolver GUC */
bool
pglogical_conflict_resolver_check_hook(int *newval, void **extra,
GucSource source)
{
/*
* Only allow PGLOGICAL_RESOLVE_APPLY_REMOTE when track_commit_timestamp
* is off, because there is no way to know where the local tuple
* originated from.
*/
if (!track_commit_timestamp &&
*newval != PGLOGICAL_RESOLVE_APPLY_REMOTE)
{
GUC_check_errdetail("track_commit_timestamp is off");
return false;
}
return true;
}