@rocicorp/zero 1.9.0-canary.3 → 1.9.0-canary.4
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/out/analyze-query/src/analyze-cli.js +2 -2
- package/out/analyze-query/src/analyze-cli.js.map +1 -1
- package/out/zero/package.js +1 -1
- package/out/zero/package.js.map +1 -1
- package/out/zero-cache/src/db/sqlite-corruption.d.ts.map +1 -1
- package/out/zero-cache/src/db/sqlite-corruption.js +2 -2
- package/out/zero-cache/src/db/sqlite-corruption.js.map +1 -1
- package/out/zero-cache/src/services/change-source/change-source.d.ts +2 -0
- package/out/zero-cache/src/services/change-source/change-source.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/custom/change-source.js +2 -2
- package/out/zero-cache/src/services/change-source/custom/change-source.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/change-source.js +3 -2
- package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.d.ts +1 -0
- package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js +15 -3
- package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +3 -3
- package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +23 -2
- package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
- package/out/zero-cache/src/workers/connection.js +2 -2
- package/out/zero-cache/src/workers/connection.js.map +1 -1
- package/out/zero-client/src/client/version.js +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"change-source.js","names":["#lc","#db","#upstreamUri","#shard","#replica","#context","#lagReporter","#textCopy","#stopped","#cleanupTimer","#stopExistingReplicationSlotSubscriber","#startStream","#logCurrentReplicaInfo","#cleanUpOlderReplicasAndSlots","#acks","#expectDownstreamAck","#ackIfDownstreamIsCaughtUp","#waitingForDownstreamAck","#sendAck","#lagIntervalMs","#lagReportRetries","#getPgVersion","#pgVersion","#expectingLagReport","#scheduleMissingReportRetry","#processLagReport","#timer","#scheduleNextReport","#shardPrefix","#shardConfig","#initialSchema","#error","#logError","#makeChanges","#handleDdlMessage","#handleRelation","#replicaIdentityTimer","#lastReplicationEvent","#makeSchemaChanges","#getTableChanges"],"sources":["../../../../../../../zero-cache/src/services/change-source/pg/change-source.ts"],"sourcesContent":["import {\n PG_ADMIN_SHUTDOWN,\n PG_INSUFFICIENT_PRIVILEGE,\n} from '@drdgvhbh/postgres-error-codes';\nimport type {LogContext} from '@rocicorp/logger';\nimport {nanoid} from 'nanoid';\nimport postgres from 'postgres';\nimport {AbortError} from '../../../../../shared/src/abort-error.ts';\nimport {assert} from '../../../../../shared/src/asserts.ts';\nimport {stringify} from '../../../../../shared/src/bigint-json.ts';\nimport {deepEqual} from '../../../../../shared/src/json.ts';\nimport {must} from '../../../../../shared/src/must.ts';\nimport {mapValues} from '../../../../../shared/src/objects.ts';\nimport {\n equals,\n intersection,\n symmetricDifferences,\n} from '../../../../../shared/src/set-utils.ts';\nimport * as v from '../../../../../shared/src/valita.ts';\nimport {Database} from '../../../../../zqlite/src/db.ts';\nimport {\n mapPostgresToLiteColumn,\n UnsupportedColumnDefaultError,\n} from '../../../db/pg-to-lite.ts';\nimport type {\n ColumnSpec,\n PublishedIndexSpec,\n PublishedTableSpec,\n} from '../../../db/specs.ts';\nimport {StatementRunner} from '../../../db/statements.ts';\nimport {getOrCreateCounter} from '../../../observability/metrics.ts';\nimport {type LexiVersion} from '../../../types/lexi-version.ts';\nimport {PG_17} from '../../../types/pg-versions.ts';\nimport {\n connectPgClient,\n isPostgresError,\n pgClient,\n type PostgresDB,\n} from '../../../types/pg.ts';\nimport {\n upstreamSchema,\n type ShardConfig,\n type ShardID,\n} from '../../../types/shards.ts';\nimport {\n majorVersionFromString,\n majorVersionToString,\n} from '../../../types/state-version.ts';\nimport type {Sink} from '../../../types/streams.ts';\nimport {AutoResetSignal} from '../../change-streamer/schema/tables.ts';\nimport {\n getSubscriptionStateAndContext,\n type SubscriptionState,\n type SubscriptionStateAndContext,\n} from '../../replicator/schema/replication-state.ts';\nimport type {ChangeSource, ChangeStream} from '../change-source.ts';\nimport {BackfillManager} from '../common/backfill-manager.ts';\nimport {\n ChangeStreamMultiplexer,\n type Listener,\n} from '../common/change-stream-multiplexer.ts';\nimport {initReplica} from '../common/replica-schema.ts';\nimport type {\n BackfillRequest,\n DownstreamStatusMessage,\n JSONObject,\n} from '../protocol/current.ts';\nimport type {\n ColumnAdd,\n Identifier,\n MessageRelation,\n SchemaChange,\n TableCreate,\n} from '../protocol/current/data.ts';\nimport type {\n ChangeStreamData,\n ChangeStreamMessage,\n Data,\n} from '../protocol/current/downstream.ts';\nimport type {ColumnMetadata, TableMetadata} from './backfill-metadata.ts';\nimport {streamBackfill} from './backfill-stream.ts';\nimport {\n initialSync,\n type InitialSyncOptions,\n type ServerContext,\n} from './initial-sync.ts';\nimport type {\n Message,\n MessageMessage,\n MessageRelation as PostgresRelation,\n} from './logical-replication/pgoutput.types.ts';\nimport {subscribe, type StreamMessage} from './logical-replication/stream.ts';\nimport {fromBigInt, toBigInt, toStateVersionString, type LSN} from './lsn.ts';\nimport {registerReplicationSlotHealthMetrics} from './replication-slot-health.ts';\nimport {dropOldReplicasAndSlots} from './replication-slots.ts';\nimport {replicationEventSchema, type ReplicationEvent} from './schema/ddl.ts';\nimport {updateShardSchema} from './schema/init.ts';\nimport {\n getPublicationInfo,\n type PublishedSchema,\n type PublishedTableWithReplicaIdentity,\n} from './schema/published.ts';\nimport {\n dropShard,\n getInternalShardConfig,\n getReplicaAtVersion,\n internalPublicationPrefix,\n replicaIdentitiesForTablesWithoutPrimaryKeys,\n replicationSlotPrefix,\n type InternalShardConfig,\n type Replica,\n} from './schema/shard.ts';\nimport {validate} from './schema/validation.ts';\n\nconst REPLICA_SLOT_CLEANUP_INTERVAL_MS = 30_000;\n\n/**\n * Initializes a Postgres change source, including the initial sync of the\n * replica, before streaming changes from the corresponding logical replication\n * stream.\n */\nexport async function initializePostgresChangeSource(\n lc: LogContext,\n upstreamURI: string,\n shard: ShardConfig,\n replicaDbFile: string,\n syncOptions: InitialSyncOptions,\n context: ServerContext,\n lagReportIntervalMs = 0,\n): Promise<{subscriptionState: SubscriptionState; changeSource: ChangeSource}> {\n await initReplica(\n lc,\n `replica-${shard.appID}-${shard.shardNum}`,\n replicaDbFile,\n (log, tx) => initialSync(log, shard, tx, upstreamURI, syncOptions, context),\n );\n\n const replica = new Database(lc, replicaDbFile);\n const subscriptionState = getSubscriptionStateAndContext(\n new StatementRunner(replica),\n );\n replica.close();\n\n // Check that upstream is properly setup, and throw an AutoReset to re-run\n // initial sync if not.\n const db = await connectPgClient(lc, upstreamURI, 'change-source-init');\n try {\n const upstreamReplica = await checkAndUpdateUpstream(\n lc,\n db,\n shard,\n subscriptionState,\n );\n\n const changeSource = new PostgresChangeSource(\n lc,\n upstreamURI,\n shard,\n upstreamReplica,\n context,\n lagReportIntervalMs,\n syncOptions.textCopy,\n );\n\n return {subscriptionState, changeSource};\n } finally {\n await db.end();\n }\n}\n\nasync function checkAndUpdateUpstream(\n lc: LogContext,\n sql: PostgresDB,\n shard: ShardConfig,\n {\n replicaVersion,\n publications: subscribed,\n initialSyncContext,\n }: SubscriptionStateAndContext,\n) {\n // Perform any shard schema updates\n await updateShardSchema(lc, sql, shard, replicaVersion);\n\n const upstreamReplica = await getReplicaAtVersion(\n lc,\n sql,\n shard,\n replicaVersion,\n initialSyncContext,\n );\n if (!upstreamReplica) {\n throw new AutoResetSignal(\n `No replication slot for replica at version ${replicaVersion}`,\n );\n }\n\n // Verify that the publications match what is being replicated.\n const requested = shard.publications.toSorted();\n const replicated = upstreamReplica.publications\n .filter(p => !p.startsWith(internalPublicationPrefix(shard)))\n .sort();\n if (!deepEqual(requested, replicated)) {\n lc.warn?.(`Dropping shard to change publications to: [${requested}]`);\n await sql.unsafe(dropShard(shard.appID, shard.shardNum));\n throw new AutoResetSignal(\n `Requested publications [${requested}] do not match configured ` +\n `publications: [${replicated}]`,\n );\n }\n\n // Sanity check: The subscription state on the replica should have the\n // same publications. This should be guaranteed by the equivalence of the\n // replicaVersion, but it doesn't hurt to verify.\n if (!deepEqual(upstreamReplica.publications, subscribed)) {\n throw new AutoResetSignal(\n `Upstream publications [${upstreamReplica.publications}] do not ` +\n `match subscribed publications [${subscribed}]`,\n );\n }\n\n // Verify that the publications exist.\n const exists = await sql`\n SELECT pubname FROM pg_publication WHERE pubname IN ${sql(subscribed)};\n `.values();\n if (exists.length !== subscribed.length) {\n throw new AutoResetSignal(\n `Upstream publications [${exists.flat()}] do not contain ` +\n `all subscribed publications [${subscribed}]`,\n );\n }\n\n const {slot} = upstreamReplica;\n const result = await sql<{restartLSN: LSN | null; walStatus: string | null}[]>\n /*sql*/ `\n SELECT restart_lsn as \"restartLSN\", wal_status as \"walStatus\" FROM pg_replication_slots\n WHERE slot_name = ${slot}`;\n if (result.length === 0) {\n throw new AutoResetSignal(`replication slot ${slot} is missing`);\n }\n const [{restartLSN, walStatus}] = result;\n if (restartLSN === null || walStatus === 'lost') {\n throw new AutoResetSignal(\n `replication slot ${slot} has been invalidated for exceeding the max_slot_wal_keep_size`,\n );\n }\n return upstreamReplica;\n}\n\n// Parameterize this if necessary. In practice starvation may never happen.\nconst MAX_LOW_PRIORITY_DELAY_MS = 1000;\n\ntype ReservationState = {\n lastWatermark?: string;\n};\n\n/**\n * Postgres implementation of a {@link ChangeSource} backed by a logical\n * replication stream.\n */\nclass PostgresChangeSource implements ChangeSource {\n readonly #lc: LogContext;\n readonly #db: PostgresDB;\n readonly #upstreamUri: string;\n readonly #shard: ShardID;\n readonly #replica: Replica;\n readonly #context: ServerContext;\n readonly #lagReporter: LagReporter | null;\n readonly #textCopy: boolean;\n #stopped = false;\n\n constructor(\n lc: LogContext,\n upstreamUri: string,\n shard: ShardID,\n replica: Replica,\n context: ServerContext,\n lagReportIntervalMs: number,\n textCopy?: boolean | undefined,\n ) {\n this.#lc = lc.withContext('component', 'change-source');\n this.#db = pgClient(lc, upstreamUri, 'replication-monitor', {\n max: 1,\n // used occasionally for schema changes, periodically for lag reporting\n ['idle_timeout']: 60,\n });\n this.#upstreamUri = upstreamUri;\n this.#shard = shard;\n this.#replica = replica;\n this.#context = context;\n this.#textCopy = textCopy ?? false;\n this.#lagReporter =\n lagReportIntervalMs > 0\n ? new LagReporter(\n lc.withContext('component', 'lag-reporter'),\n shard,\n this.#db,\n lagReportIntervalMs,\n )\n : null;\n registerReplicationSlotHealthMetrics(\n this.#lc,\n this.#db,\n replica.slot,\n () => this.#stopped,\n );\n }\n\n async stop(): Promise<void> {\n this.#stopped = true;\n this.#lagReporter?.stop();\n clearTimeout(this.#cleanupTimer);\n await this.#db.end();\n }\n\n async startLagReporter(): Promise<{nextSendTimeMs: number} | null> {\n if (this.#lagReporter) {\n try {\n return await this.#lagReporter.initiateLagReport(true);\n } catch (e) {\n if (isPostgresError(e, PG_INSUFFICIENT_PRIVILEGE)) {\n const functionName =\n (this.#lagReporter.pgVersion ?? 0) >= PG_17\n ? 'pg_logical_emit_message(boolean, text, text, boolean)'\n : 'pg_logical_emit_message(boolean, text, text)';\n this.#lc.warn?.(\n `\\n\\nUnable to initiate replication lag reports due to insufficient privileges.` +\n `\\nTo enable replication lag reporting, run:`,\n `\\n\\tGRANT EXECUTE ON FUNCTION ${functionName} TO <your_db_user>;\\n\\n`,\n e,\n );\n } else {\n this.#lc.error?.(\n `Unexpected error while initiating lag reports. Lag reports will be disabled.`,\n e,\n );\n }\n }\n }\n return null;\n }\n\n async startStream(\n clientWatermark: string,\n backfillRequests: BackfillRequest[] = [],\n ): Promise<ChangeStream> {\n await this.#stopExistingReplicationSlotSubscriber();\n const config = await getInternalShardConfig(this.#db, this.#shard);\n const {slot} = this.#replica;\n this.#lc.info?.(`starting replication stream@${slot}`);\n return this.#startStream(slot, clientWatermark, config, backfillRequests);\n }\n\n async #startStream(\n slot: string,\n clientWatermark: string,\n shardConfig: InternalShardConfig,\n backfillRequests: BackfillRequest[],\n ): Promise<ChangeStream> {\n const clientStart = majorVersionFromString(clientWatermark) + 1n;\n const {messages, acks} = await subscribe(\n this.#lc,\n this.#db,\n slot,\n [...shardConfig.publications],\n clientStart,\n );\n const acker = new Acker(acks);\n\n // The ChangeStreamMultiplexer facilitates cooperative streaming from\n // the main replication stream and backfill streams initiated by the\n // BackfillManager.\n const changes = new ChangeStreamMultiplexer(this.#lc, clientWatermark);\n const backfillManager = new BackfillManager(this.#lc, changes, req =>\n streamBackfill(this.#lc, this.#upstreamUri, this.#replica, req, {\n textCopy: this.#textCopy,\n }),\n );\n changes\n .addProducers(messages, backfillManager)\n .addListeners(backfillManager, acker);\n backfillManager.run(clientWatermark, backfillRequests);\n\n const changeMaker = new ChangeMaker(\n this.#shard,\n shardConfig,\n this.#db,\n this.#replica.initialSchema,\n );\n\n /**\n * Determines if the incoming message is transactional, otherwise handling\n * non-transactional messages with a downstream status message.\n */\n const isTransactionalMessage = (\n lsn: bigint,\n msg: StreamMessage[1],\n ): msg is Message => {\n if (\n msg.tag === 'message' &&\n msg.prefix === this.#lagReporter?.messagePrefix\n ) {\n changes.pushStatus(this.#lagReporter.processLagReportMessage(msg));\n return false;\n }\n // Checks if we are passed the LSN of the expected lag report, in which\n // case a new one is initiated.\n const status = this.#lagReporter?.checkCurrentLSN(lsn);\n if (status) {\n changes.pushStatus(status);\n }\n\n if (msg.tag === 'keepalive') {\n changes.pushStatus([\n 'status',\n {ack: msg.shouldRespond},\n {watermark: majorVersionToString(lsn)},\n ]);\n return false;\n }\n return true;\n };\n\n void (async () => {\n try {\n let reservation: ReservationState | null = null;\n let inTransaction = false;\n\n for await (const [lsn, msg] of messages) {\n if (!isTransactionalMessage(lsn, msg)) {\n // If we're not in a transaction but the last reservation was kept\n // because of pending keepalives or lag reports in the queue,\n // release the reservation.\n if (!inTransaction && reservation?.lastWatermark) {\n changes.release(reservation.lastWatermark);\n reservation = null;\n }\n continue;\n }\n\n if (!reservation) {\n const res = changes.reserve('replication');\n typeof res === 'string' || (await res); // awaits should be uncommon\n reservation = {};\n }\n\n let lastChange: ChangeStreamMessage | undefined;\n for (const change of await changeMaker.makeChanges(\n this.#lc.withContext('lsn', fromBigInt(lsn)),\n lsn,\n msg,\n )) {\n await changes.push(change); // Allow the change-streamer to push back.\n lastChange = change;\n }\n\n switch (lastChange?.[0]) {\n case 'begin':\n inTransaction = true;\n break;\n case 'commit':\n inTransaction = false;\n reservation.lastWatermark = lastChange[2].watermark;\n if (\n messages.queued === 0 ||\n changes.waiterDelay() > MAX_LOW_PRIORITY_DELAY_MS\n ) {\n // After each transaction, release the reservation:\n // - if there are no pending upstream messages\n // - or if a low priority request has been waiting for longer\n // than MAX_LOW_PRIORITY_DELAY_MS. This is to prevent\n // (backfill) starvation on very active upstreams.\n changes.release(reservation.lastWatermark);\n reservation = null;\n }\n break;\n }\n }\n } catch (e) {\n // Note: no need to worry about reservations here since downstream\n // is being completely canceled.\n const err = translateError(e);\n if (err instanceof ShutdownSignal) {\n // Log the new state of the replica to surface information about the\n // server that sent the shutdown signal, if any.\n await this.#logCurrentReplicaInfo();\n }\n changes.fail(err);\n }\n })();\n\n this.#lc.info?.(\n `started replication stream@${slot} from ${clientWatermark} (replicaVersion: ${\n this.#replica.version\n })`,\n );\n\n return {\n changes: changes.asSource(),\n acks: {push: status => acker.ack(status[2].watermark)},\n };\n }\n\n async #logCurrentReplicaInfo() {\n try {\n const replica = await getReplicaAtVersion(\n this.#lc,\n this.#db,\n this.#shard,\n this.#replica.version,\n );\n if (replica) {\n this.#lc.info?.(\n `Shutdown signal from replica@${this.#replica.version}: ${stringify(replica.subscriberContext)}`,\n );\n }\n } catch (e) {\n this.#lc.warn?.(`error logging replica info`, e);\n }\n }\n\n /**\n * Stops replication slots associated with this shard, and asynchronously\n * runs a cleanup task that drops older replicas and slots.\n */\n async #stopExistingReplicationSlotSubscriber() {\n const sql = this.#db;\n const {id: replicaID, slot} = this.#replica;\n const replicasTable = `${upstreamSchema(this.#shard)}.replicas`;\n\n const result = await sql`\n SELECT pg_terminate_backend(active_pid) as terminated, active_pid as pid\n FROM pg_replication_slots\n WHERE slot_name = ${slot}\n `;\n if (result.length === 0) {\n const slotExpression = replicationSlotPrefix(this.#shard);\n const replicas = await sql`\n SELECT id, rank, slot, version, \"initialSyncContext\", \"subscriberContext\" \n FROM ${sql(replicasTable)} ORDER BY rank DESC`;\n const slots = await sql`\n SELECT slot_name as slot, active, active_pid as pid\n FROM pg_replication_slots\n WHERE slot_name LIKE ${slotExpression}\n ORDER BY slot_name`;\n this.#lc.warn?.(`slot ${slot} not found while cleaning subscribers`, {\n slots,\n replicas,\n });\n throw new AbortError(\n `replication slot ${slot} is missing. A different ` +\n `replication-manager should now be running on a new ` +\n `replication slot.`,\n );\n }\n this.#lc.info?.(`terminated replication slots: ${JSON.stringify(result)}`);\n await sql`\n UPDATE ${sql(replicasTable)} \n SET \"subscriberContext\" = ${this.#context}\n WHERE id = ${replicaID}`;\n void this.#cleanUpOlderReplicasAndSlots();\n }\n\n #cleanupTimer: NodeJS.Timeout | undefined;\n\n async #cleanUpOlderReplicasAndSlots() {\n clearTimeout(this.#cleanupTimer);\n\n try {\n const result = await dropOldReplicasAndSlots(\n this.#lc,\n this.#db,\n this.#shard,\n this.#replica.rank,\n );\n if (result.draining === 0) {\n this.#lc.info?.(`finished cleaning up replicas and slots`, {result});\n return;\n }\n this.#lc.info?.(`old slots still draining`, {result});\n } catch (e) {\n this.#lc.warn?.(`error dropping replication slots`, e);\n }\n\n this.#cleanupTimer = setTimeout(\n () => this.#cleanUpOlderReplicasAndSlots(),\n REPLICA_SLOT_CLEANUP_INTERVAL_MS,\n );\n }\n}\n\n// Exported for testing.\nexport class Acker implements Listener {\n #acks: Sink<bigint>;\n #waitingForDownstreamAck: string | null = null;\n\n constructor(acks: Sink<bigint>) {\n this.#acks = acks;\n }\n\n onChange(change: ChangeStreamMessage): void {\n switch (change[0]) {\n case 'status':\n const {watermark} = change[2];\n if (change[1].ack) {\n this.#expectDownstreamAck(watermark);\n } else {\n // Keepalives with shouldRespond = false are sent to Listeners,\n // but for efficiency they are not sent downstream to the\n // change-streamer. Ack them here if the change-streamer is caught\n // up. This updates the replication slot's `confirmed_flush_lsn`\n // more quickly (rather than waiting for the periodic shouldRespond),\n // which is useful for monitoring replication slot lag.\n this.#ackIfDownstreamIsCaughtUp(watermark);\n }\n break;\n case 'begin':\n // Mark the commit watermark as being expected so that any intermediate\n // shouldRespond=false watermarks, which will be at the\n // commitWatermark, are *not* acked, as the ack must come from\n // change-streamer after it commits the transaction.\n if (!change[1].skipAck) {\n this.#expectDownstreamAck(change[2].commitWatermark);\n }\n break;\n }\n }\n\n #expectDownstreamAck(watermark: string) {\n this.#waitingForDownstreamAck = watermark;\n }\n\n ack(watermark: LexiVersion) {\n if (\n this.#waitingForDownstreamAck &&\n this.#waitingForDownstreamAck <= watermark\n ) {\n this.#waitingForDownstreamAck = null;\n }\n this.#sendAck(watermark);\n }\n\n #ackIfDownstreamIsCaughtUp(watermark: string) {\n if (this.#waitingForDownstreamAck === null) {\n this.#sendAck(watermark);\n }\n }\n\n #sendAck(watermark: LexiVersion) {\n const lsn = majorVersionFromString(watermark);\n this.#acks.push(lsn);\n }\n}\n\nconst lagReportSchema = v.object({\n id: v.string(),\n sendTimeMs: v.number(),\n commitTimeMs: v.number(),\n});\n\nexport type LagReport = v.Infer<typeof lagReportSchema>;\n\ntype InitiatedLagReport = LagReport & {lsn: bigint};\n\nexport class LagReporter {\n static readonly MESSAGE_SUFFIX = '/lag-report/v1';\n\n readonly #lc: LogContext;\n readonly messagePrefix: string;\n\n // oxlint-disable-next-line no-unused-private-class-members\n readonly #db: PostgresDB;\n readonly #lagIntervalMs: number;\n readonly #lagReportRetries = getOrCreateCounter(\n 'replication',\n 'lag_report_retries',\n {\n description:\n 'Number of replication lag reports retried because the expected ' +\n 'report was not received before the next report interval.',\n unit: '{report}',\n },\n );\n\n #pgVersion: number | undefined;\n #expectingLagReport: InitiatedLagReport | null = null;\n #timer: NodeJS.Timeout | undefined;\n\n constructor(\n lc: LogContext,\n shard: ShardID,\n db: PostgresDB,\n lagIntervalMs: number,\n ) {\n this.#lc = lc;\n this.messagePrefix = `${shard.appID}/${shard.shardNum}${LagReporter.MESSAGE_SUFFIX}`;\n this.#db = db;\n this.#lagIntervalMs = lagIntervalMs;\n }\n\n async #getPgVersion() {\n if (this.#pgVersion === undefined) {\n const [{pgVersion}] = await this.#db<{pgVersion: number}[]> /*sql*/ `\n SELECT current_setting('server_version_num')::int as \"pgVersion\"`;\n this.#pgVersion = pgVersion;\n }\n return this.#pgVersion;\n }\n\n get pgVersion() {\n return this.#pgVersion;\n }\n\n async initiateLagReport(log = false) {\n const pgVersion = this.#pgVersion ?? (await this.#getPgVersion());\n const now = Date.now();\n const id = nanoid();\n\n // lsn is filled in after the db call.\n const lagReport = {id, sendTimeMs: now, commitTimeMs: now, lsn: 0n};\n this.#expectingLagReport = lagReport;\n\n let commitTimeMs: number;\n let lsn: string;\n\n try {\n if (pgVersion >= PG_17) {\n [{commitTimeMs, lsn}] = await this.#db /*sql*/ `\n WITH CTE AS (SELECT extract(epoch from now()) * 1000 AS \"commitTimeMs\")\n SELECT \"commitTimeMs\", pg_logical_emit_message(\n false,\n ${this.messagePrefix},\n json_build_object(\n 'id', ${id}::text,\n 'sendTimeMs', ${now}::int8,\n 'commitTimeMs', \"commitTimeMs\"\n )::text,\n true\n ) as lsn FROM CTE;\n `;\n } else {\n // Versions before PG 17 do not support the final `flush` option of\n // pg_logical_emit_message(). This results in an extra 50~100ms latency\n // for replication reports when the db is idle, which is still\n // acceptable for the purpose for alerting on pathological lag, for\n // which the threshold is much higher (e.g. many seconds).\n [{commitTimeMs, lsn}] = await this.#db /*sql*/ `\n WITH CTE AS (SELECT extract(epoch from now()) * 1000 as \"commitTimeMs\")\n SELECT \"commitTimeMs\", pg_logical_emit_message(\n false,\n ${this.messagePrefix},\n json_build_object(\n 'id', ${id}::text,\n 'sendTimeMs', ${now}::int8,\n 'commitTimeMs', \"commitTimeMs\"\n )::text\n ) as lsn FROM CTE;\n `;\n }\n } catch (e) {\n if (this.#expectingLagReport?.id === id) {\n this.#expectingLagReport = null;\n }\n throw e;\n }\n\n // Note: We don't know the lsn until after pg_logical_emit_message()\n // returns, at which point it is possible that the report has\n // already been sent through the replication stream, but this\n // is okay since this.#expectingLagReport will have be updated.\n lagReport.lsn = toBigInt(lsn);\n lagReport.commitTimeMs = commitTimeMs;\n if (this.#expectingLagReport?.id === id) {\n this.#scheduleMissingReportRetry(id);\n }\n\n if (log) {\n this.#lc.info?.(`initiated lag report at lsn ${lsn}`, {\n id,\n lsn,\n sendTimeMs: now,\n commitTimeMs,\n });\n }\n return {nextSendTimeMs: now};\n }\n\n /**\n * In Postgres < 17, the pg_logical_emit_message lacks an immediate \"flush\"\n * option, which can cause messages to be missed when the replication stream\n * starts up:\n *\n * ```\n * * emit message → WAL write (buffered, not flushed)\n * * walsender reads up to current flush LSN\n * * emitted message's LSN is beyond flush LSN → not yet visible\n * * stream feedback/acknowledgment advances slot\n * * WAL eventually flushes → but slot has already moved past it\n * ```\n *\n * This has been seen to happen for the initial `wal_writer_delay` interval\n * of a replication session.\n *\n * To account for this, the last emitted lag report is considered \"received\"\n * if the stream has advanced beyond the LSN of the report.\n */\n checkCurrentLSN(lsn: bigint): DownstreamStatusMessage | undefined {\n if (this.#expectingLagReport?.lsn && lsn > this.#expectingLagReport.lsn) {\n this.#lc.info?.(\n `LSN ${fromBigInt(lsn)} is passed expected lag report ` +\n `${fromBigInt(this.#expectingLagReport.lsn)}. Processing it as received.`,\n );\n return this.#processLagReport(\n this.#expectingLagReport,\n majorVersionToString(lsn),\n );\n }\n return undefined;\n }\n\n stop() {\n clearTimeout(this.#timer);\n this.#timer = undefined;\n }\n\n #scheduleNextReport(delayMs: number) {\n this.#expectingLagReport = null;\n clearTimeout(this.#timer);\n this.#timer = setTimeout(async () => {\n this.#timer = undefined;\n try {\n await this.initiateLagReport();\n } catch (e) {\n this.#lc.warn?.(`error initiating lag report`, e);\n this.#scheduleNextReport(this.#lagIntervalMs);\n }\n }, delayMs);\n }\n\n #scheduleMissingReportRetry(reportID: string) {\n clearTimeout(this.#timer);\n this.#timer = setTimeout(async () => {\n this.#timer = undefined;\n const missingReport = this.#expectingLagReport;\n if (missingReport?.id !== reportID) {\n return;\n }\n\n this.#lagReportRetries.add(1);\n this.#lc.warn?.(`retrying missing lag report`, {\n id: missingReport.id,\n lsn: fromBigInt(missingReport.lsn),\n sendTimeMs: missingReport.sendTimeMs,\n });\n try {\n await this.initiateLagReport();\n } catch (e) {\n this.#lc.warn?.(`error retrying lag report`, e);\n this.#scheduleNextReport(this.#lagIntervalMs);\n }\n }, this.#lagIntervalMs);\n }\n\n processLagReportMessage(msg: MessageMessage): DownstreamStatusMessage {\n assert(\n msg.prefix === this.messagePrefix,\n `unexpected message prefix: ${msg.prefix}`,\n );\n const report = parseLogicalMessageContent(this.#lc, msg, lagReportSchema);\n return this.#processLagReport(\n report,\n toStateVersionString(msg.messageLsn ?? '0/0'),\n );\n }\n\n #processLagReport(\n report: LagReport,\n watermark: string,\n ): DownstreamStatusMessage {\n const now = Date.now();\n const expectedReport = this.#expectingLagReport;\n let nextSendTimeMs: number;\n if (report.id === expectedReport?.id) {\n nextSendTimeMs = Math.max(now, report.sendTimeMs + this.#lagIntervalMs);\n this.#scheduleNextReport(nextSendTimeMs - now);\n } else {\n // Only schedule the next report when receiving the previous report.\n // For historic reports in the WAL, or reports generated by other\n // replication-managers, status messages are still sent downstream,\n // but the next report is not actually scheduled.\n this.#lc.debug?.(`received extraneous lag report`, {report});\n nextSendTimeMs =\n expectedReport?.sendTimeMs ??\n Math.max(now, report.sendTimeMs + this.#lagIntervalMs);\n }\n const {sendTimeMs, commitTimeMs} = report;\n return [\n 'status',\n {\n ack: false,\n lagReport: {\n lastTimings: {\n sendTimeMs,\n commitTimeMs,\n receiveTimeMs: now,\n },\n nextSendTimeMs,\n },\n },\n {watermark},\n ];\n }\n}\n\ntype ReplicationError = {\n lsn: bigint;\n msg: Message;\n err: unknown;\n lastLogTime: number;\n};\n\nconst SET_REPLICA_IDENTITY_DELAY_MS = 50;\n\nclass ChangeMaker {\n readonly #shardPrefix: string;\n readonly #shardConfig: InternalShardConfig;\n readonly #initialSchema: PublishedSchema;\n readonly #db: PostgresDB;\n\n #replicaIdentityTimer: NodeJS.Timeout | undefined;\n #error: ReplicationError | undefined;\n\n constructor(\n {appID, shardNum}: ShardID,\n shardConfig: InternalShardConfig,\n db: PostgresDB,\n initialSchema: PublishedSchema,\n ) {\n // Note: This matches the prefix used in pg_logical_emit_message() in pg/schema/ddl.ts.\n this.#shardPrefix = `${appID}/${shardNum}`;\n this.#shardConfig = shardConfig;\n this.#initialSchema = initialSchema;\n this.#db = db;\n }\n\n async makeChanges(\n lc: LogContext,\n lsn: bigint,\n msg: Message,\n ): Promise<ChangeStreamMessage[]> {\n if (this.#error) {\n this.#logError(lc, this.#error);\n return [];\n }\n try {\n return await this.#makeChanges(lc, lsn, msg);\n } catch (err) {\n this.#error = {lsn, msg, err, lastLogTime: 0};\n this.#logError(lc, this.#error);\n\n const message = `Unable to continue replication from LSN ${fromBigInt(lsn)}`;\n const errorDetails: JSONObject = {error: message};\n if (err instanceof UnsupportedSchemaChangeError) {\n errorDetails.reason = err.description;\n errorDetails.context = err.event.context;\n } else {\n errorDetails.reason = String(err);\n }\n\n // Rollback the current transaction to avoid dangling transactions in\n // downstream processors (i.e. changeLog, replicator).\n return [\n ['rollback', {tag: 'rollback'}],\n ['control', {tag: 'reset-required', message, errorDetails}],\n ];\n }\n }\n\n #logError(lc: LogContext, error: ReplicationError) {\n const {lsn, msg, err, lastLogTime} = error;\n const now = Date.now();\n\n // Output an error to logs as replication messages continue to be dropped,\n // at most once a minute.\n if (now - lastLogTime > 60_000) {\n lc.error?.(\n `Unable to continue replication from LSN ${fromBigInt(lsn)}: ${String(\n err,\n )}`,\n {\n // 'content' can be a large byte Buffer. Exclude it from logging output.\n ...{change: {...msg, content: undefined}},\n ...(err instanceof UnsupportedSchemaChangeError && {\n context: err.event.context,\n }),\n ...(err instanceof Error && {\n errorMsg: err.message,\n name: err.name,\n stack: err.stack,\n }),\n },\n );\n error.lastLogTime = now;\n }\n }\n\n async #makeChanges(\n lc: LogContext,\n lsn: bigint,\n msg: Message,\n ): Promise<ChangeStreamData[]> {\n switch (msg.tag) {\n case 'begin':\n return [\n [\n 'begin',\n {...msg, json: 's'},\n {commitWatermark: toStateVersionString(must(msg.commitLsn))},\n ],\n ];\n\n case 'delete': {\n if (!(msg.key ?? msg.old)) {\n throw new Error(\n `Invalid DELETE msg (missing key): ${stringify(msg)}`,\n );\n }\n return [\n [\n 'data',\n {\n ...msg,\n relation: makeRelation(msg.relation),\n // https://www.postgresql.org/docs/current/protocol-logicalrep-message-formats.html#PROTOCOL-LOGICALREP-MESSAGE-FORMATS-DELETE\n key: must(msg.old ?? msg.key),\n },\n ],\n ];\n }\n\n case 'update': {\n return [\n [\n 'data',\n {\n ...msg,\n relation: makeRelation(msg.relation),\n // https://www.postgresql.org/docs/current/protocol-logicalrep-message-formats.html#PROTOCOL-LOGICALREP-MESSAGE-FORMATS-UPDATE\n key: msg.old ?? msg.key,\n },\n ],\n ];\n }\n\n case 'insert':\n return [['data', {...msg, relation: makeRelation(msg.relation)}]];\n case 'truncate':\n return [['data', {...msg, relations: msg.relations.map(makeRelation)}]];\n\n case 'message':\n if (!msg.prefix.startsWith(this.#shardPrefix)) {\n lc.debug?.('ignoring message for different shard', msg.prefix);\n return [];\n }\n switch (msg.prefix.substring(this.#shardPrefix.length)) {\n case '': // Legacy prefix\n case '/ddl':\n return this.#handleDdlMessage(lc, lsn, msg);\n default:\n lc.debug?.('ignoring unknown message type', msg.prefix);\n return [];\n }\n\n case 'commit':\n return [\n [\n 'commit',\n // `commitTime` is microseconds since the unix epoch. Carrying it\n // as milliseconds gives the ViewSyncer the origin timestamp for\n // the end-to-end serving lag histogram.\n {...msg, commitTimeMs: Number(msg.commitTime / 1000n)},\n {watermark: toStateVersionString(must(msg.commitLsn))},\n ],\n ];\n\n case 'relation':\n return await this.#handleRelation(msg);\n case 'type':\n return []; // Nothing need be done for custom types.\n case 'origin':\n // No need to detect replication loops since we are not a\n // PG replication source.\n return [];\n default:\n msg satisfies never;\n throw new Error(`Unexpected message type ${stringify(msg)}`);\n }\n }\n\n // The lastReplicationEvent is stored to understand the context\n // of the next one.\n #lastReplicationEvent: ReplicationEvent | undefined;\n\n #handleDdlMessage(\n lc: LogContext,\n lsn: bigint,\n msg: MessageMessage,\n ): ChangeStreamData[] {\n const event = parseLogicalMessageContent(lc, msg, replicationEventSchema);\n lc = lc\n .withContext('lsn', fromBigInt(lsn))\n .withContext('tag', event.event.tag)\n .withContext('query', event.context.query);\n\n // Cancel manual schema adjustment timeouts when an upstream schema change\n // is about to happen, so as to avoid interfering / redundant work.\n clearTimeout(this.#replicaIdentityTimer);\n\n const {type} = event;\n switch (type) {\n case 'ddlStart':\n case 'schemaSnapshot':\n case 'ddlUpdate':\n break;\n default: // Ignore unknown types for forwards compatibility\n lc.info?.(`ignoring unknown ddl message type: ${type}`);\n return [];\n }\n\n const prevEvent = this.#lastReplicationEvent;\n // Store the new event to understand the context of the next event.\n this.#lastReplicationEvent = event;\n\n const {schema} = event;\n if (schema === undefined) {\n // A schema-less (protocol v2) ddlStart event signifies that there was\n // no schema change; it is stored (above) only to provide the command\n // tag context for a subsequent event with a schema change.\n lc.debug?.(`received context-only ${msg.prefix}/${type} event`, {\n event: summarizeReplicationEventForLog(event),\n });\n return [];\n }\n\n const prevSchema =\n event.previousSchema === undefined // pre-v21 event => use prevEvent\n ? prevEvent?.schema\n : event.previousSchema;\n if (!prevSchema) {\n lc.info?.(`received ${msg.prefix}/${type} event`, {\n event: summarizeReplicationEventForLog(event),\n });\n return [];\n }\n\n // The tag (i.e. command) is used as an optimization to determine whether\n // backfill is necessary (CREATE TABLE vs ALTER TABLE vs\n // ALTER PUBLICATION). If the context is not available (rare), the tag\n // falls back to 'UNKNOWN', which conservatively initiates a backfill.\n //\n // Because ddl events may be nested, e.g.\n //\n // ```\n // [1] [2] [3] [4] [5]\n // ddl_start => ddl_start => ddl_end => ddl_start => ddl_end => ddl_end\n // ```\n //\n // The effective tag is determined from the previous event if it is\n // ddl_start (e.g. cases 1, 2, and 4), and from the current event\n // if it is a ddl_end (case 5), and 'UNKNOWN' otherwise (case 3 and\n // 'schemaSnapshot' workarounds).\n const effectiveTag =\n prevEvent?.type === 'ddlStart'\n ? prevEvent.event.tag\n : event.type === 'ddlUpdate'\n ? event.event.tag\n : 'UNKNOWN';\n lc.info?.(`processing ${effectiveTag} command from ${msg.prefix}/${type}`, {\n event: summarizeReplicationEventForLog(event),\n });\n const changes = this.#makeSchemaChanges(\n lc,\n prevSchema,\n schema,\n event,\n effectiveTag,\n ).map(change => ['data', change] satisfies Data);\n\n lc.info?.(`${changes.length} schema change(s)`, {changes});\n\n const replicaIdentities =\n replicaIdentitiesForTablesWithoutPrimaryKeys(schema);\n if (replicaIdentities) {\n this.#replicaIdentityTimer = setTimeout(async () => {\n try {\n await replicaIdentities.apply(lc, this.#db);\n } catch (err) {\n lc.warn?.(`error setting replica identities`, err);\n }\n }, SET_REPLICA_IDENTITY_DELAY_MS);\n }\n\n return changes;\n }\n\n /**\n * A note on operation order:\n *\n * Postgres will drop related indexes when columns are dropped,\n * but SQLite will error instead (https://sqlite.org/forum/forumpost/2e62dba69f?t=c&hist).\n * The current workaround is to drop indexes first.\n *\n * Also note that although it should not be possible to both rename and\n * add/drop tables/columns in a single statement, the operations are\n * ordered to handle that possibility, by always dropping old entities,\n * then modifying kept entities, and then adding new entities.\n *\n * Thus, the order of replicating DDL updates is:\n * - drop indexes\n * - drop tables\n * - alter tables\n * - drop columns\n * - alter columns\n * - add columns\n * - create tables\n * - create indexes\n *\n * In the future the replication logic should be improved to handle this\n * behavior in SQLite by dropping dependent indexes manually before dropping\n * columns. This, for example, would be needed to properly support changing\n * the type of a column that's indexed.\n */\n #makeSchemaChanges(\n lc: LogContext,\n preSchema: PublishedSchema,\n nextSchema: PublishedSchema,\n event: ReplicationEvent,\n tag: string,\n ): SchemaChange[] {\n try {\n const [prevTbl, prevIdx] = specsByID(preSchema);\n const [nextTbl, nextIdx] = specsByID(nextSchema);\n const changes: SchemaChange[] = [];\n\n // Validate the new table schemas\n for (const table of nextTbl.values()) {\n validate(lc, table);\n }\n\n const [droppedIdx, createdIdx] = symmetricDifferences(prevIdx, nextIdx);\n\n // Detect modified indexes (same name, different definition).\n // This happens when a constraint is dropped and recreated with the\n // same name in a single ALTER TABLE statement.\n // Note: We compare using stable column attnums rather than names,\n // because table/column renames change the index spec cosmetically\n // (tableName, column keys) without the index actually being recreated.\n const keptIdx = intersection(prevIdx, nextIdx);\n for (const id of keptIdx) {\n if (\n isIndexStructurallyChanged(\n must(prevIdx.get(id)),\n must(nextIdx.get(id)),\n prevTbl,\n nextTbl,\n )\n ) {\n droppedIdx.add(id);\n createdIdx.add(id);\n }\n }\n\n for (const id of droppedIdx) {\n const {schema, name} = must(prevIdx.get(id));\n changes.push({tag: 'drop-index', id: {schema, name}});\n }\n\n // DROP\n const [droppedTbl, createdTbl] = symmetricDifferences(prevTbl, nextTbl);\n for (const id of droppedTbl) {\n const {schema, name} = must(prevTbl.get(id));\n changes.push({tag: 'drop-table', id: {schema, name}});\n }\n // ALTER TABLE | ALTER PUBLICATION\n const tables = intersection(prevTbl, nextTbl);\n for (const id of tables) {\n changes.push(\n ...this.#getTableChanges(\n lc,\n must(prevTbl.get(id)),\n must(nextTbl.get(id)),\n tag,\n ),\n );\n }\n // CREATE\n for (const id of createdTbl) {\n const spec = must(nextTbl.get(id));\n const createTable: TableCreate = {\n tag: 'create-table',\n spec,\n metadata: getMetadata(spec),\n };\n // Only tables introduced by a `CREATE` statement can skip backfill.\n // All other scenarios in which tables are introduced into the\n // schema, e.g.\n // * ALTER PUBLICATION statements\n // * COMMENT statements\n // * MANUAL snapshots\n // * UNKNOWN command tags\n // must be backfilled.\n if (!tag.startsWith('CREATE')) {\n createTable.backfill = mapValues(spec.columns, ({pos: attNum}) => ({\n attNum,\n })) satisfies Record<string, ColumnMetadata>;\n }\n changes.push(createTable);\n }\n\n // Add indexes last since they may reference tables / columns that need\n // to be created first.\n for (const id of createdIdx) {\n const spec = must(nextIdx.get(id));\n changes.push({tag: 'create-index', spec});\n }\n return changes;\n } catch (e) {\n throw new UnsupportedSchemaChangeError(String(e), event, {cause: e});\n }\n }\n\n #getTableChanges(\n lc: LogContext,\n oldTable: PublishedTableWithReplicaIdentity,\n newTable: PublishedTableWithReplicaIdentity,\n ddlTag: string,\n ): SchemaChange[] {\n const changes: SchemaChange[] = [];\n if (\n oldTable.schema !== newTable.schema ||\n oldTable.name !== newTable.name\n ) {\n changes.push({\n tag: 'rename-table',\n old: {schema: oldTable.schema, name: oldTable.name},\n new: {schema: newTable.schema, name: newTable.name},\n });\n }\n const oldMetadata = getMetadata(oldTable);\n const newMetadata = getMetadata(newTable);\n if (!deepEqual(oldMetadata, newMetadata)) {\n changes.push({\n tag: 'update-table-metadata',\n table: {schema: newTable.schema, name: newTable.name},\n old: oldMetadata,\n new: newMetadata,\n });\n }\n const table = {schema: newTable.schema, name: newTable.name};\n const oldColumns = columnsByID(oldTable.columns);\n const newColumns = columnsByID(newTable.columns);\n\n // DROP\n const [dropped, added] = symmetricDifferences(oldColumns, newColumns);\n for (const id of dropped) {\n const {name: column} = must(oldColumns.get(id));\n changes.push({tag: 'drop-column', table, column});\n }\n\n // ALTER\n const both = intersection(oldColumns, newColumns);\n for (const id of both) {\n const {name: oldName, ...oldSpec} = must(oldColumns.get(id));\n const {name: newName, ...newSpec} = must(newColumns.get(id));\n // The three things that we care about are:\n // 1. name\n // 2. type\n // 3. not-null\n if (\n oldName !== newName ||\n oldSpec.dataType !== newSpec.dataType ||\n oldSpec.notNull !== newSpec.notNull\n ) {\n changes.push({\n tag: 'update-column',\n table,\n old: {name: oldName, spec: oldSpec},\n new: {name: newName, spec: newSpec},\n });\n }\n }\n\n // Only columns introduced by `ALTER TABLE` statements can potentially\n // skip backfill if they have non-constant defaults. All other scenarios\n // in which columns are introduced, e.g.\n // * ALTER PUBLICATION\n // * COMMENT\n // * MANUAL\n // * UNKNOWN\n // must be backfilled.\n const alwaysBackfill = ddlTag !== 'ALTER TABLE';\n\n // ADD\n for (const id of added) {\n const {name, ...spec} = must(newColumns.get(id));\n const column = {name, spec};\n const addColumn: ColumnAdd = {\n tag: 'add-column',\n table,\n column,\n tableMetadata: getMetadata(newTable),\n };\n if (alwaysBackfill) {\n addColumn.column.spec.dflt = null;\n addColumn.backfill = {attNum: spec.pos} satisfies ColumnMetadata;\n } else {\n // Determine if the ChangeProcessor will accept the column add as is.\n try {\n mapPostgresToLiteColumn(table.name, column);\n } catch (e) {\n if (!(e instanceof UnsupportedColumnDefaultError)) {\n // Note: mapPostgresToLiteColumn is not expected to throw any other\n // types of errors.\n throw e;\n }\n // If the column has an unsupported default (e.g. an expression or a\n // generated value), create the column as initially hidden with a\n // `null` default, and publish it after backfilling the values from\n // upstream. Note that this does require that the table have a valid\n // REPLICA IDENTITY, since backfill relies on merging new data with\n // an existing row.\n lc.info?.(`Backfilling column ${table.name}.${name}: ${String(e)}`);\n addColumn.column.spec.dflt = null;\n addColumn.backfill = {attNum: spec.pos} satisfies ColumnMetadata;\n }\n }\n changes.push(addColumn);\n }\n return changes;\n }\n\n /**\n * If `ddlDetection === true`, relation messages are irrelevant,\n * as schema changes are detected by event triggers that\n * emit custom messages.\n *\n * For degraded-mode replication (`ddlDetection === false`):\n * 1. query the current published schemas on upstream\n * 2. compare that with the InternalShardConfig.initialSchema\n * 3. compare that with the incoming MessageRelation\n * 4. On any discrepancy, throw an UnsupportedSchemaChangeError\n * to halt replication.\n *\n * Note that schemas queried in step [1] will be *post-transaction*\n * schemas, which are not necessarily suitable for actually processing\n * the statements in the transaction being replicated. In other words,\n * this mechanism cannot be used to reliably *replicate* schema changes.\n * However, they serve the purpose determining if schemas have changed.\n */\n async #handleRelation(rel: PostgresRelation): Promise<ChangeStreamData[]> {\n const {publications, ddlDetection} = this.#shardConfig;\n if (ddlDetection) {\n return [];\n }\n const currentSchema = await getPublicationInfo(this.#db, publications);\n const difference = getSchemaDifference(this.#initialSchema, currentSchema);\n if (difference !== null) {\n throw new MissingEventTriggerSupport(difference);\n }\n // Even if the currentSchema is equal to the initialSchema, the\n // MessageRelation itself must be checked to detect transient\n // schema changes within the transaction (e.g. adding and dropping\n // a table, or renaming a column and then renaming it back).\n const orel = this.#initialSchema.tables.find(\n t => t.oid === rel.relationOid,\n );\n if (!orel) {\n // Can happen if a table is created and then dropped in the same transaction.\n throw new MissingEventTriggerSupport(\n `relation not in initialSchema: ${stringify(rel)}`,\n );\n }\n if (relationDifferent(orel, rel)) {\n throw new MissingEventTriggerSupport(\n `relation has changed within the transaction: ${stringify(orel)} vs ${stringify(rel)}`,\n );\n }\n return [];\n }\n}\n\nfunction getSchemaDifference(\n a: PublishedSchema,\n b: PublishedSchema,\n): string | null {\n // Note: ignore indexes since changes need not to halt replication\n if (a.tables.length !== b.tables.length) {\n return `tables created or dropped`;\n }\n for (let i = 0; i < a.tables.length; i++) {\n const at = a.tables[i];\n const bt = b.tables[i];\n const difference = getTableDifference(at, bt);\n if (difference) {\n return difference;\n }\n }\n return null;\n}\n\n// ColumnSpec comparator\nconst byColumnPos = (a: [string, ColumnSpec], b: [string, ColumnSpec]) =>\n a[1].pos < b[1].pos ? -1 : a[1].pos > b[1].pos ? 1 : 0;\n\nfunction getTableDifference(\n a: PublishedTableSpec,\n b: PublishedTableSpec,\n): string | null {\n if (a.oid !== b.oid || a.schema !== b.schema || a.name !== b.name) {\n return `Table \"${a.name}\" differs from table \"${b.name}\"`;\n }\n if (!deepEqual(a.primaryKey, b.primaryKey)) {\n return `Primary key of table \"${a.name}\" has changed`;\n }\n const acols = Object.entries(a.columns).sort(byColumnPos);\n const bcols = Object.entries(b.columns).sort(byColumnPos);\n if (\n acols.length !== bcols.length ||\n acols.some(([aname, acol], i) => {\n const [bname, bcol] = bcols[i];\n return (\n aname !== bname ||\n acol.pos !== bcol.pos ||\n acol.typeOID !== bcol.typeOID ||\n acol.notNull !== bcol.notNull\n );\n })\n ) {\n return `Columns of table \"${a.name}\" have changed`;\n }\n return null;\n}\n\nexport function relationDifferent(a: PublishedTableSpec, b: PostgresRelation) {\n if (a.oid !== b.relationOid || a.schema !== b.schema || a.name !== b.name) {\n return true;\n }\n if (\n // The MessageRelation's `keyColumns` field contains the columns in column\n // declaration order, whereas the PublishedTableSpec's `primaryKey`\n // contains the columns in primary key (i.e. index) order. Do an\n // order-agnostic compare here since it is not possible to detect\n // key-order changes from the MessageRelation message alone.\n b.replicaIdentity === 'default' &&\n !equals(new Set(a.primaryKey), new Set(b.keyColumns))\n ) {\n return true;\n }\n const acols = Object.entries(a.columns).sort(byColumnPos);\n const bcols = b.columns;\n return (\n acols.length !== bcols.length ||\n acols.some(([aname, acol], i) => {\n const bcol = bcols[i];\n return aname !== bcol.name || acol.typeOID !== bcol.typeOid;\n })\n );\n}\n\nfunction translateError(e: unknown): Error {\n if (!(e instanceof Error)) {\n return new Error(String(e));\n }\n if (e instanceof postgres.PostgresError && e.code === PG_ADMIN_SHUTDOWN) {\n return new ShutdownSignal(e);\n }\n return e;\n}\nconst idString = (id: Identifier) => `${id.schema}.${id.name}`;\n\nfunction specsByID(published: PublishedSchema) {\n return [\n // It would have been nice to use a CustomKeyMap here, but we rely on set-utils\n // operations which use plain Sets.\n new Map(published.tables.map(t => [t.oid, t])),\n new Map(published.indexes.map(i => [idString(i), i])),\n ] as const;\n}\n\n/**\n * Determines if an index was structurally changed (e.g. constraint dropped\n * and recreated with different columns) vs cosmetically changed (e.g. the\n * index spec changed because the table or a column was renamed).\n *\n * Compares boolean properties directly and resolves column names to their\n * stable attnums (pg_attribute `attnum`) for the column comparison.\n */\nfunction isIndexStructurallyChanged(\n prev: PublishedIndexSpec,\n next: PublishedIndexSpec,\n prevTables: Map<number, PublishedTableWithReplicaIdentity>,\n nextTables: Map<number, PublishedTableWithReplicaIdentity>,\n): boolean {\n if (\n prev.unique !== next.unique ||\n prev.isPrimaryKey !== next.isPrimaryKey ||\n prev.isReplicaIdentity !== next.isReplicaIdentity ||\n prev.isImmediate !== next.isImmediate\n ) {\n return true;\n }\n\n const prevTable = findTableBySchemaAndName(\n prevTables,\n prev.schema,\n prev.tableName,\n );\n const nextTable = findTableBySchemaAndName(\n nextTables,\n next.schema,\n next.tableName,\n );\n if (!prevTable || !nextTable) {\n // Can't resolve tables; conservatively treat as changed.\n return true;\n }\n\n const prevEntries = Object.entries(prev.columns);\n const nextEntries = Object.entries(next.columns);\n if (prevEntries.length !== nextEntries.length) {\n return true;\n }\n\n // Resolve column names → attnums and compare.\n const prevByAttnum = new Map<number | undefined, string>(\n prevEntries.map(([name, dir]) => [prevTable.columns[name]?.pos, dir]),\n );\n const nextByAttnum = new Map<number | undefined, string>(\n nextEntries.map(([name, dir]) => [nextTable.columns[name]?.pos, dir]),\n );\n\n if (prevByAttnum.has(undefined) || nextByAttnum.has(undefined)) {\n // Column not found in table spec; conservatively treat as changed.\n return true;\n }\n if (prevByAttnum.size !== nextByAttnum.size) {\n return true;\n }\n for (const [attnum, dir] of prevByAttnum) {\n if (nextByAttnum.get(attnum) !== dir) {\n return true;\n }\n }\n return false;\n}\n\nfunction findTableBySchemaAndName(\n tables: Map<number, PublishedTableWithReplicaIdentity>,\n schema: string,\n name: string,\n): PublishedTableWithReplicaIdentity | undefined {\n for (const table of tables.values()) {\n if (table.schema === schema && table.name === name) {\n return table;\n }\n }\n return undefined;\n}\n\nfunction columnsByID(\n columns: Record<string, ColumnSpec>,\n): Map<number, ColumnSpec & {name: string}> {\n const colsByID = new Map<number, ColumnSpec & {name: string}>();\n for (const [name, spec] of Object.entries(columns)) {\n // The `pos` field is the `attnum` in `pg_attribute`, which is a stable\n // identifier for the column in this table (i.e. never reused).\n colsByID.set(spec.pos, {...spec, name});\n }\n return colsByID;\n}\n\nfunction getMetadata(table: PublishedTableWithReplicaIdentity): TableMetadata {\n return {\n schemaOID: must(table.schemaOID),\n relationOID: table.oid,\n rowKey: Object.fromEntries(\n table.replicaIdentityColumns.map(k => [\n k,\n {attNum: table.columns[k].pos},\n ]),\n ),\n };\n}\n\n// Avoid sending the `columns` from the Postgres MessageRelation message.\n// They are not used downstream and the message can be large.\nfunction makeRelation(relation: PostgresRelation): MessageRelation {\n // Avoid sending the `columns` from the Postgres MessageRelation message.\n // They are not used downstream and the message can be large.\n const {columns: _, keyColumns, replicaIdentity, ...rest} = relation;\n return {\n ...rest,\n rowKey: {\n columns: keyColumns,\n type: replicaIdentity,\n },\n // For now, deprecated columns are sent for backwards compatibility.\n // These can be removed when bumping the MIN_PROTOCOL_VERSION to 5.\n keyColumns,\n replicaIdentity,\n };\n}\n\nfunction summarizeSchemaForLog(schema: PublishedSchema) {\n return {\n tables: schema.tables.length,\n indexes: schema.indexes.length,\n };\n}\n\nfunction summarizeReplicationEventForLog(event: ReplicationEvent): JSONObject {\n const {schema, ...rest} = event;\n const {previousSchema, ...eventWithoutSchemas} = rest;\n return {\n ...eventWithoutSchemas,\n ...(schema !== undefined && {schema: summarizeSchemaForLog(schema)}),\n ...(previousSchema !== undefined && {\n previousSchema:\n previousSchema === null ? null : summarizeSchemaForLog(previousSchema),\n }),\n };\n}\n\nclass UnsupportedSchemaChangeError extends Error {\n readonly name = 'UnsupportedSchemaChangeError';\n readonly description: string;\n readonly event: ReplicationEvent;\n\n constructor(\n description: string,\n event: ReplicationEvent,\n options?: ErrorOptions,\n ) {\n super(\n `Replication halted. Resync the replica to recover: ${description}`,\n options,\n );\n this.description = description;\n this.event = event;\n }\n}\n\nclass MissingEventTriggerSupport extends Error {\n readonly name = 'MissingEventTriggerSupport';\n\n constructor(msg: string) {\n super(\n `${msg}. Schema changes cannot be reliably replicated without event trigger support.`,\n );\n }\n}\n\n// TODO(0xcadams): should this be a ProtocolError?\nclass ShutdownSignal extends AbortError {\n readonly name = 'ShutdownSignal';\n\n constructor(cause: unknown) {\n super(\n 'shutdown signal received (e.g. another zero-cache taking over the replication stream)',\n {\n cause,\n },\n );\n }\n}\n\nfunction parseLogicalMessageContent<T>(\n lc: LogContext,\n msg: MessageMessage,\n schema: v.Type<T>,\n) {\n const {content} = msg;\n const str =\n content instanceof Buffer\n ? content.toString('utf-8')\n : new TextDecoder().decode(content);\n try {\n const json = JSON.parse(str);\n return v.parse(json, schema, 'passthrough');\n } catch (e) {\n lc.error?.(`unable to parse logical message content: ${String(e)}`, {\n message: {...msg, content: str},\n });\n throw e;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkHA,IAAM,mCAAmC;;;;;;AAOzC,eAAsB,+BACpB,IACA,aACA,OACA,eACA,aACA,SACA,sBAAsB,GACuD;CAC7E,MAAM,YACJ,IACA,WAAW,MAAM,MAAM,GAAG,MAAM,YAChC,gBACC,KAAK,OAAO,YAAY,KAAK,OAAO,IAAI,aAAa,aAAa,OAAO,CAC5E;CAEA,MAAM,UAAU,IAAI,SAAS,IAAI,aAAa;CAC9C,MAAM,oBAAoB,+BACxB,IAAI,gBAAgB,OAAO,CAC7B;CACA,QAAQ,MAAM;CAId,MAAM,KAAK,MAAM,gBAAgB,IAAI,aAAa,oBAAoB;CACtE,IAAI;EAkBF,OAAO;GAAC;GAAmB,cAAA,IAVF,qBACvB,IACA,aACA,OACA,MAX4B,uBAC5B,IACA,IACA,OACA,iBACF,GAOE,SACA,qBACA,YAAY,QAGa;EAAY;CACzC,UAAU;EACR,MAAM,GAAG,IAAI;CACf;AACF;AAEA,eAAe,uBACb,IAC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;AACF;AAEA,IAAM,6BAAN,cAAyC,MAAM;CAC7C,OAAgB;CAEhB,YAAY,KAAa;EACvB,MACE,GAAG,IAAI,8EACT;CACF;AACF;AAGA,IAAM,iBAAN,cAA6B,WAAW;CACtC,OAAgB;CAEhB,YAAY,OAAgB;EAC1B,MACE,yFACA,EACE,MACF,CACF;CACF;AACF;AAEA,SAAS,2BACP,IACA,KACA,QACA;CACA,MAAM,EAAC,YAAW;CAClB,MAAM,MACJ,mBAAmB,SACf,QAAQ,SAAS,OAAO,IACxB,IAAI,YAAY,EAAE,OAAO,OAAO;CACtC,IAAI;EAEF,OAAO,MADM,KAAK,MAAM,GACT,GAAM,QAAQ,aAAa;CAC5C,SAAS,GAAG;EACV,GAAG,QAAQ,4CAA4C,OAAO,CAAC,KAAK,EAClE,SAAS;GAAC,GAAG;GAAK,SAAS;EAAG,EAChC,CAAC;EACD,MAAM;CACR;AACF"}
|
|
1
|
+
{"version":3,"file":"change-source.js","names":["#lc","#db","#upstreamUri","#shard","#replica","#context","#lagReporter","#textCopy","#stopped","#cleanupTimer","#stopExistingReplicationSlotSubscriber","#startStream","#logCurrentReplicaInfo","#cleanUpOlderReplicasAndSlots","#acks","#expectDownstreamAck","#ackIfDownstreamIsCaughtUp","#waitingForDownstreamAck","#sendAck","#lagIntervalMs","#lagReportRetries","#getPgVersion","#pgVersion","#expectingLagReport","#scheduleMissingReportRetry","#processLagReport","#timer","#scheduleNextReport","#shardPrefix","#shardConfig","#initialSchema","#error","#logError","#makeChanges","#handleDdlMessage","#handleRelation","#replicaIdentityTimer","#lastReplicationEvent","#makeSchemaChanges","#getTableChanges"],"sources":["../../../../../../../zero-cache/src/services/change-source/pg/change-source.ts"],"sourcesContent":["import {\n PG_ADMIN_SHUTDOWN,\n PG_INSUFFICIENT_PRIVILEGE,\n} from '@drdgvhbh/postgres-error-codes';\nimport type {LogContext} from '@rocicorp/logger';\nimport {nanoid} from 'nanoid';\nimport postgres from 'postgres';\nimport {AbortError} from '../../../../../shared/src/abort-error.ts';\nimport {assert} from '../../../../../shared/src/asserts.ts';\nimport {stringify} from '../../../../../shared/src/bigint-json.ts';\nimport {deepEqual} from '../../../../../shared/src/json.ts';\nimport {must} from '../../../../../shared/src/must.ts';\nimport {mapValues} from '../../../../../shared/src/objects.ts';\nimport {\n equals,\n intersection,\n symmetricDifferences,\n} from '../../../../../shared/src/set-utils.ts';\nimport * as v from '../../../../../shared/src/valita.ts';\nimport {Database} from '../../../../../zqlite/src/db.ts';\nimport {\n mapPostgresToLiteColumn,\n UnsupportedColumnDefaultError,\n} from '../../../db/pg-to-lite.ts';\nimport type {\n ColumnSpec,\n PublishedIndexSpec,\n PublishedTableSpec,\n} from '../../../db/specs.ts';\nimport {StatementRunner} from '../../../db/statements.ts';\nimport {getOrCreateCounter} from '../../../observability/metrics.ts';\nimport {type LexiVersion} from '../../../types/lexi-version.ts';\nimport {PG_17} from '../../../types/pg-versions.ts';\nimport {\n connectPgClient,\n isPostgresError,\n pgClient,\n type PostgresDB,\n} from '../../../types/pg.ts';\nimport {\n upstreamSchema,\n type ShardConfig,\n type ShardID,\n} from '../../../types/shards.ts';\nimport {\n majorVersionFromString,\n majorVersionToString,\n} from '../../../types/state-version.ts';\nimport type {Sink} from '../../../types/streams.ts';\nimport {AutoResetSignal} from '../../change-streamer/schema/tables.ts';\nimport {\n getSubscriptionStateAndContext,\n type SubscriptionState,\n type SubscriptionStateAndContext,\n} from '../../replicator/schema/replication-state.ts';\nimport type {ChangeSource, ChangeStream} from '../change-source.ts';\nimport {BackfillManager} from '../common/backfill-manager.ts';\nimport {\n ChangeStreamMultiplexer,\n type Listener,\n} from '../common/change-stream-multiplexer.ts';\nimport {initReplica} from '../common/replica-schema.ts';\nimport type {\n BackfillRequest,\n DownstreamStatusMessage,\n JSONObject,\n} from '../protocol/current.ts';\nimport type {\n ColumnAdd,\n Identifier,\n MessageRelation,\n SchemaChange,\n TableCreate,\n} from '../protocol/current/data.ts';\nimport type {\n ChangeStreamData,\n ChangeStreamMessage,\n Data,\n} from '../protocol/current/downstream.ts';\nimport type {ColumnMetadata, TableMetadata} from './backfill-metadata.ts';\nimport {streamBackfill} from './backfill-stream.ts';\nimport {\n initialSync,\n type InitialSyncOptions,\n type ServerContext,\n} from './initial-sync.ts';\nimport type {\n Message,\n MessageMessage,\n MessageRelation as PostgresRelation,\n} from './logical-replication/pgoutput.types.ts';\nimport {subscribe, type StreamMessage} from './logical-replication/stream.ts';\nimport {fromBigInt, toBigInt, toStateVersionString, type LSN} from './lsn.ts';\nimport {registerReplicationSlotHealthMetrics} from './replication-slot-health.ts';\nimport {dropOldReplicasAndSlots} from './replication-slots.ts';\nimport {replicationEventSchema, type ReplicationEvent} from './schema/ddl.ts';\nimport {updateShardSchema} from './schema/init.ts';\nimport {\n getPublicationInfo,\n type PublishedSchema,\n type PublishedTableWithReplicaIdentity,\n} from './schema/published.ts';\nimport {\n dropShard,\n getInternalShardConfig,\n getReplicaAtVersion,\n internalPublicationPrefix,\n replicaIdentitiesForTablesWithoutPrimaryKeys,\n replicationSlotPrefix,\n type InternalShardConfig,\n type Replica,\n} from './schema/shard.ts';\nimport {validate} from './schema/validation.ts';\n\nconst REPLICA_SLOT_CLEANUP_INTERVAL_MS = 30_000;\n\n/**\n * Initializes a Postgres change source, including the initial sync of the\n * replica, before streaming changes from the corresponding logical replication\n * stream.\n */\nexport async function initializePostgresChangeSource(\n lc: LogContext,\n upstreamURI: string,\n shard: ShardConfig,\n replicaDbFile: string,\n syncOptions: InitialSyncOptions,\n context: ServerContext,\n lagReportIntervalMs = 0,\n): Promise<{subscriptionState: SubscriptionState; changeSource: ChangeSource}> {\n await initReplica(\n lc,\n `replica-${shard.appID}-${shard.shardNum}`,\n replicaDbFile,\n (log, tx) => initialSync(log, shard, tx, upstreamURI, syncOptions, context),\n );\n\n const replica = new Database(lc, replicaDbFile);\n const subscriptionState = getSubscriptionStateAndContext(\n new StatementRunner(replica),\n );\n replica.close();\n\n // Check that upstream is properly setup, and throw an AutoReset to re-run\n // initial sync if not.\n const db = await connectPgClient(lc, upstreamURI, 'change-source-init');\n try {\n const upstreamReplica = await checkAndUpdateUpstream(\n lc,\n db,\n shard,\n subscriptionState,\n );\n\n const changeSource = new PostgresChangeSource(\n lc,\n upstreamURI,\n shard,\n upstreamReplica,\n context,\n lagReportIntervalMs,\n syncOptions.textCopy,\n );\n\n return {subscriptionState, changeSource};\n } finally {\n await db.end();\n }\n}\n\nasync function checkAndUpdateUpstream(\n lc: LogContext,\n sql: PostgresDB,\n shard: ShardConfig,\n {\n replicaVersion,\n publications: subscribed,\n initialSyncContext,\n }: SubscriptionStateAndContext,\n) {\n // Perform any shard schema updates\n await updateShardSchema(lc, sql, shard, replicaVersion);\n\n const upstreamReplica = await getReplicaAtVersion(\n lc,\n sql,\n shard,\n replicaVersion,\n initialSyncContext,\n );\n if (!upstreamReplica) {\n throw new AutoResetSignal(\n `No replication slot for replica at version ${replicaVersion}`,\n );\n }\n\n // Verify that the publications match what is being replicated.\n const requested = shard.publications.toSorted();\n const replicated = upstreamReplica.publications\n .filter(p => !p.startsWith(internalPublicationPrefix(shard)))\n .sort();\n if (!deepEqual(requested, replicated)) {\n lc.warn?.(`Dropping shard to change publications to: [${requested}]`);\n await sql.unsafe(dropShard(shard.appID, shard.shardNum));\n throw new AutoResetSignal(\n `Requested publications [${requested}] do not match configured ` +\n `publications: [${replicated}]`,\n );\n }\n\n // Sanity check: The subscription state on the replica should have the\n // same publications. This should be guaranteed by the equivalence of the\n // replicaVersion, but it doesn't hurt to verify.\n if (!deepEqual(upstreamReplica.publications, subscribed)) {\n throw new AutoResetSignal(\n `Upstream publications [${upstreamReplica.publications}] do not ` +\n `match subscribed publications [${subscribed}]`,\n );\n }\n\n // Verify that the publications exist.\n const exists = await sql`\n SELECT pubname FROM pg_publication WHERE pubname IN ${sql(subscribed)};\n `.values();\n if (exists.length !== subscribed.length) {\n throw new AutoResetSignal(\n `Upstream publications [${exists.flat()}] do not contain ` +\n `all subscribed publications [${subscribed}]`,\n );\n }\n\n const {slot} = upstreamReplica;\n const result = await sql<{restartLSN: LSN | null; walStatus: string | null}[]>\n /*sql*/ `\n SELECT restart_lsn as \"restartLSN\", wal_status as \"walStatus\" FROM pg_replication_slots\n WHERE slot_name = ${slot}`;\n if (result.length === 0) {\n throw new AutoResetSignal(`replication slot ${slot} is missing`);\n }\n const [{restartLSN, walStatus}] = result;\n if (restartLSN === null || walStatus === 'lost') {\n throw new AutoResetSignal(\n `replication slot ${slot} has been invalidated for exceeding the max_slot_wal_keep_size`,\n );\n }\n return upstreamReplica;\n}\n\n// Parameterize this if necessary. In practice starvation may never happen.\nconst MAX_LOW_PRIORITY_DELAY_MS = 1000;\n\ntype ReservationState = {\n lastWatermark?: string;\n};\n\n/**\n * Postgres implementation of a {@link ChangeSource} backed by a logical\n * replication stream.\n */\nclass PostgresChangeSource implements ChangeSource {\n readonly #lc: LogContext;\n readonly #db: PostgresDB;\n readonly #upstreamUri: string;\n readonly #shard: ShardID;\n readonly #replica: Replica;\n readonly #context: ServerContext;\n readonly #lagReporter: LagReporter | null;\n readonly #textCopy: boolean;\n #stopped = false;\n\n constructor(\n lc: LogContext,\n upstreamUri: string,\n shard: ShardID,\n replica: Replica,\n context: ServerContext,\n lagReportIntervalMs: number,\n textCopy?: boolean | undefined,\n ) {\n this.#lc = lc.withContext('component', 'change-source');\n this.#db = pgClient(lc, upstreamUri, 'replication-monitor', {\n max: 1,\n // used occasionally for schema changes, periodically for lag reporting\n ['idle_timeout']: 60,\n });\n this.#upstreamUri = upstreamUri;\n this.#shard = shard;\n this.#replica = replica;\n this.#context = context;\n this.#textCopy = textCopy ?? false;\n this.#lagReporter =\n lagReportIntervalMs > 0\n ? new LagReporter(\n lc.withContext('component', 'lag-reporter'),\n shard,\n this.#db,\n lagReportIntervalMs,\n )\n : null;\n registerReplicationSlotHealthMetrics(\n this.#lc,\n this.#db,\n replica.slot,\n () => this.#stopped,\n );\n }\n\n async stop(): Promise<void> {\n this.#stopped = true;\n this.#lagReporter?.stop();\n clearTimeout(this.#cleanupTimer);\n await this.#db.end();\n }\n\n async startLagReporter(): Promise<{nextSendTimeMs: number} | null> {\n if (this.#lagReporter) {\n try {\n return await this.#lagReporter.initiateLagReport(true);\n } catch (e) {\n if (isPostgresError(e, PG_INSUFFICIENT_PRIVILEGE)) {\n const functionName =\n (this.#lagReporter.pgVersion ?? 0) >= PG_17\n ? 'pg_logical_emit_message(boolean, text, text, boolean)'\n : 'pg_logical_emit_message(boolean, text, text)';\n this.#lc.warn?.(\n `\\n\\nUnable to initiate replication lag reports due to insufficient privileges.` +\n `\\nTo enable replication lag reporting, run:`,\n `\\n\\tGRANT EXECUTE ON FUNCTION ${functionName} TO <your_db_user>;\\n\\n`,\n e,\n );\n } else {\n this.#lc.error?.(\n `Unexpected error while initiating lag reports. Lag reports will be disabled.`,\n e,\n );\n }\n }\n }\n return null;\n }\n\n async startStream(\n clientWatermark: string,\n backfillRequests: BackfillRequest[] = [],\n ): Promise<ChangeStream> {\n await this.#stopExistingReplicationSlotSubscriber();\n const config = await getInternalShardConfig(this.#db, this.#shard);\n const {slot} = this.#replica;\n this.#lc.info?.(`starting replication stream@${slot}`);\n return this.#startStream(slot, clientWatermark, config, backfillRequests);\n }\n\n async #startStream(\n slot: string,\n clientWatermark: string,\n shardConfig: InternalShardConfig,\n backfillRequests: BackfillRequest[],\n ): Promise<ChangeStream> {\n const clientStart = majorVersionFromString(clientWatermark) + 1n;\n const {messages, acks, sourceTerminated} = await subscribe(\n this.#lc,\n this.#db,\n slot,\n [...shardConfig.publications],\n clientStart,\n );\n const acker = new Acker(acks);\n\n // The ChangeStreamMultiplexer facilitates cooperative streaming from\n // the main replication stream and backfill streams initiated by the\n // BackfillManager.\n const changes = new ChangeStreamMultiplexer(this.#lc, clientWatermark);\n const backfillManager = new BackfillManager(this.#lc, changes, req =>\n streamBackfill(this.#lc, this.#upstreamUri, this.#replica, req, {\n textCopy: this.#textCopy,\n }),\n );\n changes\n .addProducers(messages, backfillManager)\n .addListeners(backfillManager, acker);\n backfillManager.run(clientWatermark, backfillRequests);\n\n const changeMaker = new ChangeMaker(\n this.#shard,\n shardConfig,\n this.#db,\n this.#replica.initialSchema,\n );\n\n /**\n * Determines if the incoming message is transactional, otherwise handling\n * non-transactional messages with a downstream status message.\n */\n const isTransactionalMessage = (\n lsn: bigint,\n msg: StreamMessage[1],\n ): msg is Message => {\n if (\n msg.tag === 'message' &&\n msg.prefix === this.#lagReporter?.messagePrefix\n ) {\n changes.pushStatus(this.#lagReporter.processLagReportMessage(msg));\n return false;\n }\n // Checks if we are passed the LSN of the expected lag report, in which\n // case a new one is initiated.\n const status = this.#lagReporter?.checkCurrentLSN(lsn);\n if (status) {\n changes.pushStatus(status);\n }\n\n if (msg.tag === 'keepalive') {\n changes.pushStatus([\n 'status',\n {ack: msg.shouldRespond},\n {watermark: majorVersionToString(lsn)},\n ]);\n return false;\n }\n return true;\n };\n\n void (async () => {\n try {\n let reservation: ReservationState | null = null;\n let inTransaction = false;\n\n for await (const [lsn, msg] of messages) {\n if (!isTransactionalMessage(lsn, msg)) {\n // If we're not in a transaction but the last reservation was kept\n // because of pending keepalives or lag reports in the queue,\n // release the reservation.\n if (!inTransaction && reservation?.lastWatermark) {\n changes.release(reservation.lastWatermark);\n reservation = null;\n }\n continue;\n }\n\n if (!reservation) {\n const res = changes.reserve('replication');\n typeof res === 'string' || (await res); // awaits should be uncommon\n reservation = {};\n }\n\n let lastChange: ChangeStreamMessage | undefined;\n for (const change of await changeMaker.makeChanges(\n this.#lc.withContext('lsn', fromBigInt(lsn)),\n lsn,\n msg,\n )) {\n await changes.push(change); // Allow the change-streamer to push back.\n lastChange = change;\n }\n\n switch (lastChange?.[0]) {\n case 'begin':\n inTransaction = true;\n break;\n case 'commit':\n inTransaction = false;\n reservation.lastWatermark = lastChange[2].watermark;\n if (\n messages.queued === 0 ||\n changes.waiterDelay() > MAX_LOW_PRIORITY_DELAY_MS\n ) {\n // After each transaction, release the reservation:\n // - if there are no pending upstream messages\n // - or if a low priority request has been waiting for longer\n // than MAX_LOW_PRIORITY_DELAY_MS. This is to prevent\n // (backfill) starvation on very active upstreams.\n changes.release(reservation.lastWatermark);\n reservation = null;\n }\n break;\n }\n }\n } catch (e) {\n // Note: no need to worry about reservations here since downstream\n // is being completely canceled.\n const err = translateError(e);\n if (err instanceof ShutdownSignal) {\n // Log the new state of the replica to surface information about the\n // server that sent the shutdown signal, if any.\n await this.#logCurrentReplicaInfo();\n }\n changes.fail(err);\n }\n })();\n\n this.#lc.info?.(\n `started replication stream@${slot} from ${clientWatermark} (replicaVersion: ${\n this.#replica.version\n })`,\n );\n\n return {\n changes: changes.asSource(),\n acks: {push: status => acker.ack(status[2].watermark)},\n sourceTerminated: sourceTerminated.then(translateError),\n };\n }\n\n async #logCurrentReplicaInfo() {\n try {\n const replica = await getReplicaAtVersion(\n this.#lc,\n this.#db,\n this.#shard,\n this.#replica.version,\n );\n if (replica) {\n this.#lc.info?.(\n `Shutdown signal from replica@${this.#replica.version}: ${stringify(replica.subscriberContext)}`,\n );\n }\n } catch (e) {\n this.#lc.warn?.(`error logging replica info`, e);\n }\n }\n\n /**\n * Stops replication slots associated with this shard, and asynchronously\n * runs a cleanup task that drops older replicas and slots.\n */\n async #stopExistingReplicationSlotSubscriber() {\n const sql = this.#db;\n const {id: replicaID, slot} = this.#replica;\n const replicasTable = `${upstreamSchema(this.#shard)}.replicas`;\n\n const result = await sql`\n SELECT pg_terminate_backend(active_pid) as terminated, active_pid as pid\n FROM pg_replication_slots\n WHERE slot_name = ${slot}\n `;\n if (result.length === 0) {\n const slotExpression = replicationSlotPrefix(this.#shard);\n const replicas = await sql`\n SELECT id, rank, slot, version, \"initialSyncContext\", \"subscriberContext\" \n FROM ${sql(replicasTable)} ORDER BY rank DESC`;\n const slots = await sql`\n SELECT slot_name as slot, active, active_pid as pid\n FROM pg_replication_slots\n WHERE slot_name LIKE ${slotExpression}\n ORDER BY slot_name`;\n this.#lc.warn?.(`slot ${slot} not found while cleaning subscribers`, {\n slots,\n replicas,\n });\n throw new AbortError(\n `replication slot ${slot} is missing. A different ` +\n `replication-manager should now be running on a new ` +\n `replication slot.`,\n );\n }\n this.#lc.info?.(`terminated replication slots: ${JSON.stringify(result)}`);\n await sql`\n UPDATE ${sql(replicasTable)} \n SET \"subscriberContext\" = ${this.#context}\n WHERE id = ${replicaID}`;\n void this.#cleanUpOlderReplicasAndSlots();\n }\n\n #cleanupTimer: NodeJS.Timeout | undefined;\n\n async #cleanUpOlderReplicasAndSlots() {\n clearTimeout(this.#cleanupTimer);\n\n try {\n const result = await dropOldReplicasAndSlots(\n this.#lc,\n this.#db,\n this.#shard,\n this.#replica.rank,\n );\n if (result.draining === 0) {\n this.#lc.info?.(`finished cleaning up replicas and slots`, {result});\n return;\n }\n this.#lc.info?.(`old slots still draining`, {result});\n } catch (e) {\n this.#lc.warn?.(`error dropping replication slots`, e);\n }\n\n this.#cleanupTimer = setTimeout(\n () => this.#cleanUpOlderReplicasAndSlots(),\n REPLICA_SLOT_CLEANUP_INTERVAL_MS,\n );\n }\n}\n\n// Exported for testing.\nexport class Acker implements Listener {\n #acks: Sink<bigint>;\n #waitingForDownstreamAck: string | null = null;\n\n constructor(acks: Sink<bigint>) {\n this.#acks = acks;\n }\n\n onChange(change: ChangeStreamMessage): void {\n switch (change[0]) {\n case 'status':\n const {watermark} = change[2];\n if (change[1].ack) {\n this.#expectDownstreamAck(watermark);\n } else {\n // Keepalives with shouldRespond = false are sent to Listeners,\n // but for efficiency they are not sent downstream to the\n // change-streamer. Ack them here if the change-streamer is caught\n // up. This updates the replication slot's `confirmed_flush_lsn`\n // more quickly (rather than waiting for the periodic shouldRespond),\n // which is useful for monitoring replication slot lag.\n this.#ackIfDownstreamIsCaughtUp(watermark);\n }\n break;\n case 'begin':\n // Mark the commit watermark as being expected so that any intermediate\n // shouldRespond=false watermarks, which will be at the\n // commitWatermark, are *not* acked, as the ack must come from\n // change-streamer after it commits the transaction.\n if (!change[1].skipAck) {\n this.#expectDownstreamAck(change[2].commitWatermark);\n }\n break;\n }\n }\n\n #expectDownstreamAck(watermark: string) {\n this.#waitingForDownstreamAck = watermark;\n }\n\n ack(watermark: LexiVersion) {\n if (\n this.#waitingForDownstreamAck &&\n this.#waitingForDownstreamAck <= watermark\n ) {\n this.#waitingForDownstreamAck = null;\n }\n this.#sendAck(watermark);\n }\n\n #ackIfDownstreamIsCaughtUp(watermark: string) {\n if (this.#waitingForDownstreamAck === null) {\n this.#sendAck(watermark);\n }\n }\n\n #sendAck(watermark: LexiVersion) {\n const lsn = majorVersionFromString(watermark);\n this.#acks.push(lsn);\n }\n}\n\nconst lagReportSchema = v.object({\n id: v.string(),\n sendTimeMs: v.number(),\n commitTimeMs: v.number(),\n});\n\nexport type LagReport = v.Infer<typeof lagReportSchema>;\n\ntype InitiatedLagReport = LagReport & {lsn: bigint};\n\nexport class LagReporter {\n static readonly MESSAGE_SUFFIX = '/lag-report/v1';\n\n readonly #lc: LogContext;\n readonly messagePrefix: string;\n\n // oxlint-disable-next-line no-unused-private-class-members\n readonly #db: PostgresDB;\n readonly #lagIntervalMs: number;\n readonly #lagReportRetries = getOrCreateCounter(\n 'replication',\n 'lag_report_retries',\n {\n description:\n 'Number of replication lag reports retried because the expected ' +\n 'report was not received before the next report interval.',\n unit: '{report}',\n },\n );\n\n #pgVersion: number | undefined;\n #expectingLagReport: InitiatedLagReport | null = null;\n #timer: NodeJS.Timeout | undefined;\n\n constructor(\n lc: LogContext,\n shard: ShardID,\n db: PostgresDB,\n lagIntervalMs: number,\n ) {\n this.#lc = lc;\n this.messagePrefix = `${shard.appID}/${shard.shardNum}${LagReporter.MESSAGE_SUFFIX}`;\n this.#db = db;\n this.#lagIntervalMs = lagIntervalMs;\n }\n\n async #getPgVersion() {\n if (this.#pgVersion === undefined) {\n const [{pgVersion}] = await this.#db<{pgVersion: number}[]> /*sql*/ `\n SELECT current_setting('server_version_num')::int as \"pgVersion\"`;\n this.#pgVersion = pgVersion;\n }\n return this.#pgVersion;\n }\n\n get pgVersion() {\n return this.#pgVersion;\n }\n\n async initiateLagReport(log = false) {\n const pgVersion = this.#pgVersion ?? (await this.#getPgVersion());\n const now = Date.now();\n const id = nanoid();\n\n // lsn is filled in after the db call.\n const lagReport = {id, sendTimeMs: now, commitTimeMs: now, lsn: 0n};\n this.#expectingLagReport = lagReport;\n\n let commitTimeMs: number;\n let lsn: string;\n\n try {\n if (pgVersion >= PG_17) {\n [{commitTimeMs, lsn}] = await this.#db /*sql*/ `\n WITH CTE AS (SELECT extract(epoch from now()) * 1000 AS \"commitTimeMs\")\n SELECT \"commitTimeMs\", pg_logical_emit_message(\n false,\n ${this.messagePrefix},\n json_build_object(\n 'id', ${id}::text,\n 'sendTimeMs', ${now}::int8,\n 'commitTimeMs', \"commitTimeMs\"\n )::text,\n true\n ) as lsn FROM CTE;\n `;\n } else {\n // Versions before PG 17 do not support the final `flush` option of\n // pg_logical_emit_message(). This results in an extra 50~100ms latency\n // for replication reports when the db is idle, which is still\n // acceptable for the purpose for alerting on pathological lag, for\n // which the threshold is much higher (e.g. many seconds).\n [{commitTimeMs, lsn}] = await this.#db /*sql*/ `\n WITH CTE AS (SELECT extract(epoch from now()) * 1000 as \"commitTimeMs\")\n SELECT \"commitTimeMs\", pg_logical_emit_message(\n false,\n ${this.messagePrefix},\n json_build_object(\n 'id', ${id}::text,\n 'sendTimeMs', ${now}::int8,\n 'commitTimeMs', \"commitTimeMs\"\n )::text\n ) as lsn FROM CTE;\n `;\n }\n } catch (e) {\n if (this.#expectingLagReport?.id === id) {\n this.#expectingLagReport = null;\n }\n throw e;\n }\n\n // Note: We don't know the lsn until after pg_logical_emit_message()\n // returns, at which point it is possible that the report has\n // already been sent through the replication stream, but this\n // is okay since this.#expectingLagReport will have be updated.\n lagReport.lsn = toBigInt(lsn);\n lagReport.commitTimeMs = commitTimeMs;\n if (this.#expectingLagReport?.id === id) {\n this.#scheduleMissingReportRetry(id);\n }\n\n if (log) {\n this.#lc.info?.(`initiated lag report at lsn ${lsn}`, {\n id,\n lsn,\n sendTimeMs: now,\n commitTimeMs,\n });\n }\n return {nextSendTimeMs: now};\n }\n\n /**\n * In Postgres < 17, the pg_logical_emit_message lacks an immediate \"flush\"\n * option, which can cause messages to be missed when the replication stream\n * starts up:\n *\n * ```\n * * emit message → WAL write (buffered, not flushed)\n * * walsender reads up to current flush LSN\n * * emitted message's LSN is beyond flush LSN → not yet visible\n * * stream feedback/acknowledgment advances slot\n * * WAL eventually flushes → but slot has already moved past it\n * ```\n *\n * This has been seen to happen for the initial `wal_writer_delay` interval\n * of a replication session.\n *\n * To account for this, the last emitted lag report is considered \"received\"\n * if the stream has advanced beyond the LSN of the report.\n */\n checkCurrentLSN(lsn: bigint): DownstreamStatusMessage | undefined {\n if (this.#expectingLagReport?.lsn && lsn > this.#expectingLagReport.lsn) {\n this.#lc.info?.(\n `LSN ${fromBigInt(lsn)} is passed expected lag report ` +\n `${fromBigInt(this.#expectingLagReport.lsn)}. Processing it as received.`,\n );\n return this.#processLagReport(\n this.#expectingLagReport,\n majorVersionToString(lsn),\n );\n }\n return undefined;\n }\n\n stop() {\n clearTimeout(this.#timer);\n this.#timer = undefined;\n }\n\n #scheduleNextReport(delayMs: number) {\n this.#expectingLagReport = null;\n clearTimeout(this.#timer);\n this.#timer = setTimeout(async () => {\n this.#timer = undefined;\n try {\n await this.initiateLagReport();\n } catch (e) {\n this.#lc.warn?.(`error initiating lag report`, e);\n this.#scheduleNextReport(this.#lagIntervalMs);\n }\n }, delayMs);\n }\n\n #scheduleMissingReportRetry(reportID: string) {\n clearTimeout(this.#timer);\n this.#timer = setTimeout(async () => {\n this.#timer = undefined;\n const missingReport = this.#expectingLagReport;\n if (missingReport?.id !== reportID) {\n return;\n }\n\n this.#lagReportRetries.add(1);\n this.#lc.warn?.(`retrying missing lag report`, {\n id: missingReport.id,\n lsn: fromBigInt(missingReport.lsn),\n sendTimeMs: missingReport.sendTimeMs,\n });\n try {\n await this.initiateLagReport();\n } catch (e) {\n this.#lc.warn?.(`error retrying lag report`, e);\n this.#scheduleNextReport(this.#lagIntervalMs);\n }\n }, this.#lagIntervalMs);\n }\n\n processLagReportMessage(msg: MessageMessage): DownstreamStatusMessage {\n assert(\n msg.prefix === this.messagePrefix,\n `unexpected message prefix: ${msg.prefix}`,\n );\n const report = parseLogicalMessageContent(this.#lc, msg, lagReportSchema);\n return this.#processLagReport(\n report,\n toStateVersionString(msg.messageLsn ?? '0/0'),\n );\n }\n\n #processLagReport(\n report: LagReport,\n watermark: string,\n ): DownstreamStatusMessage {\n const now = Date.now();\n const expectedReport = this.#expectingLagReport;\n let nextSendTimeMs: number;\n if (report.id === expectedReport?.id) {\n nextSendTimeMs = Math.max(now, report.sendTimeMs + this.#lagIntervalMs);\n this.#scheduleNextReport(nextSendTimeMs - now);\n } else {\n // Only schedule the next report when receiving the previous report.\n // For historic reports in the WAL, or reports generated by other\n // replication-managers, status messages are still sent downstream,\n // but the next report is not actually scheduled.\n this.#lc.debug?.(`received extraneous lag report`, {report});\n nextSendTimeMs =\n expectedReport?.sendTimeMs ??\n Math.max(now, report.sendTimeMs + this.#lagIntervalMs);\n }\n const {sendTimeMs, commitTimeMs} = report;\n return [\n 'status',\n {\n ack: false,\n lagReport: {\n lastTimings: {\n sendTimeMs,\n commitTimeMs,\n receiveTimeMs: now,\n },\n nextSendTimeMs,\n },\n },\n {watermark},\n ];\n }\n}\n\ntype ReplicationError = {\n lsn: bigint;\n msg: Message;\n err: unknown;\n lastLogTime: number;\n};\n\nconst SET_REPLICA_IDENTITY_DELAY_MS = 50;\n\nclass ChangeMaker {\n readonly #shardPrefix: string;\n readonly #shardConfig: InternalShardConfig;\n readonly #initialSchema: PublishedSchema;\n readonly #db: PostgresDB;\n\n #replicaIdentityTimer: NodeJS.Timeout | undefined;\n #error: ReplicationError | undefined;\n\n constructor(\n {appID, shardNum}: ShardID,\n shardConfig: InternalShardConfig,\n db: PostgresDB,\n initialSchema: PublishedSchema,\n ) {\n // Note: This matches the prefix used in pg_logical_emit_message() in pg/schema/ddl.ts.\n this.#shardPrefix = `${appID}/${shardNum}`;\n this.#shardConfig = shardConfig;\n this.#initialSchema = initialSchema;\n this.#db = db;\n }\n\n async makeChanges(\n lc: LogContext,\n lsn: bigint,\n msg: Message,\n ): Promise<ChangeStreamMessage[]> {\n if (this.#error) {\n this.#logError(lc, this.#error);\n return [];\n }\n try {\n return await this.#makeChanges(lc, lsn, msg);\n } catch (err) {\n this.#error = {lsn, msg, err, lastLogTime: 0};\n this.#logError(lc, this.#error);\n\n const message = `Unable to continue replication from LSN ${fromBigInt(lsn)}`;\n const errorDetails: JSONObject = {error: message};\n if (err instanceof UnsupportedSchemaChangeError) {\n errorDetails.reason = err.description;\n errorDetails.context = err.event.context;\n } else {\n errorDetails.reason = String(err);\n }\n\n // Rollback the current transaction to avoid dangling transactions in\n // downstream processors (i.e. changeLog, replicator).\n return [\n ['rollback', {tag: 'rollback'}],\n ['control', {tag: 'reset-required', message, errorDetails}],\n ];\n }\n }\n\n #logError(lc: LogContext, error: ReplicationError) {\n const {lsn, msg, err, lastLogTime} = error;\n const now = Date.now();\n\n // Output an error to logs as replication messages continue to be dropped,\n // at most once a minute.\n if (now - lastLogTime > 60_000) {\n lc.error?.(\n `Unable to continue replication from LSN ${fromBigInt(lsn)}: ${String(\n err,\n )}`,\n {\n // 'content' can be a large byte Buffer. Exclude it from logging output.\n ...{change: {...msg, content: undefined}},\n ...(err instanceof UnsupportedSchemaChangeError && {\n context: err.event.context,\n }),\n ...(err instanceof Error && {\n errorMsg: err.message,\n name: err.name,\n stack: err.stack,\n }),\n },\n );\n error.lastLogTime = now;\n }\n }\n\n async #makeChanges(\n lc: LogContext,\n lsn: bigint,\n msg: Message,\n ): Promise<ChangeStreamData[]> {\n switch (msg.tag) {\n case 'begin':\n return [\n [\n 'begin',\n {...msg, json: 's'},\n {commitWatermark: toStateVersionString(must(msg.commitLsn))},\n ],\n ];\n\n case 'delete': {\n if (!(msg.key ?? msg.old)) {\n throw new Error(\n `Invalid DELETE msg (missing key): ${stringify(msg)}`,\n );\n }\n return [\n [\n 'data',\n {\n ...msg,\n relation: makeRelation(msg.relation),\n // https://www.postgresql.org/docs/current/protocol-logicalrep-message-formats.html#PROTOCOL-LOGICALREP-MESSAGE-FORMATS-DELETE\n key: must(msg.old ?? msg.key),\n },\n ],\n ];\n }\n\n case 'update': {\n return [\n [\n 'data',\n {\n ...msg,\n relation: makeRelation(msg.relation),\n // https://www.postgresql.org/docs/current/protocol-logicalrep-message-formats.html#PROTOCOL-LOGICALREP-MESSAGE-FORMATS-UPDATE\n key: msg.old ?? msg.key,\n },\n ],\n ];\n }\n\n case 'insert':\n return [['data', {...msg, relation: makeRelation(msg.relation)}]];\n case 'truncate':\n return [['data', {...msg, relations: msg.relations.map(makeRelation)}]];\n\n case 'message':\n if (!msg.prefix.startsWith(this.#shardPrefix)) {\n lc.debug?.('ignoring message for different shard', msg.prefix);\n return [];\n }\n switch (msg.prefix.substring(this.#shardPrefix.length)) {\n case '': // Legacy prefix\n case '/ddl':\n return this.#handleDdlMessage(lc, lsn, msg);\n default:\n lc.debug?.('ignoring unknown message type', msg.prefix);\n return [];\n }\n\n case 'commit':\n return [\n [\n 'commit',\n // `commitTime` is microseconds since the unix epoch. Carrying it\n // as milliseconds gives the ViewSyncer the origin timestamp for\n // the end-to-end serving lag histogram.\n {...msg, commitTimeMs: Number(msg.commitTime / 1000n)},\n {watermark: toStateVersionString(must(msg.commitLsn))},\n ],\n ];\n\n case 'relation':\n return await this.#handleRelation(msg);\n case 'type':\n return []; // Nothing need be done for custom types.\n case 'origin':\n // No need to detect replication loops since we are not a\n // PG replication source.\n return [];\n default:\n msg satisfies never;\n throw new Error(`Unexpected message type ${stringify(msg)}`);\n }\n }\n\n // The lastReplicationEvent is stored to understand the context\n // of the next one.\n #lastReplicationEvent: ReplicationEvent | undefined;\n\n #handleDdlMessage(\n lc: LogContext,\n lsn: bigint,\n msg: MessageMessage,\n ): ChangeStreamData[] {\n const event = parseLogicalMessageContent(lc, msg, replicationEventSchema);\n lc = lc\n .withContext('lsn', fromBigInt(lsn))\n .withContext('tag', event.event.tag)\n .withContext('query', event.context.query);\n\n // Cancel manual schema adjustment timeouts when an upstream schema change\n // is about to happen, so as to avoid interfering / redundant work.\n clearTimeout(this.#replicaIdentityTimer);\n\n const {type} = event;\n switch (type) {\n case 'ddlStart':\n case 'schemaSnapshot':\n case 'ddlUpdate':\n break;\n default: // Ignore unknown types for forwards compatibility\n lc.info?.(`ignoring unknown ddl message type: ${type}`);\n return [];\n }\n\n const prevEvent = this.#lastReplicationEvent;\n // Store the new event to understand the context of the next event.\n this.#lastReplicationEvent = event;\n\n const {schema} = event;\n if (schema === undefined) {\n // A schema-less (protocol v2) ddlStart event signifies that there was\n // no schema change; it is stored (above) only to provide the command\n // tag context for a subsequent event with a schema change.\n lc.debug?.(`received context-only ${msg.prefix}/${type} event`, {\n event: summarizeReplicationEventForLog(event),\n });\n return [];\n }\n\n const prevSchema =\n event.previousSchema === undefined // pre-v21 event => use prevEvent\n ? prevEvent?.schema\n : event.previousSchema;\n if (!prevSchema) {\n lc.info?.(`received ${msg.prefix}/${type} event`, {\n event: summarizeReplicationEventForLog(event),\n });\n return [];\n }\n\n // The tag (i.e. command) is used as an optimization to determine whether\n // backfill is necessary (CREATE TABLE vs ALTER TABLE vs\n // ALTER PUBLICATION). If the context is not available (rare), the tag\n // falls back to 'UNKNOWN', which conservatively initiates a backfill.\n //\n // Because ddl events may be nested, e.g.\n //\n // ```\n // [1] [2] [3] [4] [5]\n // ddl_start => ddl_start => ddl_end => ddl_start => ddl_end => ddl_end\n // ```\n //\n // The effective tag is determined from the previous event if it is\n // ddl_start (e.g. cases 1, 2, and 4), and from the current event\n // if it is a ddl_end (case 5), and 'UNKNOWN' otherwise (case 3 and\n // 'schemaSnapshot' workarounds).\n const effectiveTag =\n prevEvent?.type === 'ddlStart'\n ? prevEvent.event.tag\n : event.type === 'ddlUpdate'\n ? event.event.tag\n : 'UNKNOWN';\n lc.info?.(`processing ${effectiveTag} command from ${msg.prefix}/${type}`, {\n event: summarizeReplicationEventForLog(event),\n });\n const changes = this.#makeSchemaChanges(\n lc,\n prevSchema,\n schema,\n event,\n effectiveTag,\n ).map(change => ['data', change] satisfies Data);\n\n lc.info?.(`${changes.length} schema change(s)`, {changes});\n\n const replicaIdentities =\n replicaIdentitiesForTablesWithoutPrimaryKeys(schema);\n if (replicaIdentities) {\n this.#replicaIdentityTimer = setTimeout(async () => {\n try {\n await replicaIdentities.apply(lc, this.#db);\n } catch (err) {\n lc.warn?.(`error setting replica identities`, err);\n }\n }, SET_REPLICA_IDENTITY_DELAY_MS);\n }\n\n return changes;\n }\n\n /**\n * A note on operation order:\n *\n * Postgres will drop related indexes when columns are dropped,\n * but SQLite will error instead (https://sqlite.org/forum/forumpost/2e62dba69f?t=c&hist).\n * The current workaround is to drop indexes first.\n *\n * Also note that although it should not be possible to both rename and\n * add/drop tables/columns in a single statement, the operations are\n * ordered to handle that possibility, by always dropping old entities,\n * then modifying kept entities, and then adding new entities.\n *\n * Thus, the order of replicating DDL updates is:\n * - drop indexes\n * - drop tables\n * - alter tables\n * - drop columns\n * - alter columns\n * - add columns\n * - create tables\n * - create indexes\n *\n * In the future the replication logic should be improved to handle this\n * behavior in SQLite by dropping dependent indexes manually before dropping\n * columns. This, for example, would be needed to properly support changing\n * the type of a column that's indexed.\n */\n #makeSchemaChanges(\n lc: LogContext,\n preSchema: PublishedSchema,\n nextSchema: PublishedSchema,\n event: ReplicationEvent,\n tag: string,\n ): SchemaChange[] {\n try {\n const [prevTbl, prevIdx] = specsByID(preSchema);\n const [nextTbl, nextIdx] = specsByID(nextSchema);\n const changes: SchemaChange[] = [];\n\n // Validate the new table schemas\n for (const table of nextTbl.values()) {\n validate(lc, table);\n }\n\n const [droppedIdx, createdIdx] = symmetricDifferences(prevIdx, nextIdx);\n\n // Detect modified indexes (same name, different definition).\n // This happens when a constraint is dropped and recreated with the\n // same name in a single ALTER TABLE statement.\n // Note: We compare using stable column attnums rather than names,\n // because table/column renames change the index spec cosmetically\n // (tableName, column keys) without the index actually being recreated.\n const keptIdx = intersection(prevIdx, nextIdx);\n for (const id of keptIdx) {\n if (\n isIndexStructurallyChanged(\n must(prevIdx.get(id)),\n must(nextIdx.get(id)),\n prevTbl,\n nextTbl,\n )\n ) {\n droppedIdx.add(id);\n createdIdx.add(id);\n }\n }\n\n for (const id of droppedIdx) {\n const {schema, name} = must(prevIdx.get(id));\n changes.push({tag: 'drop-index', id: {schema, name}});\n }\n\n // DROP\n const [droppedTbl, createdTbl] = symmetricDifferences(prevTbl, nextTbl);\n for (const id of droppedTbl) {\n const {schema, name} = must(prevTbl.get(id));\n changes.push({tag: 'drop-table', id: {schema, name}});\n }\n // ALTER TABLE | ALTER PUBLICATION\n const tables = intersection(prevTbl, nextTbl);\n for (const id of tables) {\n changes.push(\n ...this.#getTableChanges(\n lc,\n must(prevTbl.get(id)),\n must(nextTbl.get(id)),\n tag,\n ),\n );\n }\n // CREATE\n for (const id of createdTbl) {\n const spec = must(nextTbl.get(id));\n const createTable: TableCreate = {\n tag: 'create-table',\n spec,\n metadata: getMetadata(spec),\n };\n // Only tables introduced by a `CREATE` statement can skip backfill.\n // All other scenarios in which tables are introduced into the\n // schema, e.g.\n // * ALTER PUBLICATION statements\n // * COMMENT statements\n // * MANUAL snapshots\n // * UNKNOWN command tags\n // must be backfilled.\n if (!tag.startsWith('CREATE')) {\n createTable.backfill = mapValues(spec.columns, ({pos: attNum}) => ({\n attNum,\n })) satisfies Record<string, ColumnMetadata>;\n }\n changes.push(createTable);\n }\n\n // Add indexes last since they may reference tables / columns that need\n // to be created first.\n for (const id of createdIdx) {\n const spec = must(nextIdx.get(id));\n changes.push({tag: 'create-index', spec});\n }\n return changes;\n } catch (e) {\n throw new UnsupportedSchemaChangeError(String(e), event, {cause: e});\n }\n }\n\n #getTableChanges(\n lc: LogContext,\n oldTable: PublishedTableWithReplicaIdentity,\n newTable: PublishedTableWithReplicaIdentity,\n ddlTag: string,\n ): SchemaChange[] {\n const changes: SchemaChange[] = [];\n if (\n oldTable.schema !== newTable.schema ||\n oldTable.name !== newTable.name\n ) {\n changes.push({\n tag: 'rename-table',\n old: {schema: oldTable.schema, name: oldTable.name},\n new: {schema: newTable.schema, name: newTable.name},\n });\n }\n const oldMetadata = getMetadata(oldTable);\n const newMetadata = getMetadata(newTable);\n if (!deepEqual(oldMetadata, newMetadata)) {\n changes.push({\n tag: 'update-table-metadata',\n table: {schema: newTable.schema, name: newTable.name},\n old: oldMetadata,\n new: newMetadata,\n });\n }\n const table = {schema: newTable.schema, name: newTable.name};\n const oldColumns = columnsByID(oldTable.columns);\n const newColumns = columnsByID(newTable.columns);\n\n // DROP\n const [dropped, added] = symmetricDifferences(oldColumns, newColumns);\n for (const id of dropped) {\n const {name: column} = must(oldColumns.get(id));\n changes.push({tag: 'drop-column', table, column});\n }\n\n // ALTER\n const both = intersection(oldColumns, newColumns);\n for (const id of both) {\n const {name: oldName, ...oldSpec} = must(oldColumns.get(id));\n const {name: newName, ...newSpec} = must(newColumns.get(id));\n // The three things that we care about are:\n // 1. name\n // 2. type\n // 3. not-null\n if (\n oldName !== newName ||\n oldSpec.dataType !== newSpec.dataType ||\n oldSpec.notNull !== newSpec.notNull\n ) {\n changes.push({\n tag: 'update-column',\n table,\n old: {name: oldName, spec: oldSpec},\n new: {name: newName, spec: newSpec},\n });\n }\n }\n\n // Only columns introduced by `ALTER TABLE` statements can potentially\n // skip backfill if they have non-constant defaults. All other scenarios\n // in which columns are introduced, e.g.\n // * ALTER PUBLICATION\n // * COMMENT\n // * MANUAL\n // * UNKNOWN\n // must be backfilled.\n const alwaysBackfill = ddlTag !== 'ALTER TABLE';\n\n // ADD\n for (const id of added) {\n const {name, ...spec} = must(newColumns.get(id));\n const column = {name, spec};\n const addColumn: ColumnAdd = {\n tag: 'add-column',\n table,\n column,\n tableMetadata: getMetadata(newTable),\n };\n if (alwaysBackfill) {\n addColumn.column.spec.dflt = null;\n addColumn.backfill = {attNum: spec.pos} satisfies ColumnMetadata;\n } else {\n // Determine if the ChangeProcessor will accept the column add as is.\n try {\n mapPostgresToLiteColumn(table.name, column);\n } catch (e) {\n if (!(e instanceof UnsupportedColumnDefaultError)) {\n // Note: mapPostgresToLiteColumn is not expected to throw any other\n // types of errors.\n throw e;\n }\n // If the column has an unsupported default (e.g. an expression or a\n // generated value), create the column as initially hidden with a\n // `null` default, and publish it after backfilling the values from\n // upstream. Note that this does require that the table have a valid\n // REPLICA IDENTITY, since backfill relies on merging new data with\n // an existing row.\n lc.info?.(`Backfilling column ${table.name}.${name}: ${String(e)}`);\n addColumn.column.spec.dflt = null;\n addColumn.backfill = {attNum: spec.pos} satisfies ColumnMetadata;\n }\n }\n changes.push(addColumn);\n }\n return changes;\n }\n\n /**\n * If `ddlDetection === true`, relation messages are irrelevant,\n * as schema changes are detected by event triggers that\n * emit custom messages.\n *\n * For degraded-mode replication (`ddlDetection === false`):\n * 1. query the current published schemas on upstream\n * 2. compare that with the InternalShardConfig.initialSchema\n * 3. compare that with the incoming MessageRelation\n * 4. On any discrepancy, throw an UnsupportedSchemaChangeError\n * to halt replication.\n *\n * Note that schemas queried in step [1] will be *post-transaction*\n * schemas, which are not necessarily suitable for actually processing\n * the statements in the transaction being replicated. In other words,\n * this mechanism cannot be used to reliably *replicate* schema changes.\n * However, they serve the purpose determining if schemas have changed.\n */\n async #handleRelation(rel: PostgresRelation): Promise<ChangeStreamData[]> {\n const {publications, ddlDetection} = this.#shardConfig;\n if (ddlDetection) {\n return [];\n }\n const currentSchema = await getPublicationInfo(this.#db, publications);\n const difference = getSchemaDifference(this.#initialSchema, currentSchema);\n if (difference !== null) {\n throw new MissingEventTriggerSupport(difference);\n }\n // Even if the currentSchema is equal to the initialSchema, the\n // MessageRelation itself must be checked to detect transient\n // schema changes within the transaction (e.g. adding and dropping\n // a table, or renaming a column and then renaming it back).\n const orel = this.#initialSchema.tables.find(\n t => t.oid === rel.relationOid,\n );\n if (!orel) {\n // Can happen if a table is created and then dropped in the same transaction.\n throw new MissingEventTriggerSupport(\n `relation not in initialSchema: ${stringify(rel)}`,\n );\n }\n if (relationDifferent(orel, rel)) {\n throw new MissingEventTriggerSupport(\n `relation has changed within the transaction: ${stringify(orel)} vs ${stringify(rel)}`,\n );\n }\n return [];\n }\n}\n\nfunction getSchemaDifference(\n a: PublishedSchema,\n b: PublishedSchema,\n): string | null {\n // Note: ignore indexes since changes need not to halt replication\n if (a.tables.length !== b.tables.length) {\n return `tables created or dropped`;\n }\n for (let i = 0; i < a.tables.length; i++) {\n const at = a.tables[i];\n const bt = b.tables[i];\n const difference = getTableDifference(at, bt);\n if (difference) {\n return difference;\n }\n }\n return null;\n}\n\n// ColumnSpec comparator\nconst byColumnPos = (a: [string, ColumnSpec], b: [string, ColumnSpec]) =>\n a[1].pos < b[1].pos ? -1 : a[1].pos > b[1].pos ? 1 : 0;\n\nfunction getTableDifference(\n a: PublishedTableSpec,\n b: PublishedTableSpec,\n): string | null {\n if (a.oid !== b.oid || a.schema !== b.schema || a.name !== b.name) {\n return `Table \"${a.name}\" differs from table \"${b.name}\"`;\n }\n if (!deepEqual(a.primaryKey, b.primaryKey)) {\n return `Primary key of table \"${a.name}\" has changed`;\n }\n const acols = Object.entries(a.columns).sort(byColumnPos);\n const bcols = Object.entries(b.columns).sort(byColumnPos);\n if (\n acols.length !== bcols.length ||\n acols.some(([aname, acol], i) => {\n const [bname, bcol] = bcols[i];\n return (\n aname !== bname ||\n acol.pos !== bcol.pos ||\n acol.typeOID !== bcol.typeOID ||\n acol.notNull !== bcol.notNull\n );\n })\n ) {\n return `Columns of table \"${a.name}\" have changed`;\n }\n return null;\n}\n\nexport function relationDifferent(a: PublishedTableSpec, b: PostgresRelation) {\n if (a.oid !== b.relationOid || a.schema !== b.schema || a.name !== b.name) {\n return true;\n }\n if (\n // The MessageRelation's `keyColumns` field contains the columns in column\n // declaration order, whereas the PublishedTableSpec's `primaryKey`\n // contains the columns in primary key (i.e. index) order. Do an\n // order-agnostic compare here since it is not possible to detect\n // key-order changes from the MessageRelation message alone.\n b.replicaIdentity === 'default' &&\n !equals(new Set(a.primaryKey), new Set(b.keyColumns))\n ) {\n return true;\n }\n const acols = Object.entries(a.columns).sort(byColumnPos);\n const bcols = b.columns;\n return (\n acols.length !== bcols.length ||\n acols.some(([aname, acol], i) => {\n const bcol = bcols[i];\n return aname !== bcol.name || acol.typeOID !== bcol.typeOid;\n })\n );\n}\n\nfunction translateError(e: unknown): Error {\n if (!(e instanceof Error)) {\n return new Error(String(e));\n }\n if (e instanceof postgres.PostgresError && e.code === PG_ADMIN_SHUTDOWN) {\n return new ShutdownSignal(e);\n }\n return e;\n}\nconst idString = (id: Identifier) => `${id.schema}.${id.name}`;\n\nfunction specsByID(published: PublishedSchema) {\n return [\n // It would have been nice to use a CustomKeyMap here, but we rely on set-utils\n // operations which use plain Sets.\n new Map(published.tables.map(t => [t.oid, t])),\n new Map(published.indexes.map(i => [idString(i), i])),\n ] as const;\n}\n\n/**\n * Determines if an index was structurally changed (e.g. constraint dropped\n * and recreated with different columns) vs cosmetically changed (e.g. the\n * index spec changed because the table or a column was renamed).\n *\n * Compares boolean properties directly and resolves column names to their\n * stable attnums (pg_attribute `attnum`) for the column comparison.\n */\nfunction isIndexStructurallyChanged(\n prev: PublishedIndexSpec,\n next: PublishedIndexSpec,\n prevTables: Map<number, PublishedTableWithReplicaIdentity>,\n nextTables: Map<number, PublishedTableWithReplicaIdentity>,\n): boolean {\n if (\n prev.unique !== next.unique ||\n prev.isPrimaryKey !== next.isPrimaryKey ||\n prev.isReplicaIdentity !== next.isReplicaIdentity ||\n prev.isImmediate !== next.isImmediate\n ) {\n return true;\n }\n\n const prevTable = findTableBySchemaAndName(\n prevTables,\n prev.schema,\n prev.tableName,\n );\n const nextTable = findTableBySchemaAndName(\n nextTables,\n next.schema,\n next.tableName,\n );\n if (!prevTable || !nextTable) {\n // Can't resolve tables; conservatively treat as changed.\n return true;\n }\n\n const prevEntries = Object.entries(prev.columns);\n const nextEntries = Object.entries(next.columns);\n if (prevEntries.length !== nextEntries.length) {\n return true;\n }\n\n // Resolve column names → attnums and compare.\n const prevByAttnum = new Map<number | undefined, string>(\n prevEntries.map(([name, dir]) => [prevTable.columns[name]?.pos, dir]),\n );\n const nextByAttnum = new Map<number | undefined, string>(\n nextEntries.map(([name, dir]) => [nextTable.columns[name]?.pos, dir]),\n );\n\n if (prevByAttnum.has(undefined) || nextByAttnum.has(undefined)) {\n // Column not found in table spec; conservatively treat as changed.\n return true;\n }\n if (prevByAttnum.size !== nextByAttnum.size) {\n return true;\n }\n for (const [attnum, dir] of prevByAttnum) {\n if (nextByAttnum.get(attnum) !== dir) {\n return true;\n }\n }\n return false;\n}\n\nfunction findTableBySchemaAndName(\n tables: Map<number, PublishedTableWithReplicaIdentity>,\n schema: string,\n name: string,\n): PublishedTableWithReplicaIdentity | undefined {\n for (const table of tables.values()) {\n if (table.schema === schema && table.name === name) {\n return table;\n }\n }\n return undefined;\n}\n\nfunction columnsByID(\n columns: Record<string, ColumnSpec>,\n): Map<number, ColumnSpec & {name: string}> {\n const colsByID = new Map<number, ColumnSpec & {name: string}>();\n for (const [name, spec] of Object.entries(columns)) {\n // The `pos` field is the `attnum` in `pg_attribute`, which is a stable\n // identifier for the column in this table (i.e. never reused).\n colsByID.set(spec.pos, {...spec, name});\n }\n return colsByID;\n}\n\nfunction getMetadata(table: PublishedTableWithReplicaIdentity): TableMetadata {\n return {\n schemaOID: must(table.schemaOID),\n relationOID: table.oid,\n rowKey: Object.fromEntries(\n table.replicaIdentityColumns.map(k => [\n k,\n {attNum: table.columns[k].pos},\n ]),\n ),\n };\n}\n\n// Avoid sending the `columns` from the Postgres MessageRelation message.\n// They are not used downstream and the message can be large.\nfunction makeRelation(relation: PostgresRelation): MessageRelation {\n // Avoid sending the `columns` from the Postgres MessageRelation message.\n // They are not used downstream and the message can be large.\n const {columns: _, keyColumns, replicaIdentity, ...rest} = relation;\n return {\n ...rest,\n rowKey: {\n columns: keyColumns,\n type: replicaIdentity,\n },\n // For now, deprecated columns are sent for backwards compatibility.\n // These can be removed when bumping the MIN_PROTOCOL_VERSION to 5.\n keyColumns,\n replicaIdentity,\n };\n}\n\nfunction summarizeSchemaForLog(schema: PublishedSchema) {\n return {\n tables: schema.tables.length,\n indexes: schema.indexes.length,\n };\n}\n\nfunction summarizeReplicationEventForLog(event: ReplicationEvent): JSONObject {\n const {schema, ...rest} = event;\n const {previousSchema, ...eventWithoutSchemas} = rest;\n return {\n ...eventWithoutSchemas,\n ...(schema !== undefined && {schema: summarizeSchemaForLog(schema)}),\n ...(previousSchema !== undefined && {\n previousSchema:\n previousSchema === null ? null : summarizeSchemaForLog(previousSchema),\n }),\n };\n}\n\nclass UnsupportedSchemaChangeError extends Error {\n readonly name = 'UnsupportedSchemaChangeError';\n readonly description: string;\n readonly event: ReplicationEvent;\n\n constructor(\n description: string,\n event: ReplicationEvent,\n options?: ErrorOptions,\n ) {\n super(\n `Replication halted. Resync the replica to recover: ${description}`,\n options,\n );\n this.description = description;\n this.event = event;\n }\n}\n\nclass MissingEventTriggerSupport extends Error {\n readonly name = 'MissingEventTriggerSupport';\n\n constructor(msg: string) {\n super(\n `${msg}. Schema changes cannot be reliably replicated without event trigger support.`,\n );\n }\n}\n\n// TODO(0xcadams): should this be a ProtocolError?\nclass ShutdownSignal extends AbortError {\n readonly name = 'ShutdownSignal';\n\n constructor(cause: unknown) {\n super(\n 'shutdown signal received (e.g. another zero-cache taking over the replication stream)',\n {\n cause,\n },\n );\n }\n}\n\nfunction parseLogicalMessageContent<T>(\n lc: LogContext,\n msg: MessageMessage,\n schema: v.Type<T>,\n) {\n const {content} = msg;\n const str =\n content instanceof Buffer\n ? content.toString('utf-8')\n : new TextDecoder().decode(content);\n try {\n const json = JSON.parse(str);\n return v.parse(json, schema, 'passthrough');\n } catch (e) {\n lc.error?.(`unable to parse logical message content: ${String(e)}`, {\n message: {...msg, content: str},\n });\n throw e;\n }\n}\n"],"mapp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,MAAM;CACR;AACF"}
|
|
@@ -15,6 +15,7 @@ type SourceWithPendingQueue<T> = Source<T> & {
|
|
|
15
15
|
export declare function subscribe(lc: LogContext, db: PostgresDB, slot: string, publications: string[], lsn: bigint, retriesIfReplicationSlotActive?: number, applicationName?: string): Promise<{
|
|
16
16
|
messages: SourceWithPendingQueue<StreamMessage>;
|
|
17
17
|
acks: Sink<bigint>;
|
|
18
|
+
sourceTerminated: Promise<Error>;
|
|
18
19
|
}>;
|
|
19
20
|
/**
|
|
20
21
|
* Derives the timings for the replication liveness timer from the server's
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../../../../../../../zero-cache/src/services/change-source/pg/logical-replication/stream.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../../../../../../../zero-cache/src/services/change-source/pg/logical-replication/stream.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAMjD,OAAO,EAAC,KAAK,UAAU,EAAC,MAAM,yBAAyB,CAAC;AAExD,OAAO,EAAO,KAAK,IAAI,EAAE,KAAK,MAAM,EAAC,MAAM,8BAA8B,CAAC;AAK1E,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,qBAAqB,CAAC;AAIjD,MAAM,MAAM,aAAa,GAAG;IAC1B,GAAG,EAAE,MAAM;IACX,OAAO,GAAG;QAAC,GAAG,EAAE,WAAW,CAAC;QAAC,aAAa,EAAE,OAAO,CAAA;KAAC;CACrD,CAAC;AAKF,KAAK,sBAAsB,CAAC,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,GAAG;IAC3C,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,wBAAsB,SAAS,CAC7B,EAAE,EAAE,UAAU,EACd,EAAE,EAAE,UAAU,EACd,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,EAAE,EACtB,GAAG,EAAE,MAAM,EACX,8BAA8B,SAA6C,EAC3E,eAAe,SAAoB,GAClC,OAAO,CAAC;IACT,QAAQ,EAAE,sBAAsB,CAAC,aAAa,CAAC,CAAC;IAChD,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;IACnB,gBAAgB,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;CAClC,CAAC,CA8ID;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CAAC,kBAAkB,EAAE,MAAM,GAAG;IAClE,OAAO,EAAE,OAAO,CAAC;IACjB,sBAAsB,EAAE,MAAM,CAAC;IAC/B,gBAAgB,EAAE,MAAM,CAAC;IACzB,eAAe,EAAE,MAAM,CAAC;CACzB,CAyBA"}
|
|
@@ -7,6 +7,7 @@ import { Subscription } from "../../../../types/subscription.js";
|
|
|
7
7
|
import { pipe } from "../../../../types/streams.js";
|
|
8
8
|
import { getTypeParsers } from "../../../../db/pg-type-parser.js";
|
|
9
9
|
import { PgoutputParser } from "./pgoutput-parser.js";
|
|
10
|
+
import { resolver } from "@rocicorp/resolver";
|
|
10
11
|
import { defu } from "defu";
|
|
11
12
|
import postgres from "postgres";
|
|
12
13
|
import { PG_ADMIN_SHUTDOWN, PG_OBJECT_IN_USE, PG_OBJECT_NOT_IN_PREREQUISITE_STATE } from "@drdgvhbh/postgres-error-codes";
|
|
@@ -48,6 +49,7 @@ async function subscribe(lc, db, slot, publications, lsn, retriesIfReplicationSl
|
|
|
48
49
|
}
|
|
49
50
|
}, timerIntervalMs) : void 0;
|
|
50
51
|
let destroyed = false;
|
|
52
|
+
const sourceTerminated = resolver();
|
|
51
53
|
const parser = new PgoutputParser(await getTypeParsers(db, { returnJsonAsString: true }));
|
|
52
54
|
const messages = Subscription.create({ cleanup: () => {
|
|
53
55
|
destroyed = true;
|
|
@@ -55,8 +57,17 @@ async function subscribe(lc, db, slot, publications, lsn, retriesIfReplicationSl
|
|
|
55
57
|
clearInterval(livenessTimer);
|
|
56
58
|
return session.end();
|
|
57
59
|
} });
|
|
58
|
-
readable.once("close", () =>
|
|
59
|
-
|
|
60
|
+
readable.once("close", () => {
|
|
61
|
+
if (!destroyed) {
|
|
62
|
+
const error = /* @__PURE__ */ new Error(`replication stream closed by ${db.options.host}`);
|
|
63
|
+
sourceTerminated.resolve(error);
|
|
64
|
+
lc.warn?.(error.message);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
readable.once("error", (e) => {
|
|
68
|
+
if (!destroyed) sourceTerminated.resolve(e);
|
|
69
|
+
e instanceof postgres.PostgresError && e.code === PG_ADMIN_SHUTDOWN || lc.warn?.(`error from ${db.options.host}`, e);
|
|
70
|
+
});
|
|
60
71
|
pipe({
|
|
61
72
|
source: readable,
|
|
62
73
|
sink: messages,
|
|
@@ -68,7 +79,8 @@ async function subscribe(lc, db, slot, publications, lsn, retriesIfReplicationSl
|
|
|
68
79
|
});
|
|
69
80
|
return {
|
|
70
81
|
messages,
|
|
71
|
-
acks: { push: sendAck }
|
|
82
|
+
acks: { push: sendAck },
|
|
83
|
+
sourceTerminated: sourceTerminated.promise
|
|
72
84
|
};
|
|
73
85
|
}
|
|
74
86
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stream.js","names":[],"sources":["../../../../../../../../zero-cache/src/services/change-source/pg/logical-replication/stream.ts"],"sourcesContent":["import {\n PG_ADMIN_SHUTDOWN,\n PG_OBJECT_IN_USE,\n PG_OBJECT_NOT_IN_PREREQUISITE_STATE,\n} from '@drdgvhbh/postgres-error-codes';\nimport type {LogContext} from '@rocicorp/logger';\nimport {defu} from 'defu';\nimport postgres, {type Options, type PostgresType} from 'postgres';\nimport {sleep} from '../../../../../../shared/src/sleep.ts';\nimport {getTypeParsers} from '../../../../db/pg-type-parser.ts';\nimport {type PostgresDB} from '../../../../types/pg.ts';\nimport {id, lit} from '../../../../types/sql.ts';\nimport {pipe, type Sink, type Source} from '../../../../types/streams.ts';\nimport {Subscription} from '../../../../types/subscription.ts';\nimport {AutoResetSignal} from '../../../change-streamer/schema/tables.ts';\nimport {fromBigInt} from '../lsn.ts';\nimport {PgoutputParser} from './pgoutput-parser.ts';\nimport type {Message} from './pgoutput.types.ts';\n\nconst DEFAULT_RETRIES_IF_REPLICATION_SLOT_ACTIVE = 5;\n\nexport type StreamMessage = [\n lsn: bigint,\n Message | {tag: 'keepalive'; shouldRespond: boolean},\n];\n\n// Expose the `queued` variable of the Subscription to allow\n// the change-source to determine if there are more messages\n// immediately available.\ntype SourceWithPendingQueue<T> = Source<T> & {\n queued: number;\n};\n\nexport async function subscribe(\n lc: LogContext,\n db: PostgresDB,\n slot: string,\n publications: string[],\n lsn: bigint,\n retriesIfReplicationSlotActive = DEFAULT_RETRIES_IF_REPLICATION_SLOT_ACTIVE,\n applicationName = 'zero-replicator',\n): Promise<{\n messages: SourceWithPendingQueue<StreamMessage>;\n acks: Sink<bigint>;\n}> {\n const session = postgres(\n defu(\n {\n max: 1,\n ['fetch_types']: false, // Necessary for the streaming protocol\n ['idle_timeout']: null,\n ['max_lifetime']: null as unknown as number,\n connection: {\n ['application_name']: applicationName,\n replication: 'database', // https://www.postgresql.org/docs/current/protocol-replication.html\n },\n },\n // ParsedOptions are technically compatible with Options, but happen\n // to not be typed that way. The postgres.js author does an equivalent\n // merge of ParsedOptions and Options here:\n // https://github.com/porsager/postgres/blob/089214e85c23c90cf142d47fb30bd03f42874984/src/subscribe.js#L13\n db.options as unknown as Options<Record<string, PostgresType>>,\n ),\n );\n\n // Postgres will send keepalives before timing out a wal_sender. It is possible that\n // these keepalives are not received if there is back-pressure in the replication\n // stream. To keep the connection alive, explicitly send keepalives if none have been\n // sent within the last 75% of the wal_sender_timeout.\n //\n // https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-WAL-SENDER-TIMEOUT\n const [{walSenderTimeoutMs}] = await session<\n {walSenderTimeoutMs: number}[]\n >`SELECT EXTRACT(EPOCH FROM (setting || unit)::interval) * 1000 \n AS \"walSenderTimeoutMs\" FROM pg_settings\n WHERE name = 'wal_sender_timeout'`.simple();\n const {\n enabled: livenessEnabled,\n manualKeepaliveTimeout,\n inboundTimeoutMs,\n timerIntervalMs,\n } = computeLivenessTimings(walSenderTimeoutMs);\n lc.info?.(\n livenessEnabled\n ? `wal_sender_timeout: ${walSenderTimeoutMs}ms. ` +\n `Ensuring manual keepalives at least every ${manualKeepaliveTimeout}ms`\n : `wal_sender_timeout is disabled (0); skipping manual keepalives and ` +\n `inbound liveness detection.`,\n );\n\n const [readable, writable] = await startReplicationStream(\n lc,\n session,\n slot,\n publications,\n lsn,\n retriesIfReplicationSlotActive + 1,\n );\n\n let lastAckTime = Date.now();\n function sendAck(lsn: bigint) {\n writable.write(makeAck(lsn));\n lastAckTime = Date.now();\n }\n\n // Tear down the readable to force a reconnect if the inbound half of the\n // connection goes silent (our outbound acks keep flowing into the void,\n // neither side errors, and the change-source would otherwise sit there\n // forever). See computeLivenessTimings for the derivation of these\n // thresholds — and why the whole timer is disabled when wal_sender_timeout\n // is 0.\n let lastReceivedTime = Date.now();\n\n const livenessTimer = livenessEnabled\n ? setInterval(() => {\n const now = Date.now();\n if (now - lastAckTime > manualKeepaliveTimeout) {\n sendAck(0n);\n lc.debug?.(`sent manual keepalive`);\n }\n const sinceLastReceived = now - lastReceivedTime;\n if (sinceLastReceived > inboundTimeoutMs && !readable.destroyed) {\n lc.warn?.(\n `no message received from ${db.options.host} in ${sinceLastReceived}ms ` +\n `(> 2x wal_sender_timeout ${walSenderTimeoutMs}ms). Destroying ` +\n `replication stream to force reconnect.`,\n );\n readable.destroy(\n new Error(\n `replication stream inbound timeout after ${sinceLastReceived}ms`,\n ),\n );\n }\n }, timerIntervalMs)\n : undefined;\n\n let destroyed = false;\n const typeParsers = await getTypeParsers(db, {returnJsonAsString: true});\n const parser = new PgoutputParser(typeParsers);\n const messages = Subscription.create<StreamMessage>({\n cleanup: () => {\n destroyed = true;\n readable.destroyed || readable.destroy();\n clearInterval(livenessTimer);\n return session.end();\n },\n });\n\n readable.once(\n 'close',\n () =>\n // Only log a warning if the stream was not manually closed.\n destroyed || lc.warn?.(`replication stream closed by ${db.options.host}`),\n );\n readable.once(\n 'error',\n e =>\n // Don't log the shutdown signal. This is the expected way for upstream\n // to close the connection (and will be logged downstream).\n (e instanceof postgres.PostgresError && e.code === PG_ADMIN_SHUTDOWN) ||\n lc.warn?.(`error from ${db.options.host}`, e),\n );\n\n pipe({\n source: readable,\n sink: messages,\n parse: buffer => {\n lastReceivedTime = Date.now();\n return parseStreamMessage(lc, buffer, parser);\n },\n // Allow a small buffer of messages to be queued in the subscription so\n // that the change-source loop can check the queue to determine if more\n // messages are immediately available.\n bufferMessages: 5,\n });\n\n return {\n messages,\n acks: {push: sendAck},\n };\n}\n\n/**\n * Derives the timings for the replication liveness timer from the server's\n * `wal_sender_timeout` (in milliseconds).\n *\n * Postgres treats `wal_sender_timeout = 0` as \"disabled\": it will neither\n * terminate an idle wal sender nor emit the periodic keepalives it otherwise\n * sends at roughly `wal_sender_timeout / 2`. Every timing below is derived from\n * that interval, so a disabled timeout disables the liveness machinery entirely\n * (`enabled: false`). Otherwise the derived thresholds would all collapse to 0,\n * inverting the intended meaning: the inbound watchdog would fire on the very\n * first tick and destroy the stream, while the timer itself would busy-spin at\n * a 0ms interval — together producing a continuous reconnect storm.\n *\n * https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-WAL-SENDER-TIMEOUT\n */\nexport function computeLivenessTimings(walSenderTimeoutMs: number): {\n enabled: boolean;\n manualKeepaliveTimeout: number;\n inboundTimeoutMs: number;\n timerIntervalMs: number;\n} {\n // Guard with `!(x > 0)` so that 0, negative, and NaN values all disable the\n // timer rather than producing degenerate (zero / NaN) thresholds.\n if (!(walSenderTimeoutMs > 0)) {\n return {\n enabled: false,\n manualKeepaliveTimeout: 0,\n inboundTimeoutMs: 0,\n timerIntervalMs: 0,\n };\n }\n // Send a manual keepalive if nothing has been sent in the last 75% of the\n // wal_sender_timeout, so Postgres never times us out under back-pressure.\n const manualKeepaliveTimeout = Math.floor(walSenderTimeoutMs * 0.75);\n return {\n enabled: true,\n manualKeepaliveTimeout,\n // Force a reconnect after 2x wal_sender_timeout — i.e. two consecutive\n // missed keepalives — without any inbound message, to avoid spurious\n // reconnects from a single delayed keepalive (PG scheduling jitter under\n // load, our own event-loop stalls, etc.).\n inboundTimeoutMs: walSenderTimeoutMs * 2,\n // Poll at 1/5th of the manual keepalive interval.\n timerIntervalMs: manualKeepaliveTimeout / 5,\n };\n}\n\n/**\n * Formats publication names for the START_REPLICATION command.\n * The replication protocol expects format: publication_names 'pub1,pub2'\n * Each name is escaped to handle any quotes that may have passed validation.\n */\nfunction formatPublicationNames(publications: string[]): string {\n // lit() returns 'escaped_name' with surrounding quotes\n // We strip the quotes since the outer quotes are in the template\n return publications.map(p => lit(p).slice(1, -1)).join(',');\n}\n\nasync function startReplicationStream(\n lc: LogContext,\n session: postgres.Sql,\n slot: string,\n publications: string[],\n lsn: bigint,\n maxAttempts: number,\n) {\n for (let i = 0; i < maxAttempts; i++) {\n try {\n const stream = session\n .unsafe(\n `START_REPLICATION SLOT ${id(slot)} LOGICAL ${fromBigInt(lsn)} (\n proto_version '1',\n publication_names '${formatPublicationNames(publications)}',\n messages 'true'\n )`,\n )\n .execute();\n return await Promise.all([stream.readable(), stream.writable()]);\n } catch (e) {\n if (e instanceof postgres.PostgresError) {\n // error: replication slot \"zero_slot_change_source_test_id\" is active for PID 268\n if (e.code === PG_OBJECT_IN_USE) {\n // The freeing up of the replication slot is not transactional;\n // sometimes it takes time for Postgres to consider the slot\n // inactive.\n lc.warn?.(`attempt ${i + 1}: ${String(e)}`, e);\n await sleep(10);\n continue;\n }\n // error: This slot has been invalidated because it exceeded the maximum reserved size.\n // (This is a different manifestation of a slot being invalidated when\n // the wal exceeds the max_slot_wal_keep_size)\n if (e.code === PG_OBJECT_NOT_IN_PREREQUISITE_STATE) {\n lc.error?.(`error starting replication stream`, e);\n throw new AutoResetSignal(`unable to start replication stream`, {\n cause: e,\n });\n }\n }\n throw e;\n }\n }\n throw new Error(\n `exceeded max attempts (${maxAttempts}) to start the Postgres stream`,\n );\n}\n\nfunction parseStreamMessage(\n lc: LogContext,\n buffer: Buffer,\n parser: PgoutputParser,\n): StreamMessage | null {\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-START-REPLICATION\n if (buffer[0] !== 0x77 && buffer[0] !== 0x6b) {\n lc.warn?.('Unknown message', buffer[0]);\n return null;\n }\n const lsn = buffer.readBigUInt64BE(1);\n if (buffer[0] === 0x77) {\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-XLOGDATA\n // (Byte 25 is where the WAL data begins)\n return [lsn, parser.parse(buffer.subarray(25))];\n }\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-PRIMARY-KEEPALIVE-MESSAGE\n // (Byte 17: shouldRespond)\n const shouldRespond = buffer.readInt8(17) !== 0;\n return [lsn, {tag: 'keepalive', shouldRespond}];\n}\n\n// https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-STANDBY-STATUS-UPDATE\nfunction makeAck(lsn: bigint): Buffer {\n const microNow = BigInt(Date.now() - Date.UTC(2000, 0, 1)) * BigInt(1000);\n\n const x = Buffer.alloc(34);\n x[0] = 'r'.charCodeAt(0);\n x.writeBigInt64BE(lsn, 1);\n x.writeBigInt64BE(lsn, 9);\n x.writeBigInt64BE(lsn, 17);\n x.writeBigInt64BE(microNow, 25);\n return x;\n}\n"],"mappings":";;;;;;;;;;;;;AAmBA,IAAM,6CAA6C;AAcnD,eAAsB,UACpB,IACA,IACA,MACA,cACA,KACA,iCAAiC,4CACjC,kBAAkB,mBAIjB;CACD,MAAM,UAAU,SACd,KACE;EACE,KAAK;GACJ,gBAAgB;GAChB,iBAAiB;GACjB,iBAAiB;EAClB,YAAY;IACT,qBAAqB;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D,GAAG,QAAQ,qCAAqC,CAAC;IACjD,MAAM,IAAI,gBAAgB,sCAAsC,EAC9D,OAAO,EACT,CAAC;GACH;EACF;EACA,MAAM;CACR;CAEF,MAAM,IAAI,MACR,0BAA0B,YAAY,+BACxC;AACF;AAEA,SAAS,mBACP,IACA,QACA,QACsB;CAEtB,IAAI,OAAO,OAAO,OAAQ,OAAO,OAAO,KAAM;EAC5C,GAAG,OAAO,mBAAmB,OAAO,EAAE;EACtC,OAAO;CACT;CACA,MAAM,MAAM,OAAO,gBAAgB,CAAC;CACpC,IAAI,OAAO,OAAO,KAGhB,OAAO,CAAC,KAAK,OAAO,MAAM,OAAO,SAAS,EAAE,CAAC,CAAC;CAKhD,OAAO,CAAC,KAAK;EAAC,KAAK;EAAa,eADV,OAAO,SAAS,EAAE,MAAM;CACD,CAAC;AAChD;AAGA,SAAS,QAAQ,KAAqB;CACpC,MAAM,WAAW,OAAO,KAAK,IAAI,IAAI,KAAK,IAAI,KAAM,GAAG,CAAC,CAAC,IAAI,OAAO,GAAI;CAExE,MAAM,IAAI,OAAO,MAAM,EAAE;CACzB,EAAE,KAAK,IAAI,WAAW,CAAC;CACvB,EAAE,gBAAgB,KAAK,CAAC;CACxB,EAAE,gBAAgB,KAAK,CAAC;CACxB,EAAE,gBAAgB,KAAK,EAAE;CACzB,EAAE,gBAAgB,UAAU,EAAE;CAC9B,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"stream.js","names":[],"sources":["../../../../../../../../zero-cache/src/services/change-source/pg/logical-replication/stream.ts"],"sourcesContent":["import {\n PG_ADMIN_SHUTDOWN,\n PG_OBJECT_IN_USE,\n PG_OBJECT_NOT_IN_PREREQUISITE_STATE,\n} from '@drdgvhbh/postgres-error-codes';\nimport type {LogContext} from '@rocicorp/logger';\nimport {resolver} from '@rocicorp/resolver';\nimport {defu} from 'defu';\nimport postgres, {type Options, type PostgresType} from 'postgres';\nimport {sleep} from '../../../../../../shared/src/sleep.ts';\nimport {getTypeParsers} from '../../../../db/pg-type-parser.ts';\nimport {type PostgresDB} from '../../../../types/pg.ts';\nimport {id, lit} from '../../../../types/sql.ts';\nimport {pipe, type Sink, type Source} from '../../../../types/streams.ts';\nimport {Subscription} from '../../../../types/subscription.ts';\nimport {AutoResetSignal} from '../../../change-streamer/schema/tables.ts';\nimport {fromBigInt} from '../lsn.ts';\nimport {PgoutputParser} from './pgoutput-parser.ts';\nimport type {Message} from './pgoutput.types.ts';\n\nconst DEFAULT_RETRIES_IF_REPLICATION_SLOT_ACTIVE = 5;\n\nexport type StreamMessage = [\n lsn: bigint,\n Message | {tag: 'keepalive'; shouldRespond: boolean},\n];\n\n// Expose the `queued` variable of the Subscription to allow\n// the change-source to determine if there are more messages\n// immediately available.\ntype SourceWithPendingQueue<T> = Source<T> & {\n queued: number;\n};\n\nexport async function subscribe(\n lc: LogContext,\n db: PostgresDB,\n slot: string,\n publications: string[],\n lsn: bigint,\n retriesIfReplicationSlotActive = DEFAULT_RETRIES_IF_REPLICATION_SLOT_ACTIVE,\n applicationName = 'zero-replicator',\n): Promise<{\n messages: SourceWithPendingQueue<StreamMessage>;\n acks: Sink<bigint>;\n sourceTerminated: Promise<Error>;\n}> {\n const session = postgres(\n defu(\n {\n max: 1,\n ['fetch_types']: false, // Necessary for the streaming protocol\n ['idle_timeout']: null,\n ['max_lifetime']: null as unknown as number,\n connection: {\n ['application_name']: applicationName,\n replication: 'database', // https://www.postgresql.org/docs/current/protocol-replication.html\n },\n },\n // ParsedOptions are technically compatible with Options, but happen\n // to not be typed that way. The postgres.js author does an equivalent\n // merge of ParsedOptions and Options here:\n // https://github.com/porsager/postgres/blob/089214e85c23c90cf142d47fb30bd03f42874984/src/subscribe.js#L13\n db.options as unknown as Options<Record<string, PostgresType>>,\n ),\n );\n\n // Postgres will send keepalives before timing out a wal_sender. It is possible that\n // these keepalives are not received if there is back-pressure in the replication\n // stream. To keep the connection alive, explicitly send keepalives if none have been\n // sent within the last 75% of the wal_sender_timeout.\n //\n // https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-WAL-SENDER-TIMEOUT\n const [{walSenderTimeoutMs}] = await session<\n {walSenderTimeoutMs: number}[]\n >`SELECT EXTRACT(EPOCH FROM (setting || unit)::interval) * 1000 \n AS \"walSenderTimeoutMs\" FROM pg_settings\n WHERE name = 'wal_sender_timeout'`.simple();\n const {\n enabled: livenessEnabled,\n manualKeepaliveTimeout,\n inboundTimeoutMs,\n timerIntervalMs,\n } = computeLivenessTimings(walSenderTimeoutMs);\n lc.info?.(\n livenessEnabled\n ? `wal_sender_timeout: ${walSenderTimeoutMs}ms. ` +\n `Ensuring manual keepalives at least every ${manualKeepaliveTimeout}ms`\n : `wal_sender_timeout is disabled (0); skipping manual keepalives and ` +\n `inbound liveness detection.`,\n );\n\n const [readable, writable] = await startReplicationStream(\n lc,\n session,\n slot,\n publications,\n lsn,\n retriesIfReplicationSlotActive + 1,\n );\n\n let lastAckTime = Date.now();\n function sendAck(lsn: bigint) {\n writable.write(makeAck(lsn));\n lastAckTime = Date.now();\n }\n\n // Tear down the readable to force a reconnect if the inbound half of the\n // connection goes silent (our outbound acks keep flowing into the void,\n // neither side errors, and the change-source would otherwise sit there\n // forever). See computeLivenessTimings for the derivation of these\n // thresholds — and why the whole timer is disabled when wal_sender_timeout\n // is 0.\n let lastReceivedTime = Date.now();\n\n const livenessTimer = livenessEnabled\n ? setInterval(() => {\n const now = Date.now();\n if (now - lastAckTime > manualKeepaliveTimeout) {\n sendAck(0n);\n lc.debug?.(`sent manual keepalive`);\n }\n const sinceLastReceived = now - lastReceivedTime;\n if (sinceLastReceived > inboundTimeoutMs && !readable.destroyed) {\n lc.warn?.(\n `no message received from ${db.options.host} in ${sinceLastReceived}ms ` +\n `(> 2x wal_sender_timeout ${walSenderTimeoutMs}ms). Destroying ` +\n `replication stream to force reconnect.`,\n );\n readable.destroy(\n new Error(\n `replication stream inbound timeout after ${sinceLastReceived}ms`,\n ),\n );\n }\n }, timerIntervalMs)\n : undefined;\n\n let destroyed = false;\n const sourceTerminated = resolver<Error>();\n const typeParsers = await getTypeParsers(db, {returnJsonAsString: true});\n const parser = new PgoutputParser(typeParsers);\n const messages = Subscription.create<StreamMessage>({\n cleanup: () => {\n destroyed = true;\n readable.destroyed || readable.destroy();\n clearInterval(livenessTimer);\n return session.end();\n },\n });\n\n readable.once('close', () => {\n if (!destroyed) {\n const error = new Error(\n `replication stream closed by ${db.options.host}`,\n );\n sourceTerminated.resolve(error);\n lc.warn?.(error.message);\n }\n });\n readable.once('error', e => {\n if (!destroyed) {\n sourceTerminated.resolve(e);\n }\n // Don't log the shutdown signal. This is the expected way for upstream\n // to close the connection (and will be logged downstream).\n (e instanceof postgres.PostgresError && e.code === PG_ADMIN_SHUTDOWN) ||\n lc.warn?.(`error from ${db.options.host}`, e);\n });\n\n pipe({\n source: readable,\n sink: messages,\n parse: buffer => {\n lastReceivedTime = Date.now();\n return parseStreamMessage(lc, buffer, parser);\n },\n // Allow a small buffer of messages to be queued in the subscription so\n // that the change-source loop can check the queue to determine if more\n // messages are immediately available.\n bufferMessages: 5,\n });\n\n return {\n messages,\n acks: {push: sendAck},\n sourceTerminated: sourceTerminated.promise,\n };\n}\n\n/**\n * Derives the timings for the replication liveness timer from the server's\n * `wal_sender_timeout` (in milliseconds).\n *\n * Postgres treats `wal_sender_timeout = 0` as \"disabled\": it will neither\n * terminate an idle wal sender nor emit the periodic keepalives it otherwise\n * sends at roughly `wal_sender_timeout / 2`. Every timing below is derived from\n * that interval, so a disabled timeout disables the liveness machinery entirely\n * (`enabled: false`). Otherwise the derived thresholds would all collapse to 0,\n * inverting the intended meaning: the inbound watchdog would fire on the very\n * first tick and destroy the stream, while the timer itself would busy-spin at\n * a 0ms interval — together producing a continuous reconnect storm.\n *\n * https://www.postgresql.org/docs/current/runtime-config-replication.html#GUC-WAL-SENDER-TIMEOUT\n */\nexport function computeLivenessTimings(walSenderTimeoutMs: number): {\n enabled: boolean;\n manualKeepaliveTimeout: number;\n inboundTimeoutMs: number;\n timerIntervalMs: number;\n} {\n // Guard with `!(x > 0)` so that 0, negative, and NaN values all disable the\n // timer rather than producing degenerate (zero / NaN) thresholds.\n if (!(walSenderTimeoutMs > 0)) {\n return {\n enabled: false,\n manualKeepaliveTimeout: 0,\n inboundTimeoutMs: 0,\n timerIntervalMs: 0,\n };\n }\n // Send a manual keepalive if nothing has been sent in the last 75% of the\n // wal_sender_timeout, so Postgres never times us out under back-pressure.\n const manualKeepaliveTimeout = Math.floor(walSenderTimeoutMs * 0.75);\n return {\n enabled: true,\n manualKeepaliveTimeout,\n // Force a reconnect after 2x wal_sender_timeout — i.e. two consecutive\n // missed keepalives — without any inbound message, to avoid spurious\n // reconnects from a single delayed keepalive (PG scheduling jitter under\n // load, our own event-loop stalls, etc.).\n inboundTimeoutMs: walSenderTimeoutMs * 2,\n // Poll at 1/5th of the manual keepalive interval.\n timerIntervalMs: manualKeepaliveTimeout / 5,\n };\n}\n\n/**\n * Formats publication names for the START_REPLICATION command.\n * The replication protocol expects format: publication_names 'pub1,pub2'\n * Each name is escaped to handle any quotes that may have passed validation.\n */\nfunction formatPublicationNames(publications: string[]): string {\n // lit() returns 'escaped_name' with surrounding quotes\n // We strip the quotes since the outer quotes are in the template\n return publications.map(p => lit(p).slice(1, -1)).join(',');\n}\n\nasync function startReplicationStream(\n lc: LogContext,\n session: postgres.Sql,\n slot: string,\n publications: string[],\n lsn: bigint,\n maxAttempts: number,\n) {\n for (let i = 0; i < maxAttempts; i++) {\n try {\n const stream = session\n .unsafe(\n `START_REPLICATION SLOT ${id(slot)} LOGICAL ${fromBigInt(lsn)} (\n proto_version '1',\n publication_names '${formatPublicationNames(publications)}',\n messages 'true'\n )`,\n )\n .execute();\n return await Promise.all([stream.readable(), stream.writable()]);\n } catch (e) {\n if (e instanceof postgres.PostgresError) {\n // error: replication slot \"zero_slot_change_source_test_id\" is active for PID 268\n if (e.code === PG_OBJECT_IN_USE) {\n // The freeing up of the replication slot is not transactional;\n // sometimes it takes time for Postgres to consider the slot\n // inactive.\n lc.warn?.(`attempt ${i + 1}: ${String(e)}`, e);\n await sleep(10);\n continue;\n }\n // error: This slot has been invalidated because it exceeded the maximum reserved size.\n // (This is a different manifestation of a slot being invalidated when\n // the wal exceeds the max_slot_wal_keep_size)\n if (e.code === PG_OBJECT_NOT_IN_PREREQUISITE_STATE) {\n lc.error?.(`error starting replication stream`, e);\n throw new AutoResetSignal(`unable to start replication stream`, {\n cause: e,\n });\n }\n }\n throw e;\n }\n }\n throw new Error(\n `exceeded max attempts (${maxAttempts}) to start the Postgres stream`,\n );\n}\n\nfunction parseStreamMessage(\n lc: LogContext,\n buffer: Buffer,\n parser: PgoutputParser,\n): StreamMessage | null {\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-START-REPLICATION\n if (buffer[0] !== 0x77 && buffer[0] !== 0x6b) {\n lc.warn?.('Unknown message', buffer[0]);\n return null;\n }\n const lsn = buffer.readBigUInt64BE(1);\n if (buffer[0] === 0x77) {\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-XLOGDATA\n // (Byte 25 is where the WAL data begins)\n return [lsn, parser.parse(buffer.subarray(25))];\n }\n // https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-PRIMARY-KEEPALIVE-MESSAGE\n // (Byte 17: shouldRespond)\n const shouldRespond = buffer.readInt8(17) !== 0;\n return [lsn, {tag: 'keepalive', shouldRespond}];\n}\n\n// https://www.postgresql.org/docs/current/protocol-replication.html#PROTOCOL-REPLICATION-STANDBY-STATUS-UPDATE\nfunction makeAck(lsn: bigint): Buffer {\n const microNow = BigInt(Date.now() - Date.UTC(2000, 0, 1)) * BigInt(1000);\n\n const x = Buffer.alloc(34);\n x[0] = 'r'.charCodeAt(0);\n x.writeBigInt64BE(lsn, 1);\n x.writeBigInt64BE(lsn, 9);\n x.writeBigInt64BE(lsn, 17);\n x.writeBigInt64BE(microNow, 25);\n return x;\n}\n"],"mappings":";;;;;;;;;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KAAK,EAAE;CACzB,EAAE,gBAAgB,UAAU,EAAE;CAC9B,OAAO;AACT"}
|
|
@@ -10,7 +10,7 @@ import { streamIn, streamOut, streamOutStringified } from "../../types/streams.j
|
|
|
10
10
|
import { URLParams } from "../../types/url-params.js";
|
|
11
11
|
import { downstreamSchema } from "./change-streamer.js";
|
|
12
12
|
import { snapshotMessageSchema } from "./snapshot.js";
|
|
13
|
-
import WebSocket
|
|
13
|
+
import WebSocket from "ws";
|
|
14
14
|
import websocket from "@fastify/websocket";
|
|
15
15
|
//#region ../zero-cache/src/services/change-streamer/change-streamer-http.ts
|
|
16
16
|
var MIN_SUPPORTED_PROTOCOL_VERSION = 1;
|
|
@@ -114,11 +114,11 @@ var ChangeStreamerHttpClient = class {
|
|
|
114
114
|
return uri;
|
|
115
115
|
}
|
|
116
116
|
async reserveSnapshot(taskID) {
|
|
117
|
-
const ws = new WebSocket
|
|
117
|
+
const ws = new WebSocket(await this.#resolveChangeStreamer(SNAPSHOT_PATH) + `?${new URLSearchParams({ taskID }).toString()}`);
|
|
118
118
|
return streamIn(this.#lc, ws, snapshotMessageSchema);
|
|
119
119
|
}
|
|
120
120
|
async subscribe(ctx) {
|
|
121
|
-
const ws = new WebSocket
|
|
121
|
+
const ws = new WebSocket(await this.#resolveChangeStreamer(CHANGES_PATH) + `?${getParams(ctx).toString()}`);
|
|
122
122
|
return streamIn(this.#lc, ws, downstreamSchema);
|
|
123
123
|
}
|
|
124
124
|
};
|