/src/postgres/src/backend/executor/execReplication.c
Line | Count | Source |
1 | | /*------------------------------------------------------------------------- |
2 | | * |
3 | | * execReplication.c |
4 | | * miscellaneous executor routines for logical replication |
5 | | * |
6 | | * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group |
7 | | * Portions Copyright (c) 1994, Regents of the University of California |
8 | | * |
9 | | * IDENTIFICATION |
10 | | * src/backend/executor/execReplication.c |
11 | | * |
12 | | *------------------------------------------------------------------------- |
13 | | */ |
14 | | |
15 | | #include "postgres.h" |
16 | | |
17 | | #include "access/amapi.h" |
18 | | #include "access/commit_ts.h" |
19 | | #include "access/genam.h" |
20 | | #include "access/gist.h" |
21 | | #include "access/relscan.h" |
22 | | #include "access/tableam.h" |
23 | | #include "access/transam.h" |
24 | | #include "access/xact.h" |
25 | | #include "access/heapam.h" |
26 | | #include "catalog/pg_am_d.h" |
27 | | #include "commands/trigger.h" |
28 | | #include "executor/executor.h" |
29 | | #include "executor/nodeModifyTable.h" |
30 | | #include "replication/conflict.h" |
31 | | #include "replication/logicalrelation.h" |
32 | | #include "storage/lmgr.h" |
33 | | #include "utils/builtins.h" |
34 | | #include "utils/lsyscache.h" |
35 | | #include "utils/rel.h" |
36 | | #include "utils/snapmgr.h" |
37 | | #include "utils/syscache.h" |
38 | | #include "utils/typcache.h" |
39 | | |
40 | | |
41 | | static bool tuples_equal(TupleTableSlot *slot1, TupleTableSlot *slot2, |
42 | | TypeCacheEntry **eq, Bitmapset *columns); |
43 | | |
44 | | /* |
45 | | * Setup a ScanKey for a search in the relation 'rel' for a tuple 'key' that |
46 | | * is setup to match 'rel' (*NOT* idxrel!). |
47 | | * |
48 | | * Returns how many columns to use for the index scan. |
49 | | * |
50 | | * This is not a generic routine, idxrel must be PK, RI, or an index that can be |
51 | | * used for a REPLICA IDENTITY FULL table. See FindUsableIndexForReplicaIdentityFull() |
52 | | * for details. |
53 | | * |
54 | | * By definition, replication identity of a rel meets all limitations associated |
55 | | * with that. Note that any other index could also meet these limitations. |
56 | | */ |
57 | | static int |
58 | | build_replindex_scan_key(ScanKey skey, Relation rel, Relation idxrel, |
59 | | TupleTableSlot *searchslot) |
60 | 0 | { |
61 | 0 | int index_attoff; |
62 | 0 | int skey_attoff = 0; |
63 | 0 | Datum indclassDatum; |
64 | 0 | oidvector *opclass; |
65 | 0 | int2vector *indkey = &idxrel->rd_index->indkey; |
66 | |
|
67 | 0 | indclassDatum = SysCacheGetAttrNotNull(INDEXRELID, idxrel->rd_indextuple, |
68 | 0 | Anum_pg_index_indclass); |
69 | 0 | opclass = (oidvector *) DatumGetPointer(indclassDatum); |
70 | | |
71 | | /* Build scankey for every non-expression attribute in the index. */ |
72 | 0 | for (index_attoff = 0; index_attoff < IndexRelationGetNumberOfKeyAttributes(idxrel); |
73 | 0 | index_attoff++) |
74 | 0 | { |
75 | 0 | Oid operator; |
76 | 0 | Oid optype; |
77 | 0 | Oid opfamily; |
78 | 0 | RegProcedure regop; |
79 | 0 | int table_attno = indkey->values[index_attoff]; |
80 | 0 | StrategyNumber eq_strategy; |
81 | |
|
82 | 0 | if (!AttributeNumberIsValid(table_attno)) |
83 | 0 | { |
84 | | /* |
85 | | * XXX: Currently, we don't support expressions in the scan key, |
86 | | * see code below. |
87 | | */ |
88 | 0 | continue; |
89 | 0 | } |
90 | | |
91 | | /* |
92 | | * Load the operator info. We need this to get the equality operator |
93 | | * function for the scan key. |
94 | | */ |
95 | 0 | optype = get_opclass_input_type(opclass->values[index_attoff]); |
96 | 0 | opfamily = get_opclass_family(opclass->values[index_attoff]); |
97 | 0 | eq_strategy = IndexAmTranslateCompareType(COMPARE_EQ, idxrel->rd_rel->relam, opfamily, false); |
98 | 0 | operator = get_opfamily_member(opfamily, optype, |
99 | 0 | optype, |
100 | 0 | eq_strategy); |
101 | |
|
102 | 0 | if (!OidIsValid(operator)) |
103 | 0 | elog(ERROR, "missing operator %d(%u,%u) in opfamily %u", |
104 | 0 | eq_strategy, optype, optype, opfamily); |
105 | | |
106 | 0 | regop = get_opcode(operator); |
107 | | |
108 | | /* Initialize the scankey. */ |
109 | 0 | ScanKeyInit(&skey[skey_attoff], |
110 | 0 | index_attoff + 1, |
111 | 0 | eq_strategy, |
112 | 0 | regop, |
113 | 0 | searchslot->tts_values[table_attno - 1]); |
114 | |
|
115 | 0 | skey[skey_attoff].sk_collation = idxrel->rd_indcollation[index_attoff]; |
116 | | |
117 | | /* Check for null value. */ |
118 | 0 | if (searchslot->tts_isnull[table_attno - 1]) |
119 | 0 | skey[skey_attoff].sk_flags |= (SK_ISNULL | SK_SEARCHNULL); |
120 | |
|
121 | 0 | skey_attoff++; |
122 | 0 | } |
123 | | |
124 | | /* There must always be at least one attribute for the index scan. */ |
125 | 0 | Assert(skey_attoff > 0); |
126 | |
|
127 | 0 | return skey_attoff; |
128 | 0 | } |
129 | | |
130 | | |
131 | | /* |
132 | | * Helper function to check if it is necessary to re-fetch and lock the tuple |
133 | | * due to concurrent modifications. This function should be called after |
134 | | * invoking table_tuple_lock. |
135 | | */ |
136 | | static bool |
137 | | should_refetch_tuple(TM_Result res, TM_FailureData *tmfd) |
138 | 0 | { |
139 | 0 | bool refetch = false; |
140 | |
|
141 | 0 | switch (res) |
142 | 0 | { |
143 | 0 | case TM_Ok: |
144 | 0 | break; |
145 | 0 | case TM_Updated: |
146 | | /* XXX: Improve handling here */ |
147 | 0 | if (ItemPointerIndicatesMovedPartitions(&tmfd->ctid)) |
148 | 0 | ereport(LOG, |
149 | 0 | (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE), |
150 | 0 | errmsg("tuple to be locked was already moved to another partition due to concurrent update, retrying"))); |
151 | 0 | else |
152 | 0 | ereport(LOG, |
153 | 0 | (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE), |
154 | 0 | errmsg("concurrent update, retrying"))); |
155 | 0 | refetch = true; |
156 | 0 | break; |
157 | 0 | case TM_Deleted: |
158 | | /* XXX: Improve handling here */ |
159 | 0 | ereport(LOG, |
160 | 0 | (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE), |
161 | 0 | errmsg("concurrent delete, retrying"))); |
162 | 0 | refetch = true; |
163 | 0 | break; |
164 | 0 | case TM_Invisible: |
165 | 0 | elog(ERROR, "attempted to lock invisible tuple"); |
166 | 0 | break; |
167 | 0 | default: |
168 | 0 | elog(ERROR, "unexpected table_tuple_lock status: %u", res); |
169 | 0 | break; |
170 | 0 | } |
171 | | |
172 | 0 | return refetch; |
173 | 0 | } |
174 | | |
175 | | /* |
176 | | * Search the relation 'rel' for tuple using the index. |
177 | | * |
178 | | * If a matching tuple is found, lock it with lockmode, fill the slot with its |
179 | | * contents, and return true. Return false otherwise. |
180 | | */ |
181 | | bool |
182 | | RelationFindReplTupleByIndex(Relation rel, Oid idxoid, |
183 | | LockTupleMode lockmode, |
184 | | TupleTableSlot *searchslot, |
185 | | TupleTableSlot *outslot) |
186 | 0 | { |
187 | 0 | ScanKeyData skey[INDEX_MAX_KEYS]; |
188 | 0 | int skey_attoff; |
189 | 0 | IndexScanDesc scan; |
190 | 0 | SnapshotData snap; |
191 | 0 | TransactionId xwait; |
192 | 0 | Relation idxrel; |
193 | 0 | bool found; |
194 | 0 | TypeCacheEntry **eq = NULL; |
195 | 0 | bool isIdxSafeToSkipDuplicates; |
196 | | |
197 | | /* Open the index. */ |
198 | 0 | idxrel = index_open(idxoid, RowExclusiveLock); |
199 | |
|
200 | 0 | isIdxSafeToSkipDuplicates = (GetRelationIdentityOrPK(rel) == idxoid); |
201 | |
|
202 | 0 | InitDirtySnapshot(snap); |
203 | | |
204 | | /* Build scan key. */ |
205 | 0 | skey_attoff = build_replindex_scan_key(skey, rel, idxrel, searchslot); |
206 | | |
207 | | /* Start an index scan. */ |
208 | 0 | scan = index_beginscan(rel, idxrel, |
209 | 0 | &snap, NULL, skey_attoff, 0, SO_NONE); |
210 | |
|
211 | 0 | retry: |
212 | 0 | found = false; |
213 | |
|
214 | 0 | index_rescan(scan, skey, skey_attoff, NULL, 0); |
215 | | |
216 | | /* Try to find the tuple */ |
217 | 0 | while (index_getnext_slot(scan, ForwardScanDirection, outslot)) |
218 | 0 | { |
219 | | /* |
220 | | * Avoid expensive equality check if the index is primary key or |
221 | | * replica identity index. |
222 | | */ |
223 | 0 | if (!isIdxSafeToSkipDuplicates) |
224 | 0 | { |
225 | 0 | if (eq == NULL) |
226 | 0 | eq = palloc0_array(TypeCacheEntry *, outslot->tts_tupleDescriptor->natts); |
227 | |
|
228 | 0 | if (!tuples_equal(outslot, searchslot, eq, NULL)) |
229 | 0 | continue; |
230 | 0 | } |
231 | | |
232 | 0 | ExecMaterializeSlot(outslot); |
233 | |
|
234 | 0 | xwait = TransactionIdIsValid(snap.xmin) ? |
235 | 0 | snap.xmin : snap.xmax; |
236 | | |
237 | | /* |
238 | | * If the tuple is locked, wait for locking transaction to finish and |
239 | | * retry. |
240 | | */ |
241 | 0 | if (TransactionIdIsValid(xwait)) |
242 | 0 | { |
243 | 0 | XactLockTableWait(xwait, NULL, NULL, XLTW_None); |
244 | 0 | goto retry; |
245 | 0 | } |
246 | | |
247 | | /* Found our tuple and it's not locked */ |
248 | 0 | found = true; |
249 | 0 | break; |
250 | 0 | } |
251 | | |
252 | | /* Found tuple, try to lock it in the lockmode. */ |
253 | 0 | if (found) |
254 | 0 | { |
255 | 0 | TM_FailureData tmfd; |
256 | 0 | TM_Result res; |
257 | |
|
258 | 0 | PushActiveSnapshot(GetLatestSnapshot()); |
259 | |
|
260 | 0 | res = table_tuple_lock(rel, &(outslot->tts_tid), GetActiveSnapshot(), |
261 | 0 | outslot, |
262 | 0 | GetCurrentCommandId(false), |
263 | 0 | lockmode, |
264 | 0 | LockWaitBlock, |
265 | 0 | 0 /* don't follow updates */ , |
266 | 0 | &tmfd); |
267 | |
|
268 | 0 | PopActiveSnapshot(); |
269 | |
|
270 | 0 | if (should_refetch_tuple(res, &tmfd)) |
271 | 0 | goto retry; |
272 | 0 | } |
273 | | |
274 | 0 | index_endscan(scan); |
275 | | |
276 | | /* Don't release lock until commit. */ |
277 | 0 | index_close(idxrel, NoLock); |
278 | |
|
279 | 0 | return found; |
280 | 0 | } |
281 | | |
282 | | /* |
283 | | * Compare the tuples in the slots by checking if they have equal values. |
284 | | * |
285 | | * If 'columns' is not null, only the columns specified within it will be |
286 | | * considered for the equality check, ignoring all other columns. |
287 | | */ |
288 | | static bool |
289 | | tuples_equal(TupleTableSlot *slot1, TupleTableSlot *slot2, |
290 | | TypeCacheEntry **eq, Bitmapset *columns) |
291 | 0 | { |
292 | 0 | int attrnum; |
293 | |
|
294 | 0 | Assert(slot1->tts_tupleDescriptor->natts == |
295 | 0 | slot2->tts_tupleDescriptor->natts); |
296 | |
|
297 | 0 | slot_getallattrs(slot1); |
298 | 0 | slot_getallattrs(slot2); |
299 | | |
300 | | /* Check equality of the attributes. */ |
301 | 0 | for (attrnum = 0; attrnum < slot1->tts_tupleDescriptor->natts; attrnum++) |
302 | 0 | { |
303 | 0 | Form_pg_attribute att; |
304 | 0 | TypeCacheEntry *typentry; |
305 | |
|
306 | 0 | att = TupleDescAttr(slot1->tts_tupleDescriptor, attrnum); |
307 | | |
308 | | /* |
309 | | * Ignore dropped and generated columns as the publisher doesn't send |
310 | | * those |
311 | | */ |
312 | 0 | if (att->attisdropped || att->attgenerated) |
313 | 0 | continue; |
314 | | |
315 | | /* |
316 | | * Ignore columns that are not listed for checking. |
317 | | */ |
318 | 0 | if (columns && |
319 | 0 | !bms_is_member(att->attnum - FirstLowInvalidHeapAttributeNumber, |
320 | 0 | columns)) |
321 | 0 | continue; |
322 | | |
323 | | /* |
324 | | * If one value is NULL and other is not, then they are certainly not |
325 | | * equal |
326 | | */ |
327 | 0 | if (slot1->tts_isnull[attrnum] != slot2->tts_isnull[attrnum]) |
328 | 0 | return false; |
329 | | |
330 | | /* |
331 | | * If both are NULL, they can be considered equal. |
332 | | */ |
333 | 0 | if (slot1->tts_isnull[attrnum] || slot2->tts_isnull[attrnum]) |
334 | 0 | continue; |
335 | | |
336 | 0 | typentry = eq[attrnum]; |
337 | 0 | if (typentry == NULL) |
338 | 0 | { |
339 | 0 | typentry = lookup_type_cache(att->atttypid, |
340 | 0 | TYPECACHE_EQ_OPR_FINFO); |
341 | 0 | if (!OidIsValid(typentry->eq_opr_finfo.fn_oid)) |
342 | 0 | ereport(ERROR, |
343 | 0 | (errcode(ERRCODE_UNDEFINED_FUNCTION), |
344 | 0 | errmsg("could not identify an equality operator for type %s", |
345 | 0 | format_type_be(att->atttypid)))); |
346 | 0 | eq[attrnum] = typentry; |
347 | 0 | } |
348 | | |
349 | 0 | if (!DatumGetBool(FunctionCall2Coll(&typentry->eq_opr_finfo, |
350 | 0 | att->attcollation, |
351 | 0 | slot1->tts_values[attrnum], |
352 | 0 | slot2->tts_values[attrnum]))) |
353 | 0 | return false; |
354 | 0 | } |
355 | | |
356 | 0 | return true; |
357 | 0 | } |
358 | | |
359 | | /* |
360 | | * Search the relation 'rel' for tuple using the sequential scan. |
361 | | * |
362 | | * If a matching tuple is found, lock it with lockmode, fill the slot with its |
363 | | * contents, and return true. Return false otherwise. |
364 | | * |
365 | | * Note that this stops on the first matching tuple. |
366 | | * |
367 | | * This can obviously be quite slow on tables that have more than few rows. |
368 | | */ |
369 | | bool |
370 | | RelationFindReplTupleSeq(Relation rel, LockTupleMode lockmode, |
371 | | TupleTableSlot *searchslot, TupleTableSlot *outslot) |
372 | 0 | { |
373 | 0 | TupleTableSlot *scanslot; |
374 | 0 | TableScanDesc scan; |
375 | 0 | SnapshotData snap; |
376 | 0 | TypeCacheEntry **eq; |
377 | 0 | TransactionId xwait; |
378 | 0 | bool found; |
379 | 0 | TupleDesc desc PG_USED_FOR_ASSERTS_ONLY = RelationGetDescr(rel); |
380 | |
|
381 | 0 | Assert(equalTupleDescs(desc, outslot->tts_tupleDescriptor)); |
382 | |
|
383 | 0 | eq = palloc0_array(TypeCacheEntry *, outslot->tts_tupleDescriptor->natts); |
384 | | |
385 | | /* Start a heap scan. */ |
386 | 0 | InitDirtySnapshot(snap); |
387 | 0 | scan = table_beginscan(rel, &snap, 0, NULL, |
388 | 0 | SO_NONE); |
389 | 0 | scanslot = table_slot_create(rel, NULL); |
390 | |
|
391 | 0 | retry: |
392 | 0 | found = false; |
393 | |
|
394 | 0 | table_rescan(scan, NULL); |
395 | | |
396 | | /* Try to find the tuple */ |
397 | 0 | while (table_scan_getnextslot(scan, ForwardScanDirection, scanslot)) |
398 | 0 | { |
399 | 0 | if (!tuples_equal(scanslot, searchslot, eq, NULL)) |
400 | 0 | continue; |
401 | | |
402 | 0 | found = true; |
403 | 0 | ExecCopySlot(outslot, scanslot); |
404 | |
|
405 | 0 | xwait = TransactionIdIsValid(snap.xmin) ? |
406 | 0 | snap.xmin : snap.xmax; |
407 | | |
408 | | /* |
409 | | * If the tuple is locked, wait for locking transaction to finish and |
410 | | * retry. |
411 | | */ |
412 | 0 | if (TransactionIdIsValid(xwait)) |
413 | 0 | { |
414 | 0 | XactLockTableWait(xwait, NULL, NULL, XLTW_None); |
415 | 0 | goto retry; |
416 | 0 | } |
417 | | |
418 | | /* Found our tuple and it's not locked */ |
419 | 0 | break; |
420 | 0 | } |
421 | | |
422 | | /* Found tuple, try to lock it in the lockmode. */ |
423 | 0 | if (found) |
424 | 0 | { |
425 | 0 | TM_FailureData tmfd; |
426 | 0 | TM_Result res; |
427 | |
|
428 | 0 | PushActiveSnapshot(GetLatestSnapshot()); |
429 | |
|
430 | 0 | res = table_tuple_lock(rel, &(outslot->tts_tid), GetActiveSnapshot(), |
431 | 0 | outslot, |
432 | 0 | GetCurrentCommandId(false), |
433 | 0 | lockmode, |
434 | 0 | LockWaitBlock, |
435 | 0 | 0 /* don't follow updates */ , |
436 | 0 | &tmfd); |
437 | |
|
438 | 0 | PopActiveSnapshot(); |
439 | |
|
440 | 0 | if (should_refetch_tuple(res, &tmfd)) |
441 | 0 | goto retry; |
442 | 0 | } |
443 | | |
444 | 0 | table_endscan(scan); |
445 | 0 | ExecDropSingleTupleTableSlot(scanslot); |
446 | |
|
447 | 0 | return found; |
448 | 0 | } |
449 | | |
450 | | /* |
451 | | * Build additional index information necessary for conflict detection. |
452 | | */ |
453 | | static void |
454 | | BuildConflictIndexInfo(ResultRelInfo *resultRelInfo, Oid conflictindex) |
455 | 0 | { |
456 | 0 | for (int i = 0; i < resultRelInfo->ri_NumIndices; i++) |
457 | 0 | { |
458 | 0 | Relation indexRelation = resultRelInfo->ri_IndexRelationDescs[i]; |
459 | 0 | IndexInfo *indexRelationInfo = resultRelInfo->ri_IndexRelationInfo[i]; |
460 | |
|
461 | 0 | if (conflictindex != RelationGetRelid(indexRelation)) |
462 | 0 | continue; |
463 | | |
464 | | /* |
465 | | * This Assert will fail if BuildSpeculativeIndexInfo() is called |
466 | | * twice for the given index. |
467 | | */ |
468 | 0 | Assert(indexRelationInfo->ii_UniqueOps == NULL); |
469 | |
|
470 | 0 | BuildSpeculativeIndexInfo(indexRelation, indexRelationInfo); |
471 | 0 | } |
472 | 0 | } |
473 | | |
474 | | /* |
475 | | * If the tuple is recently dead and was deleted by a transaction with a newer |
476 | | * commit timestamp than previously recorded, update the associated transaction |
477 | | * ID, commit time, and origin. This helps ensure that conflict detection uses |
478 | | * the most recent and relevant deletion metadata. |
479 | | */ |
480 | | static void |
481 | | update_most_recent_deletion_info(TupleTableSlot *scanslot, |
482 | | TransactionId oldestxmin, |
483 | | TransactionId *delete_xid, |
484 | | TimestampTz *delete_time, |
485 | | ReplOriginId *delete_origin) |
486 | 0 | { |
487 | 0 | BufferHeapTupleTableSlot *hslot; |
488 | 0 | HeapTuple tuple; |
489 | 0 | Buffer buf; |
490 | 0 | bool recently_dead = false; |
491 | 0 | TransactionId xmax; |
492 | 0 | TimestampTz localts; |
493 | 0 | ReplOriginId localorigin; |
494 | |
|
495 | 0 | hslot = (BufferHeapTupleTableSlot *) scanslot; |
496 | |
|
497 | 0 | tuple = ExecFetchSlotHeapTuple(scanslot, false, NULL); |
498 | 0 | buf = hslot->buffer; |
499 | |
|
500 | 0 | LockBuffer(buf, BUFFER_LOCK_SHARE); |
501 | | |
502 | | /* |
503 | | * We do not consider HEAPTUPLE_DEAD status because it indicates either |
504 | | * tuples whose inserting transaction was aborted (meaning there is no |
505 | | * commit timestamp or origin), or tuples deleted by a transaction older |
506 | | * than oldestxmin, making it safe to ignore them during conflict |
507 | | * detection (See comments atop worker.c for details). |
508 | | */ |
509 | 0 | if (HeapTupleSatisfiesVacuum(tuple, oldestxmin, buf) == HEAPTUPLE_RECENTLY_DEAD) |
510 | 0 | recently_dead = true; |
511 | |
|
512 | 0 | LockBuffer(buf, BUFFER_LOCK_UNLOCK); |
513 | |
|
514 | 0 | if (!recently_dead) |
515 | 0 | return; |
516 | | |
517 | 0 | xmax = HeapTupleHeaderGetUpdateXid(tuple->t_data); |
518 | 0 | if (!TransactionIdIsValid(xmax)) |
519 | 0 | return; |
520 | | |
521 | | /* Select the dead tuple with the most recent commit timestamp */ |
522 | 0 | if (TransactionIdGetCommitTsData(xmax, &localts, &localorigin) && |
523 | 0 | TimestampDifferenceExceeds(*delete_time, localts, 0)) |
524 | 0 | { |
525 | 0 | *delete_xid = xmax; |
526 | 0 | *delete_time = localts; |
527 | 0 | *delete_origin = localorigin; |
528 | 0 | } |
529 | 0 | } |
530 | | |
531 | | /* |
532 | | * Searches the relation 'rel' for the most recently deleted tuple that matches |
533 | | * the values in 'searchslot' and is not yet removable by VACUUM. The function |
534 | | * returns the transaction ID, origin, and commit timestamp of the transaction |
535 | | * that deleted this tuple. |
536 | | * |
537 | | * 'oldestxmin' acts as a cutoff transaction ID. Tuples deleted by transactions |
538 | | * with IDs >= 'oldestxmin' are considered recently dead and are eligible for |
539 | | * conflict detection. |
540 | | * |
541 | | * Instead of stopping at the first match, we scan all matching dead tuples to |
542 | | * identify most recent deletion. This is crucial because only the latest |
543 | | * deletion is relevant for resolving conflicts. |
544 | | * |
545 | | * For example, consider a scenario on the subscriber where a row is deleted, |
546 | | * re-inserted, and then deleted again only on the subscriber: |
547 | | * |
548 | | * - (pk, 1) - deleted at 9:00, |
549 | | * - (pk, 1) - deleted at 9:02, |
550 | | * |
551 | | * Now, a remote update arrives: (pk, 1) -> (pk, 2), timestamped at 9:01. |
552 | | * |
553 | | * If we mistakenly return the older deletion (9:00), the system may wrongly |
554 | | * apply the remote update using a last-update-wins strategy. Instead, we must |
555 | | * recognize the more recent deletion at 9:02 and skip the update. See |
556 | | * comments atop worker.c for details. Note, as of now, conflict resolution |
557 | | * is not implemented. Consequently, the system may incorrectly report the |
558 | | * older tuple as the conflicted one, leading to misleading results. |
559 | | * |
560 | | * The commit timestamp of the deleting transaction is used to determine which |
561 | | * tuple was deleted most recently. |
562 | | */ |
563 | | bool |
564 | | RelationFindDeletedTupleInfoSeq(Relation rel, TupleTableSlot *searchslot, |
565 | | TransactionId oldestxmin, |
566 | | TransactionId *delete_xid, |
567 | | ReplOriginId *delete_origin, |
568 | | TimestampTz *delete_time) |
569 | 0 | { |
570 | 0 | TupleTableSlot *scanslot; |
571 | 0 | TableScanDesc scan; |
572 | 0 | TypeCacheEntry **eq; |
573 | 0 | Bitmapset *indexbitmap; |
574 | 0 | TupleDesc desc PG_USED_FOR_ASSERTS_ONLY = RelationGetDescr(rel); |
575 | |
|
576 | 0 | Assert(equalTupleDescs(desc, searchslot->tts_tupleDescriptor)); |
577 | |
|
578 | 0 | *delete_xid = InvalidTransactionId; |
579 | 0 | *delete_origin = InvalidReplOriginId; |
580 | 0 | *delete_time = 0; |
581 | | |
582 | | /* |
583 | | * If the relation has a replica identity key or a primary key that is |
584 | | * unusable for locating deleted tuples (see |
585 | | * IsIndexUsableForFindingDeletedTuple), a full table scan becomes |
586 | | * necessary. In such cases, comparing the entire tuple is not required, |
587 | | * since the remote tuple might not include all column values. Instead, |
588 | | * the indexed columns alone are sufficient to identify the target tuple |
589 | | * (see logicalrep_rel_mark_updatable). |
590 | | */ |
591 | 0 | indexbitmap = RelationGetIndexAttrBitmap(rel, |
592 | 0 | INDEX_ATTR_BITMAP_IDENTITY_KEY); |
593 | | |
594 | | /* fallback to PK if no replica identity */ |
595 | 0 | if (!indexbitmap) |
596 | 0 | indexbitmap = RelationGetIndexAttrBitmap(rel, |
597 | 0 | INDEX_ATTR_BITMAP_PRIMARY_KEY); |
598 | |
|
599 | 0 | eq = palloc0_array(TypeCacheEntry *, searchslot->tts_tupleDescriptor->natts); |
600 | | |
601 | | /* |
602 | | * Start a heap scan using SnapshotAny to identify dead tuples that are |
603 | | * not visible under a standard MVCC snapshot. Tuples from transactions |
604 | | * not yet committed or those just committed prior to the scan are |
605 | | * excluded in update_most_recent_deletion_info(). |
606 | | */ |
607 | 0 | scan = table_beginscan(rel, SnapshotAny, 0, NULL, |
608 | 0 | SO_NONE); |
609 | 0 | scanslot = table_slot_create(rel, NULL); |
610 | |
|
611 | 0 | table_rescan(scan, NULL); |
612 | | |
613 | | /* Try to find the tuple */ |
614 | 0 | while (table_scan_getnextslot(scan, ForwardScanDirection, scanslot)) |
615 | 0 | { |
616 | 0 | if (!tuples_equal(scanslot, searchslot, eq, indexbitmap)) |
617 | 0 | continue; |
618 | | |
619 | 0 | update_most_recent_deletion_info(scanslot, oldestxmin, delete_xid, |
620 | 0 | delete_time, delete_origin); |
621 | 0 | } |
622 | |
|
623 | 0 | table_endscan(scan); |
624 | 0 | ExecDropSingleTupleTableSlot(scanslot); |
625 | |
|
626 | 0 | return *delete_time != 0; |
627 | 0 | } |
628 | | |
629 | | /* |
630 | | * Similar to RelationFindDeletedTupleInfoSeq() but using index scan to locate |
631 | | * the deleted tuple. |
632 | | */ |
633 | | bool |
634 | | RelationFindDeletedTupleInfoByIndex(Relation rel, Oid idxoid, |
635 | | TupleTableSlot *searchslot, |
636 | | TransactionId oldestxmin, |
637 | | TransactionId *delete_xid, |
638 | | ReplOriginId *delete_origin, |
639 | | TimestampTz *delete_time) |
640 | 0 | { |
641 | 0 | Relation idxrel; |
642 | 0 | ScanKeyData skey[INDEX_MAX_KEYS]; |
643 | 0 | int skey_attoff; |
644 | 0 | IndexScanDesc scan; |
645 | 0 | TupleTableSlot *scanslot; |
646 | 0 | TypeCacheEntry **eq = NULL; |
647 | 0 | bool isIdxSafeToSkipDuplicates; |
648 | 0 | TupleDesc desc PG_USED_FOR_ASSERTS_ONLY = RelationGetDescr(rel); |
649 | |
|
650 | 0 | Assert(equalTupleDescs(desc, searchslot->tts_tupleDescriptor)); |
651 | 0 | Assert(OidIsValid(idxoid)); |
652 | |
|
653 | 0 | *delete_xid = InvalidTransactionId; |
654 | 0 | *delete_time = 0; |
655 | 0 | *delete_origin = InvalidReplOriginId; |
656 | |
|
657 | 0 | isIdxSafeToSkipDuplicates = (GetRelationIdentityOrPK(rel) == idxoid); |
658 | |
|
659 | 0 | scanslot = table_slot_create(rel, NULL); |
660 | |
|
661 | 0 | idxrel = index_open(idxoid, RowExclusiveLock); |
662 | | |
663 | | /* Build scan key. */ |
664 | 0 | skey_attoff = build_replindex_scan_key(skey, rel, idxrel, searchslot); |
665 | | |
666 | | /* |
667 | | * Start an index scan using SnapshotAny to identify dead tuples that are |
668 | | * not visible under a standard MVCC snapshot. Tuples from transactions |
669 | | * not yet committed or those just committed prior to the scan are |
670 | | * excluded in update_most_recent_deletion_info(). |
671 | | */ |
672 | 0 | scan = index_beginscan(rel, idxrel, |
673 | 0 | SnapshotAny, NULL, skey_attoff, 0, SO_NONE); |
674 | |
|
675 | 0 | index_rescan(scan, skey, skey_attoff, NULL, 0); |
676 | | |
677 | | /* Try to find the tuple */ |
678 | 0 | while (index_getnext_slot(scan, ForwardScanDirection, scanslot)) |
679 | 0 | { |
680 | | /* |
681 | | * Avoid expensive equality check if the index is primary key or |
682 | | * replica identity index. |
683 | | */ |
684 | 0 | if (!isIdxSafeToSkipDuplicates) |
685 | 0 | { |
686 | 0 | if (eq == NULL) |
687 | 0 | eq = palloc0_array(TypeCacheEntry *, scanslot->tts_tupleDescriptor->natts); |
688 | |
|
689 | 0 | if (!tuples_equal(scanslot, searchslot, eq, NULL)) |
690 | 0 | continue; |
691 | 0 | } |
692 | | |
693 | 0 | update_most_recent_deletion_info(scanslot, oldestxmin, delete_xid, |
694 | 0 | delete_time, delete_origin); |
695 | 0 | } |
696 | |
|
697 | 0 | index_endscan(scan); |
698 | |
|
699 | 0 | index_close(idxrel, NoLock); |
700 | |
|
701 | 0 | ExecDropSingleTupleTableSlot(scanslot); |
702 | |
|
703 | 0 | return *delete_time != 0; |
704 | 0 | } |
705 | | |
706 | | /* |
707 | | * Find the tuple that violates the passed unique index (conflictindex). |
708 | | * |
709 | | * If the conflicting tuple is found return true, otherwise false. |
710 | | * |
711 | | * We lock the tuple to avoid getting it deleted before the caller can fetch |
712 | | * the required information. Note that if the tuple is deleted before a lock |
713 | | * is acquired, we will retry to find the conflicting tuple again. |
714 | | */ |
715 | | static bool |
716 | | FindConflictTuple(ResultRelInfo *resultRelInfo, EState *estate, |
717 | | Oid conflictindex, TupleTableSlot *slot, |
718 | | TupleTableSlot **conflictslot) |
719 | 0 | { |
720 | 0 | Relation rel = resultRelInfo->ri_RelationDesc; |
721 | 0 | ItemPointerData conflictTid; |
722 | 0 | TM_FailureData tmfd; |
723 | 0 | TM_Result res; |
724 | |
|
725 | 0 | *conflictslot = NULL; |
726 | | |
727 | | /* |
728 | | * Build additional information required to check constraints violations. |
729 | | * See check_exclusion_or_unique_constraint(). |
730 | | */ |
731 | 0 | BuildConflictIndexInfo(resultRelInfo, conflictindex); |
732 | |
|
733 | 0 | retry: |
734 | 0 | if (ExecCheckIndexConstraints(resultRelInfo, slot, estate, |
735 | 0 | &conflictTid, &slot->tts_tid, |
736 | 0 | list_make1_oid(conflictindex))) |
737 | 0 | { |
738 | 0 | if (*conflictslot) |
739 | 0 | ExecDropSingleTupleTableSlot(*conflictslot); |
740 | |
|
741 | 0 | *conflictslot = NULL; |
742 | 0 | return false; |
743 | 0 | } |
744 | | |
745 | 0 | *conflictslot = table_slot_create(rel, NULL); |
746 | |
|
747 | 0 | PushActiveSnapshot(GetLatestSnapshot()); |
748 | |
|
749 | 0 | res = table_tuple_lock(rel, &conflictTid, GetActiveSnapshot(), |
750 | 0 | *conflictslot, |
751 | 0 | GetCurrentCommandId(false), |
752 | 0 | LockTupleShare, |
753 | 0 | LockWaitBlock, |
754 | 0 | 0 /* don't follow updates */ , |
755 | 0 | &tmfd); |
756 | |
|
757 | 0 | PopActiveSnapshot(); |
758 | |
|
759 | 0 | if (should_refetch_tuple(res, &tmfd)) |
760 | 0 | goto retry; |
761 | | |
762 | 0 | return true; |
763 | 0 | } |
764 | | |
765 | | /* |
766 | | * Check all the unique indexes in 'recheckIndexes' for conflict with the |
767 | | * tuple in 'remoteslot' and report if found. |
768 | | */ |
769 | | static void |
770 | | CheckAndReportConflict(ResultRelInfo *resultRelInfo, EState *estate, |
771 | | ConflictType type, List *recheckIndexes, |
772 | | TupleTableSlot *searchslot, TupleTableSlot *remoteslot) |
773 | 0 | { |
774 | 0 | List *conflicttuples = NIL; |
775 | 0 | TupleTableSlot *conflictslot; |
776 | | |
777 | | /* Check all the unique indexes for conflicts */ |
778 | 0 | foreach_oid(uniqueidx, resultRelInfo->ri_onConflictArbiterIndexes) |
779 | 0 | { |
780 | 0 | if (list_member_oid(recheckIndexes, uniqueidx) && |
781 | 0 | FindConflictTuple(resultRelInfo, estate, uniqueidx, remoteslot, |
782 | 0 | &conflictslot)) |
783 | 0 | { |
784 | 0 | ConflictTupleInfo *conflicttuple = palloc0_object(ConflictTupleInfo); |
785 | |
|
786 | 0 | conflicttuple->slot = conflictslot; |
787 | 0 | conflicttuple->indexoid = uniqueidx; |
788 | |
|
789 | 0 | GetTupleTransactionInfo(conflictslot, &conflicttuple->xmin, |
790 | 0 | &conflicttuple->origin, &conflicttuple->ts); |
791 | |
|
792 | 0 | conflicttuples = lappend(conflicttuples, conflicttuple); |
793 | 0 | } |
794 | 0 | } |
795 | | |
796 | | /* Report the conflict, if found */ |
797 | 0 | if (conflicttuples) |
798 | 0 | ReportApplyConflict(estate, resultRelInfo, ERROR, |
799 | 0 | list_length(conflicttuples) > 1 ? CT_MULTIPLE_UNIQUE_CONFLICTS : type, |
800 | 0 | searchslot, remoteslot, conflicttuples); |
801 | 0 | } |
802 | | |
803 | | /* |
804 | | * Insert tuple represented in the slot to the relation, update the indexes, |
805 | | * and execute any constraints and per-row triggers. |
806 | | * |
807 | | * Caller is responsible for opening the indexes. |
808 | | */ |
809 | | void |
810 | | ExecSimpleRelationInsert(ResultRelInfo *resultRelInfo, |
811 | | EState *estate, TupleTableSlot *slot) |
812 | 0 | { |
813 | 0 | bool skip_tuple = false; |
814 | 0 | Relation rel = resultRelInfo->ri_RelationDesc; |
815 | | |
816 | | /* For now we support only tables. */ |
817 | 0 | Assert(rel->rd_rel->relkind == RELKIND_RELATION); |
818 | |
|
819 | 0 | CheckCmdReplicaIdentity(rel, CMD_INSERT); |
820 | | |
821 | | /* BEFORE ROW INSERT Triggers */ |
822 | 0 | if (resultRelInfo->ri_TrigDesc && |
823 | 0 | resultRelInfo->ri_TrigDesc->trig_insert_before_row) |
824 | 0 | { |
825 | 0 | if (!ExecBRInsertTriggers(estate, resultRelInfo, slot)) |
826 | 0 | skip_tuple = true; /* "do nothing" */ |
827 | 0 | } |
828 | |
|
829 | 0 | if (!skip_tuple) |
830 | 0 | { |
831 | 0 | List *recheckIndexes = NIL; |
832 | 0 | List *conflictindexes; |
833 | 0 | bool conflict = false; |
834 | | |
835 | | /* Compute stored generated columns */ |
836 | 0 | if (rel->rd_att->constr && |
837 | 0 | rel->rd_att->constr->has_generated_stored) |
838 | 0 | ExecComputeStoredGenerated(resultRelInfo, estate, slot, |
839 | 0 | CMD_INSERT); |
840 | | |
841 | | /* Check the constraints of the tuple */ |
842 | 0 | if (rel->rd_att->constr) |
843 | 0 | ExecConstraints(resultRelInfo, slot, estate); |
844 | 0 | if (rel->rd_rel->relispartition) |
845 | 0 | ExecPartitionCheck(resultRelInfo, slot, estate, true); |
846 | | |
847 | | /* OK, store the tuple and create index entries for it */ |
848 | 0 | simple_table_tuple_insert(resultRelInfo->ri_RelationDesc, slot); |
849 | |
|
850 | 0 | conflictindexes = resultRelInfo->ri_onConflictArbiterIndexes; |
851 | |
|
852 | 0 | if (resultRelInfo->ri_NumIndices > 0) |
853 | 0 | { |
854 | 0 | uint32 flags; |
855 | |
|
856 | 0 | if (conflictindexes != NIL) |
857 | 0 | flags = EIIT_NO_DUPE_ERROR; |
858 | 0 | else |
859 | 0 | flags = 0; |
860 | 0 | recheckIndexes = ExecInsertIndexTuples(resultRelInfo, |
861 | 0 | estate, flags, |
862 | 0 | slot, conflictindexes, |
863 | 0 | &conflict); |
864 | 0 | } |
865 | | |
866 | | /* |
867 | | * Checks the conflict indexes to fetch the conflicting local row and |
868 | | * reports the conflict. We perform this check here, instead of |
869 | | * performing an additional index scan before the actual insertion and |
870 | | * reporting the conflict if any conflicting rows are found. This is |
871 | | * to avoid the overhead of executing the extra scan for each INSERT |
872 | | * operation, even when no conflict arises, which could introduce |
873 | | * significant overhead to replication, particularly in cases where |
874 | | * conflicts are rare. |
875 | | * |
876 | | * XXX OTOH, this could lead to clean-up effort for dead tuples added |
877 | | * in heap and index in case of conflicts. But as conflicts shouldn't |
878 | | * be a frequent thing so we preferred to save the performance |
879 | | * overhead of extra scan before each insertion. |
880 | | */ |
881 | 0 | if (conflict) |
882 | 0 | CheckAndReportConflict(resultRelInfo, estate, CT_INSERT_EXISTS, |
883 | 0 | recheckIndexes, NULL, slot); |
884 | | |
885 | | /* AFTER ROW INSERT Triggers */ |
886 | 0 | ExecARInsertTriggers(estate, resultRelInfo, slot, |
887 | 0 | recheckIndexes, NULL); |
888 | | |
889 | | /* |
890 | | * XXX we should in theory pass a TransitionCaptureState object to the |
891 | | * above to capture transition tuples, but after statement triggers |
892 | | * don't actually get fired by replication yet anyway |
893 | | */ |
894 | |
|
895 | 0 | list_free(recheckIndexes); |
896 | 0 | } |
897 | 0 | } |
898 | | |
899 | | /* |
900 | | * Find the searchslot tuple and update it with data in the slot, |
901 | | * update the indexes, and execute any constraints and per-row triggers. |
902 | | * |
903 | | * Caller is responsible for opening the indexes. |
904 | | */ |
905 | | void |
906 | | ExecSimpleRelationUpdate(ResultRelInfo *resultRelInfo, |
907 | | EState *estate, EPQState *epqstate, |
908 | | TupleTableSlot *searchslot, TupleTableSlot *slot) |
909 | 0 | { |
910 | 0 | bool skip_tuple = false; |
911 | 0 | Relation rel = resultRelInfo->ri_RelationDesc; |
912 | 0 | ItemPointer tid = &(searchslot->tts_tid); |
913 | | |
914 | | /* |
915 | | * We support only non-system tables, with |
916 | | * check_publication_add_relation() accountable. |
917 | | */ |
918 | 0 | Assert(rel->rd_rel->relkind == RELKIND_RELATION); |
919 | 0 | Assert(!IsCatalogRelation(rel)); |
920 | |
|
921 | 0 | CheckCmdReplicaIdentity(rel, CMD_UPDATE); |
922 | | |
923 | | /* BEFORE ROW UPDATE Triggers */ |
924 | 0 | if (resultRelInfo->ri_TrigDesc && |
925 | 0 | resultRelInfo->ri_TrigDesc->trig_update_before_row) |
926 | 0 | { |
927 | 0 | if (!ExecBRUpdateTriggers(estate, epqstate, resultRelInfo, |
928 | 0 | tid, NULL, slot, NULL, NULL, false)) |
929 | 0 | skip_tuple = true; /* "do nothing" */ |
930 | 0 | } |
931 | |
|
932 | 0 | if (!skip_tuple) |
933 | 0 | { |
934 | 0 | List *recheckIndexes = NIL; |
935 | 0 | TU_UpdateIndexes update_indexes; |
936 | 0 | List *conflictindexes; |
937 | 0 | bool conflict = false; |
938 | | |
939 | | /* Compute stored generated columns */ |
940 | 0 | if (rel->rd_att->constr && |
941 | 0 | rel->rd_att->constr->has_generated_stored) |
942 | 0 | ExecComputeStoredGenerated(resultRelInfo, estate, slot, |
943 | 0 | CMD_UPDATE); |
944 | | |
945 | | /* Check the constraints of the tuple */ |
946 | 0 | if (rel->rd_att->constr) |
947 | 0 | ExecConstraints(resultRelInfo, slot, estate); |
948 | 0 | if (rel->rd_rel->relispartition) |
949 | 0 | ExecPartitionCheck(resultRelInfo, slot, estate, true); |
950 | |
|
951 | 0 | simple_table_tuple_update(rel, tid, slot, estate->es_snapshot, |
952 | 0 | &update_indexes); |
953 | |
|
954 | 0 | conflictindexes = resultRelInfo->ri_onConflictArbiterIndexes; |
955 | |
|
956 | 0 | if (resultRelInfo->ri_NumIndices > 0 && (update_indexes != TU_None)) |
957 | 0 | { |
958 | 0 | uint32 flags = EIIT_IS_UPDATE; |
959 | |
|
960 | 0 | if (conflictindexes != NIL) |
961 | 0 | flags |= EIIT_NO_DUPE_ERROR; |
962 | 0 | if (update_indexes == TU_Summarizing) |
963 | 0 | flags |= EIIT_ONLY_SUMMARIZING; |
964 | 0 | recheckIndexes = ExecInsertIndexTuples(resultRelInfo, |
965 | 0 | estate, flags, |
966 | 0 | slot, conflictindexes, |
967 | 0 | &conflict); |
968 | 0 | } |
969 | | |
970 | | /* |
971 | | * Refer to the comments above the call to CheckAndReportConflict() in |
972 | | * ExecSimpleRelationInsert to understand why this check is done at |
973 | | * this point. |
974 | | */ |
975 | 0 | if (conflict) |
976 | 0 | CheckAndReportConflict(resultRelInfo, estate, CT_UPDATE_EXISTS, |
977 | 0 | recheckIndexes, searchslot, slot); |
978 | | |
979 | | /* AFTER ROW UPDATE Triggers */ |
980 | 0 | ExecARUpdateTriggers(estate, resultRelInfo, |
981 | 0 | NULL, NULL, |
982 | 0 | tid, NULL, slot, |
983 | 0 | recheckIndexes, NULL, false); |
984 | |
|
985 | 0 | list_free(recheckIndexes); |
986 | 0 | } |
987 | 0 | } |
988 | | |
989 | | /* |
990 | | * Find the searchslot tuple and delete it, and execute any constraints |
991 | | * and per-row triggers. |
992 | | * |
993 | | * Caller is responsible for opening the indexes. |
994 | | */ |
995 | | void |
996 | | ExecSimpleRelationDelete(ResultRelInfo *resultRelInfo, |
997 | | EState *estate, EPQState *epqstate, |
998 | | TupleTableSlot *searchslot) |
999 | 0 | { |
1000 | 0 | bool skip_tuple = false; |
1001 | 0 | Relation rel = resultRelInfo->ri_RelationDesc; |
1002 | 0 | ItemPointer tid = &searchslot->tts_tid; |
1003 | |
|
1004 | 0 | CheckCmdReplicaIdentity(rel, CMD_DELETE); |
1005 | | |
1006 | | /* BEFORE ROW DELETE Triggers */ |
1007 | 0 | if (resultRelInfo->ri_TrigDesc && |
1008 | 0 | resultRelInfo->ri_TrigDesc->trig_delete_before_row) |
1009 | 0 | { |
1010 | 0 | skip_tuple = !ExecBRDeleteTriggers(estate, epqstate, resultRelInfo, |
1011 | 0 | tid, NULL, NULL, NULL, NULL, false); |
1012 | 0 | } |
1013 | |
|
1014 | 0 | if (!skip_tuple) |
1015 | 0 | { |
1016 | | /* OK, delete the tuple */ |
1017 | 0 | simple_table_tuple_delete(rel, tid, estate->es_snapshot); |
1018 | | |
1019 | | /* AFTER ROW DELETE Triggers */ |
1020 | 0 | ExecARDeleteTriggers(estate, resultRelInfo, |
1021 | 0 | tid, NULL, NULL, false); |
1022 | 0 | } |
1023 | 0 | } |
1024 | | |
1025 | | /* |
1026 | | * Check if command can be executed with current replica identity. |
1027 | | */ |
1028 | | void |
1029 | | CheckCmdReplicaIdentity(Relation rel, CmdType cmd) |
1030 | 0 | { |
1031 | 0 | PublicationDesc pubdesc; |
1032 | | |
1033 | | /* |
1034 | | * Skip checking the replica identity for partitioned tables, because the |
1035 | | * operations are actually performed on the leaf partitions. |
1036 | | */ |
1037 | 0 | if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE) |
1038 | 0 | return; |
1039 | | |
1040 | | /* We only need to do checks for UPDATE and DELETE. */ |
1041 | 0 | if (cmd != CMD_UPDATE && cmd != CMD_DELETE) |
1042 | 0 | return; |
1043 | | |
1044 | | /* |
1045 | | * It is only safe to execute UPDATE/DELETE if the relation does not |
1046 | | * publish UPDATEs or DELETEs, or all the following conditions are |
1047 | | * satisfied: |
1048 | | * |
1049 | | * 1. All columns, referenced in the row filters from publications which |
1050 | | * the relation is in, are valid - i.e. when all referenced columns are |
1051 | | * part of REPLICA IDENTITY. |
1052 | | * |
1053 | | * 2. All columns, referenced in the column lists are valid - i.e. when |
1054 | | * all columns referenced in the REPLICA IDENTITY are covered by the |
1055 | | * column list. |
1056 | | * |
1057 | | * 3. All generated columns in REPLICA IDENTITY of the relation, are valid |
1058 | | * - i.e. when all these generated columns are published. |
1059 | | * |
1060 | | * XXX We could optimize it by first checking whether any of the |
1061 | | * publications have a row filter or column list for this relation, or if |
1062 | | * the relation contains a generated column. If none of these exist and |
1063 | | * the relation has replica identity then we can avoid building the |
1064 | | * descriptor but as this happens only one time it doesn't seem worth the |
1065 | | * additional complexity. |
1066 | | */ |
1067 | 0 | RelationBuildPublicationDesc(rel, &pubdesc); |
1068 | 0 | if (cmd == CMD_UPDATE && !pubdesc.rf_valid_for_update) |
1069 | 0 | ereport(ERROR, |
1070 | 0 | (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), |
1071 | 0 | errmsg("cannot update table \"%s\"", |
1072 | 0 | RelationGetRelationName(rel)), |
1073 | 0 | errdetail("Column used in the publication WHERE expression is not part of the replica identity."))); |
1074 | 0 | else if (cmd == CMD_UPDATE && !pubdesc.cols_valid_for_update) |
1075 | 0 | ereport(ERROR, |
1076 | 0 | (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), |
1077 | 0 | errmsg("cannot update table \"%s\"", |
1078 | 0 | RelationGetRelationName(rel)), |
1079 | 0 | errdetail("Column list used by the publication does not cover the replica identity."))); |
1080 | 0 | else if (cmd == CMD_UPDATE && !pubdesc.gencols_valid_for_update) |
1081 | 0 | ereport(ERROR, |
1082 | 0 | (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), |
1083 | 0 | errmsg("cannot update table \"%s\"", |
1084 | 0 | RelationGetRelationName(rel)), |
1085 | 0 | errdetail("Replica identity must not contain unpublished generated columns."))); |
1086 | 0 | else if (cmd == CMD_DELETE && !pubdesc.rf_valid_for_delete) |
1087 | 0 | ereport(ERROR, |
1088 | 0 | (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), |
1089 | 0 | errmsg("cannot delete from table \"%s\"", |
1090 | 0 | RelationGetRelationName(rel)), |
1091 | 0 | errdetail("Column used in the publication WHERE expression is not part of the replica identity."))); |
1092 | 0 | else if (cmd == CMD_DELETE && !pubdesc.cols_valid_for_delete) |
1093 | 0 | ereport(ERROR, |
1094 | 0 | (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), |
1095 | 0 | errmsg("cannot delete from table \"%s\"", |
1096 | 0 | RelationGetRelationName(rel)), |
1097 | 0 | errdetail("Column list used by the publication does not cover the replica identity."))); |
1098 | 0 | else if (cmd == CMD_DELETE && !pubdesc.gencols_valid_for_delete) |
1099 | 0 | ereport(ERROR, |
1100 | 0 | (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), |
1101 | 0 | errmsg("cannot delete from table \"%s\"", |
1102 | 0 | RelationGetRelationName(rel)), |
1103 | 0 | errdetail("Replica identity must not contain unpublished generated columns."))); |
1104 | | |
1105 | | /* If relation has replica identity we are always good. */ |
1106 | 0 | if (OidIsValid(RelationGetReplicaIndex(rel))) |
1107 | 0 | return; |
1108 | | |
1109 | | /* REPLICA IDENTITY FULL is also good for UPDATE/DELETE. */ |
1110 | 0 | if (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL) |
1111 | 0 | return; |
1112 | | |
1113 | | /* |
1114 | | * This is UPDATE/DELETE and there is no replica identity. |
1115 | | * |
1116 | | * Check if the table publishes UPDATES or DELETES. |
1117 | | */ |
1118 | 0 | if (cmd == CMD_UPDATE && pubdesc.pubactions.pubupdate) |
1119 | 0 | ereport(ERROR, |
1120 | 0 | (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
1121 | 0 | errmsg("cannot update table \"%s\" because it does not have a replica identity and publishes updates", |
1122 | 0 | RelationGetRelationName(rel)), |
1123 | 0 | errhint("To enable updating the table, set REPLICA IDENTITY using ALTER TABLE."))); |
1124 | 0 | else if (cmd == CMD_DELETE && pubdesc.pubactions.pubdelete) |
1125 | 0 | ereport(ERROR, |
1126 | 0 | (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
1127 | 0 | errmsg("cannot delete from table \"%s\" because it does not have a replica identity and publishes deletes", |
1128 | 0 | RelationGetRelationName(rel)), |
1129 | 0 | errhint("To enable deleting from the table, set REPLICA IDENTITY using ALTER TABLE."))); |
1130 | 0 | } |
1131 | | |
1132 | | |
1133 | | /* |
1134 | | * Check if we support writing into specific relkind of local relation and check |
1135 | | * if it aligns with the relkind of the relation on the publisher. |
1136 | | * |
1137 | | * The nspname and relname are only needed for error reporting. |
1138 | | */ |
1139 | | void |
1140 | | CheckSubscriptionRelkind(char localrelkind, char remoterelkind, |
1141 | | const char *nspname, const char *relname) |
1142 | 0 | { |
1143 | 0 | if (localrelkind != RELKIND_RELATION && |
1144 | 0 | localrelkind != RELKIND_PARTITIONED_TABLE && |
1145 | 0 | localrelkind != RELKIND_SEQUENCE) |
1146 | 0 | ereport(ERROR, |
1147 | 0 | (errcode(ERRCODE_WRONG_OBJECT_TYPE), |
1148 | 0 | errmsg("cannot use relation \"%s.%s\" as logical replication target", |
1149 | 0 | nspname, relname), |
1150 | 0 | errdetail_relkind_not_supported(localrelkind))); |
1151 | | |
1152 | | /* |
1153 | | * Allow RELKIND_RELATION and RELKIND_PARTITIONED_TABLE to be treated |
1154 | | * interchangeably, but ensure that sequences (RELKIND_SEQUENCE) match |
1155 | | * exactly on both publisher and subscriber. |
1156 | | */ |
1157 | 0 | if ((localrelkind == RELKIND_SEQUENCE && remoterelkind != RELKIND_SEQUENCE) || |
1158 | 0 | (localrelkind != RELKIND_SEQUENCE && remoterelkind == RELKIND_SEQUENCE)) |
1159 | 0 | ereport(ERROR, |
1160 | 0 | errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
1161 | | /* translator: 3rd and 4th %s are "sequence" or "table" */ |
1162 | 0 | errmsg("relation \"%s.%s\" type mismatch: source \"%s\", target \"%s\"", |
1163 | 0 | nspname, relname, |
1164 | 0 | remoterelkind == RELKIND_SEQUENCE ? "sequence" : "table", |
1165 | 0 | localrelkind == RELKIND_SEQUENCE ? "sequence" : "table")); |
1166 | 0 | } |