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
//! SY-40 on Postgres (#483, #484, ADR-0110): an ingest drops every foreign
//! event whose identity the store has recorded as removed, before it draws a
//! position for it.
//!
//! The record is `event_removed`, written by migration 4's `BEFORE DELETE`
//! trigger on `event`. The ingest's first round trip takes `LOCK TABLE
//! event_removed IN SHARE MODE` and, in the same batch, reads whether the
//! record holds anything at all ([`super::FIRST_ROUND_TRIP_SQL`]). Only when it
//! does is [`REMOVED_SQL`] sent, once per call, for every foreign identity the
//! call carries. A dropped event never reaches the insert: it draws no sequence
//! value, it is counted as skipped, it is neither landed nor hidden for SY-38's
//! check, and a group made only of dropped and held events lands nothing and
//! so writes no compensation (SY-11).
//!
//! # Why the lock is `SHARE`
//!
//! The trigger's insert takes `ROW EXCLUSIVE` on the record. `SHARE` is the
//! weakest mode that conflicts with it and not with itself, so concurrent
//! ingests do not wait on each other, appends never touch the table, and a
//! removal and an ingest exclude each other: an ingest waits for a removal in
//! flight to commit, and so reads its record, and a removal waits for an ingest
//! in flight to commit, and so deletes rows that ingest wrote rather than
//! racing it (I-6, D-8). A removal is a rare operator action, so the wait costs
//! nothing in practice; `SHARE` also waits behind a manual `VACUUM` of this one
//! small table, which autovacuum yields.
//!
//! The trigger is `BEFORE`, so a deleter takes the record's lock at its first
//! row, before that row is marked deleted. An ingest holding `SHARE` that meets
//! the row under `ON CONFLICT` sees a live row and skips it, and never waits on
//! the deleter, so the two cannot deadlock.
//!
//! # Held events
//!
//! An identity the store still holds is never looked up here: it is skipped by
//! the origin pair's unique index inside the insert, as before (VT-8). The
//! record is consulted only for identities a store once held and no longer
//! does, and only under the lock that excludes every removal, so it is never a
//! race-prone read-then-append.
use HashSet;
use EventId;
use ;
use OwnedGroup;
use cratePostgresEventStoreError;
use crate;
/// Of the origin pairs bound as two parallel arrays, those the record holds.
///
/// A point lookup per pair on the record's primary key. The arrays travel as
/// `bytea[]` and `bigint[]`, so the statement's text does not depend on the
/// batch's size.
pub const REMOVED_SQL: &str = "SELECT origin_store, origin_position FROM event_removed \
WHERE (origin_store, origin_position) IN \
(SELECT * FROM unnest($1::bytea[], $2::bigint[]))";
/// Drops from every group the foreign events whose identity the record holds.
///
/// Called only when the first round trip found the record non-empty, inside
/// the ingest's transaction and under its `SHARE` lock. Each group keeps its
/// `offered` count, so a dropped event is counted as skipped.
///
/// # Errors
///
/// `Driver` if the lookup fails, and the decoding variants if the record holds
/// a store identity that is not sixteen bytes or a position below one.
pub async