@rotorsoft/act-pg 1.13.19 → 1.14.0

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/index.js CHANGED
@@ -84,6 +84,8 @@ var PostgresStore = class {
84
84
  config;
85
85
  _fqt;
86
86
  _fqs;
87
+ /** Correlate checkpoint table (#1484) — one row, id 0. */
88
+ _fqc;
87
89
  /**
88
90
  * Per-instance writer identifier embedded in every NOTIFY payload. The
89
91
  * `notify()` LISTEN handler skips payloads where `by === this._by`,
@@ -170,6 +172,7 @@ var PostgresStore = class {
170
172
  this._pool = new Pool({ ...poolConfig, types: scopedTypes });
171
173
  this._fqt = `"${this.config.schema}"."${this.config.table}"`;
172
174
  this._fqs = `"${this.config.schema}"."${this.config.table}_streams"`;
175
+ this._fqc = `"${this.config.schema}"."${this.config.table}_correlated"`;
173
176
  this._channel = notify_channel(this.config.schema, this.config.table);
174
177
  if (this.config.notify) {
175
178
  this.notify = this._subscribe_notifications.bind(this);
@@ -306,6 +309,16 @@ var PostgresStore = class {
306
309
  deferred_at timestamptz
307
310
  ) TABLESPACE pg_default;`
308
311
  );
312
+ await client.query(
313
+ `CREATE TABLE IF NOT EXISTS ${this._fqc} (
314
+ id int PRIMARY KEY DEFAULT 0,
315
+ at bigint NOT NULL DEFAULT -1,
316
+ CONSTRAINT ${this.config.table}_correlated_singleton CHECK (id = 0)
317
+ ) TABLESPACE pg_default;`
318
+ );
319
+ await client.query(
320
+ `INSERT INTO ${this._fqc} (id) VALUES (0) ON CONFLICT (id) DO NOTHING;`
321
+ );
309
322
  await client.query(
310
323
  `ALTER TABLE ${this._fqs}
311
324
  ADD COLUMN IF NOT EXISTS priority int NOT NULL DEFAULT 0;`
@@ -391,7 +404,7 @@ var PostgresStore = class {
391
404
  WHERE schema_name = '${this.config.schema}'
392
405
  ) THEN
393
406
  EXECUTE 'DROP TABLE IF EXISTS ${this._fqt}';
394
- EXECUTE 'DROP TABLE IF EXISTS ${this._fqs}';
407
+ EXECUTE 'DROP TABLE IF EXISTS ${this._fqc}, ${this._fqs}';
395
408
  IF '${this.config.schema}' <> 'public' THEN
396
409
  EXECUTE 'DROP SCHEMA "${this.config.schema}" CASCADE';
397
410
  END IF;
@@ -783,7 +796,7 @@ var PostgresStore = class {
783
796
  * @param streams - Streams to register with optional source.
784
797
  * @returns subscribed count and current max watermark.
785
798
  */
786
- async subscribe(streams) {
799
+ async subscribe(streams, correlated_at) {
787
800
  const client = await this._client("subscribe");
788
801
  try {
789
802
  await client.query("BEGIN");
@@ -824,11 +837,21 @@ var PostgresStore = class {
824
837
  [JSON.stringify(streams)]
825
838
  );
826
839
  }
840
+ if (correlated_at !== void 0)
841
+ await client.query(
842
+ `UPDATE ${this._fqc} SET at = GREATEST(at, $1::bigint) WHERE id = 0`,
843
+ [correlated_at]
844
+ );
827
845
  const { rows } = await client.query(
828
- `SELECT COALESCE(MAX(at), -1) AS max FROM ${this._fqs}`
846
+ `SELECT (SELECT COALESCE(MAX(at), -1) FROM ${this._fqs}) AS max,
847
+ (SELECT at FROM ${this._fqc} WHERE id = 0) AS correlated_at`
829
848
  );
830
849
  await client.query("COMMIT");
831
- return { subscribed, watermark: rows[0]?.max ?? -1 };
850
+ return {
851
+ subscribed,
852
+ watermark: rows[0]?.max ?? -1,
853
+ correlated_at: Number(rows[0]?.correlated_at ?? -1)
854
+ };
832
855
  } catch (error) {
833
856
  await client.query("ROLLBACK").catch(() => {
834
857
  });