@rocicorp/zero 1.11.0-canary.40 → 1.11.0-canary.41

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.
Files changed (28) hide show
  1. package/out/zero/package.js +1 -1
  2. package/out/zero/package.js.map +1 -1
  3. package/out/zero-cache/src/config/normalize.d.ts.map +1 -1
  4. package/out/zero-cache/src/config/normalize.js +1 -0
  5. package/out/zero-cache/src/config/normalize.js.map +1 -1
  6. package/out/zero-cache/src/config/zero-config.d.ts +2 -1
  7. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  8. package/out/zero-cache/src/config/zero-config.js +6 -9
  9. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  10. package/out/zero-cache/src/services/change-source/pg/backfill-stream.d.ts.map +1 -1
  11. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +6 -1
  12. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js.map +1 -1
  13. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts.map +1 -1
  14. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +5 -5
  15. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  16. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  17. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +20 -12
  18. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  19. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts +21 -1
  20. package/out/zero-cache/src/services/change-streamer/schema/tables.d.ts.map +1 -1
  21. package/out/zero-cache/src/services/change-streamer/schema/tables.js +73 -38
  22. package/out/zero-cache/src/services/change-streamer/schema/tables.js.map +1 -1
  23. package/out/zero-cache/src/services/change-streamer/storer.d.ts +13 -3
  24. package/out/zero-cache/src/services/change-streamer/storer.d.ts.map +1 -1
  25. package/out/zero-cache/src/services/change-streamer/storer.js +7 -4
  26. package/out/zero-cache/src/services/change-streamer/storer.js.map +1 -1
  27. package/out/zero-client/src/client/version.js +1 -1
  28. package/package.json +1 -1
@@ -77,8 +77,58 @@ async function markResetRequired(sql, shard) {
77
77
  UPDATE ${sql(cdcSchema(shard))}."replicationConfig"
78
78
  SET "resetRequired" = true`;
79
79
  }
80
- async function ensureReplicationConfig(lc, db, subscriptionState, shard, autoReset, purgeLock, setTimeoutFn = setTimeout, pgChangeLogEnabled = true) {
81
- const { publications, replicaVersion, watermark } = subscriptionState;
80
+ /**
81
+ * Validates the `replicationConfig` of the change db against the replica,
82
+ * throwing an {@link AutoResetSignal} if the replica cannot be served from it.
83
+ *
84
+ * @param behindSlot Whether the change log is behind the replication slot,
85
+ * in which case it is re-initialized from the replica (see
86
+ * {@link reinitializeChangeLog}) and can thus be reset for a replica
87
+ * at any watermark.
88
+ * @returns `'init'` if the tables are uninitialized, `'reset'` if they are
89
+ * for a different replica, or `'compatible'`.
90
+ */
91
+ function validateReplicationConfig(lc, current, { publications, replicaVersion, watermark }, autoReset, behindSlot) {
92
+ if (!current) return "init";
93
+ if (current.replicaVersion !== replicaVersion || !equals(new Set(current.publications), new Set(publications))) {
94
+ if (replicaVersion !== watermark && !behindSlot) throw new AutoResetSignal(`Cannot reset change db@${current.replicaVersion} to service replica@${replicaVersion} from watermark ${watermark}`);
95
+ return "reset";
96
+ }
97
+ if (current.resetRequired) {
98
+ if (autoReset) throw new AutoResetSignal("reset required by replication stream");
99
+ lc.error?.("\n\n\nReset required but --auto-reset is not enabled.\nThis can happen for upstream databases that do not support event triggers.\nTo correct this, see https://zero.rocicorp.dev/docs/connecting-to-postgres#schema-changes\n\n\n");
100
+ }
101
+ return "compatible";
102
+ }
103
+ function selectReplicationConfig(sql, shard) {
104
+ return sql`
105
+ SELECT "replicaVersion", "publications", "resetRequired"
106
+ FROM ${sql(cdcSchema(shard))}."replicationConfig"`;
107
+ }
108
+ /**
109
+ * Checks, without modifying the change db, that the replica can be served
110
+ * by the change log once {@link ensureReplicationConfig} is called upon
111
+ * taking over the change log. Throws an {@link AutoResetSignal} otherwise.
112
+ *
113
+ * This allows a replication-manager to detect an incompatible replica at
114
+ * startup while leaving the change log to its current owner until then.
115
+ */
116
+ async function checkReplicationConfig(lc, db, subscriptionState, shard, autoReset, behindSlot = false) {
117
+ const [current] = await runTx(db, (sql) => selectReplicationConfig(sql, shard));
118
+ if (validateReplicationConfig(lc, current, subscriptionState, autoReset, behindSlot) === "reset") lc.info?.(`Data in cdc tables @${current?.replicaVersion} is incompatible with replica @${subscriptionState.replicaVersion}. Tables will be reset when taking over the change log.`);
119
+ }
120
+ /**
121
+ * Initializes the cdc tables for the replica, resetting them if they were
122
+ * written for a different replica (version or publications). This disrupts
123
+ * a replication-manager that is serving the change log, and is thus only
124
+ * done when taking over the change log (see `Storer.assumeOwnershipAtHead`).
125
+ *
126
+ * @param purgeLock A purge lock held by the caller, if any, which is released
127
+ * before the tables are reset.
128
+ * @param behindSlot See {@link checkReplicationConfig}.
129
+ */
130
+ async function ensureReplicationConfig(lc, db, subscriptionState, shard, autoReset, purgeLock, setTimeoutFn = setTimeout, behindSlot = false) {
131
+ const { publications, replicaVersion } = subscriptionState;
82
132
  const replicaConfig = {
83
133
  publications,
84
134
  replicaVersion
@@ -90,43 +140,28 @@ async function ensureReplicationConfig(lc, db, subscriptionState, shard, autoRes
90
140
  };
91
141
  const schema = cdcSchema(shard);
92
142
  await runTx(db, async (sql) => {
143
+ const [current] = await selectReplicationConfig(sql, shard);
144
+ const status = validateReplicationConfig(lc, current, subscriptionState, autoReset, behindSlot);
145
+ if (status === "compatible") return [];
93
146
  const stmts = [];
94
- let needsTruncate = false;
95
- const results = await sql`
96
- SELECT "replicaVersion", "publications", "resetRequired"
97
- FROM ${sql(schema)}."replicationConfig"`;
98
- if (results.length) {
99
- const { replicaVersion, publications } = results[0];
100
- if (replicaVersion !== replicaConfig.replicaVersion || !equals(new Set(publications), new Set(replicaConfig.publications))) {
101
- if (replicaConfig.replicaVersion !== watermark) throw new AutoResetSignal(`Cannot reset change db@${replicaVersion} to service replica@${replicaConfig.replicaVersion} from watermark ${watermark}`);
102
- lc.info?.(`Data in cdc tables @${replicaVersion} is incompatible with replica @${replicaConfig.replicaVersion}. Clearing tables.`);
103
- purgeLock?.release();
104
- needsTruncate = true;
105
- stmts.push(sql`TRUNCATE TABLE ${sql(schema)}."replicationState"`, ...pgChangeLogEnabled ? [sql`TRUNCATE TABLE ${sql(schema)}."changeLog"`] : [], sql`TRUNCATE TABLE ${sql(schema)}."replicationConfig"`, sql`TRUNCATE TABLE ${sql(schema)}."tableMetadata"`, sql`TRUNCATE TABLE ${sql(schema)}."backfilling"`);
106
- }
147
+ if (status === "reset") {
148
+ lc.info?.(`Data in cdc tables @${current?.replicaVersion} is incompatible with replica @${replicaVersion}. Clearing tables.`);
149
+ purgeLock?.release();
150
+ stmts.push(sql`TRUNCATE TABLE ${sql(schema)}."replicationState"`, sql`TRUNCATE TABLE ${sql(schema)}."changeLog"`, sql`TRUNCATE TABLE ${sql(schema)}."replicationConfig"`, sql`TRUNCATE TABLE ${sql(schema)}."tableMetadata"`, sql`TRUNCATE TABLE ${sql(schema)}."backfilling"`);
107
151
  }
108
- if (results.length === 0 || needsTruncate) {
109
- const watermark = replicaConfig.replicaVersion;
110
- const initialTx = pgChangeLogEnabled ? [{
111
- watermark,
112
- pos: 0,
113
- change: { tag: "begin" }
114
- }, {
115
- watermark,
116
- pos: 1,
117
- change: { tag: "commit" }
118
- }] : [];
119
- stmts.push(sql`INSERT INTO ${sql(schema)}."replicationConfig" ${sql(replicaConfig)}`, sql`INSERT INTO ${sql(schema)}."replicationState" ${sql(replicationState)}
120
- ON CONFLICT (lock) DO UPDATE SET ${sql(replicationState)}`, ...initialTx.map((change) => sql`INSERT INTO ${sql(schema)}."changeLog" ${sql(change)}`));
121
- if (needsTruncate) return runStatementsAndTerminateLockHolders(lc, db, shard, "ensureReplicationConfig", stmts, setTimeoutFn);
122
- return Promise.all(stmts);
123
- }
124
- const { resetRequired } = results[0];
125
- if (resetRequired) {
126
- if (autoReset) throw new AutoResetSignal("reset required by replication stream");
127
- lc.error?.("\n\n\nReset required but --auto-reset is not enabled.\nThis can happen for upstream databases that do not support event triggers.\nTo correct this, see https://zero.rocicorp.dev/docs/connecting-to-postgres#schema-changes\n\n\n");
128
- }
129
- return [];
152
+ const initialTx = [{
153
+ watermark: replicaVersion,
154
+ pos: 0,
155
+ change: { tag: "begin" }
156
+ }, {
157
+ watermark: replicaVersion,
158
+ pos: 1,
159
+ change: { tag: "commit" }
160
+ }];
161
+ stmts.push(sql`INSERT INTO ${sql(schema)}."replicationConfig" ${sql(replicaConfig)}`, sql`INSERT INTO ${sql(schema)}."replicationState" ${sql(replicationState)}
162
+ ON CONFLICT (lock) DO UPDATE SET ${sql(replicationState)}`, ...initialTx.map((change) => sql`INSERT INTO ${sql(schema)}."changeLog" ${sql(change)}`));
163
+ if (status === "reset") return runStatementsAndTerminateLockHolders(lc, db, shard, "ensureReplicationConfig", stmts, setTimeoutFn);
164
+ return Promise.all(stmts);
130
165
  });
131
166
  }
132
167
  var LOCK_HOLDER_TERMINATE_TIMEOUT_MS = 5e3;
@@ -278,6 +313,6 @@ async function terminateChangeDBLockHolders(lc, db, shard) {
278
313
  else for (const { pid, applicationName, query, terminated: ok } of terminated) lc.info?.(`terminated blocking backend pid=${pid} app=${applicationName} ok=${ok} query=${query.slice(0, 200)}`);
279
314
  }
280
315
  //#endregion
281
- export { AutoResetSignal, createBackfillTables, createReplicationStateTable, discoverChangeStreamerAddress, ensureReplicationConfig, markResetRequired, reinitializeChangeLog, setupCDCTables };
316
+ export { AutoResetSignal, checkReplicationConfig, createBackfillTables, createReplicationStateTable, discoverChangeStreamerAddress, ensureReplicationConfig, markResetRequired, reinitializeChangeLog, setupCDCTables };
282
317
 
283
318
  //# sourceMappingURL=tables.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"tables.js","names":[],"sources":["../../../../../../../zero-cache/src/services/change-streamer/schema/tables.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {ident} from 'pg-format';\nimport type postgres from 'postgres';\nimport {type PendingQuery, type Row} from 'postgres';\nimport {AbortError} from '../../../../../shared/src/abort-error.ts';\nimport {equals} from '../../../../../shared/src/set-utils.ts';\nimport {runTx} from '../../../db/run-transaction.ts';\nimport {type PostgresDB} from '../../../types/pg.ts';\nimport {cdcSchema, type ShardID} from '../../../types/shards.ts';\nimport type {\n BackfillID,\n Change,\n TableMetadata,\n} from '../../change-source/protocol/current/data.ts';\nimport type {SubscriptionState} from '../../replicator/schema/replication-state.ts';\n\n// For readability in the sql statements.\nfunction schema(shard: ShardID) {\n return ident(cdcSchema(shard));\n}\n\nexport const PG_SCHEMA = 'cdc';\n\nfunction createSchema(shard: ShardID) {\n return /*sql*/ `CREATE SCHEMA IF NOT EXISTS ${schema(shard)};`;\n}\n\nexport type ChangeLogEntry = {\n // A strictly monotonically increasing, lexicographically sortable\n // value that uniquely identifies a position in the change stream.\n watermark: string;\n change: Change;\n};\n\ntype FullChangeLogEntry = ChangeLogEntry & {pos: number};\n\nfunction createChangeLogTable(shard: ShardID) {\n // Note: The \"change\" column used to be JSONB, but that was problematic in that\n // it does not handle the NULL unicode character.\n // https://vladimir.varank.in/notes/2021/01/you-dont-insert-unicode-null-character-as-postgres-jsonb/\n return /*sql*/ `\n CREATE TABLE ${schema(shard)}.\"changeLog\" (\n watermark TEXT,\n pos INT8,\n change JSON NOT NULL,\n precommit TEXT, -- Only exists on commit entries. Purely for debugging.\n PRIMARY KEY (watermark, pos)\n );\n`;\n}\n\n/**\n * Tracks the watermark from which to resume the change stream and the\n * current owner (task ID) acting as the single writer to the changeLog.\n */\nexport type ReplicationState = {\n lastWatermark: string;\n owner: string | null;\n ownerAddress: string | null;\n};\n\nexport function createReplicationStateTable(shard: ShardID) {\n return /*sql*/ `\n CREATE TABLE ${schema(shard)}.\"replicationState\" (\n \"lastWatermark\" TEXT NOT NULL,\n \"owner\" TEXT,\n \"ownerAddress\" TEXT,\n \"lock\" INTEGER PRIMARY KEY DEFAULT 1 CHECK (lock=1)\n );\n`;\n}\n\nexport async function discoverChangeStreamerAddress(\n shard: ShardID,\n sql: PostgresDB,\n): Promise<string | null> {\n const result = await sql<{ownerAddress: string | null}[]> /*sql*/ `\n SELECT \"ownerAddress\" FROM ${sql(cdcSchema(shard))}.\"replicationState\"`;\n return result[0].ownerAddress;\n}\n\n/**\n * This mirrors the analogously named table in the SQLite replica\n * (`services/replicator/schema/replication-state.ts`), and is used\n * to detect when the replica has been reset and is no longer compatible\n * with the current ChangeLog.\n */\nexport type ReplicationConfig = {\n replicaVersion: string;\n publications: readonly string[];\n};\n\nfunction createReplicationConfigTable(shard: ShardID) {\n return /*sql*/ `\n CREATE TABLE ${schema(shard)}.\"replicationConfig\" (\n \"replicaVersion\" TEXT NOT NULL,\n \"publications\" TEXT[] NOT NULL,\n \"resetRequired\" BOOL,\n \"lock\" INTEGER PRIMARY KEY DEFAULT 1 CHECK (lock=1)\n );\n`;\n}\n\nexport function createBackfillTables(shard: ShardID) {\n return /*sql*/ `\n CREATE TABLE ${schema(shard)}.\"tableMetadata\" (\n \"schema\" TEXT NOT NULL,\n \"table\" TEXT NOT NULL,\n \"metadata\" JSONB NOT NULL,\n PRIMARY KEY(\"schema\", \"table\")\n );\n\n CREATE TABLE ${schema(shard)}.\"backfilling\" (\n \"schema\" TEXT NOT NULL,\n \"table\" TEXT NOT NULL,\n \"column\" TEXT NOT NULL,\n \"backfill\" JSONB NOT NULL,\n PRIMARY KEY(\"schema\", \"table\", \"column\")\n );\n `;\n}\n\nexport type TableMetadataRow = {\n schema: string;\n table: string;\n metadata: TableMetadata;\n};\n\nexport type BackfillingColumn = {\n schema: string;\n table: string;\n column: string;\n backfill: BackfillID;\n};\n\nfunction createTables(shard: ShardID) {\n return (\n createSchema(shard) +\n createChangeLogTable(shard) +\n createReplicationStateTable(shard) +\n createReplicationConfigTable(shard) +\n createBackfillTables(shard)\n );\n}\n\ninterface PurgeLock {\n release(): Promise<void>;\n}\n\nexport async function setupCDCTables(\n lc: LogContext,\n db: postgres.TransactionSql,\n shard: ShardID,\n) {\n lc.info?.(`Setting up CDC tables`);\n await db.unsafe(createTables(shard));\n}\n\nexport async function markResetRequired(sql: PostgresDB, shard: ShardID) {\n const schema = cdcSchema(shard);\n await sql`\n UPDATE ${sql(schema)}.\"replicationConfig\"\n SET \"resetRequired\" = true`;\n}\n\nexport async function ensureReplicationConfig(\n lc: LogContext,\n db: PostgresDB,\n subscriptionState: Pick<\n SubscriptionState,\n 'publications' | 'replicaVersion' | 'watermark'\n >,\n shard: ShardID,\n autoReset: boolean,\n purgeLock?: PurgeLock,\n setTimeoutFn: typeof setTimeout = setTimeout,\n pgChangeLogEnabled = true,\n) {\n const {publications, replicaVersion, watermark} = subscriptionState;\n const replicaConfig = {publications, replicaVersion};\n const replicationState: ReplicationState = {\n lastWatermark: replicaVersion,\n owner: null,\n ownerAddress: null,\n };\n const schema = cdcSchema(shard);\n\n await runTx(db, async sql => {\n const stmts: PendingQuery<Row[]>[] = [];\n let needsTruncate = false;\n const results = await sql<\n {\n replicaVersion: string;\n publications: string[];\n resetRequired: boolean | null;\n }[]\n > /*sql*/ `\n SELECT \"replicaVersion\", \"publications\", \"resetRequired\" \n FROM ${sql(schema)}.\"replicationConfig\"`;\n\n if (results.length) {\n const {replicaVersion, publications} = results[0];\n if (\n replicaVersion !== replicaConfig.replicaVersion ||\n !equals(new Set(publications), new Set(replicaConfig.publications))\n ) {\n if (replicaConfig.replicaVersion !== watermark) {\n throw new AutoResetSignal(\n `Cannot reset change db@${replicaVersion} to ` +\n `service replica@${replicaConfig.replicaVersion} ` +\n `from watermark ${watermark}`,\n );\n }\n lc.info?.(\n `Data in cdc tables @${replicaVersion} is incompatible ` +\n `with replica @${replicaConfig.replicaVersion}. Clearing tables.`,\n );\n // Release any purge lock held by the caller; the changeLog is\n // incompatible with the replica and needs to be truncated.\n void purgeLock?.release();\n\n // Note: The order of TRUNCATE matters. The replicationState table is\n // truncated before the changeLog table, in order to acquire\n // (exclusive) locks on the tables in the same order that the\n // storer.ts acquires locks when writing the changeLog, namely:\n //\n // 1. SELECT ... FROM replicationState FOR UPDATE;\n // 2. INSERT INTO changeLog ...;\n // ...\n // n. UPDATE replicationState ...;\n needsTruncate = true;\n stmts.push(\n sql`TRUNCATE TABLE ${sql(schema)}.\"replicationState\"`,\n ...(pgChangeLogEnabled\n ? [sql`TRUNCATE TABLE ${sql(schema)}.\"changeLog\"`]\n : []),\n sql`TRUNCATE TABLE ${sql(schema)}.\"replicationConfig\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"tableMetadata\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"backfilling\"`,\n );\n }\n }\n // Initialize (or re-initialize TRUNCATED) tables\n if (results.length === 0 || needsTruncate) {\n // When enabled, the PG storer uses the earliest changeLog entry as the\n // safe watermark from which subscribers can be resumed. These initial\n // entries ensure that subscribers can start from a freshly synced\n // replica, even if new changes have been replicated and not purged.\n //\n // TODO: Replace this with an explicit `firstWatermark` column in the\n // change db.\n const watermark = replicaConfig.replicaVersion;\n const initialTx: FullChangeLogEntry[] = pgChangeLogEnabled\n ? [\n {watermark, pos: 0, change: {tag: 'begin'}},\n {watermark, pos: 1, change: {tag: 'commit'}},\n ]\n : [];\n\n stmts.push(\n sql`INSERT INTO ${sql(schema)}.\"replicationConfig\" ${sql(replicaConfig)}`,\n sql`INSERT INTO ${sql(schema)}.\"replicationState\" ${sql(replicationState)} \n ON CONFLICT (lock) DO UPDATE SET ${sql(replicationState)}`,\n ...initialTx.map(\n change => sql`INSERT INTO ${sql(schema)}.\"changeLog\" ${sql(change)}`,\n ),\n );\n\n if (needsTruncate) {\n return runStatementsAndTerminateLockHolders(\n lc,\n db,\n shard,\n 'ensureReplicationConfig',\n stmts,\n setTimeoutFn,\n );\n }\n return Promise.all(stmts);\n }\n\n const {resetRequired} = results[0];\n if (resetRequired) {\n if (autoReset) {\n throw new AutoResetSignal('reset required by replication stream');\n }\n lc.error?.(\n '\\n\\n\\n' +\n 'Reset required but --auto-reset is not enabled.\\n' +\n 'This can happen for upstream databases that do not support event triggers.\\n' +\n 'To correct this, see https://zero.rocicorp.dev/docs/connecting-to-postgres#schema-changes' +\n '\\n\\n\\n',\n );\n }\n\n return [];\n });\n}\n\n// The time to wait for a TRUNCATE in ensureReplicationConfig before\n// terminating blocking backends via terminateChangeDBLockHolders.\nconst LOCK_HOLDER_TERMINATE_TIMEOUT_MS = 5_000;\n\n/**\n * Executes `stmts` that require ACCESS EXCLUSIVE locks (e.g. `TRUNCATE`),\n * which may be blocked by old storer catchup reads. Races against a\n * timeout that terminates the blocking backends if the `stmts` take too\n * long. The check is repeated until the statements complete, since the\n * `stmts` may not yet be waiting on a lock when the timer fires.\n */\nasync function runStatementsAndTerminateLockHolders(\n lc: LogContext,\n db: PostgresDB,\n shard: ShardID,\n name: string,\n stmts: PendingQuery<Row[]>[],\n setTimeoutFn: typeof setTimeout,\n) {\n let done = false;\n let timer: ReturnType<typeof setTimeout> | undefined;\n const scheduleTerminate = () => {\n timer = setTimeoutFn(async () => {\n lc.info?.(`${name} blocked, terminating lock holders`);\n try {\n await terminateChangeDBLockHolders(lc, db, shard);\n } catch (e) {\n lc.warn?.('error terminating lock holders', e);\n }\n if (!done) {\n scheduleTerminate();\n }\n }, LOCK_HOLDER_TERMINATE_TIMEOUT_MS);\n };\n scheduleTerminate();\n try {\n return await Promise.all(stmts);\n } finally {\n done = true;\n clearTimeout(timer);\n }\n}\n\nexport type ChangeLogReinitialization = {\n /**\n * The watermark of the replica from which the change log is re-initialized,\n * i.e. the watermark from which the change stream will resume.\n */\n watermark: string;\n cookies: {\n tableMetadata: readonly {\n schema: string;\n table: string;\n metadata: TableMetadata;\n }[];\n backfilling: readonly {\n schema: string;\n table: string;\n column: string;\n backfill: BackfillID;\n }[];\n };\n owner: string;\n ownerAddress: string;\n};\n\n/**\n * Re-initializes the change log from the state of the replica if it does not\n * contain all of the replica's changes, i.e. if its `lastWatermark` is before\n * the replica's watermark. This is the case if the change log is no longer\n * maintained (e.g. after running in high-availability mode), or if its last\n * owner stopped before persisting changes that it had already forwarded\n * (and backed up). Ownership of the change log is taken in the same\n * transaction, in which:\n *\n * * The `replicationState`, `changeLog`, `tableMetadata`, and `backfilling`\n * tables are truncated. (The `replicationConfig` is kept;\n * {@link ensureReplicationConfig} handles changes of the replica version\n * or publications.)\n * * The change log is seeded with an empty transaction at the replica's\n * watermark, so that subscribers at that watermark can be caught up, while\n * subscribers at earlier watermarks are rejected as being too old.\n * * The `tableMetadata` and `backfilling` cookies are set from the replica.\n *\n * Returns `'maintained'` without modifying anything if the change log's\n * `lastWatermark` is at or past the replica's watermark, i.e. the change log\n * contains all of the replica's changes. Because a replication slot is only\n * acked past a transaction once it has been backed up, this means that the\n * change log contains every transaction up to the position of the replica's\n * replication slot, and it can be taken over (and resumed from its head).\n *\n * This is called when the change log's head is behind the position of the\n * replication slot (see `PgChangeLogPurgeLocker`). That position cannot be\n * compared with the watermarks of transactions to determine whether the\n * change log is maintained, as it is also advanced past positions of changes\n * outside of the publication.\n *\n * @param purgeLock A purge lock held by the caller, if any, which is released\n * before the tables are truncated.\n */\nexport function reinitializeChangeLog(\n lc: LogContext,\n db: PostgresDB,\n shard: ShardID,\n {watermark, cookies, owner, ownerAddress}: ChangeLogReinitialization,\n purgeLock?: PurgeLock,\n setTimeoutFn: typeof setTimeout = setTimeout,\n): Promise<'reinitialized' | 'maintained'> {\n const schema = cdcSchema(shard);\n return runTx(db, async sql => {\n // Lock the replicationState first, which is the order in which the\n // storer acquires locks when writing the changeLog.\n const state = await sql<{lastWatermark: string}[]>`\n SELECT \"lastWatermark\" FROM ${sql(schema)}.\"replicationState\" FOR UPDATE`;\n const lastWatermark = state[0]?.lastWatermark;\n if (lastWatermark !== undefined && lastWatermark >= watermark) {\n lc.info?.(\n `changeLog@${lastWatermark} contains the replica's changes (@${watermark})`,\n );\n return 'maintained';\n }\n lc.info?.(\n `re-initializing changeLog@${lastWatermark} from the replica @${watermark}`,\n );\n const replicationState: ReplicationState = {\n lastWatermark: watermark,\n owner,\n ownerAddress,\n };\n const initialTx: FullChangeLogEntry[] = [\n {watermark, pos: 0, change: {tag: 'begin'}},\n {watermark, pos: 1, change: {tag: 'commit'}},\n ];\n // The caller's purge lock would block the TRUNCATE. Releasing it is safe:\n // a concurrent purge checks the ownership after deleting, which blocks\n // on the replicationState lock held by this transaction.\n await purgeLock?.release();\n await runStatementsAndTerminateLockHolders(\n lc,\n db,\n shard,\n 'reinitializeChangeLog',\n [\n sql`TRUNCATE TABLE ${sql(schema)}.\"replicationState\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"changeLog\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"tableMetadata\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"backfilling\"`,\n ],\n setTimeoutFn,\n );\n await sql`INSERT INTO ${sql(schema)}.\"replicationState\" ${sql(replicationState)}`;\n for (const entry of initialTx) {\n await sql`INSERT INTO ${sql(schema)}.\"changeLog\" ${sql(entry)}`;\n }\n for (const row of cookies.tableMetadata) {\n await sql`INSERT INTO ${sql(schema)}.\"tableMetadata\" ${sql({...row})}`;\n }\n for (const row of cookies.backfilling) {\n await sql`INSERT INTO ${sql(schema)}.\"backfilling\" ${sql({...row})}`;\n }\n return 'reinitialized';\n });\n}\n\nexport const CHANGE_STREAMER_APP_NAME = 'zero-change-streamer';\n\nexport class AutoResetSignal extends AbortError {\n readonly name = 'AutoResetSignal';\n}\n\n/**\n * Terminates zero-cache backends that are blocking the current backend\n * from acquiring locks on CDC tables (e.g., during TRUNCATE).\n *\n * This is used during change-DB takeover when the new replication-manager's\n * `ensureReplicationConfig` needs to TRUNCATE tables, but the old\n * replication-manager's storer is still reading from them (e.g., large\n * catchup cursors).\n *\n * The function:\n * 1. Finds backends waiting for a lock on a TRUNCATE in {schema}\n * 2. Uses `pg_blocking_pids()` to identify which backends are blocking them\n * 3. Terminates blocking backends that have `application_name = 'zero-change-streamer'`\n *\n * Must be called on a **separate connection** from the one that is blocked,\n * since the blocked connection is inside a pending transaction.\n */\nexport async function terminateChangeDBLockHolders(\n lc: LogContext,\n db: PostgresDB,\n shard: ShardID,\n) {\n const schema = cdcSchema(shard);\n\n // Step 1: Find backends that are blocked waiting for a lock,\n // whose query involves a TRUNCATE on this shard's CDC schema.\n const blocked = await db<{pid: number}[]>`\n SELECT pid FROM pg_stat_activity\n WHERE wait_event_type = 'Lock'\n AND application_name = ${CHANGE_STREAMER_APP_NAME}\n AND query LIKE ${'%TRUNCATE%' + schema + '%'}`;\n\n if (blocked.length === 0) {\n lc.info?.('no blocked TRUNCATE backends found');\n return;\n }\n\n const blockedPids = blocked.map(r => r.pid);\n lc.info?.(`found blocked TRUNCATE backends: ${JSON.stringify(blockedPids)}`);\n\n // Step 2: For each blocked backend, find and terminate its blockers\n // that are zero-change-streamer connections.\n const terminated = await db<\n {pid: number; applicationName: string; query: string; terminated: boolean}[]\n >`\n SELECT pid, application_name as \"applicationName\", query,\n pg_terminate_backend(pid) as terminated\n FROM pg_stat_activity\n WHERE pid = ANY(\n SELECT unnest(pg_blocking_pids(blocked.pid))\n FROM unnest(${blockedPids}::int[]) AS blocked(pid)\n )\n AND application_name = ${CHANGE_STREAMER_APP_NAME}\n AND pid != ALL(${blockedPids}::int[])`;\n\n if (terminated.length === 0) {\n lc.info?.(`no ${CHANGE_STREAMER_APP_NAME} blockers found to terminate`);\n } else {\n for (const {pid, applicationName, query, terminated: ok} of terminated) {\n lc.info?.(\n `terminated blocking backend pid=${pid} app=${applicationName} ok=${ok} query=${query.slice(0, 200)}`,\n );\n }\n }\n}\n"],"mappings":";;;;;;;;AAiBA,SAAS,OAAO,OAAgB;CAC9B,OAAO,MAAM,UAAU,KAAK,CAAC;AAC/B;AAIA,SAAS,aAAa,OAAgB;CACpC,OAAe,+BAA+B,OAAO,KAAK,EAAE;AAC9D;AAWA,SAAS,qBAAqB,OAAgB;CAI5C,OAAe;iBACA,OAAO,KAAK,EAAE;;;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[],"sources":["../../../../../../../zero-cache/src/services/change-streamer/schema/tables.ts"],"sourcesContent":["import type {LogContext} from '@rocicorp/logger';\nimport {ident} from 'pg-format';\nimport type postgres from 'postgres';\nimport {type PendingQuery, type Row} from 'postgres';\nimport {AbortError} from '../../../../../shared/src/abort-error.ts';\nimport {equals} from '../../../../../shared/src/set-utils.ts';\nimport {runTx} from '../../../db/run-transaction.ts';\nimport {type PostgresDB, type PostgresTransaction} from '../../../types/pg.ts';\nimport {cdcSchema, type ShardID} from '../../../types/shards.ts';\nimport type {\n BackfillID,\n Change,\n TableMetadata,\n} from '../../change-source/protocol/current/data.ts';\nimport type {SubscriptionState} from '../../replicator/schema/replication-state.ts';\n\n// For readability in the sql statements.\nfunction schema(shard: ShardID) {\n return ident(cdcSchema(shard));\n}\n\nexport const PG_SCHEMA = 'cdc';\n\nfunction createSchema(shard: ShardID) {\n return /*sql*/ `CREATE SCHEMA IF NOT EXISTS ${schema(shard)};`;\n}\n\nexport type ChangeLogEntry = {\n // A strictly monotonically increasing, lexicographically sortable\n // value that uniquely identifies a position in the change stream.\n watermark: string;\n change: Change;\n};\n\ntype FullChangeLogEntry = ChangeLogEntry & {pos: number};\n\nfunction createChangeLogTable(shard: ShardID) {\n // Note: The \"change\" column used to be JSONB, but that was problematic in that\n // it does not handle the NULL unicode character.\n // https://vladimir.varank.in/notes/2021/01/you-dont-insert-unicode-null-character-as-postgres-jsonb/\n return /*sql*/ `\n CREATE TABLE ${schema(shard)}.\"changeLog\" (\n watermark TEXT,\n pos INT8,\n change JSON NOT NULL,\n precommit TEXT, -- Only exists on commit entries. Purely for debugging.\n PRIMARY KEY (watermark, pos)\n );\n`;\n}\n\n/**\n * Tracks the watermark from which to resume the change stream and the\n * current owner (task ID) acting as the single writer to the changeLog.\n */\nexport type ReplicationState = {\n lastWatermark: string;\n owner: string | null;\n ownerAddress: string | null;\n};\n\nexport function createReplicationStateTable(shard: ShardID) {\n return /*sql*/ `\n CREATE TABLE ${schema(shard)}.\"replicationState\" (\n \"lastWatermark\" TEXT NOT NULL,\n \"owner\" TEXT,\n \"ownerAddress\" TEXT,\n \"lock\" INTEGER PRIMARY KEY DEFAULT 1 CHECK (lock=1)\n );\n`;\n}\n\nexport async function discoverChangeStreamerAddress(\n shard: ShardID,\n sql: PostgresDB,\n): Promise<string | null> {\n const result = await sql<{ownerAddress: string | null}[]> /*sql*/ `\n SELECT \"ownerAddress\" FROM ${sql(cdcSchema(shard))}.\"replicationState\"`;\n return result[0].ownerAddress;\n}\n\n/**\n * This mirrors the analogously named table in the SQLite replica\n * (`services/replicator/schema/replication-state.ts`), and is used\n * to detect when the replica has been reset and is no longer compatible\n * with the current ChangeLog.\n */\nexport type ReplicationConfig = {\n replicaVersion: string;\n publications: readonly string[];\n};\n\nfunction createReplicationConfigTable(shard: ShardID) {\n return /*sql*/ `\n CREATE TABLE ${schema(shard)}.\"replicationConfig\" (\n \"replicaVersion\" TEXT NOT NULL,\n \"publications\" TEXT[] NOT NULL,\n \"resetRequired\" BOOL,\n \"lock\" INTEGER PRIMARY KEY DEFAULT 1 CHECK (lock=1)\n );\n`;\n}\n\nexport function createBackfillTables(shard: ShardID) {\n return /*sql*/ `\n CREATE TABLE ${schema(shard)}.\"tableMetadata\" (\n \"schema\" TEXT NOT NULL,\n \"table\" TEXT NOT NULL,\n \"metadata\" JSONB NOT NULL,\n PRIMARY KEY(\"schema\", \"table\")\n );\n\n CREATE TABLE ${schema(shard)}.\"backfilling\" (\n \"schema\" TEXT NOT NULL,\n \"table\" TEXT NOT NULL,\n \"column\" TEXT NOT NULL,\n \"backfill\" JSONB NOT NULL,\n PRIMARY KEY(\"schema\", \"table\", \"column\")\n );\n `;\n}\n\nexport type TableMetadataRow = {\n schema: string;\n table: string;\n metadata: TableMetadata;\n};\n\nexport type BackfillingColumn = {\n schema: string;\n table: string;\n column: string;\n backfill: BackfillID;\n};\n\nfunction createTables(shard: ShardID) {\n return (\n createSchema(shard) +\n createChangeLogTable(shard) +\n createReplicationStateTable(shard) +\n createReplicationConfigTable(shard) +\n createBackfillTables(shard)\n );\n}\n\ninterface PurgeLock {\n release(): Promise<void>;\n}\n\nexport async function setupCDCTables(\n lc: LogContext,\n db: postgres.TransactionSql,\n shard: ShardID,\n) {\n lc.info?.(`Setting up CDC tables`);\n await db.unsafe(createTables(shard));\n}\n\nexport async function markResetRequired(sql: PostgresDB, shard: ShardID) {\n const schema = cdcSchema(shard);\n await sql`\n UPDATE ${sql(schema)}.\"replicationConfig\"\n SET \"resetRequired\" = true`;\n}\n\ntype ReplicationConfigRow = ReplicationConfig & {\n resetRequired: boolean | null;\n};\n\nexport type ReplicaSubscriptionState = Pick<\n SubscriptionState,\n 'publications' | 'replicaVersion' | 'watermark'\n>;\n\n/**\n * Validates the `replicationConfig` of the change db against the replica,\n * throwing an {@link AutoResetSignal} if the replica cannot be served from it.\n *\n * @param behindSlot Whether the change log is behind the replication slot,\n * in which case it is re-initialized from the replica (see\n * {@link reinitializeChangeLog}) and can thus be reset for a replica\n * at any watermark.\n * @returns `'init'` if the tables are uninitialized, `'reset'` if they are\n * for a different replica, or `'compatible'`.\n */\nfunction validateReplicationConfig(\n lc: LogContext,\n current: ReplicationConfigRow | undefined,\n {publications, replicaVersion, watermark}: ReplicaSubscriptionState,\n autoReset: boolean,\n behindSlot: boolean,\n): 'init' | 'reset' | 'compatible' {\n if (!current) {\n return 'init';\n }\n if (\n current.replicaVersion !== replicaVersion ||\n !equals(new Set(current.publications), new Set(publications))\n ) {\n if (replicaVersion !== watermark && !behindSlot) {\n throw new AutoResetSignal(\n `Cannot reset change db@${current.replicaVersion} to ` +\n `service replica@${replicaVersion} from watermark ${watermark}`,\n );\n }\n return 'reset';\n }\n if (current.resetRequired) {\n if (autoReset) {\n throw new AutoResetSignal('reset required by replication stream');\n }\n lc.error?.(\n '\\n\\n\\n' +\n 'Reset required but --auto-reset is not enabled.\\n' +\n 'This can happen for upstream databases that do not support event triggers.\\n' +\n 'To correct this, see https://zero.rocicorp.dev/docs/connecting-to-postgres#schema-changes' +\n '\\n\\n\\n',\n );\n }\n return 'compatible';\n}\n\nfunction selectReplicationConfig(sql: PostgresTransaction, shard: ShardID) {\n return sql<ReplicationConfigRow[]> /*sql*/ `\n SELECT \"replicaVersion\", \"publications\", \"resetRequired\" \n FROM ${sql(cdcSchema(shard))}.\"replicationConfig\"`;\n}\n\n/**\n * Checks, without modifying the change db, that the replica can be served\n * by the change log once {@link ensureReplicationConfig} is called upon\n * taking over the change log. Throws an {@link AutoResetSignal} otherwise.\n *\n * This allows a replication-manager to detect an incompatible replica at\n * startup while leaving the change log to its current owner until then.\n */\nexport async function checkReplicationConfig(\n lc: LogContext,\n db: PostgresDB,\n subscriptionState: ReplicaSubscriptionState,\n shard: ShardID,\n autoReset: boolean,\n behindSlot = false,\n): Promise<void> {\n const [current] = await runTx(db, sql => selectReplicationConfig(sql, shard));\n const status = validateReplicationConfig(\n lc,\n current,\n subscriptionState,\n autoReset,\n behindSlot,\n );\n if (status === 'reset') {\n lc.info?.(\n `Data in cdc tables @${current?.replicaVersion} is incompatible ` +\n `with replica @${subscriptionState.replicaVersion}. ` +\n `Tables will be reset when taking over the change log.`,\n );\n }\n}\n\n/**\n * Initializes the cdc tables for the replica, resetting them if they were\n * written for a different replica (version or publications). This disrupts\n * a replication-manager that is serving the change log, and is thus only\n * done when taking over the change log (see `Storer.assumeOwnershipAtHead`).\n *\n * @param purgeLock A purge lock held by the caller, if any, which is released\n * before the tables are reset.\n * @param behindSlot See {@link checkReplicationConfig}.\n */\nexport async function ensureReplicationConfig(\n lc: LogContext,\n db: PostgresDB,\n subscriptionState: ReplicaSubscriptionState,\n shard: ShardID,\n autoReset: boolean,\n purgeLock?: PurgeLock,\n setTimeoutFn: typeof setTimeout = setTimeout,\n behindSlot = false,\n) {\n const {publications, replicaVersion} = subscriptionState;\n const replicaConfig = {publications, replicaVersion};\n const replicationState: ReplicationState = {\n lastWatermark: replicaVersion,\n owner: null,\n ownerAddress: null,\n };\n const schema = cdcSchema(shard);\n\n await runTx(db, async sql => {\n const [current] = await selectReplicationConfig(sql, shard);\n const status = validateReplicationConfig(\n lc,\n current,\n subscriptionState,\n autoReset,\n behindSlot,\n );\n if (status === 'compatible') {\n return [];\n }\n const stmts: PendingQuery<Row[]>[] = [];\n if (status === 'reset') {\n lc.info?.(\n `Data in cdc tables @${current?.replicaVersion} is incompatible ` +\n `with replica @${replicaVersion}. Clearing tables.`,\n );\n // Release any purge lock held by the caller; the changeLog is\n // incompatible with the replica and needs to be truncated.\n void purgeLock?.release();\n\n // Note: The order of TRUNCATE matters. The replicationState table is\n // truncated before the changeLog table, in order to acquire\n // (exclusive) locks on the tables in the same order that the\n // storer.ts acquires locks when writing the changeLog, namely:\n //\n // 1. SELECT ... FROM replicationState FOR UPDATE;\n // 2. INSERT INTO changeLog ...;\n // ...\n // n. UPDATE replicationState ...;\n stmts.push(\n sql`TRUNCATE TABLE ${sql(schema)}.\"replicationState\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"changeLog\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"replicationConfig\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"tableMetadata\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"backfilling\"`,\n );\n }\n // Initialize (or re-initialize TRUNCATED) tables.\n //\n // The PG storer uses the earliest changeLog entry as the safe watermark\n // from which subscribers can be resumed. These initial entries ensure\n // that subscribers can start from a freshly synced replica, even if new\n // changes have been replicated and not purged.\n //\n // TODO: Replace this with an explicit `firstWatermark` column in the\n // change db.\n const initialTx: FullChangeLogEntry[] = [\n {watermark: replicaVersion, pos: 0, change: {tag: 'begin'}},\n {watermark: replicaVersion, pos: 1, change: {tag: 'commit'}},\n ];\n stmts.push(\n sql`INSERT INTO ${sql(schema)}.\"replicationConfig\" ${sql(replicaConfig)}`,\n sql`INSERT INTO ${sql(schema)}.\"replicationState\" ${sql(replicationState)} \n ON CONFLICT (lock) DO UPDATE SET ${sql(replicationState)}`,\n ...initialTx.map(\n change => sql`INSERT INTO ${sql(schema)}.\"changeLog\" ${sql(change)}`,\n ),\n );\n\n if (status === 'reset') {\n return runStatementsAndTerminateLockHolders(\n lc,\n db,\n shard,\n 'ensureReplicationConfig',\n stmts,\n setTimeoutFn,\n );\n }\n return Promise.all(stmts);\n });\n}\n\n// The time to wait for a TRUNCATE in ensureReplicationConfig before\n// terminating blocking backends via terminateChangeDBLockHolders.\nconst LOCK_HOLDER_TERMINATE_TIMEOUT_MS = 5_000;\n\n/**\n * Executes `stmts` that require ACCESS EXCLUSIVE locks (e.g. `TRUNCATE`),\n * which may be blocked by old storer catchup reads. Races against a\n * timeout that terminates the blocking backends if the `stmts` take too\n * long. The check is repeated until the statements complete, since the\n * `stmts` may not yet be waiting on a lock when the timer fires.\n */\nasync function runStatementsAndTerminateLockHolders(\n lc: LogContext,\n db: PostgresDB,\n shard: ShardID,\n name: string,\n stmts: PendingQuery<Row[]>[],\n setTimeoutFn: typeof setTimeout,\n) {\n let done = false;\n let timer: ReturnType<typeof setTimeout> | undefined;\n const scheduleTerminate = () => {\n timer = setTimeoutFn(async () => {\n lc.info?.(`${name} blocked, terminating lock holders`);\n try {\n await terminateChangeDBLockHolders(lc, db, shard);\n } catch (e) {\n lc.warn?.('error terminating lock holders', e);\n }\n if (!done) {\n scheduleTerminate();\n }\n }, LOCK_HOLDER_TERMINATE_TIMEOUT_MS);\n };\n scheduleTerminate();\n try {\n return await Promise.all(stmts);\n } finally {\n done = true;\n clearTimeout(timer);\n }\n}\n\nexport type ChangeLogReinitialization = {\n /**\n * The watermark of the replica from which the change log is re-initialized,\n * i.e. the watermark from which the change stream will resume.\n */\n watermark: string;\n cookies: {\n tableMetadata: readonly {\n schema: string;\n table: string;\n metadata: TableMetadata;\n }[];\n backfilling: readonly {\n schema: string;\n table: string;\n column: string;\n backfill: BackfillID;\n }[];\n };\n owner: string;\n ownerAddress: string;\n};\n\n/**\n * Re-initializes the change log from the state of the replica if it does not\n * contain all of the replica's changes, i.e. if its `lastWatermark` is before\n * the replica's watermark. This is the case if the change log is no longer\n * maintained (e.g. after running in high-availability mode), or if its last\n * owner stopped before persisting changes that it had already forwarded\n * (and backed up). Ownership of the change log is taken in the same\n * transaction, in which:\n *\n * * The `replicationState`, `changeLog`, `tableMetadata`, and `backfilling`\n * tables are truncated. (The `replicationConfig` is kept;\n * {@link ensureReplicationConfig} handles changes of the replica version\n * or publications.)\n * * The change log is seeded with an empty transaction at the replica's\n * watermark, so that subscribers at that watermark can be caught up, while\n * subscribers at earlier watermarks are rejected as being too old.\n * * The `tableMetadata` and `backfilling` cookies are set from the replica.\n *\n * Returns `'maintained'` without modifying anything if the change log's\n * `lastWatermark` is at or past the replica's watermark, i.e. the change log\n * contains all of the replica's changes. Because a replication slot is only\n * acked past a transaction once it has been backed up, this means that the\n * change log contains every transaction up to the position of the replica's\n * replication slot, and it can be taken over (and resumed from its head).\n *\n * This is called when the change log's head is behind the position of the\n * replication slot (see `PgChangeLogPurgeLocker`). That position cannot be\n * compared with the watermarks of transactions to determine whether the\n * change log is maintained, as it is also advanced past positions of changes\n * outside of the publication.\n *\n * @param purgeLock A purge lock held by the caller, if any, which is released\n * before the tables are truncated.\n */\nexport function reinitializeChangeLog(\n lc: LogContext,\n db: PostgresDB,\n shard: ShardID,\n {watermark, cookies, owner, ownerAddress}: ChangeLogReinitialization,\n purgeLock?: PurgeLock,\n setTimeoutFn: typeof setTimeout = setTimeout,\n): Promise<'reinitialized' | 'maintained'> {\n const schema = cdcSchema(shard);\n return runTx(db, async sql => {\n // Lock the replicationState first, which is the order in which the\n // storer acquires locks when writing the changeLog.\n const state = await sql<{lastWatermark: string}[]>`\n SELECT \"lastWatermark\" FROM ${sql(schema)}.\"replicationState\" FOR UPDATE`;\n const lastWatermark = state[0]?.lastWatermark;\n if (lastWatermark !== undefined && lastWatermark >= watermark) {\n lc.info?.(\n `changeLog@${lastWatermark} contains the replica's changes (@${watermark})`,\n );\n return 'maintained';\n }\n lc.info?.(\n `re-initializing changeLog@${lastWatermark} from the replica @${watermark}`,\n );\n const replicationState: ReplicationState = {\n lastWatermark: watermark,\n owner,\n ownerAddress,\n };\n const initialTx: FullChangeLogEntry[] = [\n {watermark, pos: 0, change: {tag: 'begin'}},\n {watermark, pos: 1, change: {tag: 'commit'}},\n ];\n // The caller's purge lock would block the TRUNCATE. Releasing it is safe:\n // a concurrent purge checks the ownership after deleting, which blocks\n // on the replicationState lock held by this transaction.\n await purgeLock?.release();\n await runStatementsAndTerminateLockHolders(\n lc,\n db,\n shard,\n 'reinitializeChangeLog',\n [\n sql`TRUNCATE TABLE ${sql(schema)}.\"replicationState\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"changeLog\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"tableMetadata\"`,\n sql`TRUNCATE TABLE ${sql(schema)}.\"backfilling\"`,\n ],\n setTimeoutFn,\n );\n await sql`INSERT INTO ${sql(schema)}.\"replicationState\" ${sql(replicationState)}`;\n for (const entry of initialTx) {\n await sql`INSERT INTO ${sql(schema)}.\"changeLog\" ${sql(entry)}`;\n }\n for (const row of cookies.tableMetadata) {\n await sql`INSERT INTO ${sql(schema)}.\"tableMetadata\" ${sql({...row})}`;\n }\n for (const row of cookies.backfilling) {\n await sql`INSERT INTO ${sql(schema)}.\"backfilling\" ${sql({...row})}`;\n }\n return 'reinitialized';\n });\n}\n\nexport const CHANGE_STREAMER_APP_NAME = 'zero-change-streamer';\n\nexport class AutoResetSignal extends AbortError {\n readonly name = 'AutoResetSignal';\n}\n\n/**\n * Terminates zero-cache backends that are blocking the current backend\n * from acquiring locks on CDC tables (e.g., during TRUNCATE).\n *\n * This is used during change-DB takeover when the new replication-manager's\n * `ensureReplicationConfig` needs to TRUNCATE tables, but the old\n * replication-manager's storer is still reading from them (e.g., large\n * catchup cursors).\n *\n * The function:\n * 1. Finds backends waiting for a lock on a TRUNCATE in {schema}\n * 2. Uses `pg_blocking_pids()` to identify which backends are blocking them\n * 3. Terminates blocking backends that have `application_name = 'zero-change-streamer'`\n *\n * Must be called on a **separate connection** from the one that is blocked,\n * since the blocked connection is inside a pending transaction.\n */\nexport async function terminateChangeDBLockHolders(\n lc: LogContext,\n db: PostgresDB,\n shard: ShardID,\n) {\n const schema = cdcSchema(shard);\n\n // Step 1: Find backends that are blocked waiting for a lock,\n // whose query involves a TRUNCATE on this shard's CDC schema.\n const blocked = await db<{pid: number}[]>`\n SELECT pid FROM pg_stat_activity\n WHERE wait_event_type = 'Lock'\n AND application_name = ${CHANGE_STREAMER_APP_NAME}\n AND query LIKE ${'%TRUNCATE%' + schema + '%'}`;\n\n if (blocked.length === 0) {\n lc.info?.('no blocked TRUNCATE backends found');\n return;\n }\n\n const blockedPids = blocked.map(r => r.pid);\n lc.info?.(`found blocked TRUNCATE backends: ${JSON.stringify(blockedPids)}`);\n\n // Step 2: For each blocked backend, find and terminate its blockers\n // that are zero-change-streamer connections.\n const terminated = await db<\n {pid: number; applicationName: string; query: string; terminated: boolean}[]\n >`\n SELECT pid, application_name as \"applicationName\", query,\n pg_terminate_backend(pid) as terminated\n FROM pg_stat_activity\n WHERE pid = ANY(\n SELECT unnest(pg_blocking_pids(blocked.pid))\n FROM unnest(${blockedPids}::int[]) AS blocked(pid)\n )\n AND application_name = ${CHANGE_STREAMER_APP_NAME}\n AND pid != ALL(${blockedPids}::int[])`;\n\n if (terminated.length === 0) {\n lc.info?.(`no ${CHANGE_STREAMER_APP_NAME} blockers found to terminate`);\n } else {\n for (const {pid, applicationName, query, terminated: ok} of terminated) {\n lc.info?.(\n `terminated blocking backend pid=${pid} app=${applicationName} ok=${ok} query=${query.slice(0, 200)}`,\n );\n }\n }\n}\n"],"mappings":";;;;;;;;AAiBA,SAAS,OAAO,OAAgB;CAC9B,OAAO,MAAM,UAAU,KAAK,CAAC;AAC/B;AAIA,SAAS,aAAa,OAAgB;CACpC,OAAe,+BAA+B,OAAO,KAAK,EAAE;AAC9D;AAWA,SAAS,qBAAqB,OAAgB;CAI5C,OAAe;iBACA,OAA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CAC1C,GAAG,OAAO,oCAAoC,KAAK,UAAU,WAAW,GAAG;CAI3E,MAAM,aAAa,MAAM,EAExB;;;;;;wBAMqB,YAAY;;+BAEL,yBAAyB;uBACjC,YAAY;CAEjC,IAAI,WAAW,WAAW,GACxB,GAAG,OAAO,MAAM,yBAAyB,6BAA6B;MAEtE,KAAK,MAAM,EAAC,KAAK,iBAAiB,OAAO,YAAY,QAAO,YAC1D,GAAG,OACD,mCAAmC,IAAI,OAAO,gBAAgB,MAAM,GAAG,SAAS,MAAM,MAAM,GAAG,GAAG,GACpG;AAGN"}
@@ -10,7 +10,7 @@ import { type CookieSet } from '../replicator/change-log-cookies.ts';
10
10
  import type { ReplicatorMode } from '../replicator/replicator.ts';
11
11
  import type { Service } from '../service.ts';
12
12
  import { type ChangeLogEntry, type SerializedChangeStreamData } from './change-log-codec.ts';
13
- import { type ChangeLogReinitialization } from './schema/tables.ts';
13
+ import { type ReplicaSubscriptionState, type ChangeLogReinitialization } from './schema/tables.ts';
14
14
  import type { Subscriber } from './subscriber.ts';
15
15
  /**
16
16
  * Factory for creating dynamic connection pools used by the Storer to
@@ -29,6 +29,16 @@ export type OwnershipTakeover = {
29
29
  * which is used to start pending backfills once ownership is assumed.
30
30
  */
31
31
  requestRestart: (reason: string) => void;
32
+ /**
33
+ * Whether to throw an AutoResetSignal if the change db requires a reset.
34
+ */
35
+ autoReset: boolean;
36
+ /**
37
+ * Whether the change log was behind the replication slot, in which case it
38
+ * is re-initialized from the replica (see `reinitializeFromReplica()`), and
39
+ * can thus be reset for a replica at any watermark.
40
+ */
41
+ pgChangeLogBehindSlot: boolean;
32
42
  };
33
43
  export type TuningOptions = {
34
44
  backPressureLimitHeapProportion: number;
@@ -69,7 +79,7 @@ export type TuningOptions = {
69
79
  export declare class Storer implements Service {
70
80
  #private;
71
81
  readonly id = "storer";
72
- constructor(lc: LogContext, shard: ShardID, taskID: string, discoveryAddress: string, discoveryProtocol: string, dbProvider: PostgresDBProvider, replicaVersion: string, onCommitted: (c: Commit | UpstreamStatusMessage) => void, onFatal: (err: Error) => void, { backPressureLimitHeapProportion, statementTimeoutMs, changeLogBatchSize, drainTimeoutMs, }: TuningOptions);
82
+ constructor(lc: LogContext, shard: ShardID, taskID: string, discoveryAddress: string, discoveryProtocol: string, dbProvider: PostgresDBProvider, subscriptionState: ReplicaSubscriptionState, onCommitted: (c: Commit | UpstreamStatusMessage) => void, onFatal: (err: Error) => void, { backPressureLimitHeapProportion, statementTimeoutMs, changeLogBatchSize, drainTimeoutMs, }: TuningOptions);
73
83
  /**
74
84
  * Re-initializes a change log that is no longer maintained from the
75
85
  * replica's state, taking ownership of it in the process (which is then
@@ -109,7 +119,7 @@ export declare class Storer implements Service {
109
119
  * assumed, a restart of the change stream is requested if the change log has
110
120
  * pending backfills or if a backfill transaction was skipped.
111
121
  */
112
- assumeOwnershipAtHead(takeover: OwnershipTakeover): void;
122
+ assumeOwnershipAtHead(takeover: OwnershipTakeover): Promise<void>;
113
123
  /**
114
124
  * Whether ownership of the change log is pending, i.e. after
115
125
  * {@link assumeOwnershipAtHead} and before ownership is assumed.
@@ -1 +1 @@
1
- {"version":3,"file":"storer.d.ts","sourceRoot":"","sources":["../../../../../../zero-cache/src/services/change-streamer/storer.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAKjD,OAAO,EAEL,KAAK,UAAU,EAChB,MAAM,uCAAuC,CAAC;AAO/C,OAAO,EAAiB,eAAe,EAAC,MAAM,8BAA8B,CAAC;AAC7E,OAAO,EAAC,KAAK,UAAU,EAA2B,MAAM,mBAAmB,CAAC;AAC5E,OAAO,EAAY,KAAK,OAAO,EAAC,MAAM,uBAAuB,CAAC;AAE9D,OAAO,EAKL,KAAK,eAAe,EAMrB,MAAM,sCAAsC,CAAC;AAC9C,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,MAAM,EACZ,MAAM,iDAAiD,CAAC;AACzD,OAAO,KAAK,EAAC,qB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runTx } from "../../db/run-transaction.js";
17
17
  import { Queue } from "../../../../shared/src/queue.js";
18
- import { AutoResetSignal, markResetRequired, reinitializeChangeLog } from "./schema/tables.js";
18
+ import { AutoResetSignal, ensureReplicationConfig, markResetRequired, reinitializeChangeLog } from "./schema/tables.js";
19
19
  import { TransactionPool, sharedSnapshot } from "../../db/transaction-pool.js";
20
20
  import { reconstructWatermarkedChange, serializeChangeStreamDataWithChange } from "./change-log-codec.js";
21
21
  import { resolver } from "@rocicorp/resolver";
@@ -64,6 +64,7 @@ var Storer = class {
64
64
  #db;
65
65
  #inserter;
66
66
  #purger;
67
+ #subscriptionState;
67
68
  #replicaVersion;
68
69
  #onCommitted;
69
70
  #onFatal;
@@ -80,7 +81,7 @@ var Storer = class {
80
81
  #progressMonitor;
81
82
  #approximateQueuedBytes = 0;
82
83
  #running = false;
83
- constructor(lc, shard, taskID, discoveryAddress, discoveryProtocol, dbProvider, replicaVersion, onCommitted, onFatal, { backPressureLimitHeapProportion, statementTimeoutMs, changeLogBatchSize, drainTimeoutMs = 3e4 }) {
84
+ constructor(lc, shard, taskID, discoveryAddress, discoveryProtocol, dbProvider, subscriptionState, onCommitted, onFatal, { backPressureLimitHeapProportion, statementTimeoutMs, changeLogBatchSize, drainTimeoutMs = 3e4 }) {
84
85
  this.#lc = lc.withContext("component", "change-log");
85
86
  this.#shard = shard;
86
87
  this.#taskID = taskID;
@@ -90,7 +91,8 @@ var Storer = class {
90
91
  this.#db = dbProvider("change-stream-init", 3);
91
92
  this.#inserter = dbProvider("change-log-inserter", 1);
92
93
  this.#purger = dbProvider("change-log-purger", 1);
93
- this.#replicaVersion = replicaVersion;
94
+ this.#subscriptionState = subscriptionState;
95
+ this.#replicaVersion = subscriptionState.replicaVersion;
94
96
  this.#onCommitted = onCommitted;
95
97
  this.#onFatal = onFatal;
96
98
  this.#statementTimeoutMs = statementTimeoutMs;
@@ -176,7 +178,8 @@ var Storer = class {
176
178
  * assumed, a restart of the change stream is requested if the change log has
177
179
  * pending backfills or if a backfill transaction was skipped.
178
180
  */
179
- assumeOwnershipAtHead(takeover) {
181
+ async assumeOwnershipAtHead(takeover) {
182
+ await ensureReplicationConfig(this.#lc, this.#db, this.#subscriptionState, this.#shard, takeover.autoReset, takeover.purgeLock ?? void 0, void 0, takeover.pgChangeLogBehindSlot);
180
183
  this.#pendingOwnership = {
181
184
  ...takeover,
182
185
  head: "",