@syncular/server 0.81.0 → 0.82.1
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.
- package/dist/authoritative-query.js +3 -1
- package/dist/operations.js +3 -6
- package/dist/postgres-storage.js +522 -443
- package/dist/push.d.ts +5 -0
- package/dist/push.js +18 -2
- package/dist/relational-rows.d.ts +5 -3
- package/dist/relational-rows.js +6 -4
- package/dist/storage-errors.d.ts +10 -0
- package/dist/storage-errors.js +12 -0
- package/dist/storage.d.ts +21 -6
- package/package.json +2 -2
- package/src/authoritative-query.ts +3 -1
- package/src/operations.ts +3 -7
- package/src/postgres-storage.ts +696 -538
- package/src/push.ts +39 -1
- package/src/relational-rows.ts +6 -3
- package/src/storage-errors.ts +14 -0
- package/src/storage.ts +22 -5
package/src/postgres-storage.ts
CHANGED
|
@@ -125,13 +125,13 @@ import type {
|
|
|
125
125
|
IndexRowScanQuery,
|
|
126
126
|
LiveWindow,
|
|
127
127
|
LiveWindowQuery,
|
|
128
|
-
NewChange,
|
|
129
128
|
NewCommit,
|
|
130
129
|
NewReaction,
|
|
131
130
|
OutboxSequence,
|
|
132
131
|
OutboxSettlement,
|
|
133
132
|
PartitionRegistryEntry,
|
|
134
133
|
PreparedPushApply,
|
|
134
|
+
PushApplyOptions,
|
|
135
135
|
PrunedReactionCounts,
|
|
136
136
|
PushApplyCommit,
|
|
137
137
|
PushWatermark,
|
|
@@ -159,6 +159,7 @@ import type {
|
|
|
159
159
|
Tombstone,
|
|
160
160
|
} from './storage';
|
|
161
161
|
import {
|
|
162
|
+
ClosedCommitConstraintError,
|
|
162
163
|
isPostgresConstraintError,
|
|
163
164
|
StorageConstraintError,
|
|
164
165
|
} from './storage-errors';
|
|
@@ -358,15 +359,20 @@ interface SerializedResult {
|
|
|
358
359
|
details?: import('@syncular/core').RejectionDetails;
|
|
359
360
|
}
|
|
360
361
|
|
|
362
|
+
/** Lock the partition row; returns its `max_commit_seq`, which the lock freezes. */
|
|
361
363
|
async function lockPartitionOn(
|
|
362
364
|
client: PgQueryable,
|
|
363
365
|
partition: string,
|
|
364
|
-
): Promise<
|
|
365
|
-
const
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
366
|
+
): Promise<number> {
|
|
367
|
+
const lock = () =>
|
|
368
|
+
client.query<{ max_commit_seq: unknown }>(
|
|
369
|
+
'SELECT max_commit_seq FROM sync_partitions WHERE partition=$1 FOR UPDATE',
|
|
370
|
+
[partition],
|
|
371
|
+
);
|
|
372
|
+
const locked = await lock();
|
|
373
|
+
if (locked.rows[0] !== undefined) {
|
|
374
|
+
return asNumber(locked.rows[0].max_commit_seq);
|
|
375
|
+
}
|
|
370
376
|
// The first writer initializes the partition. Concurrent initializers may
|
|
371
377
|
// wait on this insert, so acquire the row lock again before applying writes.
|
|
372
378
|
await client.query(
|
|
@@ -374,10 +380,7 @@ async function lockPartitionOn(
|
|
|
374
380
|
ON CONFLICT (partition) DO NOTHING`,
|
|
375
381
|
[partition],
|
|
376
382
|
);
|
|
377
|
-
await
|
|
378
|
-
'SELECT max_commit_seq FROM sync_partitions WHERE partition=$1 FOR UPDATE',
|
|
379
|
-
[partition],
|
|
380
|
-
);
|
|
383
|
+
return asNumber((await lock()).rows[0]?.max_commit_seq);
|
|
381
384
|
}
|
|
382
385
|
|
|
383
386
|
/**
|
|
@@ -488,6 +491,7 @@ interface ServeGateRow {
|
|
|
488
491
|
pending: unknown;
|
|
489
492
|
results?: unknown;
|
|
490
493
|
delete_floor?: unknown;
|
|
494
|
+
watermark?: unknown;
|
|
491
495
|
}
|
|
492
496
|
|
|
493
497
|
/**
|
|
@@ -545,13 +549,22 @@ interface PrefetchRow {
|
|
|
545
549
|
type AdmissionRow = ServeGateRow & Partial<PrefetchRow>;
|
|
546
550
|
|
|
547
551
|
/**
|
|
548
|
-
* The push admission
|
|
549
|
-
*
|
|
550
|
-
*
|
|
551
|
-
*
|
|
552
|
-
* statement
|
|
553
|
-
*
|
|
554
|
-
*
|
|
552
|
+
* The push admission statement: the §9 settlement and §6.3.2 steps 1 and 2
|
|
553
|
+
* when the request carries an outbox, the RFC 0007 serve gate, the §2.3
|
|
554
|
+
* idempotency records of every commit of the request, and (when the caller
|
|
555
|
+
* already knows the commits' row targets) the read-ahead rows, in one
|
|
556
|
+
* statement with one snapshot. The caller takes the partition lock first:
|
|
557
|
+
* under READ COMMITTED a statement's snapshot is fixed at its start, so the
|
|
558
|
+
* read must be the statement *after* the lock to observe a duplicate that
|
|
559
|
+
* committed while this transaction waited for it.
|
|
560
|
+
*
|
|
561
|
+
* The settlement's writable CTEs see the same snapshot as the read, so they
|
|
562
|
+
* touch disjoint rows and the read leaves out the results they delete. §9
|
|
563
|
+
* (b): with no pre-upgrade result on the request's last commit, every
|
|
564
|
+
* remaining one outside the request left the outbox. (a) assigns the
|
|
565
|
+
* request's sequences to its pre-upgrade results at or above the new
|
|
566
|
+
* watermark; step 2 deletes the outbox's results below it, the pre-upgrade
|
|
567
|
+
* results of the request below it included.
|
|
555
568
|
*/
|
|
556
569
|
async function readAdmissionOn(
|
|
557
570
|
client: PgQueryable,
|
|
@@ -568,10 +581,12 @@ async function readAdmissionOn(
|
|
|
568
581
|
}[];
|
|
569
582
|
}
|
|
570
583
|
| undefined,
|
|
584
|
+
outbox: OutboxSettlement | undefined,
|
|
571
585
|
): Promise<{
|
|
572
586
|
gate: ServeGate;
|
|
573
587
|
persisted: ReadonlyMap<string, unknown>;
|
|
574
588
|
deleteFloor: number;
|
|
589
|
+
watermark: number | undefined;
|
|
575
590
|
prefetched: readonly PrefetchRow[];
|
|
576
591
|
}> {
|
|
577
592
|
const params: unknown[] = [
|
|
@@ -580,10 +595,57 @@ async function readAdmissionOn(
|
|
|
580
595
|
clientId,
|
|
581
596
|
JSON.stringify(clientCommitIds),
|
|
582
597
|
];
|
|
583
|
-
let
|
|
598
|
+
let settlement = '';
|
|
599
|
+
if (outbox !== undefined) {
|
|
600
|
+
const first = outbox.commits[0];
|
|
601
|
+
const last = outbox.commits.at(-1);
|
|
602
|
+
if (first === undefined || last === undefined) {
|
|
603
|
+
throw new Error('outbox settlement needs the request commits');
|
|
604
|
+
}
|
|
605
|
+
params.push(
|
|
606
|
+
outbox.outboxId,
|
|
607
|
+
JSON.stringify(
|
|
608
|
+
outbox.commits.map((commit) => ({
|
|
609
|
+
id: commit.clientCommitId,
|
|
610
|
+
seq: commit.clientSeq,
|
|
611
|
+
})),
|
|
612
|
+
),
|
|
613
|
+
last.clientCommitId,
|
|
614
|
+
first.clientSeq,
|
|
615
|
+
);
|
|
616
|
+
settlement = `WITH c AS (SELECT * FROM jsonb_to_recordset($6::text::jsonb) AS c(id text, seq bigint)),
|
|
617
|
+
w AS (
|
|
618
|
+
INSERT INTO sync_push_watermarks(partition, client_id, outbox_id, through_seq)
|
|
619
|
+
VALUES ($1,$3,$5,$8)
|
|
620
|
+
ON CONFLICT (partition, client_id, outbox_id) DO UPDATE
|
|
621
|
+
SET through_seq=GREATEST(sync_push_watermarks.through_seq, EXCLUDED.through_seq)
|
|
622
|
+
RETURNING through_seq),
|
|
623
|
+
b AS (DELETE FROM sync_push_results
|
|
624
|
+
WHERE partition=$1 AND client_id=$3 AND outbox_id IS NULL
|
|
625
|
+
AND client_commit_id NOT IN (SELECT id FROM c)
|
|
626
|
+
AND NOT EXISTS (SELECT 1 FROM sync_push_results
|
|
627
|
+
WHERE partition=$1 AND client_id=$3 AND client_commit_id=$7
|
|
628
|
+
AND outbox_id IS NULL)),
|
|
629
|
+
a AS (UPDATE sync_push_results r SET outbox_id=$5, client_seq=c.seq FROM c, w
|
|
630
|
+
WHERE r.partition=$1 AND r.client_id=$3 AND r.outbox_id IS NULL
|
|
631
|
+
AND r.client_commit_id=c.id AND c.seq>=w.through_seq),
|
|
632
|
+
expired AS (DELETE FROM sync_push_results r USING w
|
|
633
|
+
WHERE r.partition=$1 AND r.client_id=$3
|
|
634
|
+
AND ((r.outbox_id=$5 AND r.client_seq<w.through_seq)
|
|
635
|
+
OR (r.outbox_id IS NULL AND EXISTS (SELECT 1 FROM c
|
|
636
|
+
WHERE c.id=r.client_commit_id AND c.seq<w.through_seq)))
|
|
637
|
+
RETURNING r.client_commit_id)
|
|
638
|
+
`;
|
|
639
|
+
}
|
|
640
|
+
let statement = `${settlement}SELECT ${SERVE_GATE_PROJECTION},
|
|
584
641
|
(SELECT jsonb_object_agg(client_commit_id, result) FROM sync_push_results
|
|
585
|
-
WHERE partition=$1 AND client_id=$3 AND client_commit_id IN (SELECT jsonb_array_elements_text($4::text::jsonb))
|
|
586
|
-
|
|
642
|
+
WHERE partition=$1 AND client_id=$3 AND client_commit_id IN (SELECT jsonb_array_elements_text($4::text::jsonb))${
|
|
643
|
+
outbox === undefined
|
|
644
|
+
? ''
|
|
645
|
+
: ' AND client_commit_id NOT IN (SELECT client_commit_id FROM expired)'
|
|
646
|
+
}) AS results,
|
|
647
|
+
(SELECT delete_floor FROM sync_partitions WHERE partition=$1) AS delete_floor,
|
|
648
|
+
${outbox === undefined ? 'NULL::bigint' : '(SELECT through_seq FROM w)'} AS watermark`;
|
|
587
649
|
if (prefetch !== undefined && prefetch.specs.length > 0) {
|
|
588
650
|
params.push(JSON.stringify(prefetch.specs));
|
|
589
651
|
// The gate row pads the read-ahead columns with typed NULLs so both
|
|
@@ -596,6 +658,7 @@ async function readAdmissionOn(
|
|
|
596
658
|
NULL::jsonb AS tombstone_scopes
|
|
597
659
|
UNION ALL
|
|
598
660
|
SELECT NULL::bigint, NULL::text, NULL::json, NULL::jsonb, NULL::bigint,
|
|
661
|
+
NULL::bigint,
|
|
599
662
|
p.tbl, p.lookup_id, p.row_id, p.server_version, p.scopes,
|
|
600
663
|
p.payload, p.column_versions, p.tombstone_seq,
|
|
601
664
|
p.tombstone_version, p.tombstone_scopes
|
|
@@ -619,6 +682,7 @@ async function readAdmissionOn(
|
|
|
619
682
|
),
|
|
620
683
|
// asNumber maps the NULL of a partition without a commit to 0.
|
|
621
684
|
deleteFloor: asNumber(gateRow?.delete_floor),
|
|
685
|
+
watermark: outbox === undefined ? undefined : asNumber(gateRow?.watermark),
|
|
622
686
|
prefetched: rows.filter(
|
|
623
687
|
(row): row is AdmissionRow & PrefetchRow =>
|
|
624
688
|
row.tbl !== null && row.tbl !== undefined,
|
|
@@ -965,11 +1029,13 @@ async function rewriteRowsOn(
|
|
|
965
1029
|
// database) every statement is a round trip, and the bump holds the
|
|
966
1030
|
// migration lock for its whole duration. The page's rows bind as one
|
|
967
1031
|
// VALUES list with typed placeholders (`rewriteValues` order: projection
|
|
968
|
-
// columns, re-keyed scopes, payload, partition, row id). The scopes
|
|
969
|
-
// text before the JSONB cast (see `scanRowBatchSql`).
|
|
1032
|
+
// columns, re-keyed scopes, payload, partition, row id). The scopes and
|
|
1033
|
+
// `json` columns bind as text before the JSONB cast (see `scanRowBatchSql`).
|
|
970
1034
|
const projected = table.materialize ? table.columns : [];
|
|
971
1035
|
const types = [
|
|
972
|
-
...projected.map((column) =>
|
|
1036
|
+
...projected.map((column) =>
|
|
1037
|
+
column.type === 'json' ? 'TEXT' : columnSqlType(column, 'postgres'),
|
|
1038
|
+
),
|
|
973
1039
|
...(rekey ? ['TEXT'] : []),
|
|
974
1040
|
'BYTEA',
|
|
975
1041
|
'TEXT',
|
|
@@ -977,7 +1043,10 @@ async function rewriteRowsOn(
|
|
|
977
1043
|
];
|
|
978
1044
|
const payloadAt = projected.length + (rekey ? 1 : 0);
|
|
979
1045
|
const sets = [
|
|
980
|
-
...projected.map(
|
|
1046
|
+
...projected.map(
|
|
1047
|
+
(column, i) =>
|
|
1048
|
+
`${quoteIdent(column.name)}=v.c${i}${column.type === 'json' ? '::jsonb' : ''}`,
|
|
1049
|
+
),
|
|
981
1050
|
...(rekey
|
|
982
1051
|
? [`${quoteIdent(SYNC_SCOPES_COLUMN)}=v.c${projected.length}::jsonb`]
|
|
983
1052
|
: []),
|
|
@@ -1079,145 +1148,294 @@ async function scanRowsByIndexOn(
|
|
|
1079
1148
|
return rows.map(toStoredRow);
|
|
1080
1149
|
}
|
|
1081
1150
|
|
|
1151
|
+
/** One row a transaction has staged and not yet written. */
|
|
1152
|
+
interface StagedRow {
|
|
1153
|
+
readonly row: StoredRow;
|
|
1154
|
+
/** `upsertValues` of the row, computed when it was staged. */
|
|
1155
|
+
readonly values: readonly unknown[];
|
|
1156
|
+
readonly opIndex?: number;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1082
1159
|
/**
|
|
1083
|
-
*
|
|
1084
|
-
*
|
|
1085
|
-
* ones inserted), and the §5.9.4 blob-reference index synced to the ids the
|
|
1086
|
-
* row now references (entries outside the new set deleted, missing ones
|
|
1087
|
-
* inserted). Row ids are distinct, so no index entry is touched twice and the
|
|
1088
|
-
* writable CTEs cannot conflict. The scopes and blob ids bind as text before
|
|
1089
|
-
* the JSONB cast (see `appendCommit`); the refs set is part of the same
|
|
1090
|
-
* statement, so a blob-referencing commit costs no extra round trip per row.
|
|
1091
|
-
* Every chunk is handed to `q` before the first one settles, so a statement
|
|
1092
|
-
* issued after this call queues behind all of them on the transaction FIFO.
|
|
1160
|
+
* A staged commit append. `commitSeq` is known when the transaction holds
|
|
1161
|
+
* the partition lock; otherwise the append's own allocation decides it.
|
|
1093
1162
|
*/
|
|
1094
|
-
|
|
1095
|
-
|
|
1163
|
+
interface PendingAppend {
|
|
1164
|
+
readonly commit: NewCommit;
|
|
1165
|
+
readonly commitSeq?: number;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
/**
|
|
1169
|
+
* One writing statement: writable CTEs and the parameters they bind. The
|
|
1170
|
+
* CTEs touch disjoint rows, so their order inside the statement is free.
|
|
1171
|
+
*/
|
|
1172
|
+
interface WriteStatement {
|
|
1173
|
+
readonly ctes: string[];
|
|
1174
|
+
readonly params: unknown[];
|
|
1175
|
+
/** The lowest operation index among the rows it writes. */
|
|
1176
|
+
opIndex?: number;
|
|
1177
|
+
/** True when the statement appends commits and returns the new cursor. */
|
|
1178
|
+
appends?: boolean;
|
|
1179
|
+
/** The `max_commit_seq` the statement's commit append must return. */
|
|
1180
|
+
allocates?: number;
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
/**
|
|
1184
|
+
* Bound parameters per write statement. The Bind message counts parameters
|
|
1185
|
+
* in 16 bits; PostgreSQL reads up to 65,535, and PGlite 0.5 corrupts its
|
|
1186
|
+
* state above 32,767.
|
|
1187
|
+
*/
|
|
1188
|
+
const PG_PARAMS_PER_STATEMENT = 30_000;
|
|
1189
|
+
|
|
1190
|
+
function bindParam(statement: WriteStatement, value: unknown): string {
|
|
1191
|
+
statement.params.push(value);
|
|
1192
|
+
return `$${statement.params.length}`;
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
/**
|
|
1196
|
+
* The CTEs that write rows of one table: the upsert, the scope index synced
|
|
1197
|
+
* to each row's scope map (entries outside it deleted, missing ones
|
|
1198
|
+
* inserted), and the §5.9.4 blob-reference index synced to the ids the row
|
|
1199
|
+
* now references. Row ids are distinct, so no index entry is touched twice.
|
|
1200
|
+
* The scopes and blob ids bind as text before the JSONB cast; drivers that
|
|
1201
|
+
* encode JSONB parameters would otherwise store the string as a scalar.
|
|
1202
|
+
*/
|
|
1203
|
+
function addRowWrites(
|
|
1204
|
+
statement: WriteStatement,
|
|
1096
1205
|
compiled: CompiledTable,
|
|
1097
1206
|
partition: string,
|
|
1098
|
-
rows: readonly
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
):
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
const
|
|
1112
|
-
|
|
1113
|
-
|
|
1207
|
+
rows: readonly StagedRow[],
|
|
1208
|
+
blobRefs: readonly { row_id: string; blob_ids: readonly string[] }[],
|
|
1209
|
+
): void {
|
|
1210
|
+
const k = statement.ctes.length;
|
|
1211
|
+
const offset = statement.params.length;
|
|
1212
|
+
statement.params.push(...rows.flatMap((entry) => entry.values));
|
|
1213
|
+
const scopes = bindParam(
|
|
1214
|
+
statement,
|
|
1215
|
+
JSON.stringify(
|
|
1216
|
+
rows.map(({ row }) => ({ row_id: row.rowId, scopes: row.scopes })),
|
|
1217
|
+
),
|
|
1218
|
+
);
|
|
1219
|
+
const p = bindParam(statement, partition);
|
|
1220
|
+
const tbl = bindParam(statement, compiled.name);
|
|
1221
|
+
statement.ctes.push(
|
|
1222
|
+
`u${k} AS (${upsertSql(compiled, 'postgres', rows.length, offset)})`,
|
|
1223
|
+
`b${k} AS (SELECT row_id, scopes FROM jsonb_to_recordset(${scopes}::text::jsonb) AS r(row_id text, scopes jsonb))`,
|
|
1224
|
+
`rs${k} AS (DELETE FROM sync_row_scopes existing USING b${k}
|
|
1225
|
+
WHERE existing.partition=${p} AND existing.tbl=${tbl}
|
|
1226
|
+
AND existing.row_id=b${k}.row_id
|
|
1227
|
+
AND b${k}.scopes->>existing.var IS DISTINCT FROM existing.value)`,
|
|
1228
|
+
`is${k} AS (INSERT INTO sync_row_scopes(partition, tbl, var, value, row_id)
|
|
1229
|
+
SELECT ${p}, ${tbl}, entry.key, entry.value, b${k}.row_id
|
|
1230
|
+
FROM b${k} CROSS JOIN LATERAL jsonb_each_text(b${k}.scopes) AS entry
|
|
1231
|
+
ON CONFLICT DO NOTHING)`,
|
|
1114
1232
|
);
|
|
1115
|
-
const
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
for (const entry of slice) {
|
|
1122
|
-
const blobIds = pendingRefs.get(entry.row.rowId);
|
|
1123
|
-
if (blobIds === undefined) continue;
|
|
1124
|
-
pendingRefs.delete(entry.row.rowId);
|
|
1125
|
-
refs.push({ row_id: entry.row.rowId, blob_ids: blobIds });
|
|
1233
|
+
for (const entry of rows) {
|
|
1234
|
+
if (
|
|
1235
|
+
entry.opIndex !== undefined &&
|
|
1236
|
+
(statement.opIndex === undefined || entry.opIndex < statement.opIndex)
|
|
1237
|
+
) {
|
|
1238
|
+
statement.opIndex = entry.opIndex;
|
|
1126
1239
|
}
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
FROM jsonb_to_recordset($${n + 4}::text::jsonb) AS r(row_id text, blob_ids jsonb)
|
|
1148
|
-
),
|
|
1149
|
-
removed_refs AS (
|
|
1150
|
-
DELETE FROM sync_blob_refs existing
|
|
1151
|
-
USING changed_refs
|
|
1152
|
-
WHERE existing.partition=$${n + 2} AND existing.tbl=$${n + 3}
|
|
1153
|
-
AND existing.row_id=changed_refs.row_id
|
|
1154
|
-
AND NOT EXISTS (
|
|
1155
|
-
SELECT 1
|
|
1156
|
-
FROM jsonb_to_recordset($${n + 4}::text::jsonb) AS r(row_id text, blob_ids jsonb)
|
|
1157
|
-
CROSS JOIN LATERAL jsonb_array_elements_text(r.blob_ids) AS entry(value)
|
|
1158
|
-
WHERE r.row_id=existing.row_id AND entry.value=existing.blob_id)
|
|
1159
|
-
),
|
|
1160
|
-
inserted_refs AS (
|
|
1161
|
-
INSERT INTO sync_blob_refs(partition, tbl, row_id, blob_id)
|
|
1162
|
-
SELECT $${n + 2}, $${n + 3}, r.row_id, entry.value
|
|
1163
|
-
FROM jsonb_to_recordset($${n + 4}::text::jsonb) AS r(row_id text, blob_ids jsonb)
|
|
1240
|
+
}
|
|
1241
|
+
if (blobRefs.length > 0) addBlobRefWrites(statement, p, tbl, blobRefs);
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
/** Replace the §5.9.4 reference sets of rows of one table. */
|
|
1245
|
+
function addBlobRefWrites(
|
|
1246
|
+
statement: WriteStatement,
|
|
1247
|
+
p: string,
|
|
1248
|
+
tbl: string,
|
|
1249
|
+
blobRefs: readonly { row_id: string; blob_ids: readonly string[] }[],
|
|
1250
|
+
): void {
|
|
1251
|
+
const k = statement.ctes.length;
|
|
1252
|
+
const refs = bindParam(statement, JSON.stringify(blobRefs));
|
|
1253
|
+
statement.ctes.push(
|
|
1254
|
+
`rr${k} AS (DELETE FROM sync_blob_refs existing
|
|
1255
|
+
WHERE existing.partition=${p} AND existing.tbl=${tbl}
|
|
1256
|
+
AND existing.row_id IN (SELECT r.row_id FROM jsonb_to_recordset(${refs}::text::jsonb) AS r(row_id text, blob_ids jsonb))
|
|
1257
|
+
AND NOT EXISTS (
|
|
1258
|
+
SELECT 1
|
|
1259
|
+
FROM jsonb_to_recordset(${refs}::text::jsonb) AS r(row_id text, blob_ids jsonb)
|
|
1164
1260
|
CROSS JOIN LATERAL jsonb_array_elements_text(r.blob_ids) AS entry(value)
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1261
|
+
WHERE r.row_id=existing.row_id AND entry.value=existing.blob_id))`,
|
|
1262
|
+
`ir${k} AS (INSERT INTO sync_blob_refs(partition, tbl, row_id, blob_id)
|
|
1263
|
+
SELECT ${p}, ${tbl}, r.row_id, entry.value
|
|
1264
|
+
FROM jsonb_to_recordset(${refs}::text::jsonb) AS r(row_id text, blob_ids jsonb)
|
|
1265
|
+
CROSS JOIN LATERAL jsonb_array_elements_text(r.blob_ids) AS entry(value)
|
|
1266
|
+
ON CONFLICT DO NOTHING)`,
|
|
1267
|
+
);
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
/**
|
|
1271
|
+
* The CTEs that append commits with consecutive `commitSeq`s: the partition
|
|
1272
|
+
* cursor and delete floor (§2.2), the commit rows, their changes with the
|
|
1273
|
+
* inverted scope entries, and the §5 delete tombstones. The cursor advances
|
|
1274
|
+
* by the number of commits, and every row takes its commit's sequence from
|
|
1275
|
+
* the advanced cursor; the statement returns the cursor, which a caller that
|
|
1276
|
+
* computed the sequences under the partition lock checks. Every list binds
|
|
1277
|
+
* as one JSON text parameter, so the parameter count does not grow with the
|
|
1278
|
+
* change count. A tombstone keeps the change's row version (§2.2); a row
|
|
1279
|
+
* live again at the end of its commit (a §2.2 scope move) gets none, and a
|
|
1280
|
+
* later delete of the same row overwrites an earlier one.
|
|
1281
|
+
*/
|
|
1282
|
+
function addAppends(
|
|
1283
|
+
statement: WriteStatement,
|
|
1284
|
+
partition: string,
|
|
1285
|
+
writerVersion: number,
|
|
1286
|
+
appends: readonly PendingAppend[],
|
|
1287
|
+
): void {
|
|
1288
|
+
if (appends.length === 0) return;
|
|
1289
|
+
const p = bindParam(statement, partition);
|
|
1290
|
+
const changes: unknown[] = [];
|
|
1291
|
+
const tombstones = new Map<string, unknown>();
|
|
1292
|
+
let deleteFloor = 0;
|
|
1293
|
+
for (const [index, { commit }] of appends.entries()) {
|
|
1294
|
+
const k = index + 1;
|
|
1295
|
+
deleteFloor = Math.max(deleteFloor, commitDeleteFloor(commit.changes));
|
|
1296
|
+
const live = rowsLiveAfterDelete(commit.changes);
|
|
1297
|
+
for (const [idx, change] of commit.changes.entries()) {
|
|
1298
|
+
changes.push({
|
|
1299
|
+
k,
|
|
1300
|
+
idx,
|
|
1301
|
+
tbl: change.table,
|
|
1302
|
+
row_id: change.rowId,
|
|
1303
|
+
op: change.op === 'upsert' ? 1 : 2,
|
|
1304
|
+
version: change.rowVersion,
|
|
1305
|
+
scopes: change.scopes,
|
|
1306
|
+
payload: change.payload === undefined ? null : toBase64(change.payload),
|
|
1307
|
+
});
|
|
1308
|
+
const key = `${change.table}\u0000${change.rowId}`;
|
|
1309
|
+
if (change.op === 'delete' && !live.has(key)) {
|
|
1310
|
+
tombstones.set(key, {
|
|
1311
|
+
k,
|
|
1312
|
+
tbl: change.table,
|
|
1313
|
+
row_id: change.rowId,
|
|
1314
|
+
version: change.rowVersion,
|
|
1315
|
+
scopes: change.scopes,
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
statement.appends = true;
|
|
1321
|
+
const expected = appends.at(-1)?.commitSeq;
|
|
1322
|
+
if (expected !== undefined) statement.allocates = expected;
|
|
1323
|
+
const n = bindParam(statement, appends.length);
|
|
1324
|
+
statement.ctes.push(
|
|
1325
|
+
`alloc AS (INSERT INTO sync_partitions(partition, max_commit_seq, delete_floor)
|
|
1326
|
+
VALUES (${p}, ${n}, ${bindParam(statement, deleteFloor)})
|
|
1327
|
+
ON CONFLICT (partition) DO UPDATE
|
|
1328
|
+
SET max_commit_seq=sync_partitions.max_commit_seq + EXCLUDED.max_commit_seq,
|
|
1329
|
+
delete_floor=GREATEST(sync_partitions.delete_floor, EXCLUDED.delete_floor)
|
|
1330
|
+
RETURNING max_commit_seq, max_commit_seq - ${n}::bigint AS base)`,
|
|
1331
|
+
`commits AS (INSERT INTO sync_commits(partition, commit_seq, client_id, client_commit_id, actor_id, created_at_ms, writer_version)
|
|
1332
|
+
SELECT ${p}, alloc.base + c.k, c.client_id, c.client_commit_id, c.actor_id, c.created_at_ms, ${bindParam(statement, writerVersion)}
|
|
1333
|
+
FROM alloc CROSS JOIN jsonb_to_recordset(${bindParam(
|
|
1334
|
+
statement,
|
|
1335
|
+
JSON.stringify(
|
|
1336
|
+
appends.map(({ commit }, index) => ({
|
|
1337
|
+
k: index + 1,
|
|
1338
|
+
client_id: commit.clientId,
|
|
1339
|
+
client_commit_id: commit.clientCommitId,
|
|
1340
|
+
actor_id: commit.actorId,
|
|
1341
|
+
created_at_ms: commit.createdAtMs,
|
|
1342
|
+
})),
|
|
1343
|
+
),
|
|
1344
|
+
)}::text::jsonb)
|
|
1345
|
+
AS c(k int, client_id text, client_commit_id text, actor_id text, created_at_ms bigint))`,
|
|
1346
|
+
);
|
|
1347
|
+
if (changes.length > 0) {
|
|
1348
|
+
statement.ctes.push(
|
|
1349
|
+
`changed AS (INSERT INTO sync_changes(partition, commit_seq, idx, tbl, row_id, op, row_version, scopes, payload)
|
|
1350
|
+
SELECT ${p}, alloc.base + c.k, c.idx, c.tbl, c.row_id, c.op, c.version, c.scopes, decode(c.payload, 'base64')
|
|
1351
|
+
FROM alloc CROSS JOIN jsonb_to_recordset(${bindParam(statement, JSON.stringify(changes))}::text::jsonb)
|
|
1352
|
+
AS c(k int, idx int, tbl text, row_id text, op smallint, version bigint, scopes jsonb, payload text)
|
|
1353
|
+
RETURNING commit_seq, tbl, scopes)`,
|
|
1354
|
+
`change_scopes AS (INSERT INTO sync_change_scopes(partition, tbl, var, value, commit_seq)
|
|
1355
|
+
SELECT ${p}, changed.tbl, scope.key, scope.value, changed.commit_seq
|
|
1356
|
+
FROM changed CROSS JOIN LATERAL jsonb_each_text(changed.scopes) AS scope
|
|
1357
|
+
ON CONFLICT DO NOTHING)`,
|
|
1180
1358
|
);
|
|
1181
1359
|
}
|
|
1182
|
-
if (
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
DELETE FROM sync_blob_refs existing
|
|
1193
|
-
USING changed_refs
|
|
1194
|
-
WHERE existing.partition=$1 AND existing.tbl=$2
|
|
1195
|
-
AND existing.row_id=changed_refs.row_id
|
|
1196
|
-
AND NOT EXISTS (
|
|
1197
|
-
SELECT 1
|
|
1198
|
-
FROM jsonb_to_recordset($3::text::jsonb) AS r(row_id text, blob_ids jsonb)
|
|
1199
|
-
CROSS JOIN LATERAL jsonb_array_elements_text(r.blob_ids) AS entry(value)
|
|
1200
|
-
WHERE r.row_id=existing.row_id AND entry.value=existing.blob_id)
|
|
1201
|
-
)
|
|
1202
|
-
INSERT INTO sync_blob_refs(partition, tbl, row_id, blob_id)
|
|
1203
|
-
SELECT $1, $2, r.row_id, entry.value
|
|
1204
|
-
FROM jsonb_to_recordset($3::text::jsonb) AS r(row_id text, blob_ids jsonb)
|
|
1205
|
-
CROSS JOIN LATERAL jsonb_array_elements_text(r.blob_ids) AS entry(value)
|
|
1206
|
-
ON CONFLICT DO NOTHING`,
|
|
1207
|
-
[
|
|
1208
|
-
partition,
|
|
1209
|
-
compiled.name,
|
|
1210
|
-
JSON.stringify(
|
|
1211
|
-
[...pendingRefs].map(([row_id, blob_ids]) => ({
|
|
1212
|
-
row_id,
|
|
1213
|
-
blob_ids,
|
|
1214
|
-
})),
|
|
1215
|
-
),
|
|
1216
|
-
],
|
|
1217
|
-
),
|
|
1360
|
+
if (tombstones.size > 0) {
|
|
1361
|
+
statement.ctes.push(
|
|
1362
|
+
`tombstoned AS (INSERT INTO sync_tombstones(partition, tbl, row_id, commit_seq, server_version, scopes)
|
|
1363
|
+
SELECT ${p}, t.tbl, t.row_id, alloc.base + t.k, t.version, t.scopes
|
|
1364
|
+
FROM alloc CROSS JOIN jsonb_to_recordset(${bindParam(statement, JSON.stringify([...tombstones.values()]))}::text::jsonb)
|
|
1365
|
+
AS t(k int, tbl text, row_id text, version bigint, scopes jsonb)
|
|
1366
|
+
ON CONFLICT (partition, tbl, row_id)
|
|
1367
|
+
DO UPDATE SET commit_seq=excluded.commit_seq,
|
|
1368
|
+
server_version=excluded.server_version,
|
|
1369
|
+
scopes=excluded.scopes)`,
|
|
1218
1370
|
);
|
|
1219
1371
|
}
|
|
1220
|
-
|
|
1372
|
+
}
|
|
1373
|
+
|
|
1374
|
+
/**
|
|
1375
|
+
* The statements that write staged rows, blob references, commit appends
|
|
1376
|
+
* and push results. Everything shares the first statement while the bound
|
|
1377
|
+
* parameters fit; a large row set continues in further statements of the
|
|
1378
|
+
* same shape.
|
|
1379
|
+
*/
|
|
1380
|
+
function writeStatements(
|
|
1381
|
+
partition: string,
|
|
1382
|
+
writerVersion: number,
|
|
1383
|
+
resolveTable: (name: string) => CompiledTable,
|
|
1384
|
+
rows: ReadonlyMap<string, ReadonlyMap<string, StagedRow>>,
|
|
1385
|
+
blobRefs: ReadonlyMap<string, ReadonlyMap<string, readonly string[]>>,
|
|
1386
|
+
appends: readonly PendingAppend[],
|
|
1387
|
+
results: readonly unknown[],
|
|
1388
|
+
): WriteStatement[] {
|
|
1389
|
+
let current: WriteStatement = { ctes: [], params: [] };
|
|
1390
|
+
const statements = [current];
|
|
1391
|
+
addAppends(current, partition, writerVersion, appends);
|
|
1392
|
+
if (results.length > 0) {
|
|
1393
|
+
current.ctes.push(
|
|
1394
|
+
`results AS (INSERT INTO sync_push_results(partition, client_id, client_commit_id, result, outbox_id, client_seq)
|
|
1395
|
+
SELECT ${bindParam(current, partition)}, r.client_id, r.client_commit_id, r.result, r.outbox_id, r.client_seq
|
|
1396
|
+
FROM jsonb_to_recordset(${bindParam(current, JSON.stringify(results))}::text::jsonb)
|
|
1397
|
+
AS r(client_id text, client_commit_id text, result jsonb, outbox_id text, client_seq bigint)
|
|
1398
|
+
ON CONFLICT (partition, client_id, client_commit_id) DO NOTHING)`,
|
|
1399
|
+
);
|
|
1400
|
+
}
|
|
1401
|
+
for (const table of new Set([...rows.keys(), ...blobRefs.keys()])) {
|
|
1402
|
+
const compiled = resolveTable(table);
|
|
1403
|
+
const width = tableColumnNames(compiled).length;
|
|
1404
|
+
const chunk = Math.max(
|
|
1405
|
+
1,
|
|
1406
|
+
Math.floor((PG_PARAMS_PER_STATEMENT - 4) / width),
|
|
1407
|
+
);
|
|
1408
|
+
const pending = new Map(blobRefs.get(table));
|
|
1409
|
+
const staged = [...(rows.get(table)?.values() ?? [])];
|
|
1410
|
+
for (let first = 0; first < staged.length; first += chunk) {
|
|
1411
|
+
const slice = staged.slice(first, first + chunk);
|
|
1412
|
+
if (
|
|
1413
|
+
current.params.length + slice.length * width + 4 >
|
|
1414
|
+
PG_PARAMS_PER_STATEMENT
|
|
1415
|
+
) {
|
|
1416
|
+
current = { ctes: [], params: [] };
|
|
1417
|
+
statements.push(current);
|
|
1418
|
+
}
|
|
1419
|
+
const refs: { row_id: string; blob_ids: readonly string[] }[] = [];
|
|
1420
|
+
for (const { row } of slice) {
|
|
1421
|
+
const blobIds = pending.get(row.rowId);
|
|
1422
|
+
if (blobIds === undefined) continue;
|
|
1423
|
+
pending.delete(row.rowId);
|
|
1424
|
+
refs.push({ row_id: row.rowId, blob_ids: blobIds });
|
|
1425
|
+
}
|
|
1426
|
+
addRowWrites(current, compiled, partition, slice, refs);
|
|
1427
|
+
}
|
|
1428
|
+
if (pending.size > 0) {
|
|
1429
|
+
// Refs staged for a row written by an earlier statement.
|
|
1430
|
+
addBlobRefWrites(
|
|
1431
|
+
current,
|
|
1432
|
+
bindParam(current, partition),
|
|
1433
|
+
bindParam(current, table),
|
|
1434
|
+
[...pending].map(([row_id, blob_ids]) => ({ row_id, blob_ids })),
|
|
1435
|
+
);
|
|
1436
|
+
}
|
|
1437
|
+
}
|
|
1438
|
+
return statements.filter((statement) => statement.ctes.length > 0);
|
|
1221
1439
|
}
|
|
1222
1440
|
|
|
1223
1441
|
async function getPushResultOn(
|
|
@@ -1271,7 +1489,7 @@ async function queryAuthoritativeOn(
|
|
|
1271
1489
|
|
|
1272
1490
|
class PostgresTransaction implements StorageTransaction {
|
|
1273
1491
|
#client: PgQueryable;
|
|
1274
|
-
/** The FIFO without the
|
|
1492
|
+
/** The FIFO without the pending-write prelude (see the constructor). */
|
|
1275
1493
|
#serial: PgQueryable;
|
|
1276
1494
|
#partition: string;
|
|
1277
1495
|
/** Storage-owned relation rewrite, bound to this transaction's partition. */
|
|
@@ -1284,13 +1502,41 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1284
1502
|
/** The storage's `chooseScanVariable` picks. */
|
|
1285
1503
|
readonly #scanVariables: Map<string, string>;
|
|
1286
1504
|
#open = true;
|
|
1505
|
+
/** Set by `preparePushApply`: commits close with a result and roll back alone. */
|
|
1506
|
+
#pushApply = false;
|
|
1507
|
+
/** Set by `preparePushApply` on the rerun after a closed commit's write failed. */
|
|
1508
|
+
#closeEagerly = false;
|
|
1287
1509
|
/** True once the request's current commit has written behind its savepoint. */
|
|
1288
1510
|
#pushApplySavepoint = false;
|
|
1289
1511
|
/**
|
|
1290
|
-
* The
|
|
1291
|
-
*
|
|
1292
|
-
* the transaction ends.
|
|
1512
|
+
* The `commitSeq` the next appended commit takes. The partition lock read
|
|
1513
|
+
* sets it and freezes the cursor, so an append needs no allocation read.
|
|
1293
1514
|
*/
|
|
1515
|
+
#nextCommitSeq: number | undefined;
|
|
1516
|
+
/** The `commitSeq` the open push commit appended, until it closes. */
|
|
1517
|
+
#openCommitSeq: number | undefined;
|
|
1518
|
+
/**
|
|
1519
|
+
* The open commit's upserts staged per table name (row id → row and its
|
|
1520
|
+
* push operation index), its §5.9.4 reference sets, and its commit append.
|
|
1521
|
+
* They reach the database with the next statement that needs them, behind
|
|
1522
|
+
* the commit's savepoint, as one statement: a commit's rows cost one round
|
|
1523
|
+
* trip per statement instead of one per row. Tables with a unique index
|
|
1524
|
+
* write immediately: a violation must name its operation.
|
|
1525
|
+
*/
|
|
1526
|
+
readonly #staged = new Map<string, Map<string, StagedRow>>();
|
|
1527
|
+
readonly #stagedBlobRefs = new Map<string, Map<string, readonly string[]>>();
|
|
1528
|
+
#stagedAppend: PendingAppend | undefined;
|
|
1529
|
+
/**
|
|
1530
|
+
* The writes of push commits that closed and are not yet written: rows (a
|
|
1531
|
+
* later commit's row replaces an earlier one), reference sets, appends in
|
|
1532
|
+
* `commitSeq` order, and the §2.3 results. They reach the database in one
|
|
1533
|
+
* statement ahead of the next statement of the transaction, outside any
|
|
1534
|
+
* commit's savepoint; a request whose commits never read their own writes
|
|
1535
|
+
* writes them all in one statement before COMMIT (§6.4).
|
|
1536
|
+
*/
|
|
1537
|
+
readonly #closedRows = new Map<string, Map<string, StagedRow>>();
|
|
1538
|
+
readonly #closedBlobRefs = new Map<string, Map<string, readonly string[]>>();
|
|
1539
|
+
readonly #closedAppends: PendingAppend[] = [];
|
|
1294
1540
|
readonly #pushResults: {
|
|
1295
1541
|
readonly client_id: string;
|
|
1296
1542
|
readonly client_commit_id: string;
|
|
@@ -1305,39 +1551,6 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1305
1551
|
*/
|
|
1306
1552
|
readonly #prefetchedRows = new Map<string, StoredRow | null>();
|
|
1307
1553
|
readonly #prefetchedTombstones = new Map<string, Tombstone | null>();
|
|
1308
|
-
/**
|
|
1309
|
-
* Upserts staged per table (row id → row and its push operation index).
|
|
1310
|
-
* Every statement of this transaction writes them first (see the
|
|
1311
|
-
* constructor), so reads and later writes observe them; a commit's rows
|
|
1312
|
-
* reach the database as one statement per table instead of one per row.
|
|
1313
|
-
* Tables with a unique index write immediately: a violation must name its
|
|
1314
|
-
* operation.
|
|
1315
|
-
*/
|
|
1316
|
-
readonly #staged = new Map<
|
|
1317
|
-
CompiledTable,
|
|
1318
|
-
Map<
|
|
1319
|
-
string,
|
|
1320
|
-
{
|
|
1321
|
-
readonly row: StoredRow;
|
|
1322
|
-
readonly values: readonly unknown[];
|
|
1323
|
-
readonly opIndex?: number;
|
|
1324
|
-
}
|
|
1325
|
-
>
|
|
1326
|
-
>();
|
|
1327
|
-
/**
|
|
1328
|
-
* §5.9.4 reference sets staged per table (row id → blob ids). They flush
|
|
1329
|
-
* with the table's row statement, so a blob-referencing row costs no extra
|
|
1330
|
-
* round trip; a ref set staged for an already-written row flushes on the
|
|
1331
|
-
* next statement of its table.
|
|
1332
|
-
*/
|
|
1333
|
-
readonly #stagedBlobRefs = new Map<string, Map<string, readonly string[]>>();
|
|
1334
|
-
/**
|
|
1335
|
-
* The partition cursor (`max_commit_seq`) read by the first registered
|
|
1336
|
-
* query in this transaction. The partition lock freezes it, so later
|
|
1337
|
-
* registered queries in the same transaction reuse the value instead of
|
|
1338
|
-
* paying a round trip each.
|
|
1339
|
-
*/
|
|
1340
|
-
#cursorSeq: number | undefined;
|
|
1341
1554
|
/** Resolves/rejects the `transaction(fn)` wrapper (see `begin`). */
|
|
1342
1555
|
#resolve: () => void;
|
|
1343
1556
|
#reject: (error: unknown) => void;
|
|
@@ -1360,7 +1573,7 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1360
1573
|
// issued through this transaction is chained on one FIFO — the same
|
|
1361
1574
|
// shape as `SqliteServerStorage.queryAuthoritative`.
|
|
1362
1575
|
let tail: Promise<void> = Promise.resolve();
|
|
1363
|
-
|
|
1576
|
+
this.#serial = {
|
|
1364
1577
|
query: <Row>(text: string, params?: readonly unknown[]) => {
|
|
1365
1578
|
const run = tail.then(() => client.query<Row>(text, params));
|
|
1366
1579
|
tail = run.then(
|
|
@@ -1370,22 +1583,8 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1370
1583
|
return run;
|
|
1371
1584
|
},
|
|
1372
1585
|
};
|
|
1373
|
-
this.#serial = serial;
|
|
1374
1586
|
this.#client = {
|
|
1375
|
-
query:
|
|
1376
|
-
// Queue the staged writes ahead of this statement synchronously, so
|
|
1377
|
-
// a concurrently issued statement cannot overtake them.
|
|
1378
|
-
const staged = this.#writeStaged();
|
|
1379
|
-
const run = serial.query<Row>(text, params);
|
|
1380
|
-
try {
|
|
1381
|
-
await staged;
|
|
1382
|
-
} catch (error) {
|
|
1383
|
-
// The aborted transaction fails the queued statement too.
|
|
1384
|
-
run.catch(() => undefined);
|
|
1385
|
-
throw error;
|
|
1386
|
-
}
|
|
1387
|
-
return run;
|
|
1388
|
-
},
|
|
1587
|
+
query: (text, params) => this.#statement(text, params, 'read'),
|
|
1389
1588
|
};
|
|
1390
1589
|
this.#partition = partition;
|
|
1391
1590
|
this.#writerVersion = writerVersion;
|
|
@@ -1399,74 +1598,179 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1399
1598
|
if (!this.#open) throw new Error('transaction already finished');
|
|
1400
1599
|
}
|
|
1401
1600
|
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1601
|
+
/**
|
|
1602
|
+
* Queue the pending writes and then one statement. `write` marks a
|
|
1603
|
+
* candidate write of the open commit, which needs the commit's savepoint
|
|
1604
|
+
* even when nothing is staged.
|
|
1605
|
+
*/
|
|
1606
|
+
async #statement<Row>(
|
|
1607
|
+
text: string,
|
|
1608
|
+
params: readonly unknown[] | undefined,
|
|
1609
|
+
mode: 'read' | 'write',
|
|
1610
|
+
): Promise<{ rows: Row[] }> {
|
|
1611
|
+
// Queue the pending writes ahead of this statement synchronously, so a
|
|
1612
|
+
// concurrently issued statement cannot overtake them.
|
|
1613
|
+
const pending = this.#flush(mode);
|
|
1614
|
+
const run = this.#serial.query<Row>(text, params);
|
|
1615
|
+
try {
|
|
1616
|
+
await pending;
|
|
1617
|
+
} catch (error) {
|
|
1618
|
+
// The aborted transaction fails the queued statement too.
|
|
1619
|
+
run.catch(() => undefined);
|
|
1620
|
+
throw error;
|
|
1621
|
+
}
|
|
1622
|
+
return run;
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
/**
|
|
1626
|
+
* Queue every pending write, in order: the closed commits' writes, then
|
|
1627
|
+
* the open commit's savepoint (taken before its first write reaches the
|
|
1628
|
+
* database, so a refused commit that wrote nothing pays for none) and its
|
|
1629
|
+
* staged writes. `final` is the flush before COMMIT, which no rejection
|
|
1630
|
+
* follows.
|
|
1631
|
+
*
|
|
1632
|
+
* A relational constraint that fails on a closed commit's writes can no
|
|
1633
|
+
* longer reject that commit alone: the transaction is aborted, and
|
|
1634
|
+
* `ClosedCommitConstraintError` tells the push layer to rerun the request
|
|
1635
|
+
* with each commit written before it closes (§6.4).
|
|
1636
|
+
*
|
|
1637
|
+
* Resolves to the partition cursor the open commit's append returned.
|
|
1638
|
+
*/
|
|
1639
|
+
async #flush(mode: 'read' | 'write' | 'final'): Promise<number | undefined> {
|
|
1640
|
+
const pending: Promise<unknown>[] = [];
|
|
1641
|
+
let cursor: Promise<number | undefined> = Promise.resolve(undefined);
|
|
1642
|
+
if (
|
|
1643
|
+
this.#closedRows.size > 0 ||
|
|
1644
|
+
this.#closedBlobRefs.size > 0 ||
|
|
1645
|
+
this.#closedAppends.length > 0 ||
|
|
1646
|
+
this.#pushResults.length > 0
|
|
1647
|
+
) {
|
|
1648
|
+
const statements = writeStatements(
|
|
1649
|
+
this.#partition,
|
|
1650
|
+
this.#writerVersion,
|
|
1651
|
+
this.#resolveTable,
|
|
1652
|
+
this.#closedRows,
|
|
1653
|
+
this.#closedBlobRefs,
|
|
1654
|
+
this.#closedAppends,
|
|
1655
|
+
this.#pushResults,
|
|
1656
|
+
);
|
|
1657
|
+
this.#closedRows.clear();
|
|
1658
|
+
this.#closedBlobRefs.clear();
|
|
1659
|
+
this.#closedAppends.length = 0;
|
|
1660
|
+
this.#pushResults.length = 0;
|
|
1661
|
+
pending.push(
|
|
1662
|
+
this.#runWrites(statements, (error) =>
|
|
1663
|
+
isPostgresConstraintError(error)
|
|
1664
|
+
? new ClosedCommitConstraintError(error)
|
|
1665
|
+
: error,
|
|
1666
|
+
),
|
|
1667
|
+
);
|
|
1405
1668
|
}
|
|
1406
|
-
const
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
throw new StorageConstraintError(error, rows[0]?.opIndex);
|
|
1440
|
-
}
|
|
1441
|
-
throw error;
|
|
1442
|
-
}),
|
|
1669
|
+
const staged =
|
|
1670
|
+
this.#staged.size > 0 ||
|
|
1671
|
+
this.#stagedBlobRefs.size > 0 ||
|
|
1672
|
+
this.#stagedAppend !== undefined;
|
|
1673
|
+
if (
|
|
1674
|
+
mode !== 'final' &&
|
|
1675
|
+
this.#pushApply &&
|
|
1676
|
+
!this.#pushApplySavepoint &&
|
|
1677
|
+
(staged || mode === 'write')
|
|
1678
|
+
) {
|
|
1679
|
+
// Each commit takes its own savepoint under the same name: `ROLLBACK
|
|
1680
|
+
// TO` targets the newest, and an earlier commit's savepoint stays open
|
|
1681
|
+
// until the transaction ends, so no `RELEASE` round trip is needed.
|
|
1682
|
+
this.#pushApplySavepoint = true;
|
|
1683
|
+
pending.push(this.#serial.query('SAVEPOINT syncular_push_candidate'));
|
|
1684
|
+
}
|
|
1685
|
+
if (staged) {
|
|
1686
|
+
const statements = writeStatements(
|
|
1687
|
+
this.#partition,
|
|
1688
|
+
this.#writerVersion,
|
|
1689
|
+
this.#resolveTable,
|
|
1690
|
+
this.#staged,
|
|
1691
|
+
this.#stagedBlobRefs,
|
|
1692
|
+
this.#stagedAppend === undefined ? [] : [this.#stagedAppend],
|
|
1693
|
+
[],
|
|
1694
|
+
);
|
|
1695
|
+
this.#staged.clear();
|
|
1696
|
+
this.#stagedBlobRefs.clear();
|
|
1697
|
+
this.#stagedAppend = undefined;
|
|
1698
|
+
cursor = this.#runWrites(statements, (error, statement) =>
|
|
1699
|
+
isPostgresConstraintError(error)
|
|
1700
|
+
? new StorageConstraintError(error, statement.opIndex)
|
|
1701
|
+
: error,
|
|
1443
1702
|
);
|
|
1703
|
+
pending.push(cursor);
|
|
1444
1704
|
}
|
|
1445
|
-
|
|
1705
|
+
await Promise.all(pending);
|
|
1706
|
+
return cursor;
|
|
1446
1707
|
}
|
|
1447
1708
|
|
|
1448
1709
|
/**
|
|
1449
|
-
*
|
|
1450
|
-
*
|
|
1451
|
-
* refused commit that wrote nothing must not pay to create one. Each commit
|
|
1452
|
-
* takes its own savepoint under the same name: `ROLLBACK TO` targets the
|
|
1453
|
-
* newest, and an earlier commit's savepoint stays open until the
|
|
1454
|
-
* transaction ends, so no `RELEASE` round trip is needed (§6.4).
|
|
1710
|
+
* Queue write statements on the FIFO. Resolves to the cursor a commit
|
|
1711
|
+
* append returned, checked against the sequences computed under the lock.
|
|
1455
1712
|
*/
|
|
1456
|
-
#
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1713
|
+
async #runWrites(
|
|
1714
|
+
statements: readonly WriteStatement[],
|
|
1715
|
+
mapError: (error: unknown, statement: WriteStatement) => unknown,
|
|
1716
|
+
): Promise<number | undefined> {
|
|
1717
|
+
const cursors = await Promise.all(
|
|
1718
|
+
statements.map((statement) =>
|
|
1719
|
+
this.#serial
|
|
1720
|
+
.query<{ allocated: unknown }>(
|
|
1721
|
+
`WITH ${statement.ctes.join(',\n')} SELECT ${
|
|
1722
|
+
statement.appends === true
|
|
1723
|
+
? '(SELECT max_commit_seq FROM alloc)'
|
|
1724
|
+
: 'NULL::bigint'
|
|
1725
|
+
} AS allocated`,
|
|
1726
|
+
statement.params,
|
|
1727
|
+
)
|
|
1728
|
+
.then(
|
|
1729
|
+
({ rows }) => {
|
|
1730
|
+
if (statement.appends !== true) return undefined;
|
|
1731
|
+
const allocated = asNumber(rows[0]?.allocated);
|
|
1732
|
+
if (
|
|
1733
|
+
statement.allocates !== undefined &&
|
|
1734
|
+
allocated !== statement.allocates
|
|
1735
|
+
) {
|
|
1736
|
+
throw new Error(
|
|
1737
|
+
'partition commit sequence moved under the partition lock',
|
|
1738
|
+
);
|
|
1739
|
+
}
|
|
1740
|
+
return allocated;
|
|
1741
|
+
},
|
|
1742
|
+
(error: unknown) => {
|
|
1743
|
+
throw mapError(error, statement);
|
|
1744
|
+
},
|
|
1745
|
+
),
|
|
1746
|
+
),
|
|
1747
|
+
);
|
|
1748
|
+
return cursors.find((cursor) => cursor !== undefined);
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
/** The candidate row this transaction staged and has not yet written. */
|
|
1752
|
+
#stagedRow(table: string, rowId: string): StoredRow | undefined {
|
|
1753
|
+
return (
|
|
1754
|
+
this.#staged.get(table)?.get(rowId)?.row ??
|
|
1755
|
+
this.#closedRows.get(table)?.get(rowId)?.row
|
|
1756
|
+
);
|
|
1460
1757
|
}
|
|
1461
1758
|
|
|
1759
|
+
/**
|
|
1760
|
+
* A read by primary key. A staged row answers from memory, so the read
|
|
1761
|
+
* needs no pending write: it runs without flushing them, and a validator
|
|
1762
|
+
* that reads rows by id adds no savepoint or write round trip (§6.4).
|
|
1763
|
+
*/
|
|
1462
1764
|
getRow(table: string, rowId: string): Promise<StoredRow | undefined> {
|
|
1463
1765
|
this.#assertOpen();
|
|
1766
|
+
const staged = this.#stagedRow(table, rowId);
|
|
1767
|
+
if (staged !== undefined) return Promise.resolve(staged);
|
|
1464
1768
|
const prefetched = this.#prefetchedRows.get(`${table}\u0000${rowId}`);
|
|
1465
1769
|
if (prefetched !== undefined) {
|
|
1466
1770
|
return Promise.resolve(prefetched ?? undefined);
|
|
1467
1771
|
}
|
|
1468
1772
|
return getRowOn(
|
|
1469
|
-
this.#
|
|
1773
|
+
this.#serial,
|
|
1470
1774
|
this.#resolveTable(table),
|
|
1471
1775
|
this.#partition,
|
|
1472
1776
|
rowId,
|
|
@@ -1481,15 +1785,16 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1481
1785
|
const rows = new Map<string, StoredRow>();
|
|
1482
1786
|
const missing: string[] = [];
|
|
1483
1787
|
for (const rowId of new Set(rowIds)) {
|
|
1788
|
+
const staged = this.#stagedRow(table, rowId);
|
|
1484
1789
|
const prefetched = this.#prefetchedRows.get(`${table}\u0000${rowId}`);
|
|
1485
|
-
if (
|
|
1790
|
+
if (staged !== undefined) rows.set(rowId, staged);
|
|
1791
|
+
else if (prefetched === undefined) missing.push(rowId);
|
|
1486
1792
|
else if (prefetched !== null) rows.set(rowId, prefetched);
|
|
1487
1793
|
}
|
|
1488
1794
|
if (missing.length === 0) return rows;
|
|
1489
|
-
// One statement for every id the
|
|
1490
|
-
//
|
|
1491
|
-
|
|
1492
|
-
const { rows: records } = await this.#client.query<RowRecord>(
|
|
1795
|
+
// One statement for every id neither the staged writes nor the
|
|
1796
|
+
// admission read answered (see `getRow`).
|
|
1797
|
+
const { rows: records } = await this.#serial.query<RowRecord>(
|
|
1493
1798
|
selectRowsSql(this.#resolveTable(table), 'postgres'),
|
|
1494
1799
|
[this.#partition, JSON.stringify(missing)],
|
|
1495
1800
|
);
|
|
@@ -1508,7 +1813,9 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1508
1813
|
const specs: { tbl: string; ids: string[] }[] = [];
|
|
1509
1814
|
for (const target of targets) {
|
|
1510
1815
|
const ids = [...new Set(target.rowIds)].filter(
|
|
1511
|
-
(rowId) =>
|
|
1816
|
+
(rowId) =>
|
|
1817
|
+
!this.#prefetchedRows.has(`${target.table}\u0000${rowId}`) &&
|
|
1818
|
+
this.#stagedRow(target.table, rowId) === undefined,
|
|
1512
1819
|
);
|
|
1513
1820
|
if (ids.length === 0) continue;
|
|
1514
1821
|
tables.push(this.#resolveTable(target.table));
|
|
@@ -1568,10 +1875,10 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1568
1875
|
async clearTombstone(table: string, rowId: string): Promise<void> {
|
|
1569
1876
|
this.#assertOpen();
|
|
1570
1877
|
this.#prefetchedTombstones.delete(`${table}\u0000${rowId}`);
|
|
1571
|
-
await this.#
|
|
1572
|
-
await this.#client.query(
|
|
1878
|
+
await this.#statement(
|
|
1573
1879
|
'DELETE FROM sync_tombstones WHERE partition=$1 AND tbl=$2 AND row_id=$3',
|
|
1574
1880
|
[this.#partition, table, rowId],
|
|
1881
|
+
'write',
|
|
1575
1882
|
);
|
|
1576
1883
|
}
|
|
1577
1884
|
|
|
@@ -1643,19 +1950,20 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1643
1950
|
postgresPlaceholders(prepared.sql),
|
|
1644
1951
|
prepared.params,
|
|
1645
1952
|
);
|
|
1646
|
-
//
|
|
1647
|
-
//
|
|
1648
|
-
if (this.#
|
|
1649
|
-
|
|
1650
|
-
'SELECT max_commit_seq FROM sync_partitions WHERE partition=$1',
|
|
1651
|
-
[this.#partition],
|
|
1652
|
-
);
|
|
1653
|
-
this.#cursorSeq =
|
|
1654
|
-
cursor.rows[0] === undefined
|
|
1655
|
-
? 0
|
|
1656
|
-
: asNumber(cursor.rows[0].max_commit_seq);
|
|
1953
|
+
// Under the partition lock the cursor is the locked `max_commit_seq` plus
|
|
1954
|
+
// this transaction's appends, which the query above flushed: no read.
|
|
1955
|
+
if (this.#nextCommitSeq !== undefined) {
|
|
1956
|
+
return { rows: result.rows, maxCommitSeq: this.#nextCommitSeq - 1 };
|
|
1657
1957
|
}
|
|
1658
|
-
|
|
1958
|
+
const cursor = await this.#client.query<{ max_commit_seq: unknown }>(
|
|
1959
|
+
'SELECT max_commit_seq FROM sync_partitions WHERE partition=$1',
|
|
1960
|
+
[this.#partition],
|
|
1961
|
+
);
|
|
1962
|
+
// asNumber maps the absent row of a partition without a commit to 0.
|
|
1963
|
+
return {
|
|
1964
|
+
rows: result.rows,
|
|
1965
|
+
maxCommitSeq: asNumber(cursor.rows[0]?.max_commit_seq),
|
|
1966
|
+
};
|
|
1659
1967
|
}
|
|
1660
1968
|
|
|
1661
1969
|
async preparePushApply(
|
|
@@ -1663,18 +1971,17 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1663
1971
|
commits: readonly PushApplyCommit[],
|
|
1664
1972
|
runningSchemaVersion: number,
|
|
1665
1973
|
outbox?: OutboxSettlement,
|
|
1974
|
+
options?: PushApplyOptions,
|
|
1666
1975
|
): Promise<PreparedPushApply> {
|
|
1667
1976
|
this.#assertOpen();
|
|
1977
|
+
this.#pushApply = true;
|
|
1978
|
+
this.#closeEagerly = options?.closeEagerly === true;
|
|
1668
1979
|
// The lock is its own statement: a READ COMMITTED statement's snapshot is
|
|
1669
1980
|
// fixed at its start, so the admission read must run *after* a duplicate's
|
|
1670
1981
|
// commit has released the partition lock to observe it. Folding the lock
|
|
1671
1982
|
// and the read into one statement would miss a duplicate that committed
|
|
1672
1983
|
// while this transaction waited.
|
|
1673
1984
|
await this.lockPartition();
|
|
1674
|
-
const watermark =
|
|
1675
|
-
outbox === undefined
|
|
1676
|
-
? undefined
|
|
1677
|
-
: await this.#settleOutbox(clientId, outbox);
|
|
1678
1985
|
const ids = new Map<string, Set<string>>();
|
|
1679
1986
|
for (const commit of commits) {
|
|
1680
1987
|
for (const target of commit.targets ?? []) {
|
|
@@ -1689,8 +1996,10 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1689
1996
|
}
|
|
1690
1997
|
const tables = [...ids.keys()].map((table) => this.#resolveTable(table));
|
|
1691
1998
|
const specs = [...ids].map(([tbl, rowIds]) => ({ tbl, ids: [...rowIds] }));
|
|
1692
|
-
// One statement for the whole request: the
|
|
1693
|
-
// idempotency record, and the read-ahead of every
|
|
1999
|
+
// One statement for the whole request: the outbox settlement, the serve
|
|
2000
|
+
// gate, every commit's idempotency record, and the read-ahead of every
|
|
2001
|
+
// commit's known rows. The settlement precedes every commit's savepoint,
|
|
2002
|
+
// so no rejection discards it.
|
|
1694
2003
|
const admission = await readAdmissionOn(
|
|
1695
2004
|
this.#client,
|
|
1696
2005
|
this.#partition,
|
|
@@ -1698,6 +2007,7 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1698
2007
|
commits.map((commit) => commit.clientCommitId),
|
|
1699
2008
|
runningSchemaVersion,
|
|
1700
2009
|
specs.length === 0 ? undefined : { tables, specs },
|
|
2010
|
+
outbox,
|
|
1701
2011
|
);
|
|
1702
2012
|
this.#recordPrefetched(admission.prefetched);
|
|
1703
2013
|
return {
|
|
@@ -1705,73 +2015,16 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1705
2015
|
persisted: (clientCommitId) =>
|
|
1706
2016
|
toPersistedPushResult(admission.persisted.get(clientCommitId)),
|
|
1707
2017
|
deleteFloor: admission.deleteFloor,
|
|
1708
|
-
...(watermark === undefined
|
|
2018
|
+
...(admission.watermark === undefined
|
|
2019
|
+
? {}
|
|
2020
|
+
: { watermark: admission.watermark }),
|
|
1709
2021
|
};
|
|
1710
2022
|
}
|
|
1711
2023
|
|
|
1712
|
-
/**
|
|
1713
|
-
* The §9 settlement and §6.3.2 steps 1 and 2 under the partition lock,
|
|
1714
|
-
* before the first commit's savepoint, so no rejection discards them. Two
|
|
1715
|
-
* statements: the settlement touches disjoint rows (outside and inside the
|
|
1716
|
-
* request), and the watermark delete must see its sequences.
|
|
1717
|
-
*/
|
|
1718
|
-
async #settleOutbox(
|
|
1719
|
-
clientId: string,
|
|
1720
|
-
outbox: OutboxSettlement,
|
|
1721
|
-
): Promise<number> {
|
|
1722
|
-
const first = outbox.commits[0];
|
|
1723
|
-
const last = outbox.commits.at(-1);
|
|
1724
|
-
if (first === undefined || last === undefined) {
|
|
1725
|
-
throw new Error('outbox settlement needs the request commits');
|
|
1726
|
-
}
|
|
1727
|
-
const commits = JSON.stringify(
|
|
1728
|
-
outbox.commits.map((commit) => ({
|
|
1729
|
-
id: commit.clientCommitId,
|
|
1730
|
-
seq: commit.clientSeq,
|
|
1731
|
-
})),
|
|
1732
|
-
);
|
|
1733
|
-
// §9 (b): with no pre-upgrade result on the request's last commit, every
|
|
1734
|
-
// remaining one outside the request left the outbox; (a) assigns the
|
|
1735
|
-
// request's sequences to its pre-upgrade results.
|
|
1736
|
-
await this.#client.query(
|
|
1737
|
-
`WITH c AS (SELECT * FROM jsonb_to_recordset($4::text::jsonb) AS c(id text, seq bigint)),
|
|
1738
|
-
b AS (DELETE FROM sync_push_results
|
|
1739
|
-
WHERE partition=$1 AND client_id=$2 AND outbox_id IS NULL
|
|
1740
|
-
AND client_commit_id NOT IN (SELECT id FROM c)
|
|
1741
|
-
AND NOT EXISTS (SELECT 1 FROM sync_push_results
|
|
1742
|
-
WHERE partition=$1 AND client_id=$2 AND client_commit_id=$5
|
|
1743
|
-
AND outbox_id IS NULL)),
|
|
1744
|
-
a AS (UPDATE sync_push_results r SET outbox_id=$3, client_seq=c.seq FROM c
|
|
1745
|
-
WHERE r.partition=$1 AND r.client_id=$2 AND r.outbox_id IS NULL
|
|
1746
|
-
AND r.client_commit_id=c.id)
|
|
1747
|
-
SELECT 1`,
|
|
1748
|
-
[
|
|
1749
|
-
this.#partition,
|
|
1750
|
-
clientId,
|
|
1751
|
-
outbox.outboxId,
|
|
1752
|
-
commits,
|
|
1753
|
-
last.clientCommitId,
|
|
1754
|
-
],
|
|
1755
|
-
);
|
|
1756
|
-
const { rows } = await this.#client.query<{ through_seq: unknown }>(
|
|
1757
|
-
`WITH w AS (
|
|
1758
|
-
INSERT INTO sync_push_watermarks(partition, client_id, outbox_id, through_seq)
|
|
1759
|
-
VALUES ($1,$2,$3,$4)
|
|
1760
|
-
ON CONFLICT (partition, client_id, outbox_id) DO UPDATE
|
|
1761
|
-
SET through_seq=GREATEST(sync_push_watermarks.through_seq, EXCLUDED.through_seq)
|
|
1762
|
-
RETURNING through_seq),
|
|
1763
|
-
d AS (DELETE FROM sync_push_results
|
|
1764
|
-
WHERE partition=$1 AND client_id=$2 AND outbox_id=$3
|
|
1765
|
-
AND client_seq<(SELECT through_seq FROM w))
|
|
1766
|
-
SELECT through_seq FROM w`,
|
|
1767
|
-
[this.#partition, clientId, outbox.outboxId, first.clientSeq],
|
|
1768
|
-
);
|
|
1769
|
-
return asNumber(rows[0]?.through_seq);
|
|
1770
|
-
}
|
|
1771
|
-
|
|
1772
2024
|
async lockPartition(): Promise<void> {
|
|
1773
2025
|
this.#assertOpen();
|
|
1774
|
-
|
|
2026
|
+
this.#nextCommitSeq =
|
|
2027
|
+
(await lockPartitionOn(this.#client, this.#partition)) + 1;
|
|
1775
2028
|
}
|
|
1776
2029
|
|
|
1777
2030
|
async pinSnapshot(): Promise<void> {
|
|
@@ -1790,15 +2043,15 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1790
2043
|
nowMs: number,
|
|
1791
2044
|
): Promise<boolean> {
|
|
1792
2045
|
this.#assertOpen();
|
|
1793
|
-
await this.#ensurePushSavepoint();
|
|
1794
2046
|
// `RETURNING`: `PgQueryable` reports returned rows only (no affected-row
|
|
1795
2047
|
// count), so every affected-row check counts returned rows.
|
|
1796
|
-
const result = await this.#
|
|
2048
|
+
const result = await this.#statement<{ name: string }>(
|
|
1797
2049
|
`UPDATE sync_backfill_checkpoints
|
|
1798
2050
|
SET watermark=$1, observed_rows=$2, updated_at_ms=$3
|
|
1799
2051
|
WHERE partition=$4 AND name=$5 AND owner_epoch=$6
|
|
1800
2052
|
RETURNING name`,
|
|
1801
2053
|
[watermark, observedRows, nowMs, this.#partition, name, ownerEpoch],
|
|
2054
|
+
'write',
|
|
1802
2055
|
);
|
|
1803
2056
|
return result.rows.length === 1;
|
|
1804
2057
|
}
|
|
@@ -1817,13 +2070,20 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1817
2070
|
sequence?: OutboxSequence,
|
|
1818
2071
|
): Promise<void> {
|
|
1819
2072
|
this.#assertOpen();
|
|
1820
|
-
// Candidate
|
|
1821
|
-
// staging
|
|
2073
|
+
// Candidate writes that never reached the database are discarded with the
|
|
2074
|
+
// staging maps; a savepoint exists only when one did. It stays open: the
|
|
1822
2075
|
// rollback returns the database to where the next commit starts (the
|
|
1823
|
-
//
|
|
2076
|
+
// closed commits' writes wait in memory or precede it), so the next
|
|
2077
|
+
// commit writes behind it.
|
|
1824
2078
|
this.#staged.clear();
|
|
1825
2079
|
this.#stagedBlobRefs.clear();
|
|
2080
|
+
this.#stagedAppend = undefined;
|
|
2081
|
+
if (this.#openCommitSeq !== undefined) {
|
|
2082
|
+
this.#nextCommitSeq = this.#openCommitSeq;
|
|
2083
|
+
this.#openCommitSeq = undefined;
|
|
2084
|
+
}
|
|
1826
2085
|
if (this.#pushApplySavepoint) {
|
|
2086
|
+
this.#pushApplySavepoint = false;
|
|
1827
2087
|
await this.#serial.query('ROLLBACK TO SAVEPOINT syncular_push_candidate');
|
|
1828
2088
|
}
|
|
1829
2089
|
this.#recordPushResult(clientId, clientCommitId, result, sequence);
|
|
@@ -1854,19 +2114,20 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1854
2114
|
const compiled = this.#resolveTable(table);
|
|
1855
2115
|
// Decodes the payload: an undecodable row fails here, at its operation.
|
|
1856
2116
|
const values = upsertValues(compiled, this.#partition, row, 'postgres');
|
|
2117
|
+
const entry = {
|
|
2118
|
+
row,
|
|
2119
|
+
values,
|
|
2120
|
+
...(context === undefined ? {} : { opIndex: context.opIndex }),
|
|
2121
|
+
};
|
|
1857
2122
|
if (!compiled.indexes.some((index) => index.unique === true)) {
|
|
1858
|
-
let rows = this.#staged.get(
|
|
2123
|
+
let rows = this.#staged.get(table);
|
|
1859
2124
|
if (rows === undefined) {
|
|
1860
2125
|
rows = new Map();
|
|
1861
|
-
this.#staged.set(
|
|
2126
|
+
this.#staged.set(table, rows);
|
|
1862
2127
|
}
|
|
1863
2128
|
// A later upsert of the same row in this transaction replaces it.
|
|
1864
2129
|
rows.delete(row.rowId);
|
|
1865
|
-
rows.set(row.rowId,
|
|
1866
|
-
row,
|
|
1867
|
-
values,
|
|
1868
|
-
...(context === undefined ? {} : { opIndex: context.opIndex }),
|
|
1869
|
-
});
|
|
2130
|
+
rows.set(row.rowId, entry);
|
|
1870
2131
|
return;
|
|
1871
2132
|
}
|
|
1872
2133
|
const refs = this.#stagedBlobRefs.get(table);
|
|
@@ -1875,15 +2136,25 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1875
2136
|
refs.delete(row.rowId);
|
|
1876
2137
|
if (refs.size === 0) this.#stagedBlobRefs.delete(table);
|
|
1877
2138
|
}
|
|
1878
|
-
|
|
2139
|
+
const statements = writeStatements(
|
|
2140
|
+
this.#partition,
|
|
2141
|
+
this.#writerVersion,
|
|
2142
|
+
this.#resolveTable,
|
|
2143
|
+
new Map([[table, new Map([[row.rowId, entry]])]]),
|
|
2144
|
+
blobIds === undefined
|
|
2145
|
+
? new Map()
|
|
2146
|
+
: new Map([[table, new Map([[row.rowId, blobIds]])]]),
|
|
2147
|
+
[],
|
|
2148
|
+
[],
|
|
2149
|
+
);
|
|
1879
2150
|
try {
|
|
1880
|
-
|
|
1881
|
-
this.#
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
2151
|
+
for (const statement of statements) {
|
|
2152
|
+
await this.#statement(
|
|
2153
|
+
`WITH ${statement.ctes.join(',\n')} SELECT 1`,
|
|
2154
|
+
statement.params,
|
|
2155
|
+
'write',
|
|
2156
|
+
);
|
|
2157
|
+
}
|
|
1887
2158
|
} catch (error) {
|
|
1888
2159
|
if (isPostgresConstraintError(error)) {
|
|
1889
2160
|
throw new StorageConstraintError(error, context?.opIndex);
|
|
@@ -1898,16 +2169,16 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1898
2169
|
const refs = this.#stagedBlobRefs.get(table);
|
|
1899
2170
|
refs?.delete(rowId);
|
|
1900
2171
|
if (refs?.size === 0) this.#stagedBlobRefs.delete(table);
|
|
1901
|
-
await this.#ensurePushSavepoint();
|
|
1902
2172
|
// One statement: the row, its scope-index entries, and (§5.9.4: a
|
|
1903
2173
|
// deleted row references no blobs) its blob references.
|
|
1904
|
-
await this.#
|
|
2174
|
+
await this.#statement(
|
|
1905
2175
|
`WITH deleted AS (${deleteRowSql(this.#resolveTable(table), 'postgres')}),
|
|
1906
2176
|
unscoped AS (
|
|
1907
2177
|
DELETE FROM sync_row_scopes WHERE partition=$1 AND tbl=$3 AND row_id=$2
|
|
1908
2178
|
)
|
|
1909
2179
|
DELETE FROM sync_blob_refs WHERE partition=$1 AND tbl=$3 AND row_id=$2`,
|
|
1910
2180
|
[this.#partition, rowId, table],
|
|
2181
|
+
'write',
|
|
1911
2182
|
);
|
|
1912
2183
|
}
|
|
1913
2184
|
|
|
@@ -1928,9 +2199,20 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1928
2199
|
return Promise.resolve();
|
|
1929
2200
|
}
|
|
1930
2201
|
|
|
2202
|
+
/**
|
|
2203
|
+
* Stage the append with the commit's rows. Under the partition lock the
|
|
2204
|
+
* cursor is frozen, so the next `commitSeq` is known without a statement
|
|
2205
|
+
* and a push commit's append waits for the next statement. Outside a push
|
|
2206
|
+
* apply the append is written at once (its allocation takes the lock when
|
|
2207
|
+
* no earlier statement did), so a relational constraint on the staged rows
|
|
2208
|
+
* fails this call.
|
|
2209
|
+
*/
|
|
1931
2210
|
async appendCommit(commit: NewCommit): Promise<number> {
|
|
1932
2211
|
this.#assertOpen();
|
|
1933
|
-
|
|
2212
|
+
if (this.#stagedAppend !== undefined || this.#openCommitSeq !== undefined) {
|
|
2213
|
+
throw new Error('the open commit already appended');
|
|
2214
|
+
}
|
|
2215
|
+
// The commit writes its delete tombstones; every other prefetched
|
|
1934
2216
|
// tombstone still holds for the request's later commits.
|
|
1935
2217
|
for (const change of commit.changes) {
|
|
1936
2218
|
if (change.op === 'delete') {
|
|
@@ -1939,151 +2221,20 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1939
2221
|
);
|
|
1940
2222
|
}
|
|
1941
2223
|
}
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
const allocate = `INSERT INTO sync_partitions(partition, max_commit_seq, delete_floor) VALUES ($1, 1, $7)
|
|
1952
|
-
ON CONFLICT (partition) DO UPDATE
|
|
1953
|
-
SET max_commit_seq = sync_partitions.max_commit_seq + 1,
|
|
1954
|
-
delete_floor = GREATEST(sync_partitions.delete_floor, EXCLUDED.delete_floor)
|
|
1955
|
-
RETURNING max_commit_seq`;
|
|
1956
|
-
const commitRow = `INSERT INTO sync_commits(partition, commit_seq, client_id, client_commit_id, actor_id, created_at_ms, writer_version)
|
|
1957
|
-
SELECT $1, allocated.max_commit_seq, $2, $3, $4, $5, $6 FROM allocated
|
|
1958
|
-
RETURNING commit_seq`;
|
|
1959
|
-
const head = [
|
|
1960
|
-
p,
|
|
1961
|
-
commit.clientId,
|
|
1962
|
-
commit.clientCommitId,
|
|
1963
|
-
commit.actorId,
|
|
1964
|
-
commit.createdAtMs,
|
|
1965
|
-
this.#writerVersion,
|
|
1966
|
-
commitDeleteFloor(commit.changes),
|
|
1967
|
-
];
|
|
1968
|
-
if (commit.changes.length === 0) {
|
|
1969
|
-
const { rows } = await q.query<{ commit_seq: unknown }>(
|
|
1970
|
-
`WITH allocated AS (${allocate}) ${commitRow}`,
|
|
1971
|
-
head,
|
|
1972
|
-
);
|
|
1973
|
-
return asNumber(rows[0]?.commit_seq);
|
|
1974
|
-
}
|
|
1975
|
-
// §2.2: a delete whose commit follows it with an upsert is a scope move.
|
|
1976
|
-
// The row is live, so the tombstone insert below skips it.
|
|
1977
|
-
const moved = JSON.stringify(
|
|
1978
|
-
[...rowsLiveAfterDelete(commit.changes)].map((key) => {
|
|
1979
|
-
const at = key.indexOf('\u0000');
|
|
1980
|
-
return { tbl: key.slice(0, at), row_id: key.slice(at + 1) };
|
|
1981
|
-
}),
|
|
1982
|
-
);
|
|
1983
|
-
// One writable-CTE chain per chunk of changes: the changes, their
|
|
1984
|
-
// inverted scope entries, and the §5 delete tombstones. A statement binds
|
|
1985
|
-
// at most a 16-bit count of parameters, so a chunk carries
|
|
1986
|
-
// PG_CHANGES_PER_STATEMENT changes. The first chunk folds the allocation,
|
|
1987
|
-
// the commit row, and the change-log write into one statement; later
|
|
1988
|
-
// chunks run under the same commitSeq with their global idx. Serialized
|
|
1989
|
-
// scopes bind as text before parsing JSONB; drivers that encode JSONB
|
|
1990
|
-
// parameters would otherwise store the string as a scalar. A commit can
|
|
1991
|
-
// delete one row twice, so a chunk's tombstone insert keeps the latest
|
|
1992
|
-
// delete per key and a later chunk overwrites it with its own. `version`
|
|
1993
|
-
// is the change's row version (§2.2), which a delete's tombstone keeps.
|
|
1994
|
-
const valuesOf = (
|
|
1995
|
-
params: unknown[],
|
|
1996
|
-
slice: readonly NewChange[],
|
|
1997
|
-
firstIdx: number,
|
|
1998
|
-
): string[] =>
|
|
1999
|
-
slice.map((change, offset) => {
|
|
2000
|
-
const base = params.length;
|
|
2001
|
-
params.push(
|
|
2002
|
-
firstIdx + offset,
|
|
2003
|
-
change.table,
|
|
2004
|
-
change.rowId,
|
|
2005
|
-
change.op === 'upsert' ? 1 : 2,
|
|
2006
|
-
change.rowVersion,
|
|
2007
|
-
JSON.stringify(change.scopes),
|
|
2008
|
-
change.payload ?? null,
|
|
2009
|
-
);
|
|
2010
|
-
return `($${base + 1}::int,$${base + 2}::text,$${base + 3}::text,$${base + 4}::smallint,$${base + 5}::bigint,$${base + 6}::text::jsonb,$${base + 7}::bytea)`;
|
|
2011
|
-
});
|
|
2012
|
-
const scopesCte = `scoped AS (
|
|
2013
|
-
INSERT INTO sync_change_scopes(partition, tbl, var, value, commit_seq)
|
|
2014
|
-
SELECT inserted.partition, inserted.tbl, scope.key, scope.value, inserted.commit_seq
|
|
2015
|
-
FROM inserted CROSS JOIN LATERAL jsonb_each_text(inserted.scopes) AS scope
|
|
2016
|
-
ON CONFLICT DO NOTHING
|
|
2017
|
-
)`;
|
|
2018
|
-
const tombstones = (
|
|
2019
|
-
movedParam: string,
|
|
2020
|
-
) => `INSERT INTO sync_tombstones(partition, tbl, row_id, commit_seq, server_version, scopes)
|
|
2021
|
-
SELECT DISTINCT ON (inserted.tbl, inserted.row_id) inserted.partition,
|
|
2022
|
-
inserted.tbl, inserted.row_id, inserted.commit_seq,
|
|
2023
|
-
v.version, inserted.scopes
|
|
2024
|
-
FROM inserted JOIN v ON v.idx = inserted.idx
|
|
2025
|
-
WHERE inserted.op = 2
|
|
2026
|
-
AND NOT EXISTS (
|
|
2027
|
-
SELECT 1
|
|
2028
|
-
FROM jsonb_to_recordset(${movedParam}::text::jsonb) AS moved(tbl text, row_id text)
|
|
2029
|
-
WHERE moved.tbl = inserted.tbl AND moved.row_id = inserted.row_id)
|
|
2030
|
-
ORDER BY inserted.tbl, inserted.row_id, inserted.idx DESC
|
|
2031
|
-
ON CONFLICT (partition, tbl, row_id)
|
|
2032
|
-
DO UPDATE SET commit_seq = excluded.commit_seq,
|
|
2033
|
-
server_version = excluded.server_version,
|
|
2034
|
-
scopes = excluded.scopes`;
|
|
2035
|
-
const firstSlice = commit.changes.slice(0, PG_CHANGES_PER_STATEMENT);
|
|
2036
|
-
const params = [...head];
|
|
2037
|
-
const values = valuesOf(params, firstSlice, 0);
|
|
2038
|
-
params.push(moved);
|
|
2039
|
-
const movedParam = `$${params.length}`;
|
|
2040
|
-
const { rows } = await q.query<{ max_commit_seq: unknown }>(
|
|
2041
|
-
`WITH allocated AS (${allocate}),
|
|
2042
|
-
allocated_commit AS (${commitRow}),
|
|
2043
|
-
v(idx, tbl, row_id, op, version, scopes, payload) AS (
|
|
2044
|
-
VALUES ${values.join(',')}
|
|
2045
|
-
),
|
|
2046
|
-
inserted AS (
|
|
2047
|
-
INSERT INTO sync_changes(partition, commit_seq, idx, tbl, row_id, op, row_version, scopes, payload)
|
|
2048
|
-
SELECT $1, allocated.max_commit_seq, v.idx, v.tbl, v.row_id, v.op, v.version, v.scopes, v.payload
|
|
2049
|
-
FROM allocated CROSS JOIN v
|
|
2050
|
-
RETURNING partition, idx, tbl, row_id, op, commit_seq, scopes
|
|
2051
|
-
),
|
|
2052
|
-
${scopesCte},
|
|
2053
|
-
tombstoned AS (${tombstones(movedParam)})
|
|
2054
|
-
SELECT max_commit_seq FROM allocated`,
|
|
2055
|
-
params,
|
|
2056
|
-
);
|
|
2057
|
-
const commitSeq = asNumber(rows[0]?.max_commit_seq);
|
|
2058
|
-
for (
|
|
2059
|
-
let first = PG_CHANGES_PER_STATEMENT;
|
|
2060
|
-
first < commit.changes.length;
|
|
2061
|
-
first += PG_CHANGES_PER_STATEMENT
|
|
2062
|
-
) {
|
|
2063
|
-
const chunkParams: unknown[] = [p, commitSeq];
|
|
2064
|
-
const chunkValues = valuesOf(
|
|
2065
|
-
chunkParams,
|
|
2066
|
-
commit.changes.slice(first, first + PG_CHANGES_PER_STATEMENT),
|
|
2067
|
-
first,
|
|
2068
|
-
);
|
|
2069
|
-
chunkParams.push(moved);
|
|
2070
|
-
const chunkMovedParam = `$${chunkParams.length}`;
|
|
2071
|
-
await q.query(
|
|
2072
|
-
`WITH v(idx, tbl, row_id, op, version, scopes, payload) AS (
|
|
2073
|
-
VALUES ${chunkValues.join(',')}
|
|
2074
|
-
),
|
|
2075
|
-
inserted AS (
|
|
2076
|
-
INSERT INTO sync_changes(partition, commit_seq, idx, tbl, row_id, op, row_version, scopes, payload)
|
|
2077
|
-
SELECT $1, $2, v.idx, v.tbl, v.row_id, v.op, v.version, v.scopes, v.payload
|
|
2078
|
-
FROM v
|
|
2079
|
-
RETURNING partition, idx, tbl, row_id, op, commit_seq, scopes
|
|
2080
|
-
),
|
|
2081
|
-
${scopesCte},
|
|
2082
|
-
tombstoned AS (${tombstones(chunkMovedParam)})
|
|
2083
|
-
SELECT 1`,
|
|
2084
|
-
chunkParams,
|
|
2085
|
-
);
|
|
2224
|
+
const commitSeq = this.#nextCommitSeq;
|
|
2225
|
+
if (commitSeq === undefined) {
|
|
2226
|
+
this.#stagedAppend = { commit };
|
|
2227
|
+
const allocated = await this.#flush('write');
|
|
2228
|
+
if (allocated === undefined) {
|
|
2229
|
+
throw new Error('the commit append returned no cursor');
|
|
2230
|
+
}
|
|
2231
|
+
this.#nextCommitSeq = allocated + 1;
|
|
2232
|
+
return allocated;
|
|
2086
2233
|
}
|
|
2234
|
+
this.#nextCommitSeq = commitSeq + 1;
|
|
2235
|
+
this.#stagedAppend = { commit, commitSeq };
|
|
2236
|
+
if (this.#pushApply) this.#openCommitSeq = commitSeq;
|
|
2237
|
+
else await this.#flush('write');
|
|
2087
2238
|
return commitSeq;
|
|
2088
2239
|
}
|
|
2089
2240
|
|
|
@@ -2094,21 +2245,45 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
2094
2245
|
sequence?: OutboxSequence,
|
|
2095
2246
|
): Promise<void> {
|
|
2096
2247
|
this.#assertOpen();
|
|
2248
|
+
// On the rerun after a closed commit's write failed, the commit writes
|
|
2249
|
+
// behind its savepoint before it closes, so a relational constraint
|
|
2250
|
+
// rejects it alone (`StorageConstraintError`).
|
|
2251
|
+
if (this.#closeEagerly) await this.#flush('read');
|
|
2097
2252
|
this.#recordPushResult(clientId, clientCommitId, result, sequence);
|
|
2098
|
-
// The commit is closed: the
|
|
2099
|
-
// savepoint.
|
|
2253
|
+
// The commit is closed: its writes join the closed commits', and the next
|
|
2254
|
+
// commit's first write takes its own savepoint.
|
|
2255
|
+
for (const [table, rows] of this.#staged) {
|
|
2256
|
+
const closed = this.#closedRows.get(table) ?? new Map();
|
|
2257
|
+
for (const [rowId, entry] of rows) {
|
|
2258
|
+
closed.delete(rowId);
|
|
2259
|
+
closed.set(rowId, entry);
|
|
2260
|
+
}
|
|
2261
|
+
this.#closedRows.set(table, closed);
|
|
2262
|
+
}
|
|
2263
|
+
for (const [table, refs] of this.#stagedBlobRefs) {
|
|
2264
|
+
const closed = this.#closedBlobRefs.get(table) ?? new Map();
|
|
2265
|
+
for (const [rowId, blobIds] of refs) closed.set(rowId, blobIds);
|
|
2266
|
+
this.#closedBlobRefs.set(table, closed);
|
|
2267
|
+
}
|
|
2268
|
+
if (this.#stagedAppend !== undefined) {
|
|
2269
|
+
this.#closedAppends.push(this.#stagedAppend);
|
|
2270
|
+
}
|
|
2271
|
+
this.#staged.clear();
|
|
2272
|
+
this.#stagedBlobRefs.clear();
|
|
2273
|
+
this.#stagedAppend = undefined;
|
|
2274
|
+
this.#openCommitSeq = undefined;
|
|
2100
2275
|
this.#pushApplySavepoint = false;
|
|
2101
2276
|
}
|
|
2102
2277
|
|
|
2103
2278
|
async enqueueReactions(reactions: readonly NewReaction[]): Promise<void> {
|
|
2104
2279
|
this.#assertOpen();
|
|
2105
2280
|
for (const reaction of reactions) {
|
|
2106
|
-
await this.#
|
|
2281
|
+
await this.#statement(
|
|
2107
2282
|
`INSERT INTO sync_reactions(
|
|
2108
2283
|
partition, idempotency_key, type, version, payload,
|
|
2109
2284
|
source_client_id, source_client_commit_id, source_commit_seq,
|
|
2110
2285
|
created_at_ms, available_at_ms, status, attempts, max_attempts
|
|
2111
|
-
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$9,'pending',0,$10)`,
|
|
2286
|
+
) VALUES ($1,$2,$3,$4,$5::text::jsonb,$6,$7,$8,$9,$9,'pending',0,$10)`,
|
|
2112
2287
|
[
|
|
2113
2288
|
this.#partition,
|
|
2114
2289
|
reaction.idempotencyKey,
|
|
@@ -2121,26 +2296,15 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
2121
2296
|
reaction.createdAtMs,
|
|
2122
2297
|
reaction.maxAttempts,
|
|
2123
2298
|
],
|
|
2299
|
+
'write',
|
|
2124
2300
|
);
|
|
2125
2301
|
}
|
|
2126
2302
|
}
|
|
2127
2303
|
|
|
2128
2304
|
async commit(): Promise<void> {
|
|
2129
2305
|
this.#assertOpen();
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
// staged writes first.
|
|
2133
|
-
await this.#client.query(
|
|
2134
|
-
`INSERT INTO sync_push_results(partition, client_id, client_commit_id, result, outbox_id, client_seq)
|
|
2135
|
-
SELECT $1, r.client_id, r.client_commit_id, r.result, r.outbox_id, r.client_seq
|
|
2136
|
-
FROM jsonb_to_recordset($2::text::jsonb)
|
|
2137
|
-
AS r(client_id text, client_commit_id text, result jsonb, outbox_id text, client_seq bigint)
|
|
2138
|
-
ON CONFLICT (partition, client_id, client_commit_id) DO NOTHING`,
|
|
2139
|
-
[this.#partition, JSON.stringify(this.#pushResults)],
|
|
2140
|
-
);
|
|
2141
|
-
this.#pushResults.length = 0;
|
|
2142
|
-
}
|
|
2143
|
-
await this.#writeStaged();
|
|
2306
|
+
// Every pending write in one statement where the parameters fit.
|
|
2307
|
+
await this.#flush('final');
|
|
2144
2308
|
this.#open = false;
|
|
2145
2309
|
// Signal the transaction wrapper to COMMIT; wait for it to actually land.
|
|
2146
2310
|
this.#resolve();
|
|
@@ -2149,7 +2313,6 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
2149
2313
|
|
|
2150
2314
|
async rollback(): Promise<void> {
|
|
2151
2315
|
if (!this.#open) return;
|
|
2152
|
-
this.#staged.clear();
|
|
2153
2316
|
this.#open = false;
|
|
2154
2317
|
this.#reject(new RollbackSignal());
|
|
2155
2318
|
// Swallow — a rollback is a normal outcome, not an error to the caller.
|
|
@@ -2171,11 +2334,6 @@ class RollbackSignal extends Error {
|
|
|
2171
2334
|
}
|
|
2172
2335
|
}
|
|
2173
2336
|
|
|
2174
|
-
/** Changes per sync_changes statement: 2 + 7 × 4,096 = 28,674 bound
|
|
2175
|
-
* parameters. The Bind message counts parameters in 16 bits; PostgreSQL reads
|
|
2176
|
-
* up to 65,535, and PGlite 0.5 corrupts its state above 32,767. */
|
|
2177
|
-
const PG_CHANGES_PER_STATEMENT = 4096;
|
|
2178
|
-
|
|
2179
2337
|
/** One page read waiting in `PostgresServerStorage#batchedPage`. */
|
|
2180
2338
|
interface PendingPage {
|
|
2181
2339
|
readonly spec: Record<string, unknown>;
|
|
@@ -3199,7 +3357,7 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
3199
3357
|
): Promise<boolean> {
|
|
3200
3358
|
const result = await this.#exec.query(
|
|
3201
3359
|
`UPDATE sync_reactions
|
|
3202
|
-
SET status=$4, available_at_ms=$5, last_failure=$6,
|
|
3360
|
+
SET status=$4, available_at_ms=$5, last_failure=$6::text::jsonb,
|
|
3203
3361
|
lease_owner=NULL, lease_expires_at_ms=NULL
|
|
3204
3362
|
WHERE partition=$1 AND idempotency_key=$2
|
|
3205
3363
|
AND status='leased' AND lease_owner=$3
|