@rocicorp/zero 1.4.0-canary.1 → 1.4.0-canary.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/out/analyze-query/src/bin-analyze.js +1 -1
  2. package/out/analyze-query/src/bin-analyze.js.map +1 -1
  3. package/out/analyze-query/src/bin-transform.js +1 -1
  4. package/out/analyze-query/src/bin-transform.js.map +1 -1
  5. package/out/replicache/src/btree/node.d.ts +1 -1
  6. package/out/replicache/src/btree/node.d.ts.map +1 -1
  7. package/out/replicache/src/btree/node.js +34 -21
  8. package/out/replicache/src/btree/node.js.map +1 -1
  9. package/out/replicache/src/btree/write.js +1 -2
  10. package/out/replicache/src/btree/write.js.map +1 -1
  11. package/out/shared/src/btree-set.d.ts +6 -0
  12. package/out/shared/src/btree-set.d.ts.map +1 -1
  13. package/out/shared/src/btree-set.js +34 -0
  14. package/out/shared/src/btree-set.js.map +1 -1
  15. package/out/zero/package.js +1 -1
  16. package/out/zero/package.js.map +1 -1
  17. package/out/zero-cache/src/config/zero-config.d.ts +18 -0
  18. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  19. package/out/zero-cache/src/config/zero-config.js +35 -3
  20. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  21. package/out/zero-cache/src/scripts/decommission.d.ts.map +1 -1
  22. package/out/zero-cache/src/scripts/decommission.js +3 -3
  23. package/out/zero-cache/src/scripts/decommission.js.map +1 -1
  24. package/out/zero-cache/src/scripts/deploy-permissions.js +1 -1
  25. package/out/zero-cache/src/scripts/deploy-permissions.js.map +1 -1
  26. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  27. package/out/zero-cache/src/server/change-streamer.js +2 -5
  28. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  29. package/out/zero-cache/src/server/main.d.ts.map +1 -1
  30. package/out/zero-cache/src/server/main.js +6 -1
  31. package/out/zero-cache/src/server/main.js.map +1 -1
  32. package/out/zero-cache/src/server/reaper.d.ts.map +1 -1
  33. package/out/zero-cache/src/server/reaper.js +1 -4
  34. package/out/zero-cache/src/server/reaper.js.map +1 -1
  35. package/out/zero-cache/src/server/shadow-syncer.js +35 -0
  36. package/out/zero-cache/src/server/shadow-syncer.js.map +1 -0
  37. package/out/zero-cache/src/server/syncer.d.ts.map +1 -1
  38. package/out/zero-cache/src/server/syncer.js +2 -8
  39. package/out/zero-cache/src/server/syncer.js.map +1 -1
  40. package/out/zero-cache/src/server/worker-urls.d.ts +1 -0
  41. package/out/zero-cache/src/server/worker-urls.d.ts.map +1 -1
  42. package/out/zero-cache/src/server/worker-urls.js +2 -1
  43. package/out/zero-cache/src/server/worker-urls.js.map +1 -1
  44. package/out/zero-cache/src/services/change-source/pg/backfill-stream.d.ts +8 -1
  45. package/out/zero-cache/src/services/change-source/pg/backfill-stream.d.ts.map +1 -1
  46. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +31 -18
  47. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js.map +1 -1
  48. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  49. package/out/zero-cache/src/services/change-source/pg/change-source.js +44 -46
  50. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  51. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts +6 -1
  52. package/out/zero-cache/src/services/change-source/pg/initial-sync.d.ts.map +1 -1
  53. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +62 -22
  54. package/out/zero-cache/src/services/change-source/pg/initial-sync.js.map +1 -1
  55. package/out/zero-cache/src/services/change-streamer/change-streamer-http.d.ts.map +1 -1
  56. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js +2 -3
  57. package/out/zero-cache/src/services/change-streamer/change-streamer-http.js.map +1 -1
  58. package/out/zero-cache/src/services/change-streamer/schema/tables.js +1 -1
  59. package/out/zero-cache/src/services/shadow-sync/shadow-sync-service.js +49 -0
  60. package/out/zero-cache/src/services/shadow-sync/shadow-sync-service.js.map +1 -0
  61. package/out/zero-cache/src/services/statz.js +3 -3
  62. package/out/zero-cache/src/services/statz.js.map +1 -1
  63. package/out/zero-cache/src/services/view-syncer/cvr-store.d.ts +1 -0
  64. package/out/zero-cache/src/services/view-syncer/cvr-store.d.ts.map +1 -1
  65. package/out/zero-cache/src/services/view-syncer/cvr-store.js +34 -11
  66. package/out/zero-cache/src/services/view-syncer/cvr-store.js.map +1 -1
  67. package/out/zero-cache/src/services/view-syncer/cvr.d.ts +16 -1
  68. package/out/zero-cache/src/services/view-syncer/cvr.d.ts.map +1 -1
  69. package/out/zero-cache/src/services/view-syncer/cvr.js +19 -1
  70. package/out/zero-cache/src/services/view-syncer/cvr.js.map +1 -1
  71. package/out/zero-cache/src/services/view-syncer/inspect-handler.js +1 -1
  72. package/out/zero-cache/src/services/view-syncer/inspect-handler.js.map +1 -1
  73. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts +6 -0
  74. package/out/zero-cache/src/services/view-syncer/pipeline-driver.d.ts.map +1 -1
  75. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +45 -2
  76. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  77. package/out/zero-cache/src/services/view-syncer/row-set-signature.d.ts +17 -0
  78. package/out/zero-cache/src/services/view-syncer/row-set-signature.d.ts.map +1 -0
  79. package/out/zero-cache/src/services/view-syncer/row-set-signature.js +29 -0
  80. package/out/zero-cache/src/services/view-syncer/row-set-signature.js.map +1 -0
  81. package/out/zero-cache/src/services/view-syncer/schema/cvr.d.ts +1 -0
  82. package/out/zero-cache/src/services/view-syncer/schema/cvr.d.ts.map +1 -1
  83. package/out/zero-cache/src/services/view-syncer/schema/cvr.js +1 -0
  84. package/out/zero-cache/src/services/view-syncer/schema/cvr.js.map +1 -1
  85. package/out/zero-cache/src/services/view-syncer/schema/init.d.ts.map +1 -1
  86. package/out/zero-cache/src/services/view-syncer/schema/init.js +5 -1
  87. package/out/zero-cache/src/services/view-syncer/schema/init.js.map +1 -1
  88. package/out/zero-cache/src/services/view-syncer/schema/types.d.ts +105 -0
  89. package/out/zero-cache/src/services/view-syncer/schema/types.d.ts.map +1 -1
  90. package/out/zero-cache/src/services/view-syncer/schema/types.js +8 -4
  91. package/out/zero-cache/src/services/view-syncer/schema/types.js.map +1 -1
  92. package/out/zero-cache/src/services/view-syncer/view-syncer.d.ts.map +1 -1
  93. package/out/zero-cache/src/services/view-syncer/view-syncer.js +2 -2
  94. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  95. package/out/zero-cache/src/types/pg.d.ts +1 -1
  96. package/out/zero-cache/src/types/pg.d.ts.map +1 -1
  97. package/out/zero-cache/src/types/pg.js +8 -2
  98. package/out/zero-cache/src/types/pg.js.map +1 -1
  99. package/out/zero-cache/src/types/timeout.d.ts +11 -0
  100. package/out/zero-cache/src/types/timeout.d.ts.map +1 -0
  101. package/out/zero-cache/src/types/timeout.js +26 -0
  102. package/out/zero-cache/src/types/timeout.js.map +1 -0
  103. package/out/zero-cache/src/workers/connection.js +3 -3
  104. package/out/zero-cache/src/workers/connection.js.map +1 -1
  105. package/out/zero-client/src/client/version.js +1 -1
  106. package/out/zero-server/src/adapters/postgresjs.d.ts.map +1 -1
  107. package/out/zero-server/src/adapters/postgresjs.js +1 -1
  108. package/out/zero-server/src/adapters/postgresjs.js.map +1 -1
  109. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  110. package/out/zql/src/ivm/memory-source.js +3 -3
  111. package/out/zql/src/ivm/memory-source.js.map +1 -1
  112. package/out/zql/src/query/query-internals.d.ts.map +1 -1
  113. package/out/zql/src/query/query-internals.js +1 -1
  114. package/out/zql/src/query/query-internals.js.map +1 -1
  115. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"backfill-stream.js","names":[],"sources":["../../../../../../../zero-cache/src/services/change-source/pg/backfill-stream.ts"],"sourcesContent":["import {\n PG_UNDEFINED_COLUMN,\n PG_UNDEFINED_TABLE,\n} from '@drdgvhbh/postgres-error-codes';\nimport type {LogContext} from '@rocicorp/logger';\nimport postgres from 'postgres';\nimport {equals} from '../../../../../shared/src/set-utils.ts';\nimport * as v from '../../../../../shared/src/valita.ts';\nimport {READONLY} from '../../../db/mode-enum.ts';\nimport {TsvParser} from '../../../db/pg-copy.ts';\nimport {getTypeParsers, type TypeParser} from '../../../db/pg-type-parser.ts';\nimport type {PublishedTableSpec} from '../../../db/specs.ts';\nimport {importSnapshot, TransactionPool} from '../../../db/transaction-pool.ts';\nimport {pgClient, type PostgresDB} from '../../../types/pg.ts';\nimport {SchemaIncompatibilityError} from '../common/backfill-manager.ts';\nimport type {\n BackfillCompleted,\n BackfillRequest,\n DownloadStatus,\n JSONValue,\n MessageBackfill,\n} from '../protocol/current.ts';\nimport {\n columnMetadataSchema,\n tableMetadataSchema,\n} from './backfill-metadata.ts';\nimport {\n createReplicationSlot,\n makeDownloadStatements,\n type DownloadStatements,\n} from './initial-sync.ts';\nimport {toStateVersionString} from './lsn.ts';\nimport {getPublicationInfo} from './schema/published.ts';\nimport type {Replica} from './schema/shard.ts';\n\ntype BackfillParams = Omit<BackfillCompleted, 'tag'>;\n\ntype StreamOptions = {\n /**\n * The number of bytes at which to flush a batch of rows in a\n * backfill message. Defaults to Node's getDefaultHighWatermark().\n */\n flushThresholdBytes?: number;\n};\n\n// The size of chunks that Postgres sends on COPY stream.\n// This happens to match NodeJS's getDefaultHighWatermark()\n// (for Node v20+).\nconst POSTGRES_COPY_CHUNK_SIZE = 64 * 1024;\n\n/**\n * Streams a series of `backfill` messages (ending with `backfill-complete`)\n * at a set watermark (i.e. LSN). The data is retrieved via a COPY stream\n * made at a transaction snapshot corresponding to specific LSN, obtained by\n * creating a short-lived replication slot.\n */\nexport async function* streamBackfill(\n lc: LogContext,\n upstreamURI: string,\n {slot, publications}: Pick<Replica, 'slot' | 'publications'>,\n bf: BackfillRequest,\n opts: StreamOptions = {},\n): AsyncGenerator<MessageBackfill | BackfillCompleted> {\n lc = lc\n .withContext('component', 'backfill')\n .withContext('table', bf.table.name);\n\n const {flushThresholdBytes = POSTGRES_COPY_CHUNK_SIZE} = opts;\n const db = pgClient(lc, upstreamURI, {\n connection: {['application_name']: 'backfill-stream'},\n ['max_lifetime']: 120 * 60, // set a long (2h) limit for COPY streaming\n });\n let tx: TransactionPool | undefined;\n let watermark: string;\n try {\n ({tx, watermark} = await createSnapshotTransaction(\n lc,\n upstreamURI,\n db,\n slot,\n ));\n const {tableSpec, backfill} = await validateSchema(\n tx,\n publications,\n bf,\n watermark,\n );\n const types = await getTypeParsers(db, {returnJsonAsString: true});\n\n // Note: validateSchema ensures that the rowKey and columns are disjoint\n const {relation, columns} = backfill;\n const cols = [...relation.rowKey.columns, ...columns];\n\n yield* stream(\n lc,\n tx,\n backfill,\n makeDownloadStatements(tableSpec, cols),\n cols.map(col => types.getTypeParser(tableSpec.columns[col].typeOID)),\n flushThresholdBytes,\n );\n } catch (e) {\n // Although we make the best effort to validate the schema at the\n // transaction snapshot, certain forms of `ALTER TABLE` are not\n // MVCC safe and not \"frozen\" in the snapshot:\n //\n // https://www.postgresql.org/docs/current/mvcc-caveats.html\n //\n // Handle these errors as schema incompatibility errors rather than\n // unknown runtime errors.\n if (\n e instanceof postgres.PostgresError &&\n (e.code === PG_UNDEFINED_TABLE || e.code === PG_UNDEFINED_COLUMN)\n ) {\n throw new SchemaIncompatibilityError(bf, String(e), {cause: e});\n }\n throw e;\n } finally {\n tx?.setDone();\n // Workaround postgres.js hanging at the end of some COPY commands:\n // https://github.com/porsager/postgres/issues/499\n void db.end().catch(e => lc.warn?.(`error closing backfill connection`, e));\n }\n}\n\nasync function* stream(\n lc: LogContext,\n tx: TransactionPool,\n backfill: BackfillParams,\n {select, getTotalRows, getTotalBytes}: DownloadStatements,\n colParsers: TypeParser[],\n flushThresholdBytes: number,\n): AsyncGenerator<MessageBackfill | BackfillCompleted> {\n const start = performance.now();\n const [rows, bytes] = await tx.processReadTask(sql =>\n Promise.all([\n sql.unsafe<{totalRows: bigint}[]>(getTotalRows),\n sql.unsafe<{totalBytes: bigint}[]>(getTotalBytes),\n ]),\n );\n const status: DownloadStatus = {\n rows: 0,\n totalRows: Number(rows[0].totalRows),\n totalBytes: Number(bytes[0].totalBytes),\n };\n\n let elapsed = (performance.now() - start).toFixed(3);\n lc.info?.(`Computed total rows and bytes for: ${select} (${elapsed} ms)`, {\n status,\n });\n const copyStream = await tx.processReadTask(sql =>\n sql.unsafe(`COPY (${select}) TO STDOUT`).readable(),\n );\n\n const tsvParser = new TsvParser();\n let totalBytes = 0;\n let totalMsgs = 0;\n let rowValues: JSONValue[][] = [];\n let bufferedBytes = 0;\n\n const logFlushed = () => {\n lc.debug?.(\n `Flushed ${rowValues.length} rows, ${bufferedBytes} bytes ` +\n `(total: rows=${status.rows}, msgs=${totalMsgs}, bytes=${totalBytes})`,\n );\n };\n\n // Tracks the row being parsed.\n let row: JSONValue[] = Array.from({length: colParsers.length});\n let col = 0;\n\n for await (const data of copyStream) {\n const chunk = data as Buffer;\n for (const text of tsvParser.parse(chunk)) {\n row[col] = text === null ? null : (colParsers[col](text) as JSONValue);\n\n if (++col === colParsers.length) {\n rowValues.push(row);\n status.rows++;\n row = Array.from({length: colParsers.length});\n col = 0;\n }\n }\n bufferedBytes += chunk.byteLength;\n totalBytes += chunk.byteLength;\n\n if (bufferedBytes >= flushThresholdBytes) {\n yield {tag: 'backfill', ...backfill, rowValues, status};\n totalMsgs++;\n logFlushed();\n rowValues = [];\n bufferedBytes = 0;\n }\n }\n\n // Flush the last batch of rows.\n if (rowValues.length > 0) {\n yield {tag: 'backfill', ...backfill, rowValues, status};\n totalMsgs++;\n logFlushed();\n }\n\n yield {tag: 'backfill-completed', ...backfill, status};\n elapsed = (performance.now() - start).toFixed(3);\n lc.info?.(\n `Finished streaming ${status.rows} rows, ${totalMsgs} msgs, ${totalBytes} bytes ` +\n `(${elapsed} ms)`,\n );\n}\n\n/**\n * Creates (and drops) a replication slot in order to obtain a snapshot\n * that corresponds with a specific LSN. Sets the snapshot on the\n * TransactionPool and returns the watermark corresponding to the LSN.\n *\n * (Note that PG's other LSN-related functions are not scoped to a\n * transaction; this is the only way to get set a transaction at a specific\n * LSN.)\n */\nasync function createSnapshotTransaction(\n lc: LogContext,\n upstreamURI: string,\n db: PostgresDB,\n slotNamePrefix: string,\n) {\n const replicationSession = pgClient(lc, upstreamURI, {\n ['fetch_types']: false, // Necessary for the streaming protocol\n connection: {replication: 'database'}, // https://www.postgresql.org/docs/current/protocol-replication.html\n });\n const tempSlot = `${slotNamePrefix}_bf_${Date.now()}`;\n try {\n const {snapshot_name: snapshot, consistent_point: lsn} =\n await createReplicationSlot(lc, replicationSession, tempSlot);\n\n const {init, imported} = importSnapshot(snapshot);\n const tx = new TransactionPool(lc, {mode: READONLY, init}).run(db);\n await imported;\n await replicationSession.unsafe(`DROP_REPLICATION_SLOT \"${tempSlot}\"`);\n\n const watermark = toStateVersionString(lsn);\n lc.info?.(`Opened snapshot transaction at LSN ${lsn} (${watermark})`);\n return {tx, watermark};\n } catch (e) {\n // In the event of a failure, clean up the replication slot if created.\n await replicationSession.unsafe(\n /*sql*/\n `SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots\n WHERE slot_name = '${tempSlot}'`,\n );\n lc.error?.(`Failed to create backfill snapshot`, e);\n throw e;\n } finally {\n await replicationSession.end();\n }\n}\n\nfunction validateSchema(\n tx: TransactionPool,\n publications: string[],\n bf: BackfillRequest,\n watermark: string,\n): Promise<{\n tableSpec: PublishedTableSpec;\n backfill: BackfillParams;\n}> {\n return tx.processReadTask(async sql => {\n const {tables} = await getPublicationInfo(sql, publications);\n const spec = tables.find(\n spec => spec.schema === bf.table.schema && spec.name === bf.table.name,\n );\n if (!spec) {\n throw new SchemaIncompatibilityError(\n bf,\n `Table has been renamed or dropped`,\n );\n }\n const tableMeta = v.parse(bf.table.metadata, tableMetadataSchema);\n if (spec.schemaOID !== tableMeta.schemaOID) {\n throw new SchemaIncompatibilityError(\n bf,\n `Schema no longer corresponds to the original schema`,\n );\n }\n if (spec.oid !== tableMeta.relationOID) {\n throw new SchemaIncompatibilityError(\n bf,\n `Table no longer corresponds to the original table`,\n );\n }\n if (\n !equals(\n new Set(Object.keys(tableMeta.rowKey)),\n new Set(spec.replicaIdentityColumns),\n )\n ) {\n throw new SchemaIncompatibilityError(\n bf,\n 'Row key (e.g. PRIMARY KEY or INDEX) has changed',\n );\n }\n const allCols = [\n ...Object.entries(tableMeta.rowKey),\n ...Object.entries(bf.columns),\n ];\n for (const [col, val] of allCols) {\n const colSpec = spec.columns[col];\n if (!colSpec) {\n throw new SchemaIncompatibilityError(\n bf,\n `Column ${col} has been renamed or dropped`,\n );\n }\n const colMeta = v.parse(val, columnMetadataSchema);\n if (colMeta.attNum !== colSpec.pos) {\n throw new SchemaIncompatibilityError(\n bf,\n `Column ${col} no longer corresponds to the original column`,\n );\n }\n }\n const backfill: BackfillParams = {\n relation: {\n schema: bf.table.schema,\n name: bf.table.name,\n rowKey: {columns: Object.keys(tableMeta.rowKey)},\n },\n columns: Object.keys(bf.columns).filter(\n col => !(col in tableMeta.rowKey),\n ),\n watermark,\n };\n return {tableSpec: spec, backfill};\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;AAgDA,IAAM,2BAA2B,KAAK;;;;;;;AAQtC,gBAAuB,eACrB,IACA,aACA,EAAC,MAAM,gBACP,IACA,OAAsB,EAAE,EAC6B;AACrD,MAAK,GACF,YAAY,aAAa,WAAW,CACpC,YAAY,SAAS,GAAG,MAAM,KAAK;CAEtC,MAAM,EAAC,sBAAsB,6BAA4B;CACzD,MAAM,KAAK,SAAS,IAAI,aAAa;EACnC,YAAY,GAAE,qBAAqB,mBAAkB;GACpD,iBAAiB;EACnB,CAAC;CACF,IAAI;CACJ,IAAI;AACJ,KAAI;AACF,GAAC,CAAC,IAAI,aAAa,MAAM,0BACvB,IACA,aACA,IACA,KACD;EACD,MAAM,EAAC,WAAW,aAAY,MAAM,eAClC,IACA,cACA,IACA,UAC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AM,IAAI,2BACR,IACA,UAAU,IAAI,8BACf;AAGH,OADgB,MAAQ,KAAK,qBAAqB,CACtC,WAAW,QAAQ,IAC7B,OAAM,IAAI,2BACR,IACA,UAAU,IAAI,+CACf;;AAcL,SAAO;GAAC,WAAW;GAAM,UAXQ;IAC/B,UAAU;KACR,QAAQ,GAAG,MAAM;KACjB,MAAM,GAAG,MAAM;KACf,QAAQ,EAAC,SAAS,OAAO,KAAK,UAAU,OAAO,EAAC;KACjD;IACD,SAAS,OAAO,KAAK,GAAG,QAAQ,CAAC,QAC/B,QAAO,EAAE,OAAO,UAAU,QAC3B;IACD;IACD;GACiC;GAClC"}
1
+ {"version":3,"file":"backfill-stream.js","names":[],"sources":["../../../../../../../zero-cache/src/services/change-source/pg/backfill-stream.ts"],"sourcesContent":["import {\n PG_UNDEFINED_COLUMN,\n PG_UNDEFINED_TABLE,\n} from '@drdgvhbh/postgres-error-codes';\nimport type {LogContext} from '@rocicorp/logger';\nimport postgres from 'postgres';\nimport {assert} from '../../../../../shared/src/asserts.ts';\nimport {equals} from '../../../../../shared/src/set-utils.ts';\nimport * as v from '../../../../../shared/src/valita.ts';\nimport {READONLY} from '../../../db/mode-enum.ts';\nimport {\n BinaryCopyParser,\n hasBinaryDecoder,\n makeBinaryDecoder,\n textCastDecoder,\n} from '../../../db/pg-copy-binary.ts';\nimport {TsvParser} from '../../../db/pg-copy.ts';\nimport {getTypeParsers} from '../../../db/pg-type-parser.ts';\nimport type {PublishedTableSpec} from '../../../db/specs.ts';\nimport {importSnapshot, TransactionPool} from '../../../db/transaction-pool.ts';\nimport {pgClient, type PostgresDB} from '../../../types/pg.ts';\nimport {SchemaIncompatibilityError} from '../common/backfill-manager.ts';\nimport type {\n BackfillCompleted,\n BackfillRequest,\n DownloadStatus,\n JSONValue,\n MessageBackfill,\n} from '../protocol/current.ts';\nimport {\n columnMetadataSchema,\n tableMetadataSchema,\n} from './backfill-metadata.ts';\nimport {\n createReplicationSlot,\n makeBinarySelectExprs,\n makeDownloadStatements,\n type DownloadStatements,\n} from './initial-sync.ts';\nimport {toStateVersionString} from './lsn.ts';\nimport {getPublicationInfo} from './schema/published.ts';\nimport type {Replica} from './schema/shard.ts';\n\ntype BackfillParams = Omit<BackfillCompleted, 'tag'>;\n\ntype StreamOptions = {\n /**\n * The number of bytes at which to flush a batch of rows in a\n * backfill message. Defaults to Node's getDefaultHighWatermark().\n */\n flushThresholdBytes?: number | undefined;\n\n /**\n * Use text-format COPY instead of binary COPY.\n * Binary is faster and handles all types (unknown types are cast to\n * `::text` in the SELECT). This flag exists as an escape hatch to\n * revert to the old code path if needed.\n */\n textCopy?: boolean | undefined;\n};\n\n// The size of chunks that Postgres sends on COPY stream.\n// This happens to match NodeJS's getDefaultHighWatermark()\n// (for Node v20+).\nconst POSTGRES_COPY_CHUNK_SIZE = 64 * 1024;\n\n// Matches the exact clauses emitted by makeDownloadStatements; quoted\n// identifiers like \"limit\" won't match because they lack the surrounding\n// whitespace.\nconst SAMPLE_OR_LIMIT_RE = /\\sTABLESAMPLE\\s+BERNOULLI\\b|\\sLIMIT\\s+\\d/i;\n\n/**\n * Streams a series of `backfill` messages (ending with `backfill-complete`)\n * at a set watermark (i.e. LSN). The data is retrieved via a COPY stream\n * made at a transaction snapshot corresponding to specific LSN, obtained by\n * creating a short-lived replication slot.\n */\nexport async function* streamBackfill(\n lc: LogContext,\n upstreamURI: string,\n {slot, publications}: Pick<Replica, 'slot' | 'publications'>,\n bf: BackfillRequest,\n opts: StreamOptions = {},\n): AsyncGenerator<MessageBackfill | BackfillCompleted> {\n lc = lc\n .withContext('component', 'backfill')\n .withContext('table', bf.table.name);\n\n const {flushThresholdBytes = POSTGRES_COPY_CHUNK_SIZE, textCopy = false} =\n opts;\n const db = pgClient(lc, upstreamURI, 'backfill-stream', {\n ['max_lifetime']: 120 * 60, // set a long (2h) limit for COPY streaming\n });\n let tx: TransactionPool | undefined;\n let watermark: string;\n try {\n ({tx, watermark} = await createSnapshotTransaction(\n lc,\n upstreamURI,\n db,\n slot,\n ));\n const {tableSpec, backfill} = await validateSchema(\n tx,\n publications,\n bf,\n watermark,\n );\n\n // Note: validateSchema ensures that the rowKey and columns are disjoint\n const {relation, columns} = backfill;\n const cols = [...relation.rowKey.columns, ...columns];\n const stmts = makeDownloadStatements(tableSpec, cols);\n\n if (textCopy) {\n const types = await getTypeParsers(db, {returnJsonAsString: true});\n yield* stream(\n lc,\n tx,\n backfill,\n stmts,\n `COPY (${stmts.select}) TO STDOUT`,\n new TsvParser(),\n cols.map(col => {\n const parser = types.getTypeParser(tableSpec.columns[col].typeOID);\n return (text: string) => parser(text) as JSONValue;\n }),\n flushThresholdBytes,\n );\n } else {\n const binaryStmts = makeDownloadStatements(\n tableSpec,\n cols,\n undefined,\n undefined,\n makeBinarySelectExprs(tableSpec, cols),\n );\n\n yield* stream(\n lc,\n tx,\n backfill,\n stmts,\n `COPY (${binaryStmts.select}) TO STDOUT WITH (FORMAT binary)`,\n new BinaryCopyParser(),\n cols.map(col => {\n const spec = tableSpec.columns[col];\n const decoder = hasBinaryDecoder(spec)\n ? makeBinaryDecoder(spec)\n : textCastDecoder;\n return (buf: Buffer) => decoder(buf) as unknown as JSONValue;\n }),\n flushThresholdBytes,\n );\n }\n } catch (e) {\n // Although we make the best effort to validate the schema at the\n // transaction snapshot, certain forms of `ALTER TABLE` are not\n // MVCC safe and not \"frozen\" in the snapshot:\n //\n // https://www.postgresql.org/docs/current/mvcc-caveats.html\n //\n // Handle these errors as schema incompatibility errors rather than\n // unknown runtime errors.\n if (\n e instanceof postgres.PostgresError &&\n (e.code === PG_UNDEFINED_TABLE || e.code === PG_UNDEFINED_COLUMN)\n ) {\n throw new SchemaIncompatibilityError(bf, String(e), {cause: e});\n }\n throw e;\n } finally {\n tx?.setDone();\n // Workaround postgres.js hanging at the end of some COPY commands:\n // https://github.com/porsager/postgres/issues/499\n void db.end().catch(e => lc.warn?.(`error closing backfill connection`, e));\n }\n}\n\nasync function* stream<T>(\n lc: LogContext,\n tx: TransactionPool,\n backfill: BackfillParams,\n {\n getTotalRows,\n getTotalBytes,\n }: Pick<DownloadStatements, 'getTotalRows' | 'getTotalBytes'>,\n copyCommand: string,\n parser: {parse(chunk: Buffer): Iterable<T | null>},\n decoders: ((field: T) => JSONValue)[],\n flushThresholdBytes: number,\n): AsyncGenerator<MessageBackfill | BackfillCompleted> {\n // Backfill must read every row: TABLESAMPLE / LIMIT are reserved for shadow\n // sync and must never appear in a backfill COPY.\n assert(\n !SAMPLE_OR_LIMIT_RE.test(copyCommand),\n `backfill COPY must not sample or limit: ${copyCommand}`,\n );\n const start = performance.now();\n const [rows, bytes] = await tx.processReadTask(sql =>\n Promise.all([\n sql.unsafe<{totalRows: bigint}[]>(getTotalRows),\n sql.unsafe<{totalBytes: bigint}[]>(getTotalBytes),\n ]),\n );\n const status: DownloadStatus = {\n rows: 0,\n totalRows: Number(rows[0].totalRows),\n totalBytes: Number(bytes[0].totalBytes),\n };\n\n let elapsed = (performance.now() - start).toFixed(3);\n lc.info?.(\n `Computed total rows and bytes for: ${copyCommand} (${elapsed} ms)`,\n {\n status,\n },\n );\n const copyStream = await tx.processReadTask(sql =>\n sql.unsafe(copyCommand).readable(),\n );\n\n let totalBytes = 0;\n let totalMsgs = 0;\n let rowValues: JSONValue[][] = [];\n let bufferedBytes = 0;\n\n const logFlushed = () => {\n lc.debug?.(\n `Flushed ${rowValues.length} rows, ${bufferedBytes} bytes ` +\n `(total: rows=${status.rows}, msgs=${totalMsgs}, bytes=${totalBytes})`,\n );\n };\n\n // Tracks the row being parsed.\n let row: JSONValue[] = Array.from({length: decoders.length});\n let col = 0;\n\n for await (const data of copyStream) {\n const chunk = data as Buffer;\n for (const field of parser.parse(chunk)) {\n row[col] = field === null ? null : decoders[col](field);\n\n if (++col === decoders.length) {\n rowValues.push(row);\n status.rows++;\n row = Array.from({length: decoders.length});\n col = 0;\n }\n }\n bufferedBytes += chunk.byteLength;\n totalBytes += chunk.byteLength;\n\n if (bufferedBytes >= flushThresholdBytes) {\n yield {tag: 'backfill', ...backfill, rowValues, status};\n totalMsgs++;\n logFlushed();\n rowValues = [];\n bufferedBytes = 0;\n }\n }\n\n // Flush the last batch of rows.\n if (rowValues.length > 0) {\n yield {tag: 'backfill', ...backfill, rowValues, status};\n totalMsgs++;\n logFlushed();\n }\n\n yield {tag: 'backfill-completed', ...backfill, status};\n elapsed = (performance.now() - start).toFixed(3);\n lc.info?.(\n `Finished streaming ${status.rows} rows, ${totalMsgs} msgs, ${totalBytes} bytes ` +\n `(${elapsed} ms)`,\n );\n}\n\n/**\n * Creates (and drops) a replication slot in order to obtain a snapshot\n * that corresponds with a specific LSN. Sets the snapshot on the\n * TransactionPool and returns the watermark corresponding to the LSN.\n *\n * (Note that PG's other LSN-related functions are not scoped to a\n * transaction; this is the only way to get set a transaction at a specific\n * LSN.)\n */\nasync function createSnapshotTransaction(\n lc: LogContext,\n upstreamURI: string,\n db: PostgresDB,\n slotNamePrefix: string,\n) {\n const replicationSession = pgClient(\n lc,\n upstreamURI,\n 'backfill-replication-session',\n {\n ['fetch_types']: false, // Necessary for the streaming protocol\n connection: {replication: 'database'}, // https://www.postgresql.org/docs/current/protocol-replication.html\n },\n );\n const tempSlot = `${slotNamePrefix}_bf_${Date.now()}`;\n try {\n const {snapshot_name: snapshot, consistent_point: lsn} =\n await createReplicationSlot(lc, replicationSession, tempSlot);\n\n const {init, imported} = importSnapshot(snapshot);\n const tx = new TransactionPool(lc, {mode: READONLY, init}).run(db);\n await imported;\n await replicationSession.unsafe(`DROP_REPLICATION_SLOT \"${tempSlot}\"`);\n\n const watermark = toStateVersionString(lsn);\n lc.info?.(`Opened snapshot transaction at LSN ${lsn} (${watermark})`);\n return {tx, watermark};\n } catch (e) {\n // In the event of a failure, clean up the replication slot if created.\n await replicationSession.unsafe(\n /*sql*/\n `SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots\n WHERE slot_name = '${tempSlot}'`,\n );\n lc.error?.(`Failed to create backfill snapshot`, e);\n throw e;\n } finally {\n await replicationSession.end();\n }\n}\n\nfunction validateSchema(\n tx: TransactionPool,\n publications: string[],\n bf: BackfillRequest,\n watermark: string,\n): Promise<{\n tableSpec: PublishedTableSpec;\n backfill: BackfillParams;\n}> {\n return tx.processReadTask(async sql => {\n const {tables} = await getPublicationInfo(sql, publications);\n const spec = tables.find(\n spec => spec.schema === bf.table.schema && spec.name === bf.table.name,\n );\n if (!spec) {\n throw new SchemaIncompatibilityError(\n bf,\n `Table has been renamed or dropped`,\n );\n }\n const tableMeta = v.parse(bf.table.metadata, tableMetadataSchema);\n if (spec.schemaOID !== tableMeta.schemaOID) {\n throw new SchemaIncompatibilityError(\n bf,\n `Schema no longer corresponds to the original schema`,\n );\n }\n if (spec.oid !== tableMeta.relationOID) {\n throw new SchemaIncompatibilityError(\n bf,\n `Table no longer corresponds to the original table`,\n );\n }\n if (\n !equals(\n new Set(Object.keys(tableMeta.rowKey)),\n new Set(spec.replicaIdentityColumns),\n )\n ) {\n throw new SchemaIncompatibilityError(\n bf,\n 'Row key (e.g. PRIMARY KEY or INDEX) has changed',\n );\n }\n const allCols = [\n ...Object.entries(tableMeta.rowKey),\n ...Object.entries(bf.columns),\n ];\n for (const [col, val] of allCols) {\n const colSpec = spec.columns[col];\n if (!colSpec) {\n throw new SchemaIncompatibilityError(\n bf,\n `Column ${col} has been renamed or dropped`,\n );\n }\n const colMeta = v.parse(val, columnMetadataSchema);\n if (colMeta.attNum !== colSpec.pos) {\n throw new SchemaIncompatibilityError(\n bf,\n `Column ${col} no longer corresponds to the original column`,\n );\n }\n }\n const backfill: BackfillParams = {\n relation: {\n schema: bf.table.schema,\n name: bf.table.name,\n rowKey: {columns: Object.keys(tableMeta.rowKey)},\n },\n columns: Object.keys(bf.columns).filter(\n col => !(col in tableMeta.rowKey),\n ),\n watermark,\n };\n return {tableSpec: spec, backfill};\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAgEA,IAAM,2BAA2B,KAAK;AAKtC,IAAM,qBA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Q,CAAC,QAC/B,QAAO,EAAE,OAAO,UAAU,QAC3B;IACD;IACD;GACiC;GAClC"}
@@ -1 +1 @@
1
- {"version":3,"file":"change-source.d.ts","sourceRoot":"","sources":["../../../../../../../zero-cache/src/services/change-source/pg/change-source.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAejD,OAAO,KAAK,CAAC,MAAM,qCAAqC,CAAC;AAOzD,OAAO,KAAK,EAGV,kBAAkB,EACnB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAC,KAAK,WAAW,EAAC,MAAM,gCAAgC,CAAC;AAGhE,OAAO,EAEL,KAAK,WAAW,EAEjB,MAAM,0BAA0B,CAAC;AAKlC,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,2BAA2B,CAAC;AAEpD,OAAO,EAEL,KAAK,iBAAiB,EAEvB,MAAM,8CAA8C,CAAC;AACtD,OAAO,KAAK,EAAC,YAAY,EAAe,MAAM,qBAAqB,CAAC;AAEpE,OAAO,EAEL,KAAK,QAAQ,EACd,MAAM,wCAAwC,CAAC;AAchD,OAAO,KAAK,EAEV,mBAAmB,EAEpB,MAAM,mCAAmC,CAAC;AAG3C,OAAO,EAEL,KAAK,kBAAkB,EACvB,KAAK,aAAa,EACnB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAGV,eAAe,IAAI,gBAAgB,EACpC,MAAM,yCAAyC,CAAC;AAuBjD;;;;GAIG;AACH,wBAAsB,8BAA8B,CAClD,EAAE,EAAE,UAAU,EACd,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,WAAW,EAClB,aAAa,EAAE,MAAM,EACrB,WAAW,EAAE,kBAAkB,EAC/B,OAAO,EAAE,aAAa,EACtB,mBAAmB,SAAI,GACtB,OAAO,CAAC;IAAC,iBAAiB,EAAE,iBAAiB,CAAC;IAAC,YAAY,EAAE,YAAY,CAAA;CAAC,CAAC,CAsC7E;AAqcD,qBAAa,KAAM,YAAW,QAAQ;;gBAIxB,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC;IAI9B,QAAQ,CAAC,MAAM,EAAE,mBAAmB,GAAG,IAAI;IAgC3C,GAAG,CAAC,SAAS,EAAE,WAAW;CAoB3B;AAED,QAAA,MAAM,eAAe;;;;aAInB,CAAC;AAEH,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AA2wBxD,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,kBAAkB,EAAE,CAAC,EAAE,gBAAgB,WAwB3E"}
1
+ {"version":3,"file":"change-source.d.ts","sourceRoot":"","sources":["../../../../../../../zero-cache/src/services/change-source/pg/change-source.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAejD,OAAO,KAAK,CAAC,MAAM,qCAAqC,CAAC;AAOzD,OAAO,KAAK,EAGV,kBAAkB,EACnB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAC,KAAK,WAAW,EAAC,MAAM,gCAAgC,CAAC;AAGhE,OAAO,EAEL,KAAK,WAAW,EAEjB,MAAM,0BAA0B,CAAC;AAKlC,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,2BAA2B,CAAC;AAEpD,OAAO,EAEL,KAAK,iBAAiB,EAEvB,MAAM,8CAA8C,CAAC;AACtD,OAAO,KAAK,EAAC,YAAY,EAAe,MAAM,qBAAqB,CAAC;AAEpE,OAAO,EAEL,KAAK,QAAQ,EACd,MAAM,wCAAwC,CAAC;AAchD,OAAO,KAAK,EAEV,mBAAmB,EAEpB,MAAM,mCAAmC,CAAC;AAG3C,OAAO,EAEL,KAAK,kBAAkB,EACvB,KAAK,aAAa,EACnB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAGV,eAAe,IAAI,gBAAgB,EACpC,MAAM,yCAAyC,CAAC;AAuBjD;;;;GAIG;AACH,wBAAsB,8BAA8B,CAClD,EAAE,EAAE,UAAU,EACd,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,WAAW,EAClB,aAAa,EAAE,MAAM,EACrB,WAAW,EAAE,kBAAkB,EAC/B,OAAO,EAAE,aAAa,EACtB,mBAAmB,SAAI,GACtB,OAAO,CAAC;IAAC,iBAAiB,EAAE,iBAAiB,CAAC;IAAC,YAAY,EAAE,YAAY,CAAA;CAAC,CAAC,CAuC7E;AA4cD,qBAAa,KAAM,YAAW,QAAQ;;gBAIxB,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC;IAI9B,QAAQ,CAAC,MAAM,EAAE,mBAAmB,GAAG,IAAI;IAgC3C,GAAG,CAAC,SAAS,EAAE,WAAW;CAoB3B;AAED,QAAA,MAAM,eAAe;;;;aAInB,CAAC;AAEH,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AAgxBxD,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,kBAAkB,EAAE,CAAC,EAAE,gBAAgB,WAwB3E"}
@@ -43,11 +43,11 @@ async function initializePostgresChangeSource(lc, upstreamURI, shard, replicaDbF
43
43
  const replica = new Database(lc, replicaDbFile);
44
44
  const subscriptionState = getSubscriptionStateAndContext(new StatementRunner(replica));
45
45
  replica.close();
46
- const db = pgClient(lc, upstreamURI);
46
+ const db = pgClient(lc, upstreamURI, "change-source-init");
47
47
  try {
48
48
  return {
49
49
  subscriptionState,
50
- changeSource: new PostgresChangeSource(lc, upstreamURI, shard, await checkAndUpdateUpstream(lc, db, shard, subscriptionState), context, lagReportIntervalMs)
50
+ changeSource: new PostgresChangeSource(lc, upstreamURI, shard, await checkAndUpdateUpstream(lc, db, shard, subscriptionState), context, lagReportIntervalMs, syncOptions.textCopy)
51
51
  };
52
52
  } finally {
53
53
  await db.end();
@@ -91,17 +91,18 @@ var PostgresChangeSource = class {
91
91
  #replica;
92
92
  #context;
93
93
  #lagReporter;
94
- constructor(lc, upstreamUri, shard, replica, context, lagReportIntervalMs) {
94
+ #textCopy;
95
+ constructor(lc, upstreamUri, shard, replica, context, lagReportIntervalMs, textCopy) {
95
96
  this.#lc = lc.withContext("component", "change-source");
96
- this.#db = pgClient(lc, upstreamUri, {
97
+ this.#db = pgClient(lc, upstreamUri, "replication-monitor", {
97
98
  max: 1,
98
- ["idle_timeout"]: 60,
99
- connection: { ["application_name"]: "zero-replication-monitor" }
99
+ ["idle_timeout"]: 60
100
100
  });
101
101
  this.#upstreamUri = upstreamUri;
102
102
  this.#shard = shard;
103
103
  this.#replica = replica;
104
104
  this.#context = context;
105
+ this.#textCopy = textCopy ?? false;
105
106
  this.#lagReporter = lagReportIntervalMs > 0 ? new LagReporter(lc.withContext("component", "lag-reporter"), shard, this.#db, lagReportIntervalMs) : null;
106
107
  }
107
108
  async stop() {
@@ -131,10 +132,10 @@ var PostgresChangeSource = class {
131
132
  const { messages, acks } = await subscribe(this.#lc, this.#db, slot, [...shardConfig.publications], clientStart);
132
133
  const acker = new Acker(acks);
133
134
  const changes = new ChangeStreamMultiplexer(this.#lc, clientWatermark);
134
- const backfillManager = new BackfillManager(this.#lc, changes, (req) => streamBackfill(this.#lc, this.#upstreamUri, this.#replica, req));
135
+ const backfillManager = new BackfillManager(this.#lc, changes, (req) => streamBackfill(this.#lc, this.#upstreamUri, this.#replica, req, { textCopy: this.#textCopy }));
135
136
  changes.addProducers(messages, backfillManager).addListeners(backfillManager, acker);
136
137
  backfillManager.run(clientWatermark, backfillRequests);
137
- const changeMaker = new ChangeMaker(this.#lc, this.#shard, shardConfig, this.#db, this.#replica.initialSchema);
138
+ const changeMaker = new ChangeMaker(this.#shard, shardConfig, this.#db, this.#replica.initialSchema);
138
139
  /**
139
140
  * Determines if the incoming message is transactional, otherwise handling
140
141
  * non-transactional messages with a downstream status message.
@@ -174,7 +175,7 @@ var PostgresChangeSource = class {
174
175
  reservation = {};
175
176
  }
176
177
  let lastChange;
177
- for (const change of await changeMaker.makeChanges(lsn, msg)) {
178
+ for (const change of await changeMaker.makeChanges(this.#lc.withContext("lsn", fromBigInt(lsn)), lsn, msg)) {
178
179
  await changes.push(change);
179
180
  lastChange = change;
180
181
  }
@@ -465,27 +466,25 @@ var LagReporter = class LagReporter {
465
466
  };
466
467
  var SET_REPLICA_IDENTITY_DELAY_MS = 50;
467
468
  var ChangeMaker = class {
468
- #lc;
469
469
  #shardPrefix;
470
470
  #shardConfig;
471
471
  #initialSchema;
472
472
  #db;
473
473
  #replicaIdentityTimer;
474
474
  #error;
475
- constructor(lc, { appID, shardNum }, shardConfig, db, initialSchema) {
476
- this.#lc = lc;
475
+ constructor({ appID, shardNum }, shardConfig, db, initialSchema) {
477
476
  this.#shardPrefix = `${appID}/${shardNum}`;
478
477
  this.#shardConfig = shardConfig;
479
478
  this.#initialSchema = initialSchema;
480
479
  this.#db = db;
481
480
  }
482
- async makeChanges(lsn, msg) {
481
+ async makeChanges(lc, lsn, msg) {
483
482
  if (this.#error) {
484
- this.#logError(this.#error);
483
+ this.#logError(lc, this.#error);
485
484
  return [];
486
485
  }
487
486
  try {
488
- return await this.#makeChanges(msg);
487
+ return await this.#makeChanges(lc, msg);
489
488
  } catch (err) {
490
489
  this.#error = {
491
490
  lsn,
@@ -493,7 +492,7 @@ var ChangeMaker = class {
493
492
  err,
494
493
  lastLogTime: 0
495
494
  };
496
- this.#logError(this.#error);
495
+ this.#logError(lc, this.#error);
497
496
  const message = `Unable to continue replication from LSN ${fromBigInt(lsn)}`;
498
497
  const errorDetails = { error: message };
499
498
  if (err instanceof UnsupportedSchemaChangeError) {
@@ -507,18 +506,18 @@ var ChangeMaker = class {
507
506
  }]];
508
507
  }
509
508
  }
510
- #logError(error) {
509
+ #logError(lc, error) {
511
510
  const { lsn, msg, err, lastLogTime } = error;
512
511
  const now = Date.now();
513
512
  if (now - lastLogTime > 6e4) {
514
- this.#lc.error?.(`Unable to continue replication from LSN ${fromBigInt(lsn)}: ${String(err)}`, err instanceof UnsupportedSchemaChangeError ? err.event.context : {
513
+ lc.error?.(`Unable to continue replication from LSN ${fromBigInt(lsn)}: ${String(err)}`, err instanceof UnsupportedSchemaChangeError ? err.event.context : {
515
514
  ...msg,
516
515
  content: void 0
517
516
  });
518
517
  error.lastLogTime = now;
519
518
  }
520
519
  }
521
- async #makeChanges(msg) {
520
+ async #makeChanges(lc, msg) {
522
521
  switch (msg.tag) {
523
522
  case "begin": return [[
524
523
  "begin",
@@ -550,34 +549,33 @@ var ChangeMaker = class {
550
549
  }]];
551
550
  case "message":
552
551
  if (!msg.prefix.startsWith(this.#shardPrefix)) {
553
- this.#lc.debug?.("ignoring message for different shard", msg.prefix);
552
+ lc.debug?.("ignoring message for different shard", msg.prefix);
554
553
  return [];
555
554
  }
556
555
  switch (msg.prefix.substring(this.#shardPrefix.length)) {
557
556
  case "":
558
- case "/ddl": return this.#handleDdlMessage(msg);
557
+ case "/ddl": return this.#handleDdlMessage(lc, msg);
559
558
  default:
560
- this.#lc.debug?.("ignoring unknown message type", msg.prefix);
559
+ lc.debug?.("ignoring unknown message type", msg.prefix);
561
560
  return [];
562
561
  }
563
- case "commit":
564
- this.#lastReplicationEventInTx = void 0;
565
- return [[
566
- "commit",
567
- msg,
568
- { watermark: toStateVersionString(must(msg.commitLsn)) }
569
- ]];
570
- case "relation": return this.#handleRelation(msg);
562
+ case "commit": return [[
563
+ "commit",
564
+ msg,
565
+ { watermark: toStateVersionString(must(msg.commitLsn)) }
566
+ ]];
567
+ case "relation": return await this.#handleRelation(msg);
571
568
  case "type": return [];
572
569
  case "origin": return [];
573
570
  default: throw new Error(`Unexpected message type ${stringify(msg)}`);
574
571
  }
575
572
  }
576
- #lastReplicationEventInTx;
577
- #handleDdlMessage(msg) {
573
+ #lastReplicationEvent;
574
+ #handleDdlMessage(lc, msg) {
578
575
  const event = parseLogicalMessageContent(msg, replicationEventSchema);
576
+ lc = lc.withContext("tag", event.event.tag).withContext("query", event.context.query);
579
577
  clearTimeout(this.#replicaIdentityTimer);
580
- let prevEvent = this.#lastReplicationEventInTx;
578
+ let prevEvent = this.#lastReplicationEvent;
581
579
  const { type } = event;
582
580
  switch (type) {
583
581
  case "ddlStart":
@@ -586,24 +584,24 @@ var ChangeMaker = class {
586
584
  assert(prevEvent, `ddlUpdate received without a ddlStart`);
587
585
  break;
588
586
  default:
589
- this.#lc.info?.(`ignoring unknown ddl message type: ${type}`);
587
+ lc.info?.(`ignoring unknown ddl message type: ${type}`);
590
588
  return [];
591
589
  }
592
- this.#lastReplicationEventInTx = event;
590
+ this.#lastReplicationEvent = event;
593
591
  if (!prevEvent) {
594
- this.#lc.info?.(`received ${msg.prefix}/${type} event`, event);
592
+ lc.info?.(`received ${msg.prefix}/${type} event`, event);
595
593
  return [];
596
594
  }
597
- this.#lc.info?.(`processing ${msg.prefix}/${type} event`, event);
595
+ lc.info?.(`processing ${msg.prefix}/${type} event`, event);
598
596
  const effectiveTag = prevEvent.type === "ddlStart" ? prevEvent.event.tag : event.event.tag;
599
- const changes = this.#makeSchemaChanges(prevEvent.schema, event, effectiveTag).map((change) => ["data", change]);
600
- this.#lc.withContext("tag", event.event.tag).withContext("query", event.context.query).info?.(`${changes.length} schema change(s)`, { changes });
597
+ const changes = this.#makeSchemaChanges(lc, prevEvent.schema, event, effectiveTag).map((change) => ["data", change]);
598
+ lc.info?.(`${changes.length} schema change(s)`, { changes });
601
599
  const replicaIdentities = replicaIdentitiesForTablesWithoutPrimaryKeys(event.schema);
602
600
  if (replicaIdentities) this.#replicaIdentityTimer = setTimeout(async () => {
603
601
  try {
604
- await replicaIdentities.apply(this.#lc, this.#db);
602
+ await replicaIdentities.apply(lc, this.#db);
605
603
  } catch (err) {
606
- this.#lc.warn?.(`error setting replica identities`, err);
604
+ lc.warn?.(`error setting replica identities`, err);
607
605
  }
608
606
  }, SET_REPLICA_IDENTITY_DELAY_MS);
609
607
  return changes;
@@ -635,12 +633,12 @@ var ChangeMaker = class {
635
633
  * columns. This, for example, would be needed to properly support changing
636
634
  * the type of a column that's indexed.
637
635
  */
638
- #makeSchemaChanges(preSchema, event, tag) {
636
+ #makeSchemaChanges(lc, preSchema, event, tag) {
639
637
  try {
640
638
  const [prevTbl, prevIdx] = specsByID(preSchema);
641
639
  const [nextTbl, nextIdx] = specsByID(event.schema);
642
640
  const changes = [];
643
- for (const table of nextTbl.values()) validate(this.#lc, table);
641
+ for (const table of nextTbl.values()) validate(lc, table);
644
642
  const [droppedIdx, createdIdx] = symmetricDifferences(prevIdx, nextIdx);
645
643
  const keptIdx = intersection(prevIdx, nextIdx);
646
644
  for (const id of keptIdx) if (isIndexStructurallyChanged(must(prevIdx.get(id)), must(nextIdx.get(id)), prevTbl, nextTbl)) {
@@ -669,7 +667,7 @@ var ChangeMaker = class {
669
667
  });
670
668
  }
671
669
  const tables = intersection(prevTbl, nextTbl);
672
- for (const id of tables) changes.push(...this.#getTableChanges(must(prevTbl.get(id)), must(nextTbl.get(id)), tag));
670
+ for (const id of tables) changes.push(...this.#getTableChanges(lc, must(prevTbl.get(id)), must(nextTbl.get(id)), tag));
673
671
  for (const id of createdTbl) {
674
672
  const spec = must(nextTbl.get(id));
675
673
  const createTable = {
@@ -692,7 +690,7 @@ var ChangeMaker = class {
692
690
  throw new UnsupportedSchemaChangeError(String(e), event, { cause: e });
693
691
  }
694
692
  }
695
- #getTableChanges(oldTable, newTable, ddlTag) {
693
+ #getTableChanges(lc, oldTable, newTable, ddlTag) {
696
694
  const changes = [];
697
695
  if (oldTable.schema !== newTable.schema || oldTable.name !== newTable.name) changes.push({
698
696
  tag: "rename-table",
@@ -768,7 +766,7 @@ var ChangeMaker = class {
768
766
  mapPostgresToLiteColumn(table.name, column);
769
767
  } catch (e) {
770
768
  if (!(e instanceof UnsupportedColumnDefaultError)) throw e;
771
- this.#lc.info?.(`Backfilling column ${table.name}.${name}: ${String(e)}`);
769
+ lc.info?.(`Backfilling column ${table.name}.${name}: ${String(e)}`);
772
770
  addColumn.column.spec.dflt = null;
773
771
  addColumn.backfill = { attNum: spec.pos };
774
772
  }