@syncular/server 0.82.1 → 0.82.2
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/postgres-storage.js +35 -8
- package/package.json +2 -2
- package/src/postgres-storage.ts +46 -14
package/dist/postgres-storage.js
CHANGED
|
@@ -1153,6 +1153,26 @@ class PostgresTransaction {
|
|
|
1153
1153
|
})));
|
|
1154
1154
|
return cursors.find((cursor) => cursor !== undefined);
|
|
1155
1155
|
}
|
|
1156
|
+
/**
|
|
1157
|
+
* The executor of a read over `tables`: it writes the pending writes first
|
|
1158
|
+
* only when one of them is a row of those tables or a change of the commit
|
|
1159
|
+
* log to them (§6.4). A read of other tables runs without them, so it takes
|
|
1160
|
+
* no savepoint and moves no write ahead of itself.
|
|
1161
|
+
*/
|
|
1162
|
+
#readOf(tables) {
|
|
1163
|
+
return {
|
|
1164
|
+
query: (text, params) => {
|
|
1165
|
+
const appends = this.#stagedAppend === undefined
|
|
1166
|
+
? this.#closedAppends
|
|
1167
|
+
: [...this.#closedAppends, this.#stagedAppend];
|
|
1168
|
+
return tables.some((table) => this.#staged.has(table) ||
|
|
1169
|
+
this.#closedRows.has(table) ||
|
|
1170
|
+
appends.some(({ commit }) => commit.changes.some((change) => change.table === table)))
|
|
1171
|
+
? this.#client.query(text, params)
|
|
1172
|
+
: this.#serial.query(text, params);
|
|
1173
|
+
},
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1156
1176
|
/** The candidate row this transaction staged and has not yet written. */
|
|
1157
1177
|
#stagedRow(table, rowId) {
|
|
1158
1178
|
return (this.#staged.get(table)?.get(rowId)?.row ??
|
|
@@ -1211,7 +1231,10 @@ class PostgresTransaction {
|
|
|
1211
1231
|
}
|
|
1212
1232
|
if (specs.length === 0)
|
|
1213
1233
|
return;
|
|
1214
|
-
const { rows } = await this.#
|
|
1234
|
+
const { rows } = await this.#readOf(specs.map((spec) => spec.tbl)).query(prefetchRowsBatchSql(tables), [
|
|
1235
|
+
this.#partition,
|
|
1236
|
+
JSON.stringify(specs),
|
|
1237
|
+
]);
|
|
1215
1238
|
this.#recordPrefetched(rows);
|
|
1216
1239
|
}
|
|
1217
1240
|
/** Answer the following `getRow`/`getTombstone` calls from a read snapshot. */
|
|
@@ -1229,7 +1252,7 @@ class PostgresTransaction {
|
|
|
1229
1252
|
const prefetched = this.#prefetchedTombstones.get(`${table}\u0000${rowId}`);
|
|
1230
1253
|
if (prefetched !== undefined)
|
|
1231
1254
|
return prefetched ?? undefined;
|
|
1232
|
-
const { rows } = await this.#
|
|
1255
|
+
const { rows } = await this.#readOf([table]).query('SELECT commit_seq, server_version, scopes FROM sync_tombstones WHERE partition=$1 AND tbl=$2 AND row_id=$3', [this.#partition, table, rowId]);
|
|
1233
1256
|
const row = rows[0];
|
|
1234
1257
|
return (toTombstone(row?.commit_seq, row?.server_version, row?.scopes) ??
|
|
1235
1258
|
undefined);
|
|
@@ -1241,7 +1264,8 @@ class PostgresTransaction {
|
|
|
1241
1264
|
}
|
|
1242
1265
|
async scanRows(query) {
|
|
1243
1266
|
this.#assertOpen();
|
|
1244
|
-
const
|
|
1267
|
+
const client = this.#readOf([query.table]);
|
|
1268
|
+
const variable = await chooseScanVariable(this.#partition, query, this.#scanVariables, (probes, cap) => client
|
|
1245
1269
|
.query(scanProbeSql('postgres'), [
|
|
1246
1270
|
this.#partition,
|
|
1247
1271
|
JSON.stringify(probes.map((probe) => ({ ...probe, lim: cap }))),
|
|
@@ -1255,7 +1279,7 @@ class PostgresTransaction {
|
|
|
1255
1279
|
let afterRowId = query.afterRowId ?? '';
|
|
1256
1280
|
const batchSize = Math.max(64, query.limit);
|
|
1257
1281
|
while (rows.length < query.limit) {
|
|
1258
|
-
const { rows: records } = await
|
|
1282
|
+
const { rows: records } = await client.query(sql, [
|
|
1259
1283
|
this.#partition,
|
|
1260
1284
|
query.table,
|
|
1261
1285
|
variable,
|
|
@@ -1283,16 +1307,19 @@ class PostgresTransaction {
|
|
|
1283
1307
|
}
|
|
1284
1308
|
scanRowsByIndex(query) {
|
|
1285
1309
|
this.#assertOpen();
|
|
1286
|
-
return scanRowsByIndexOn(this.#
|
|
1310
|
+
return scanRowsByIndexOn(this.#readOf([query.table]), this.#resolveTable(query.table), this.#partition, query);
|
|
1287
1311
|
}
|
|
1288
1312
|
async queryAuthoritative(query) {
|
|
1289
1313
|
this.#assertOpen();
|
|
1290
1314
|
// Runs on this transaction's pinned client, so the read observes every
|
|
1291
|
-
// staged row and never waits for a second pool connection.
|
|
1315
|
+
// staged row and never waits for a second pool connection. The prepared
|
|
1316
|
+
// statement reads exactly `query.tables`: every relation of the plan is
|
|
1317
|
+
// one of them, rewritten to a derived relation over its own table.
|
|
1292
1318
|
const prepared = this.#prepareQuery(query);
|
|
1293
|
-
const result = await this.#
|
|
1319
|
+
const result = await this.#readOf(query.tables).query(postgresPlaceholders(prepared.sql), prepared.params);
|
|
1294
1320
|
// Under the partition lock the cursor is the locked `max_commit_seq` plus
|
|
1295
|
-
// this transaction's appends
|
|
1321
|
+
// this transaction's appends: no read. An append still held back changed
|
|
1322
|
+
// no table the query read, so the rows are those of that cursor.
|
|
1296
1323
|
if (this.#nextCommitSeq !== undefined) {
|
|
1297
1324
|
return { rows: result.rows, maxCommitSeq: this.#nextCommitSeq - 1 };
|
|
1298
1325
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/server",
|
|
3
|
-
"version": "0.82.
|
|
3
|
+
"version": "0.82.2",
|
|
4
4
|
"description": "Syncular server: handleSyncRequest + storage/auth interfaces for the sync protocol",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"verify:node": "cd ../.. && bun build ./packages/server/test/sqlite-runtime/verify-node.mjs --target=node --conditions=bun --outfile=./packages/server/.verify-node.built.mjs && node ./packages/server/.verify-node.built.mjs"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@syncular/core": "0.82.
|
|
71
|
+
"@syncular/core": "0.82.2"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@electric-sql/pglite": "^0.5.4"
|
package/src/postgres-storage.ts
CHANGED
|
@@ -1748,6 +1748,33 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1748
1748
|
return cursors.find((cursor) => cursor !== undefined);
|
|
1749
1749
|
}
|
|
1750
1750
|
|
|
1751
|
+
/**
|
|
1752
|
+
* The executor of a read over `tables`: it writes the pending writes first
|
|
1753
|
+
* only when one of them is a row of those tables or a change of the commit
|
|
1754
|
+
* log to them (§6.4). A read of other tables runs without them, so it takes
|
|
1755
|
+
* no savepoint and moves no write ahead of itself.
|
|
1756
|
+
*/
|
|
1757
|
+
#readOf(tables: readonly string[]): PgQueryable {
|
|
1758
|
+
return {
|
|
1759
|
+
query: (text, params) => {
|
|
1760
|
+
const appends =
|
|
1761
|
+
this.#stagedAppend === undefined
|
|
1762
|
+
? this.#closedAppends
|
|
1763
|
+
: [...this.#closedAppends, this.#stagedAppend];
|
|
1764
|
+
return tables.some(
|
|
1765
|
+
(table) =>
|
|
1766
|
+
this.#staged.has(table) ||
|
|
1767
|
+
this.#closedRows.has(table) ||
|
|
1768
|
+
appends.some(({ commit }) =>
|
|
1769
|
+
commit.changes.some((change) => change.table === table),
|
|
1770
|
+
),
|
|
1771
|
+
)
|
|
1772
|
+
? this.#client.query(text, params)
|
|
1773
|
+
: this.#serial.query(text, params);
|
|
1774
|
+
},
|
|
1775
|
+
};
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1751
1778
|
/** The candidate row this transaction staged and has not yet written. */
|
|
1752
1779
|
#stagedRow(table: string, rowId: string): StoredRow | undefined {
|
|
1753
1780
|
return (
|
|
@@ -1822,10 +1849,12 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1822
1849
|
specs.push({ tbl: target.table, ids });
|
|
1823
1850
|
}
|
|
1824
1851
|
if (specs.length === 0) return;
|
|
1825
|
-
const { rows } = await this.#
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1852
|
+
const { rows } = await this.#readOf(
|
|
1853
|
+
specs.map((spec) => spec.tbl),
|
|
1854
|
+
).query<PrefetchRow>(prefetchRowsBatchSql(tables), [
|
|
1855
|
+
this.#partition,
|
|
1856
|
+
JSON.stringify(specs),
|
|
1857
|
+
]);
|
|
1829
1858
|
this.#recordPrefetched(rows);
|
|
1830
1859
|
}
|
|
1831
1860
|
|
|
@@ -1857,7 +1886,7 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1857
1886
|
this.#assertOpen();
|
|
1858
1887
|
const prefetched = this.#prefetchedTombstones.get(`${table}\u0000${rowId}`);
|
|
1859
1888
|
if (prefetched !== undefined) return prefetched ?? undefined;
|
|
1860
|
-
const { rows } = await this.#
|
|
1889
|
+
const { rows } = await this.#readOf([table]).query<{
|
|
1861
1890
|
commit_seq: unknown;
|
|
1862
1891
|
server_version: unknown;
|
|
1863
1892
|
scopes: unknown;
|
|
@@ -1884,12 +1913,13 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1884
1913
|
|
|
1885
1914
|
async scanRows(query: RowScanQuery): Promise<StoredRow[]> {
|
|
1886
1915
|
this.#assertOpen();
|
|
1916
|
+
const client = this.#readOf([query.table]);
|
|
1887
1917
|
const variable = await chooseScanVariable(
|
|
1888
1918
|
this.#partition,
|
|
1889
1919
|
query,
|
|
1890
1920
|
this.#scanVariables,
|
|
1891
1921
|
(probes, cap) =>
|
|
1892
|
-
|
|
1922
|
+
client
|
|
1893
1923
|
.query<{ sub: unknown; n: unknown }>(scanProbeSql('postgres'), [
|
|
1894
1924
|
this.#partition,
|
|
1895
1925
|
JSON.stringify(probes.map((probe) => ({ ...probe, lim: cap }))),
|
|
@@ -1907,7 +1937,7 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1907
1937
|
let afterRowId = query.afterRowId ?? '';
|
|
1908
1938
|
const batchSize = Math.max(64, query.limit);
|
|
1909
1939
|
while (rows.length < query.limit) {
|
|
1910
|
-
const { rows: records } = await
|
|
1940
|
+
const { rows: records } = await client.query<RowRecord>(sql, [
|
|
1911
1941
|
this.#partition,
|
|
1912
1942
|
query.table,
|
|
1913
1943
|
variable,
|
|
@@ -1932,7 +1962,7 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1932
1962
|
scanRowsByIndex(query: IndexRowScanQuery): Promise<StoredRow[]> {
|
|
1933
1963
|
this.#assertOpen();
|
|
1934
1964
|
return scanRowsByIndexOn(
|
|
1935
|
-
this.#
|
|
1965
|
+
this.#readOf([query.table]),
|
|
1936
1966
|
this.#resolveTable(query.table),
|
|
1937
1967
|
this.#partition,
|
|
1938
1968
|
query,
|
|
@@ -1944,14 +1974,16 @@ class PostgresTransaction implements StorageTransaction {
|
|
|
1944
1974
|
): Promise<AuthoritativeQueryResult> {
|
|
1945
1975
|
this.#assertOpen();
|
|
1946
1976
|
// Runs on this transaction's pinned client, so the read observes every
|
|
1947
|
-
// staged row and never waits for a second pool connection.
|
|
1977
|
+
// staged row and never waits for a second pool connection. The prepared
|
|
1978
|
+
// statement reads exactly `query.tables`: every relation of the plan is
|
|
1979
|
+
// one of them, rewritten to a derived relation over its own table.
|
|
1948
1980
|
const prepared = this.#prepareQuery(query);
|
|
1949
|
-
const result = await this.#
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
);
|
|
1981
|
+
const result = await this.#readOf(query.tables).query<
|
|
1982
|
+
Readonly<Record<string, unknown>>
|
|
1983
|
+
>(postgresPlaceholders(prepared.sql), prepared.params);
|
|
1953
1984
|
// Under the partition lock the cursor is the locked `max_commit_seq` plus
|
|
1954
|
-
// this transaction's appends
|
|
1985
|
+
// this transaction's appends: no read. An append still held back changed
|
|
1986
|
+
// no table the query read, so the rows are those of that cursor.
|
|
1955
1987
|
if (this.#nextCommitSeq !== undefined) {
|
|
1956
1988
|
return { rows: result.rows, maxCommitSeq: this.#nextCommitSeq - 1 };
|
|
1957
1989
|
}
|