@rotorsoft/act-pg 1.13.16 → 1.13.18
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/.tsbuildinfo +1 -1
- package/dist/@types/postgres-store.d.ts.map +1 -1
- package/dist/index.cjs +69 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +71 -17
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgres-store.d.ts","sourceRoot":"","sources":["../../src/postgres-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,SAAS,EACT,SAAS,EACT,KAAK,EAEL,OAAO,EACP,cAAc,EACd,KAAK,EACL,iBAAiB,EACjB,YAAY,EACZ,kBAAkB,EAClB,MAAM,EACN,OAAO,EACP,KAAK,EACL,iBAAiB,EACjB,YAAY,EACZ,cAAc,EACd,WAAW,EACZ,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"postgres-store.d.ts","sourceRoot":"","sources":["../../src/postgres-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,SAAS,EACT,SAAS,EACT,KAAK,EAEL,OAAO,EACP,cAAc,EACd,KAAK,EACL,iBAAiB,EACjB,YAAY,EACZ,kBAAkB,EAClB,MAAM,EACN,OAAO,EACP,KAAK,EACL,iBAAiB,EACjB,YAAY,EACZ,cAAc,EACd,WAAW,EACZ,MAAM,gBAAgB,CAAC;AAUxB,OAAO,EAEL,KAAK,UAAU,EAGhB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,MAAM,IAAI,CAAC;AAqCpB,KAAK,MAAM,GAAG,QAAQ,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;;;;;;;;;;;;;;;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,MAAM,CAAC,EAAE,IAAI,CAAC;QACd,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,CAAC,GACD,OAAO,CACR,GAAG,CACD,MAAM,EACN;QACE,OAAO,EAAE,MAAM,CAAC;QAChB,SAAS,EAAE,SAAS,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC;QAC7C,MAAM,CAAC,EAAE,IAAI,CAAC;KACf,CACF,CACF,CAsFA;IAED;;;;;;;;;;;OAWG;IACG,OAAO,CACX,MAAM,EAAE,CACN,QAAQ,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,KACpE,OAAO,CAAC,IAAI,CAAC,GACjB,OAAO,CAAC,IAAI,CAAC,CA4Cf;IAED;;;;;;;;;;;;;;;OAeG;IACG,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAMhD;CACF"}
|
package/dist/index.cjs
CHANGED
|
@@ -51,6 +51,7 @@ var scopedTypes = {
|
|
|
51
51
|
};
|
|
52
52
|
var SAFE_IDENTIFIER = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
53
53
|
var PG_UNIQUE_VIOLATION = "23505";
|
|
54
|
+
var PG_NUL_BYTE = /* @__PURE__ */ new Set(["22P05", "22021"]);
|
|
54
55
|
var NOTIFY_CHANNEL_PREFIX = "act_commit";
|
|
55
56
|
var NOTIFY_MAX_PAYLOAD_BYTES = 8e3;
|
|
56
57
|
var NOTIFY_RECONNECT_BASE_MS = 250;
|
|
@@ -264,6 +265,9 @@ var PostgresStore = class {
|
|
|
264
265
|
const client = await this._client("seed");
|
|
265
266
|
try {
|
|
266
267
|
await client.query("BEGIN");
|
|
268
|
+
await client.query(`SELECT pg_advisory_xact_lock(hashtext($1))`, [
|
|
269
|
+
this.config.schema
|
|
270
|
+
]);
|
|
267
271
|
await client.query(`SELECT pg_advisory_xact_lock(hashtext($1))`, [
|
|
268
272
|
`${this.config.schema}.${this.config.table}`
|
|
269
273
|
]);
|
|
@@ -306,6 +310,11 @@ var PostgresStore = class {
|
|
|
306
310
|
ON ${this._fqt} (stream COLLATE pg_catalog."default", id)
|
|
307
311
|
WHERE name = '${import_act.SNAP_EVENT}';`
|
|
308
312
|
);
|
|
313
|
+
await client.query(
|
|
314
|
+
`CREATE INDEX IF NOT EXISTS "${this.config.table}_stream_id_ix"
|
|
315
|
+
ON ${this._fqt} (stream COLLATE pg_catalog."default", id)
|
|
316
|
+
WHERE name <> '${import_act.SNAP_EVENT}';`
|
|
317
|
+
);
|
|
309
318
|
await client.query(
|
|
310
319
|
`CREATE TABLE IF NOT EXISTS ${this._fqs} (
|
|
311
320
|
stream text COLLATE pg_catalog."default" PRIMARY KEY,
|
|
@@ -598,6 +607,13 @@ var PostgresStore = class {
|
|
|
598
607
|
expectedVersion ?? -1
|
|
599
608
|
);
|
|
600
609
|
}
|
|
610
|
+
if (PG_NUL_BYTE.has(error?.code ?? "")) {
|
|
611
|
+
throw new import_act.ValidationError(
|
|
612
|
+
stream,
|
|
613
|
+
msgs,
|
|
614
|
+
`Postgres cannot store a NUL byte (\\u0000). One of the ${msgs.length} event(s) committed to "${stream}" (${msgs.map((m) => String(m.name)).join(", ")}) carries one in its name, data, meta or pii. InMemory and SQLite accept it, so this commit would have succeeded there \u2014 strip NUL bytes at the edge where the data enters. Driver: ${error.message}`
|
|
615
|
+
);
|
|
616
|
+
}
|
|
601
617
|
throw error;
|
|
602
618
|
}
|
|
603
619
|
} finally {
|
|
@@ -633,29 +649,66 @@ var PostgresStore = class {
|
|
|
633
649
|
-- and lock EVERY claimable stream for this transaction, starving
|
|
634
650
|
-- overlapping competing consumers. We only lock the small
|
|
635
651
|
-- lagging+leading candidate slice, down in the "locked" CTE.
|
|
636
|
-
|
|
652
|
+
-- Eligibility, split by SOURCE CLASS rather than expressed as one
|
|
653
|
+
-- disjunction (#1448). The three classes are mutually exclusive, so
|
|
654
|
+
-- the UNION ALL returns exactly what the old OR-chain did \u2014 but each
|
|
655
|
+
-- arm now carries a single, planner-legible predicate.
|
|
656
|
+
--
|
|
657
|
+
-- That matters for one arm in particular. Written as
|
|
658
|
+
-- "s.source IS NULL OR (... AND e.stream = s.source) OR (... ~ ...)",
|
|
659
|
+
-- the equality is buried inside a disjunction and is NOT sargable:
|
|
660
|
+
-- no index on (stream, id) can serve it, so every probe degenerated
|
|
661
|
+
-- into a forward scan of the pk from the stream's watermark. With
|
|
662
|
+
-- the classes separated, the literal arm \u2014 every per-aggregate
|
|
663
|
+
-- .to(e => ({target: e.stream})) reaction, i.e. the overwhelming
|
|
664
|
+
-- majority \u2014 becomes an index seek on (stream, id).
|
|
665
|
+
--
|
|
666
|
+
-- Measured at 10k subscribed streams with 10 pending: 5,792 ms
|
|
667
|
+
-- before, 10.3 ms after. See libs/act-pg/PERFORMANCE.md for #1448.
|
|
668
|
+
eligible AS (
|
|
637
669
|
SELECT stream, source, at, priority, lane
|
|
638
670
|
FROM ${this._fqs} s
|
|
639
671
|
WHERE blocked = false
|
|
640
672
|
${lane_clause}
|
|
641
673
|
AND (leased_by IS NULL OR leased_until <= NOW())
|
|
642
674
|
AND (deferred_at IS NULL OR deferred_at <= NOW())
|
|
643
|
-
|
|
675
|
+
),
|
|
676
|
+
available AS (
|
|
677
|
+
-- Never processed: claimable without probing the log at all.
|
|
678
|
+
SELECT stream, source, at, priority, lane FROM eligible WHERE at < 0
|
|
679
|
+
UNION ALL
|
|
680
|
+
-- Source-less subscription: any non-snapshot event past the
|
|
681
|
+
-- watermark counts, so the id index alone answers it.
|
|
682
|
+
SELECT stream, source, at, priority, lane FROM eligible s
|
|
683
|
+
WHERE s.at >= 0 AND s.source IS NULL
|
|
684
|
+
AND EXISTS (
|
|
644
685
|
SELECT 1 FROM ${this._fqt} e
|
|
645
|
-
WHERE e.id > s.at
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
686
|
+
WHERE e.id > s.at AND e.name <> '${import_act.SNAP_EVENT}' LIMIT 1
|
|
687
|
+
)
|
|
688
|
+
UNION ALL
|
|
689
|
+
-- Literal source (no regex metacharacter): exact equality, so
|
|
690
|
+
-- "s1" never claims "s12", and (stream, id) is usable.
|
|
691
|
+
SELECT stream, source, at, priority, lane FROM eligible s
|
|
692
|
+
WHERE s.at >= 0 AND s.source IS NOT NULL
|
|
693
|
+
AND s.source !~ '${SOURCE_METACHARACTER_CLASS}'
|
|
694
|
+
AND EXISTS (
|
|
695
|
+
SELECT 1 FROM ${this._fqt} e
|
|
696
|
+
WHERE e.stream = s.source AND e.id > s.at
|
|
697
|
+
AND e.name <> '${import_act.SNAP_EVENT}' LIMIT 1
|
|
698
|
+
)
|
|
699
|
+
UNION ALL
|
|
700
|
+
-- Pattern source (e.g. '^(A|B)$'): POSIX match, so the
|
|
701
|
+
-- calculator's static regex reaction is claimed for every stream
|
|
702
|
+
-- it anchors. Unavoidably a scan \u2014 but only for the subscriptions
|
|
703
|
+
-- that actually declare a pattern.
|
|
704
|
+
SELECT stream, source, at, priority, lane FROM eligible s
|
|
705
|
+
WHERE s.at >= 0 AND s.source IS NOT NULL
|
|
706
|
+
AND s.source ~ '${SOURCE_METACHARACTER_CLASS}'
|
|
707
|
+
AND EXISTS (
|
|
708
|
+
SELECT 1 FROM ${this._fqt} e
|
|
709
|
+
WHERE e.id > s.at AND e.name <> '${import_act.SNAP_EVENT}'
|
|
710
|
+
AND e.stream ~ s.source LIMIT 1
|
|
711
|
+
)
|
|
659
712
|
),
|
|
660
713
|
-- Priority lanes (ACT-102): higher priority first, then
|
|
661
714
|
-- lagging-watermark order. With everyone at priority=0 the
|