@reventlessdev/reventless-postgres 3.0.0-alpha.2 → 3.0.0-alpha.200

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/CHANGELOG.md +1584 -0
  2. package/README.md +83 -0
  3. package/lib/bs/.compiler.log +2 -2
  4. package/lib/bs/ReventlessPostgres.cmi +0 -0
  5. package/lib/bs/ReventlessPostgres.cmt +0 -0
  6. package/lib/bs/ReventlessPostgres.mlmap +4 -1
  7. package/lib/bs/compiler-info.json +4 -4
  8. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  9. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  10. package/lib/bs/src/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  11. package/lib/bs/src/DcbEventLogStorage_Postgres.ast +0 -0
  12. package/lib/bs/src/DcbEventLogStorage_Postgres.res +25 -340
  13. package/lib/bs/src/DcbEventLogStorage_Postgres.res.mjs +38 -348
  14. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  15. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  16. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  17. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.ast +0 -0
  18. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res +422 -0
  19. package/lib/bs/src/DcbEventLogStorage_Postgres_Ops.res.mjs +437 -0
  20. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  21. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmj +0 -0
  22. package/lib/bs/src/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  23. package/lib/bs/src/EventLogChangeFeed.ast +0 -0
  24. package/lib/bs/src/EventLogChangeFeed.res +193 -0
  25. package/lib/bs/src/EventLogChangeFeed.res.mjs +134 -0
  26. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  27. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  28. package/lib/bs/src/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  29. package/lib/bs/src/EventLogStorage_Postgres.ast +0 -0
  30. package/lib/bs/src/EventLogStorage_Postgres.res +12 -170
  31. package/lib/bs/src/EventLogStorage_Postgres.res.mjs +23 -186
  32. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  33. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  34. package/lib/bs/src/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  35. package/lib/bs/src/EventLogStorage_Postgres_Ops.ast +0 -0
  36. package/lib/bs/src/EventLogStorage_Postgres_Ops.res +240 -0
  37. package/lib/bs/src/EventLogStorage_Postgres_Ops.res.mjs +232 -0
  38. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  39. package/lib/bs/src/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  40. package/lib/bs/src/PgChangeFeed.ast +0 -0
  41. package/lib/bs/src/PgChangeFeed.res +21 -9
  42. package/lib/bs/src/PgChangeFeed.res.mjs +6 -6
  43. package/lib/bs/src/PgDriver-ReventlessPostgres.cmi +0 -0
  44. package/lib/bs/src/PgDriver-ReventlessPostgres.cmt +0 -0
  45. package/lib/bs/src/PgDriver-ReventlessPostgres.cmti +0 -0
  46. package/lib/bs/src/PgDriver.ast +0 -0
  47. package/lib/bs/src/PgDriver.iast +0 -0
  48. package/lib/bs/src/PgDriver.res +1 -2
  49. package/lib/bs/src/PgSchema-ReventlessPostgres.cmi +0 -0
  50. package/lib/bs/src/PgSchema-ReventlessPostgres.cmt +0 -0
  51. package/lib/bs/src/PgSchema.ast +0 -0
  52. package/lib/bs/src/PgSchema.res +44 -2
  53. package/lib/bs/src/PgSchema.res.mjs +13 -3
  54. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  55. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmj +0 -0
  56. package/lib/bs/src/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  57. package/lib/bs/src/QueryDbStorage_Postgres.ast +0 -0
  58. package/lib/bs/src/QueryDbStorage_Postgres.res +16 -213
  59. package/lib/bs/src/QueryDbStorage_Postgres.res.mjs +27 -191
  60. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  61. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  62. package/lib/bs/src/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  63. package/lib/bs/src/QueryDbStorage_Postgres_Ops.ast +0 -0
  64. package/lib/bs/src/QueryDbStorage_Postgres_Ops.res +229 -0
  65. package/lib/bs/src/QueryDbStorage_Postgres_Ops.res.mjs +222 -0
  66. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  67. package/lib/bs/src/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  68. package/lib/bs/src/QueryEnginePostgres.ast +0 -0
  69. package/lib/bs/src/QueryEnginePostgres.res +372 -14
  70. package/lib/bs/src/QueryEnginePostgres.res.mjs +203 -15
  71. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
  72. package/lib/bs/tests/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
  73. package/lib/bs/tests/PostgresIntegrationTest.ast +0 -0
  74. package/lib/bs/tests/PostgresIntegrationTest.res +168 -47
  75. package/lib/bs/tests/PostgresIntegrationTest.res.mjs +50 -1
  76. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmi +0 -0
  77. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmj +0 -0
  78. package/lib/bs/tests/QueryEnginePostgresListPageTest-ReventlessPostgres.cmt +0 -0
  79. package/lib/bs/tests/QueryEnginePostgresListPageTest.ast +0 -0
  80. package/lib/bs/tests/QueryEnginePostgresListPageTest.res +335 -0
  81. package/lib/bs/tests/QueryEnginePostgresListPageTest.res.mjs +461 -0
  82. package/lib/ocaml/.compiler.log +2 -2
  83. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  84. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  85. package/lib/ocaml/DcbEventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  86. package/lib/ocaml/DcbEventLogStorage_Postgres.ast +0 -0
  87. package/lib/ocaml/DcbEventLogStorage_Postgres.res +25 -340
  88. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  89. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  90. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  91. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.ast +0 -0
  92. package/lib/ocaml/DcbEventLogStorage_Postgres_Ops.res +422 -0
  93. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmi +0 -0
  94. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmj +0 -0
  95. package/lib/ocaml/EventLogChangeFeed-ReventlessPostgres.cmt +0 -0
  96. package/lib/ocaml/EventLogChangeFeed.ast +0 -0
  97. package/lib/ocaml/EventLogChangeFeed.res +193 -0
  98. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmi +0 -0
  99. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmj +0 -0
  100. package/lib/ocaml/EventLogStorage_Postgres-ReventlessPostgres.cmt +0 -0
  101. package/lib/ocaml/EventLogStorage_Postgres.ast +0 -0
  102. package/lib/ocaml/EventLogStorage_Postgres.res +12 -170
  103. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  104. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  105. package/lib/ocaml/EventLogStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  106. package/lib/ocaml/EventLogStorage_Postgres_Ops.ast +0 -0
  107. package/lib/ocaml/EventLogStorage_Postgres_Ops.res +240 -0
  108. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmi +0 -0
  109. package/lib/ocaml/PgChangeFeed-ReventlessPostgres.cmt +0 -0
  110. package/lib/ocaml/PgChangeFeed.ast +0 -0
  111. package/lib/ocaml/PgChangeFeed.res +21 -9
  112. package/lib/ocaml/PgDriver-ReventlessPostgres.cmi +0 -0
  113. package/lib/ocaml/PgDriver-ReventlessPostgres.cmt +0 -0
  114. package/lib/ocaml/PgDriver-ReventlessPostgres.cmti +0 -0
  115. package/lib/ocaml/PgDriver.ast +0 -0
  116. package/lib/ocaml/PgDriver.iast +0 -0
  117. package/lib/ocaml/PgDriver.res +1 -2
  118. package/lib/ocaml/PgSchema-ReventlessPostgres.cmi +0 -0
  119. package/lib/ocaml/PgSchema-ReventlessPostgres.cmt +0 -0
  120. package/lib/ocaml/PgSchema.ast +0 -0
  121. package/lib/ocaml/PgSchema.res +44 -2
  122. package/lib/ocaml/PostgresIntegrationTest.res +168 -47
  123. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmi +0 -0
  124. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmj +0 -0
  125. package/lib/ocaml/QueryDbStorage_Postgres-ReventlessPostgres.cmt +0 -0
  126. package/lib/ocaml/QueryDbStorage_Postgres.ast +0 -0
  127. package/lib/ocaml/QueryDbStorage_Postgres.res +16 -213
  128. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmi +0 -0
  129. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmj +0 -0
  130. package/lib/ocaml/QueryDbStorage_Postgres_Ops-ReventlessPostgres.cmt +0 -0
  131. package/lib/ocaml/QueryDbStorage_Postgres_Ops.ast +0 -0
  132. package/lib/ocaml/QueryDbStorage_Postgres_Ops.res +229 -0
  133. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmi +0 -0
  134. package/lib/ocaml/QueryEnginePostgres-ReventlessPostgres.cmt +0 -0
  135. package/lib/ocaml/QueryEnginePostgres.ast +0 -0
  136. package/lib/ocaml/QueryEnginePostgres.res +372 -14
  137. package/lib/ocaml/QueryEnginePostgresListPageTest.res +335 -0
  138. package/lib/ocaml/ReventlessPostgres.cmi +0 -0
  139. package/lib/ocaml/ReventlessPostgres.cmt +0 -0
  140. package/lib/ocaml/ReventlessPostgres.mlmap +5 -0
  141. package/package.json +14 -14
  142. package/rescript.json +2 -1
  143. package/src/DcbEventLogStorage_Postgres.res +25 -340
  144. package/src/DcbEventLogStorage_Postgres.res.mjs +38 -348
  145. package/src/DcbEventLogStorage_Postgres_Ops.res +422 -0
  146. package/src/DcbEventLogStorage_Postgres_Ops.res.mjs +437 -0
  147. package/src/EventLogChangeFeed.res +193 -0
  148. package/src/EventLogChangeFeed.res.mjs +134 -0
  149. package/src/EventLogStorage_Postgres.res +12 -170
  150. package/src/EventLogStorage_Postgres.res.mjs +23 -186
  151. package/src/EventLogStorage_Postgres_Ops.res +240 -0
  152. package/src/EventLogStorage_Postgres_Ops.res.mjs +232 -0
  153. package/src/PgChangeFeed.res +21 -9
  154. package/src/PgChangeFeed.res.mjs +6 -6
  155. package/src/PgDriver.res +1 -2
  156. package/src/PgSchema.res +44 -2
  157. package/src/PgSchema.res.mjs +13 -3
  158. package/src/QueryDbStorage_Postgres.res +16 -213
  159. package/src/QueryDbStorage_Postgres.res.mjs +27 -191
  160. package/src/QueryDbStorage_Postgres_Ops.res +229 -0
  161. package/src/QueryDbStorage_Postgres_Ops.res.mjs +222 -0
  162. package/src/QueryEnginePostgres.res +372 -14
  163. package/src/QueryEnginePostgres.res.mjs +203 -15
  164. package/tests/PostgresIntegrationTest.res +168 -47
  165. package/tests/QueryEnginePostgresListPageTest.res +335 -0
  166. package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmi +0 -0
  167. package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmj +0 -0
  168. package/lib/ocaml/PostgresIntegrationTest-ReventlessPostgres.cmt +0 -0
  169. package/lib/ocaml/PostgresIntegrationTest.ast +0 -0
  170. package/tests/PostgresIntegrationTest.res.mjs +0 -328
@@ -0,0 +1,422 @@
1
+ // Runtime-pure DCB event-log operations for the Postgres backend.
2
+ //
3
+ // Holds everything the DEPLOYED Lambda runtime needs and imports NO
4
+ // `@pulumi/pulumi` (deploy-time only). The deployed DCB command entry point pulls
5
+ // these ops in unconditionally at cold start, so a deploy-time dependency here
6
+ // would leak into the Lambda import graph and fail resolution on real Lambda. The
7
+ // thin `DcbEventLogStorage_Postgres` wrapper `include`s this and adds the
8
+ // deploy-time `Pulumi.Output`-shaped adapter for local/deploy consumers.
9
+ // See docs/plans/done/deployed-lambda-esm-self-containment.md (Rung-3 finding).
10
+
11
+ open ReventlessCore
12
+ open Reventless
13
+
14
+ // Lock strategy for concurrent appends (B4). Advisory (default) is safe under
15
+ // PgBouncer transaction mode; RowLocks avoids advisory-lock connection pinning
16
+ // on RDS Proxy at the cost of a companion `dcb_scope` table.
17
+ type lockStrategy = [#AdvisoryLocks | #RowLocks]
18
+ let strategyParam = s =>
19
+ switch s {
20
+ | #AdvisoryLocks => "advisory"
21
+ | #RowLocks => "rows"
22
+ }
23
+
24
+ // Injected by reventless-local to feed the projection-checkpoint low-watermark
25
+ // (ProjectionPending, Dcb axis). Standalone/deploy use passes nothing.
26
+ type onAppended = array<(string, int)> => unit
27
+ let noTracking: onAppended = _ => ()
28
+
29
+ // SELECT list shared by read and readStream. `data`/`meta` are jsonb (returned
30
+ // already parsed); `tags` is text[] (a JS array); the cursor and recorded_at are
31
+ // computed in SQL so the row maps straight to a rawSequencedEvent.
32
+ let selectColumns = "
33
+ lpad(transaction_id::text, 20, '0') || ':' || lpad(position::text, 20, '0') AS cursor,
34
+ event_type,
35
+ tags,
36
+ data,
37
+ meta,
38
+ to_char(recorded_at AT TIME ZONE 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.MS\"Z\"') AS recorded_at
39
+ "
40
+
41
+ // Decode a 'key=value' tag string (split on the first '=') back to a DcbTag.tag.
42
+ let decodeTag = (s: string): DcbTag.tag =>
43
+ switch s->String.indexOf("=") {
44
+ | -1 => {key: s, value: ""}
45
+ | i => {
46
+ key: s->String.slice(~start=0, ~end=i),
47
+ value: s->String.slice(~start=i + 1, ~end=s->String.length),
48
+ }
49
+ }
50
+
51
+ let decodeTags = (v: option<JSON.t>): array<DcbTag.tag> =>
52
+ switch v {
53
+ | Some(JSON.Array(arr)) =>
54
+ arr->Array.filterMap(x =>
55
+ switch x {
56
+ | JSON.String(s) => Some(decodeTag(s))
57
+ | _ => None
58
+ }
59
+ )
60
+ | _ => []
61
+ }
62
+
63
+ let rowToEvent = (row: dict<JSON.t>): DcbEventLog_Adapter.rawSequencedEvent => {
64
+ let str = key =>
65
+ switch row->Dict.get(key) {
66
+ | Some(JSON.String(s)) => s
67
+ | _ => ""
68
+ }
69
+ let meta = switch row->Dict.get("meta") {
70
+ | Some(metaJson) => metaJson->Reventless.Util_Sury.fromJson(Reventless.Message.metaSchema)
71
+ | None => JsError.throwWithMessage("missing meta column in dcb_event row")
72
+ }
73
+ {
74
+ DcbEventLog_Adapter.position: str("cursor"),
75
+ eventType: str("event_type"),
76
+ data: row->Dict.get("data")->Option.getOr(JSON.Encode.null),
77
+ tags: decodeTags(row->Dict.get("tags")),
78
+ meta,
79
+ recordedAt: str("recorded_at"),
80
+ }
81
+ }
82
+
83
+ // A tiny positional-parameter builder. Postgres uses `$1, $2, …` (not `?`), so we
84
+ // accumulate params and mint tokens as we build the WHERE. A token may be reused
85
+ // (same `$N`) to reference one param twice — used for the `after` cursor columns.
86
+ type paramBuilder = {mutable params: array<JSON.t>}
87
+ let mkBuilder = () => {params: []}
88
+ let param = (b: paramBuilder, v: JSON.t): string => {
89
+ b.params->Array.push(v)
90
+ "$" ++ Int.toString(b.params->Array.length)
91
+ }
92
+
93
+ // Build the read WHERE + params for a query. `applyFence` gates the xmin read
94
+ // barrier (true for consumer reads, so cursors are stable). `after` is the
95
+ // decoded (tx, pos) cursor pair as numeric strings.
96
+ let buildReadWhere = (
97
+ b: paramBuilder,
98
+ ~name: string,
99
+ ~query: DcbTag.query,
100
+ ~after: option<(string, string)>,
101
+ ~applyFence: bool,
102
+ ): string => {
103
+ let parts = [`log_name = ${b->param(JSON.Encode.string(name))}`]
104
+ if applyFence {
105
+ parts->Array.push("transaction_id < pg_snapshot_xmin(pg_current_snapshot())")
106
+ }
107
+ switch after {
108
+ | Some((tx, pos)) =>
109
+ let txP = b->param(JSON.Encode.string(tx))
110
+ let posP = b->param(JSON.Encode.string(pos))
111
+ parts->Array.push(
112
+ `(transaction_id > ${txP}::xid8 OR (transaction_id = ${txP}::xid8 AND position > ${posP}::bigint))`,
113
+ )
114
+ | None => ()
115
+ }
116
+
117
+ if query->Array.length > 0 {
118
+ let clauseSqls = query->Array.map(clause => {
119
+ let clauseParts = []
120
+ switch clause.eventTypes {
121
+ // A clause with no event type matches nothing (`DcbTag.clauseMatchesNothing`).
122
+ | Some([]) => clauseParts->Array.push("FALSE")
123
+ | Some(types) =>
124
+ let ph = types->Array.map(t => b->param(JSON.Encode.string(t)))->Array.join(",")
125
+ clauseParts->Array.push(`event_type IN (${ph})`)
126
+ | None => ()
127
+ }
128
+ switch clause.tags {
129
+ | Some(tags) if tags->Array.length > 0 =>
130
+ let arr =
131
+ tags
132
+ ->Array.map(t => b->param(JSON.Encode.string(t.key ++ "=" ++ t.value)))
133
+ ->Array.join(",")
134
+ clauseParts->Array.push(`tags @> ARRAY[${arr}]::text[]`)
135
+ | _ => ()
136
+ }
137
+ clauseParts->Array.length == 0 ? "TRUE" : "(" ++ clauseParts->Array.join(" AND ") ++ ")"
138
+ })
139
+ parts->Array.push("(" ++ clauseSqls->Array.join(" OR ") ++ ")")
140
+ }
141
+
142
+ parts->Array.join(" AND ")
143
+ }
144
+
145
+ // Strip the zero-padding a cursor carries for string-sortability, leaving a
146
+ // plain decimal both `::xid8` and `::bigint` parse unambiguously ("0" if empty).
147
+ let stripZeros = (s: string): string => {
148
+ let t = s->String.replaceRegExp(/^0+/, "")
149
+ t == "" ? "0" : t
150
+ }
151
+
152
+ // Split a '<xid8>:<position>' cursor into its two numeric-string columns.
153
+ let decodeCursor = (pos: DcbTag.sequencePosition): option<(string, string)> =>
154
+ switch pos->String.split(":") {
155
+ | [tx, p] => Some((stripZeros(tx), stripZeros(p)))
156
+ | _ => None
157
+ }
158
+
159
+ // Rows-per-page for readStream keyset pagination.
160
+ let pageSize = 500
161
+
162
+ let isConflict = (exn: exn): bool => {
163
+ let msg = exn->JsExn.fromException->Option.flatMap(JsExn.message)->Option.getOr("")
164
+ msg->String.includes("dcb_conflict") ||
165
+ msg->String.includes("duplicate key") ||
166
+ msg->String.includes("23505")
167
+ }
168
+
169
+ let countAll = async (pool: PgDriver.pool): int =>
170
+ switch await pool->PgDriver.queryOne("SELECT COUNT(*)::bigint AS c FROM dcb_event", []) {
171
+ | Some(row) =>
172
+ switch row->Dict.get("c") {
173
+ | Some(JSON.String(s)) => s->Int.fromString->Option.getOr(0)
174
+ | Some(JSON.Number(n)) => Float.toInt(n)
175
+ | _ => 0
176
+ }
177
+ | None => 0
178
+ }
179
+
180
+ /** Where a backwards read of one log starts: strictly below a position, or at
181
+ and below it. */
182
+ type bound =
183
+ | Below(float)
184
+ | AtMost(float)
185
+
186
+ /** One log's events newest first by `position`, narrowed to one correlation id
187
+ when given. Rows are `(position, event)`; the event's own `position` field is
188
+ the read cursor, as `read` returns it. */
189
+ let readBackwards = async (
190
+ pool: PgDriver.pool,
191
+ ~name: string,
192
+ ~bound: option<bound>,
193
+ ~limit: int,
194
+ ~correlationId: option<string>,
195
+ ): array<(float, DcbEventLog_Adapter.rawSequencedEvent)> => {
196
+ let b = mkBuilder()
197
+ let parts = [`log_name = ${b->param(JSON.Encode.string(name))}`]
198
+ switch bound {
199
+ | Some(Below(p)) => parts->Array.push(`position < ${b->param(JSON.Encode.float(p))}::bigint`)
200
+ | Some(AtMost(p)) => parts->Array.push(`position <= ${b->param(JSON.Encode.float(p))}::bigint`)
201
+ | None => ()
202
+ }
203
+ switch correlationId {
204
+ | Some(id) => parts->Array.push(`(meta->>'correlationId') = ${b->param(JSON.Encode.string(id))}`)
205
+ | None => ()
206
+ }
207
+ let limitP = b->param(JSON.Encode.int(limit))
208
+
209
+ (
210
+ await pool->PgDriver.query(
211
+ `SELECT ${selectColumns}, position::text AS pos_raw FROM dcb_event WHERE ${parts->Array.join(
212
+ " AND ",
213
+ )} ORDER BY position DESC LIMIT ${limitP}::int`,
214
+ b.params,
215
+ )
216
+ )->Array.map(row => (
217
+ switch row->Dict.get("pos_raw") {
218
+ | Some(JSON.String(s)) => s->Float.fromString->Option.getOr(0.)
219
+ | _ => 0.
220
+ },
221
+ rowToEvent(row),
222
+ ))
223
+ }
224
+
225
+ /** The position of each event one append wrote, by message id. The cursor an
226
+ append returns names its transaction, and every event of the append shares
227
+ it. */
228
+ let positionsOfAppend = async (pool: PgDriver.pool, ~name: string, ~cursor: string): array<(
229
+ string,
230
+ float,
231
+ )> =>
232
+ switch decodeCursor(cursor) {
233
+ | None => []
234
+ | Some((tx, _)) =>
235
+ (
236
+ await pool->PgDriver.query(
237
+ "SELECT position::text AS pos, meta->>'msgId' AS msg_id FROM dcb_event WHERE log_name = $1 AND transaction_id = $2::xid8",
238
+ [JSON.Encode.string(name), JSON.Encode.string(tx)],
239
+ )
240
+ )->Array.filterMap(row =>
241
+ switch (row->Dict.get("msg_id"), row->Dict.get("pos")) {
242
+ | (Some(JSON.String(msgId)), Some(JSON.String(pos))) =>
243
+ pos->Float.fromString->Option.map(p => (msgId, p))
244
+ | _ => None
245
+ }
246
+ )
247
+ }
248
+
249
+ // Builds the DCB runtime op set bound to `pool`. Returns `(name, ops)`; the
250
+ // deploy-time `DcbEventLog_Adapter` wrapper lives in `DcbEventLogStorage_Postgres`.
251
+ let makeOps = (
252
+ ~pool: PgDriver.pool,
253
+ ~name: string,
254
+ ~indexes as _,
255
+ ~partitionTag as _,
256
+ ~crossPartitionTagKeys as _=?,
257
+ ~opts as _,
258
+ ~lockStrategy: lockStrategy=#AdvisoryLocks,
259
+ ~onAppended=noTracking,
260
+ ): (string, DcbEventLog_Adapter.operations) => {
261
+ // Fenced head cursor: the highest (transaction_id, position) visible under the
262
+ // xmin barrier. Stable across reads, so it is a safe consistency marker.
263
+ let currentHead = async (): option<string> =>
264
+ switch await pool->PgDriver.queryOne(
265
+ "SELECT lpad(transaction_id::text, 20, '0') || ':' || lpad(position::text, 20, '0') AS head
266
+ FROM dcb_event
267
+ WHERE log_name = $1 AND transaction_id < pg_snapshot_xmin(pg_current_snapshot())
268
+ ORDER BY transaction_id DESC, position DESC
269
+ LIMIT 1",
270
+ [JSON.Encode.string(name)],
271
+ ) {
272
+ | Some(row) =>
273
+ switch row->Dict.get("head") {
274
+ | Some(JSON.String(s)) => Some(s)
275
+ | _ => None
276
+ }
277
+ | None => None
278
+ }
279
+
280
+ let runQuery = async (~query, ~after): array<DcbEventLog_Adapter.rawSequencedEvent> => {
281
+ let b = mkBuilder()
282
+ let where = buildReadWhere(b, ~name, ~query, ~after, ~applyFence=true)
283
+ let sql = `SELECT ${selectColumns} FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC`
284
+ (await pool->PgDriver.query(sql, b.params))->Array.map(rowToEvent)
285
+ }
286
+
287
+ let read = async (~query: DcbTag.query, ~after=?): DcbEventLog_Adapter.rawReadResult => {
288
+ let afterCols = after->Option.flatMap(decodeCursor)
289
+ let events = await runQuery(~query, ~after=afterCols)
290
+ switch await currentHead() {
291
+ | Some(head) => {DcbEventLog_Adapter.events, headPosition: head}
292
+ | None => ({DcbEventLog_Adapter.events: events}: DcbEventLog_Adapter.rawReadResult)
293
+ }
294
+ }
295
+
296
+ let append = async (newEvents: array<DcbEventLog_Adapter.rawStoredEvent>, ~condition=?): result<
297
+ DcbTag.sequencePosition,
298
+ ReventlessInfra.DcbEventLog.appendError,
299
+ > => {
300
+ let eventsJson = JSON.Array(
301
+ newEvents->Array.map(ev => JSON.Object(
302
+ Dict.fromArray([
303
+ ("event_type", JSON.Encode.string(ev.eventType)),
304
+ (
305
+ "tags",
306
+ JSON.Array(ev.tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value))),
307
+ ),
308
+ ("data", ev.data),
309
+ ("meta", ev.meta->Reventless.Util_Sury.toJson(Reventless.Message.metaSchema)),
310
+ ]),
311
+ )),
312
+ )
313
+ let conditionParam = switch condition {
314
+ | Some(cond: DcbTag.appendCondition) =>
315
+ let queryJson = JSON.Array(
316
+ cond.query->Array.map(clause => JSON.Object(
317
+ Dict.fromArray([
318
+ (
319
+ "eventTypes",
320
+ switch clause.eventTypes {
321
+ | Some(types) => JSON.Array(types->Array.map(JSON.Encode.string))
322
+ | None => JSON.Encode.null
323
+ },
324
+ ),
325
+ (
326
+ "tags",
327
+ switch clause.tags {
328
+ | Some(tags) =>
329
+ JSON.Array(tags->Array.map(t => JSON.Encode.string(t.key ++ "=" ++ t.value)))
330
+ | None => JSON.Encode.null
331
+ },
332
+ ),
333
+ ]),
334
+ )),
335
+ )
336
+ let fields = [("query", queryJson)]
337
+ switch cond.after {
338
+ | Some(a) => fields->Array.push(("after", JSON.Encode.string(a)))
339
+ | None => ()
340
+ }
341
+ JSON.Encode.string(JSON.stringify(JSON.Object(Dict.fromArray(fields))))
342
+ | None => JSON.Encode.null
343
+ }
344
+
345
+ try {
346
+ let rows = await pool->PgDriver.query(
347
+ "SELECT dcb_append($1, $2::jsonb, $3::jsonb, $4) AS pos",
348
+ [
349
+ JSON.Encode.string(name),
350
+ JSON.Encode.string(JSON.stringify(eventsJson)),
351
+ conditionParam,
352
+ JSON.Encode.string(strategyParam(lockStrategy)),
353
+ ],
354
+ )
355
+ switch rows->Array.get(0)->Option.flatMap(r => r->Dict.get("pos")) {
356
+ | Some(JSON.String(cursor)) =>
357
+ // Feed the projection checkpoint (Dcb axis): (msgId, numeric position).
358
+ onAppended(
359
+ newEvents->Array.filterMap(ev =>
360
+ switch decodeCursor(cursor) {
361
+ | Some((_, pos)) =>
362
+ pos->Int.fromString->Option.map(p => (ev.meta.Reventless.Message.msgId, p))
363
+ | None => None
364
+ }
365
+ ),
366
+ )
367
+ Ok(cursor)
368
+ | _ => Error(ReventlessInfra.DcbEventLog.StorageFailure("dcb_append returned no position"))
369
+ }
370
+ } catch {
371
+ | exn =>
372
+ isConflict(exn)
373
+ ? Error(ReventlessInfra.DcbEventLog.Conflict)
374
+ : Error(
375
+ ReventlessInfra.DcbEventLog.StorageFailure(
376
+ exn
377
+ ->JsExn.fromException
378
+ ->Option.flatMap(JsExn.message)
379
+ ->Option.getOr("storage error"),
380
+ ),
381
+ )
382
+ }
383
+ }
384
+
385
+ // ~strongConsistency is a genuine no-op here: every read applies the xmin
386
+ // barrier, so it is always safely consistent (at the cost of a few ms lag).
387
+ //
388
+ // Keyset pagination on (transaction_id, position): one ordered SQL source, no
389
+ // per-clause fan-out / k-way merge / dedup (the DynamoDB runtime's layers have
390
+ // no Postgres counterpart). Each page reads strictly after the previous page's
391
+ // last row; a short page ends the stream.
392
+ let readStream = (~query: DcbTag.query, ~after=?, ~strongConsistency as _=?) =>
393
+ Stream.paginateEffect(after->Option.flatMap(decodeCursor), cursor =>
394
+ Effect.promise(() => {
395
+ let b = mkBuilder()
396
+ let where = buildReadWhere(b, ~name, ~query, ~after=cursor, ~applyFence=true)
397
+ let sql = `SELECT ${selectColumns}, transaction_id::text AS tx_raw, position::text AS pos_raw FROM dcb_event WHERE ${where} ORDER BY transaction_id ASC, position ASC LIMIT ${Int.toString(
398
+ pageSize,
399
+ )}`
400
+ pool->PgDriver.query(sql, b.params)
401
+ })->Effect.map(rows => {
402
+ let events = rows->Array.map(rowToEvent)
403
+ // Continue only on a full page; the next `after` is this page's last row.
404
+ let next = if rows->Array.length < pageSize {
405
+ None
406
+ } else {
407
+ switch rows->Array.get(rows->Array.length - 1) {
408
+ | Some(lastRow) =>
409
+ switch (lastRow->Dict.get("tx_raw"), lastRow->Dict.get("pos_raw")) {
410
+ | (Some(JSON.String(tx)), Some(JSON.String(p))) => Some(Some((tx, p)))
411
+ | _ => None
412
+ }
413
+ | None => None
414
+ }
415
+ }
416
+ (events, next)
417
+ })
418
+ )
419
+
420
+ let ops: DcbEventLog_Adapter.operations = {read, append, readStream}
421
+ (name, ops)
422
+ }