@rotorsoft/act-pg 1.13.17 → 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 +66 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +68 -17
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -6,7 +6,8 @@ import {
|
|
|
6
6
|
log,
|
|
7
7
|
SNAP_EVENT,
|
|
8
8
|
StoreError,
|
|
9
|
-
TOMBSTONE_EVENT
|
|
9
|
+
TOMBSTONE_EVENT,
|
|
10
|
+
ValidationError
|
|
10
11
|
} from "@rotorsoft/act";
|
|
11
12
|
import {
|
|
12
13
|
decrypt,
|
|
@@ -26,6 +27,7 @@ var scopedTypes = {
|
|
|
26
27
|
};
|
|
27
28
|
var SAFE_IDENTIFIER = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
|
28
29
|
var PG_UNIQUE_VIOLATION = "23505";
|
|
30
|
+
var PG_NUL_BYTE = /* @__PURE__ */ new Set(["22P05", "22021"]);
|
|
29
31
|
var NOTIFY_CHANNEL_PREFIX = "act_commit";
|
|
30
32
|
var NOTIFY_MAX_PAYLOAD_BYTES = 8e3;
|
|
31
33
|
var NOTIFY_RECONNECT_BASE_MS = 250;
|
|
@@ -284,6 +286,11 @@ var PostgresStore = class {
|
|
|
284
286
|
ON ${this._fqt} (stream COLLATE pg_catalog."default", id)
|
|
285
287
|
WHERE name = '${SNAP_EVENT}';`
|
|
286
288
|
);
|
|
289
|
+
await client.query(
|
|
290
|
+
`CREATE INDEX IF NOT EXISTS "${this.config.table}_stream_id_ix"
|
|
291
|
+
ON ${this._fqt} (stream COLLATE pg_catalog."default", id)
|
|
292
|
+
WHERE name <> '${SNAP_EVENT}';`
|
|
293
|
+
);
|
|
287
294
|
await client.query(
|
|
288
295
|
`CREATE TABLE IF NOT EXISTS ${this._fqs} (
|
|
289
296
|
stream text COLLATE pg_catalog."default" PRIMARY KEY,
|
|
@@ -576,6 +583,13 @@ var PostgresStore = class {
|
|
|
576
583
|
expectedVersion ?? -1
|
|
577
584
|
);
|
|
578
585
|
}
|
|
586
|
+
if (PG_NUL_BYTE.has(error?.code ?? "")) {
|
|
587
|
+
throw new ValidationError(
|
|
588
|
+
stream,
|
|
589
|
+
msgs,
|
|
590
|
+
`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}`
|
|
591
|
+
);
|
|
592
|
+
}
|
|
579
593
|
throw error;
|
|
580
594
|
}
|
|
581
595
|
} finally {
|
|
@@ -611,29 +625,66 @@ var PostgresStore = class {
|
|
|
611
625
|
-- and lock EVERY claimable stream for this transaction, starving
|
|
612
626
|
-- overlapping competing consumers. We only lock the small
|
|
613
627
|
-- lagging+leading candidate slice, down in the "locked" CTE.
|
|
614
|
-
|
|
628
|
+
-- Eligibility, split by SOURCE CLASS rather than expressed as one
|
|
629
|
+
-- disjunction (#1448). The three classes are mutually exclusive, so
|
|
630
|
+
-- the UNION ALL returns exactly what the old OR-chain did \u2014 but each
|
|
631
|
+
-- arm now carries a single, planner-legible predicate.
|
|
632
|
+
--
|
|
633
|
+
-- That matters for one arm in particular. Written as
|
|
634
|
+
-- "s.source IS NULL OR (... AND e.stream = s.source) OR (... ~ ...)",
|
|
635
|
+
-- the equality is buried inside a disjunction and is NOT sargable:
|
|
636
|
+
-- no index on (stream, id) can serve it, so every probe degenerated
|
|
637
|
+
-- into a forward scan of the pk from the stream's watermark. With
|
|
638
|
+
-- the classes separated, the literal arm \u2014 every per-aggregate
|
|
639
|
+
-- .to(e => ({target: e.stream})) reaction, i.e. the overwhelming
|
|
640
|
+
-- majority \u2014 becomes an index seek on (stream, id).
|
|
641
|
+
--
|
|
642
|
+
-- Measured at 10k subscribed streams with 10 pending: 5,792 ms
|
|
643
|
+
-- before, 10.3 ms after. See libs/act-pg/PERFORMANCE.md for #1448.
|
|
644
|
+
eligible AS (
|
|
615
645
|
SELECT stream, source, at, priority, lane
|
|
616
646
|
FROM ${this._fqs} s
|
|
617
647
|
WHERE blocked = false
|
|
618
648
|
${lane_clause}
|
|
619
649
|
AND (leased_by IS NULL OR leased_until <= NOW())
|
|
620
650
|
AND (deferred_at IS NULL OR deferred_at <= NOW())
|
|
621
|
-
|
|
651
|
+
),
|
|
652
|
+
available AS (
|
|
653
|
+
-- Never processed: claimable without probing the log at all.
|
|
654
|
+
SELECT stream, source, at, priority, lane FROM eligible WHERE at < 0
|
|
655
|
+
UNION ALL
|
|
656
|
+
-- Source-less subscription: any non-snapshot event past the
|
|
657
|
+
-- watermark counts, so the id index alone answers it.
|
|
658
|
+
SELECT stream, source, at, priority, lane FROM eligible s
|
|
659
|
+
WHERE s.at >= 0 AND s.source IS NULL
|
|
660
|
+
AND EXISTS (
|
|
661
|
+
SELECT 1 FROM ${this._fqt} e
|
|
662
|
+
WHERE e.id > s.at AND e.name <> '${SNAP_EVENT}' LIMIT 1
|
|
663
|
+
)
|
|
664
|
+
UNION ALL
|
|
665
|
+
-- Literal source (no regex metacharacter): exact equality, so
|
|
666
|
+
-- "s1" never claims "s12", and (stream, id) is usable.
|
|
667
|
+
SELECT stream, source, at, priority, lane FROM eligible s
|
|
668
|
+
WHERE s.at >= 0 AND s.source IS NOT NULL
|
|
669
|
+
AND s.source !~ '${SOURCE_METACHARACTER_CLASS}'
|
|
670
|
+
AND EXISTS (
|
|
671
|
+
SELECT 1 FROM ${this._fqt} e
|
|
672
|
+
WHERE e.stream = s.source AND e.id > s.at
|
|
673
|
+
AND e.name <> '${SNAP_EVENT}' LIMIT 1
|
|
674
|
+
)
|
|
675
|
+
UNION ALL
|
|
676
|
+
-- Pattern source (e.g. '^(A|B)$'): POSIX match, so the
|
|
677
|
+
-- calculator's static regex reaction is claimed for every stream
|
|
678
|
+
-- it anchors. Unavoidably a scan \u2014 but only for the subscriptions
|
|
679
|
+
-- that actually declare a pattern.
|
|
680
|
+
SELECT stream, source, at, priority, lane FROM eligible s
|
|
681
|
+
WHERE s.at >= 0 AND s.source IS NOT NULL
|
|
682
|
+
AND s.source ~ '${SOURCE_METACHARACTER_CLASS}'
|
|
683
|
+
AND EXISTS (
|
|
622
684
|
SELECT 1 FROM ${this._fqt} e
|
|
623
|
-
WHERE e.id > s.at
|
|
624
|
-
AND e.
|
|
625
|
-
|
|
626
|
-
-- equality \u2014 index-friendly, and exact so "s1" never
|
|
627
|
-
-- claims "s12". A pattern source (e.g. '^(A|B)$') matches
|
|
628
|
-
-- with the POSIX regex operator so the calculator's static
|
|
629
|
-
-- regex reaction is claimed for every stream it anchors.
|
|
630
|
-
AND (
|
|
631
|
-
s.source IS NULL
|
|
632
|
-
OR (s.source !~ '${SOURCE_METACHARACTER_CLASS}' AND e.stream = s.source)
|
|
633
|
-
OR (s.source ~ '${SOURCE_METACHARACTER_CLASS}' AND e.stream ~ s.source)
|
|
634
|
-
)
|
|
635
|
-
LIMIT 1
|
|
636
|
-
))
|
|
685
|
+
WHERE e.id > s.at AND e.name <> '${SNAP_EVENT}'
|
|
686
|
+
AND e.stream ~ s.source LIMIT 1
|
|
687
|
+
)
|
|
637
688
|
),
|
|
638
689
|
-- Priority lanes (ACT-102): higher priority first, then
|
|
639
690
|
-- lagging-watermark order. With everyone at priority=0 the
|