-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathWalStream.ts
More file actions
1309 lines (1161 loc) · 48.7 KB
/
WalStream.ts
File metadata and controls
1309 lines (1161 loc) · 48.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as lib_postgres from '@powersync/lib-service-postgres';
import {
container,
DatabaseConnectionError,
logger as defaultLogger,
ErrorCode,
Logger,
ReplicationAbortedError,
ReplicationAssertionError
} from '@powersync/lib-services-framework';
import {
BucketStorageBatch,
getUuidReplicaIdentityBson,
MetricsEngine,
RelationCache,
ReplicationLagTracker,
SaveUpdate,
SourceEntityDescriptor,
SourceTable,
storage
} from '@powersync/service-core';
import * as pgwire from '@powersync/service-jpgwire';
import {
applyValueContext,
CompatibilityContext,
HydratedSyncRules,
SqliteInputRow,
SqliteInputValue,
SqliteRow,
TablePattern,
ToastableSqliteRow,
toSyncRulesValue
} from '@powersync/service-sync-rules';
import { ReplicationMetric } from '@powersync/service-types';
import { PostgresTypeResolver } from '../types/resolver.js';
import { MissingReplicationSlotError } from './MissingReplicationSlotError.js';
import { PgManager } from './PgManager.js';
import { getPgOutputRelation, getRelId, referencedColumnTypeIds } from './PgRelation.js';
import { checkSourceConfiguration, checkTableRls, getReplicationIdentityColumns } from './replication-utils.js';
import {
ChunkedSnapshotQuery,
IdSnapshotQuery,
MissingRow,
PrimaryKeyValue,
SimpleSnapshotQuery,
SnapshotQuery
} from './SnapshotQuery.js';
import { computeWalBudgetReport, formatBytes, formatWalBudgetLine } from './wal-budget-utils.js';
export interface WalStreamOptions {
logger?: Logger;
connections: PgManager;
storage: storage.SyncRulesBucketStorage;
metrics: MetricsEngine;
abort_signal: AbortSignal;
/**
* Override snapshot chunk length (number of rows), for testing.
*
* Defaults to 10_000.
*
* Note that queries are streamed, so we don't actually keep that much data in memory.
*/
snapshotChunkLength?: number;
/**
* Called after each snapshot chunk is flushed, for testing.
* This allows tests to perform actions (like generating WAL) synchronously
* with the snapshot's chunk processing.
*/
onSnapshotChunkFlushed?: () => Promise<void>;
/**
* Interval for slot health checks during snapshot, in milliseconds.
* Set to 0 for every-chunk checking (useful for testing).
* Defaults to 120_000 (2 minutes).
*/
slotHealthCheckIntervalMs?: number;
}
interface InitResult {
/** True if initial snapshot is not yet done. */
needsInitialSync: boolean;
/** True if snapshot must be started from scratch with a new slot. */
needsNewSlot: boolean;
}
export const ZERO_LSN = '00000000/00000000';
export const PUBLICATION_NAME = 'powersync';
export const POSTGRES_DEFAULT_SCHEMA = 'public';
export const KEEPALIVE_CONTENT = 'ping';
export const KEEPALIVE_BUFFER = Buffer.from(KEEPALIVE_CONTENT);
export const KEEPALIVE_STATEMENT: pgwire.Statement = {
statement: /* sql */ `
SELECT
*
FROM
pg_logical_emit_message(FALSE, 'powersync', $1)
`,
params: [{ type: 'varchar', value: KEEPALIVE_CONTENT }]
} as const;
export const isKeepAliveMessage = (msg: pgwire.PgoutputMessage) => {
return (
msg.tag == 'message' &&
msg.prefix == 'powersync' &&
msg.content &&
Buffer.from(msg.content).equals(KEEPALIVE_BUFFER)
);
};
export const sendKeepAlive = async (db: pgwire.PgClient) => {
await lib_postgres.retriedQuery(db, KEEPALIVE_STATEMENT);
};
export class WalStream {
sync_rules: HydratedSyncRules;
group_id: number;
connection_id = 1;
private logger: Logger;
private readonly storage: storage.SyncRulesBucketStorage;
private readonly metrics: MetricsEngine;
private readonly slot_name: string;
private connections: PgManager;
private abort_signal: AbortSignal;
private relationCache = new RelationCache((relation: number | SourceTable) => {
if (typeof relation == 'number') {
return relation;
}
return relation.objectId!;
});
private startedStreaming = false;
private snapshotChunkLength: number;
private onSnapshotChunkFlushed?: () => Promise<void>;
private replicationLag = new ReplicationLagTracker();
private initialSnapshotPromise: Promise<void> | null = null;
private slotHealthCheckIntervalMs: number;
/** Cached max_slot_wal_keep_size in bytes. null = not yet queried. */
private maxSlotWalKeepSize: number | null = null;
/** Whether max_slot_wal_keep_size has been queried (distinguishes "not
* queried" from "queried and found unlimited"). */
private maxSlotWalKeepSizeQueried = false;
/** Previous safe_wal_size sample for rate calculation. */
private prevWalBudgetSample: { safeWalSize: number; timestamp: number } | null = null;
/** Timestamp of last slot health check. */
private lastSlotHealthCheckTime = 0;
constructor(options: WalStreamOptions) {
this.logger = options.logger ?? defaultLogger;
this.storage = options.storage;
this.metrics = options.metrics;
this.sync_rules = options.storage.getParsedSyncRules({ defaultSchema: POSTGRES_DEFAULT_SCHEMA });
this.group_id = options.storage.group_id;
this.slot_name = options.storage.slot_name;
this.connections = options.connections;
this.snapshotChunkLength = options.snapshotChunkLength ?? 10_000;
this.onSnapshotChunkFlushed = options.onSnapshotChunkFlushed;
this.slotHealthCheckIntervalMs = options.slotHealthCheckIntervalMs ?? 120_000;
this.abort_signal = options.abort_signal;
this.abort_signal.addEventListener(
'abort',
() => {
if (this.startedStreaming) {
// Ping to speed up cancellation of streaming replication
// We're not using pg_snapshot here, since it could be in the middle of
// an initial replication transaction.
const promise = sendKeepAlive(this.connections.pool);
promise.catch((e) => {
// Failures here are okay - this only speeds up stopping the process.
this.logger.warn('Failed to ping connection', e);
});
} else {
// If we haven't started streaming yet, it could be due to something like
// and invalid password. In that case, don't attempt to ping.
}
},
{ once: true }
);
}
get stopped() {
return this.abort_signal.aborted;
}
async getQualifiedTableNames(
batch: storage.BucketStorageBatch,
db: pgwire.PgConnection,
tablePattern: TablePattern
): Promise<storage.SourceTable[]> {
const schema = tablePattern.schema;
if (tablePattern.connectionTag != this.connections.connectionTag) {
return [];
}
let tableRows: any[];
const prefix = tablePattern.isWildcard ? tablePattern.tablePrefix : undefined;
{
let query = `
SELECT
c.oid AS relid,
c.relname AS table_name,
(SELECT
json_agg(DISTINCT a.atttypid)
FROM pg_attribute a
WHERE a.attnum > 0 AND NOT a.attisdropped AND a.attrelid = c.oid)
AS column_types
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = $1
AND c.relkind = 'r'`;
if (tablePattern.isWildcard) {
query += ' AND c.relname LIKE $2';
} else {
query += ' AND c.relname = $2';
}
const result = await db.query({
statement: query,
params: [
{ type: 'varchar', value: schema },
{ type: 'varchar', value: tablePattern.tablePattern }
]
});
tableRows = pgwire.pgwireRows(result);
}
let result: storage.SourceTable[] = [];
for (let row of tableRows) {
const name = row.table_name as string;
if (typeof row.relid != 'bigint') {
throw new ReplicationAssertionError(`Missing relid for ${name}`);
}
const relid = Number(row.relid as bigint);
if (prefix && !name.startsWith(prefix)) {
continue;
}
const rs = await db.query({
statement: `SELECT 1 FROM pg_publication_tables WHERE pubname = $1 AND schemaname = $2 AND tablename = $3`,
params: [
{ type: 'varchar', value: PUBLICATION_NAME },
{ type: 'varchar', value: tablePattern.schema },
{ type: 'varchar', value: name }
]
});
if (rs.rows.length == 0) {
this.logger.info(`Skipping ${tablePattern.schema}.${name} - not part of ${PUBLICATION_NAME} publication`);
continue;
}
try {
const result = await checkTableRls(db, relid);
if (!result.canRead) {
// We log the message, then continue anyway, since the check does not cover all cases.
this.logger.warn(result.message!);
}
} catch (e) {
// It's possible that we just don't have permission to access pg_roles - log the error and continue.
this.logger.warn(`Could not check RLS access for ${tablePattern.schema}.${name}`, e);
}
const cresult = await getReplicationIdentityColumns(db, relid);
const columnTypes = (JSON.parse(row.column_types) as string[]).map((e) => Number(e));
const table = await this.handleRelation({
batch,
descriptor: {
name,
schema,
objectId: relid,
replicaIdColumns: cresult.replicationColumns
} as SourceEntityDescriptor,
snapshot: false,
referencedTypeIds: columnTypes
});
result.push(table);
}
return result;
}
async initSlot(): Promise<InitResult> {
await checkSourceConfiguration(this.connections.pool, PUBLICATION_NAME);
await this.ensureStorageCompatibility();
const slotName = this.slot_name;
const status = await this.storage.getStatus();
const snapshotDone = status.snapshot_done && status.checkpoint_lsn != null;
if (snapshotDone) {
// Snapshot is done, but we still need to check the replication slot status
this.logger.info(`Initial replication already done`);
}
// Check if replication slot exists
const slot = pgwire.pgwireRows(
await this.connections.pool.query({
// We specifically want wal_status and invalidation_reason, but it's not available on older versions,
// so we just query *.
statement: 'SELECT * FROM pg_replication_slots WHERE slot_name = $1',
params: [{ type: 'varchar', value: slotName }]
})
)[0];
// Previously we also used pg_catalog.pg_logical_slot_peek_binary_changes to confirm that we can query the slot.
// However, there were some edge cases where the query times out, repeating the query, ultimately
// causing high load on the source database and never recovering automatically.
// We now instead jump straight to replication if the wal_status is not "lost", rather detecting those
// errors during streaming replication, which is a little more robust.
// We can have:
// 1. needsInitialSync: true, lost slot -> MissingReplicationSlotError (starts new sync rules version).
// Theoretically we could handle this the same as (2).
// 2. needsInitialSync: true, no slot -> create new slot
// 3. needsInitialSync: true, valid slot -> resume initial sync
// 4. needsInitialSync: false, lost slot -> MissingReplicationSlotError (starts new sync rules version)
// 5. needsInitialSync: false, no slot -> MissingReplicationSlotError (starts new sync rules version)
// 6. needsInitialSync: false, valid slot -> resume streaming replication
// The main advantage of MissingReplicationSlotError are:
// 1. If there was a complete snapshot already (cases 4/5), users can still sync from that snapshot while
// we do the reprocessing under a new slot name.
// 2. If there was a partial snapshot (case 1), we can start with the new slot faster by not waiting for
// the partial data to be cleared.
if (slot != null) {
// This checks that the slot is still valid
// wal_status is present in postgres 13+
// invalidation_reason is present in postgres 17+
const lost = slot.wal_status == 'lost';
if (lost) {
// Case 1 / 4
const fixGuidance =
slot.invalidation_reason === 'idle_timeout'
? `Increase idle_replication_slot_timeout on the source database.`
: `Increase max_slot_wal_keep_size on the source database and delete the existing slot to recover.`;
throw new MissingReplicationSlotError(
`[PSYNC_S1146] Replication slot ${slotName} was invalidated ` +
`(reason: ${slot.invalidation_reason ?? 'unknown'}). ` +
`${fixGuidance}`,
{
walStatus: 'lost',
phase: snapshotDone ? 'streaming' : 'snapshot',
invalidationReason: slot.invalidation_reason ?? undefined
}
);
}
// Case 3 / 6
return {
needsInitialSync: !snapshotDone,
needsNewSlot: false
};
} else {
if (snapshotDone) {
// Case 5
// This will create a new slot, while keeping the current sync rules active
throw new MissingReplicationSlotError(`Replication slot ${slotName} is missing`, {
walStatus: 'missing',
phase: 'streaming'
});
}
// Case 2
// This will clear data (if any) and re-create the same slot
return { needsInitialSync: true, needsNewSlot: true };
}
}
private async queryMaxSlotWalKeepSize(): Promise<number | null> {
if (this.maxSlotWalKeepSizeQueried) {
return this.maxSlotWalKeepSize;
}
this.maxSlotWalKeepSizeQueried = true;
try {
const rows = pgwire.pgwireRows(
await this.connections.pool.query({
statement: `SELECT setting, unit FROM pg_settings WHERE name = 'max_slot_wal_keep_size'`
})
);
if (rows.length === 0) {
// PG < 13 or setting doesn't exist
return null;
}
const setting = Number(rows[0].setting);
if (setting < 0) {
// -1 = unlimited
this.maxSlotWalKeepSize = null;
return null;
}
// setting is in MB, convert to bytes
const unit = rows[0].unit;
const multiplier = unit === 'kB' ? 1024 : unit === '8kB' ? 8192 : 1024 * 1024; // default MB
this.maxSlotWalKeepSize = setting * multiplier;
return this.maxSlotWalKeepSize;
} catch (e) {
// Non-fatal — budget reporting is best-effort
this.logger.warn(`Could not query max_slot_wal_keep_size`, e);
return null;
}
}
private async logWalBudget(slot: { safe_wal_size?: any; wal_status?: string }, now: number): Promise<void> {
const maxSize = await this.queryMaxSlotWalKeepSize();
// No limit configured
if (maxSize == null) {
this.logger.info(`WAL budget: no limit configured (max_slot_wal_keep_size is unlimited).`);
return;
}
const safeWalSize = slot.safe_wal_size != null ? Number(slot.safe_wal_size) : null;
if (safeWalSize == null) {
// safe_wal_size is null on PG < 13, or when no limit is set
return;
}
const report = computeWalBudgetReport({
safeWalSize,
maxSize,
walStatus: slot.wal_status ?? 'unknown',
prevSample: this.prevWalBudgetSample,
now
});
// Update sample for next rate calculation
this.prevWalBudgetSample = { safeWalSize, timestamp: now };
const budgetLine = formatWalBudgetLine(report);
if (report.isWarning) {
this.logger.warn(budgetLine);
this.logger.warn(
`Replication slot may be invalidated before snapshot completes. ` +
`Increase max_slot_wal_keep_size on the source database.`
);
} else {
this.logger.info(budgetLine);
}
}
/**
* Check if the replication slot is still valid. Called after each chunk
* flush during snapshot to detect slot invalidation early.
*
* The query hits pg_replication_slots (shared memory, not a table scan)
* and costs ~1-2ms per round-trip — negligible next to the per-chunk
* storage flush.
*/
private async checkSlotHealth(): Promise<void> {
// Ensure maxSlotWalKeepSize is populated for diagnostic context in error messages
await this.queryMaxSlotWalKeepSize();
const rows = pgwire.pgwireRows(
await this.connections.pool.query({
statement: 'SELECT * FROM pg_replication_slots WHERE slot_name = $1',
params: [{ type: 'varchar', value: this.slot_name }]
})
);
if (rows.length === 0) {
// Slot row gone from pg_replication_slots — dropped externally.
// Possible causes: pg_drop_replication_slot() call (operator, management tool, cleanup cron)
throw new MissingReplicationSlotError(
`[PSYNC_S1146] Replication slot ${this.slot_name} disappeared during snapshot.`,
{ walStatus: 'missing', phase: 'snapshot' }
);
}
const walStatus = rows[0].wal_status;
if (walStatus === 'lost') {
// Postgres marked the slot invalid. Possible invalidation_reason values (PG 14+):
// - wal_removed: WAL growth exceeded max_slot_wal_keep_size (the primary case)
// - wal_level_insufficient: wal_level changed away from 'logical'
// - idle_timeout (PG 18+): idle_replication_slot_timeout expired
// - rows_removed: catalog rows needed by the slot were vacuumed (safe to retry — fresh slot won't need them)
throw new MissingReplicationSlotError(
`[PSYNC_S1146] Replication slot ${this.slot_name} was invalidated during snapshot` +
`${this.formatWalBudgetContext()}. ` +
`Increase max_slot_wal_keep_size on the source database.`,
{ walStatus: 'lost', phase: 'snapshot', invalidationReason: rows[0].invalidation_reason ?? undefined }
);
}
await this.logWalBudget(rows[0], performance.now());
}
private formatWalBudgetContext(): string {
if (this.maxSlotWalKeepSize == null) {
return '';
}
return ` (limit: ${formatBytes(this.maxSlotWalKeepSize)})`;
}
async estimatedCountNumber(db: pgwire.PgConnection, table: storage.SourceTable): Promise<number> {
const results = await db.query({
statement: `SELECT reltuples::bigint AS estimate
FROM pg_class
WHERE oid = $1::regclass`,
params: [{ value: table.qualifiedName, type: 'varchar' }]
});
const row = results.rows[0];
return Number(row?.decodeWithoutCustomTypes(0) ?? -1n);
}
/**
* Start initial replication.
*
* If (partial) replication was done before on this slot, this clears the state
* and starts again from scratch.
*/
async startInitialReplication(replicationConnection: pgwire.PgConnection, status: InitResult) {
// If anything here errors, the entire replication process is aborted,
// and all connections are closed, including this one.
const db = await this.connections.snapshotConnection();
const slotName = this.slot_name;
if (status.needsNewSlot) {
// This happens when there is no existing replication slot, or if the
// existing one is unhealthy.
// In those cases, we have to start replication from scratch.
// If there is an existing healthy slot, we can skip this and continue
// initial replication where we left off.
await this.storage.clear({ signal: this.abort_signal });
await db.query({
statement: 'SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots WHERE slot_name = $1',
params: [{ type: 'varchar', value: slotName }]
});
// We use the replication connection here, not a pool.
// The replication slot must be created before we start snapshotting tables.
await replicationConnection.query(`CREATE_REPLICATION_SLOT ${slotName} LOGICAL pgoutput`);
this.logger.info(`Created replication slot ${slotName}`);
}
await this.initialReplication(db);
}
async initialReplication(db: pgwire.PgConnection) {
const sourceTables = this.sync_rules.getSourceTables();
const flushResults = await this.storage.startBatch(
{
logger: this.logger,
zeroLSN: ZERO_LSN,
defaultSchema: POSTGRES_DEFAULT_SCHEMA,
storeCurrentData: true,
skipExistingRows: true
},
async (batch) => {
let tablesWithStatus: SourceTable[] = [];
for (let tablePattern of sourceTables) {
const tables = await this.getQualifiedTableNames(batch, db, tablePattern);
// Pre-get counts
for (let table of tables) {
if (table.snapshotComplete) {
this.logger.info(`Skipping ${table.qualifiedName} - snapshot already done`);
continue;
}
const count = await this.estimatedCountNumber(db, table);
table = await batch.updateTableProgress(table, { totalEstimatedCount: count });
this.relationCache.update(table);
tablesWithStatus.push(table);
this.logger.info(`To replicate: ${table.qualifiedName} ${table.formatSnapshotProgress()}`);
}
}
for (let table of tablesWithStatus) {
await this.snapshotTableInTx(batch, db, table);
this.touch();
}
// Always commit the initial snapshot at zero.
// This makes sure we don't skip any changes applied before starting this snapshot,
// in the case of snapshot retries.
// We could alternatively commit at the replication slot LSN.
// Get the current LSN for the snapshot.
// We could also use the LSN from the last table snapshot.
const rs = await db.query(`select pg_current_wal_lsn() as lsn`);
const noCommitBefore = rs.rows[0].decodeWithoutCustomTypes(0);
await batch.markAllSnapshotDone(noCommitBefore);
await batch.commit(ZERO_LSN);
}
);
/**
* Send a keepalive message after initial replication.
* In some edge cases we wait for a keepalive after the initial snapshot.
* If we don't explicitly check the contents of keepalive messages then a keepalive is detected
* rather quickly after initial replication - perhaps due to other WAL events.
* If we do explicitly check the contents of messages, we need an actual keepalive payload in order
* to advance the active sync rules LSN.
*/
await sendKeepAlive(db);
const lastOp = flushResults?.flushed_op;
if (lastOp != null) {
// Populate the cache _after_ initial replication, but _before_ we switch to this sync rules.
await this.storage.populatePersistentChecksumCache({
// No checkpoint yet, but we do have the opId.
maxOpId: lastOp,
signal: this.abort_signal
});
}
}
static decodeRow(row: pgwire.PgRow, types: PostgresTypeResolver): SqliteInputRow {
let result: SqliteInputRow = {};
row.raw.forEach((rawValue, i) => {
const column = row.columns[i];
let mappedValue: SqliteInputValue;
if (typeof rawValue == 'string') {
mappedValue = toSyncRulesValue(types.registry.decodeDatabaseValue(rawValue, column.typeOid), false, true);
} else {
// Binary format, expose as-is.
mappedValue = rawValue;
}
result[column.name] = mappedValue;
});
return result;
}
private async snapshotTableInTx(
batch: storage.BucketStorageBatch,
db: pgwire.PgConnection,
table: storage.SourceTable,
limited?: PrimaryKeyValue[]
): Promise<storage.SourceTable> {
// Note: We use the default "Read Committed" isolation level here, not snapshot isolation.
// The data may change during the transaction, but that is compensated for in the streaming
// replication afterwards.
await db.query('BEGIN');
try {
await this.snapshotTable(batch, db, table, limited);
// Get the current LSN.
// The data will only be consistent once incremental replication has passed that point.
// We have to get this LSN _after_ we have finished the table snapshot.
//
// There are basically two relevant LSNs here:
// A: The LSN before the snapshot starts. We don't explicitly record this on the PowerSync side,
// but it is implicitly recorded in the replication slot.
// B: The LSN after the table snapshot is complete, which is what we get here.
// When we do the snapshot queries, the data that we get back for each chunk could match the state
// anywhere between A and B. To actually have a consistent state on our side, we need to:
// 1. Complete the snapshot.
// 2. Wait until logical replication has caught up with all the change between A and B.
// Calling `markSnapshotDone(LSN B)` covers that.
const rs = await db.query(`select pg_current_wal_lsn() as lsn`);
const tableLsnNotBefore = rs.rows[0].decodeWithoutCustomTypes(0);
// Side note: A ROLLBACK would probably also be fine here, since we only read in this transaction.
await db.query('COMMIT');
const [resultTable] = await batch.markTableSnapshotDone([table], tableLsnNotBefore);
this.relationCache.update(resultTable);
return resultTable;
} catch (e) {
await db.query('ROLLBACK');
throw e;
}
}
private async snapshotTable(
batch: storage.BucketStorageBatch,
db: pgwire.PgConnection,
table: storage.SourceTable,
limited?: PrimaryKeyValue[]
) {
let totalEstimatedCount = table.snapshotStatus?.totalEstimatedCount;
let at = table.snapshotStatus?.replicatedCount ?? 0;
let lastCountTime = 0;
let q: SnapshotQuery;
// We do streaming on two levels:
// 1. Coarse level: DELCARE CURSOR, FETCH 10000 at a time.
// 2. Fine level: Stream chunks from each fetch call.
if (limited) {
q = new IdSnapshotQuery(db, table, limited);
} else if (ChunkedSnapshotQuery.supports(table)) {
// Single primary key - we can use the primary key for chunking
const orderByKey = table.replicaIdColumns[0];
q = new ChunkedSnapshotQuery(db, table, this.snapshotChunkLength, table.snapshotStatus?.lastKey ?? null);
if (table.snapshotStatus?.lastKey != null) {
this.logger.info(
`Replicating ${table.qualifiedName} ${table.formatSnapshotProgress()} - resuming from ${orderByKey.name} > ${(q as ChunkedSnapshotQuery).lastKey}`
);
} else {
this.logger.info(`Replicating ${table.qualifiedName} ${table.formatSnapshotProgress()} - resumable`);
}
} else {
// Fallback case - query the entire table
this.logger.info(`Replicating ${table.qualifiedName} ${table.formatSnapshotProgress()} - not resumable`);
q = new SimpleSnapshotQuery(db, table, this.snapshotChunkLength);
at = 0;
}
await q.initialize();
let hasRemainingData = true;
while (hasRemainingData) {
// Fetch 10k at a time.
// The balance here is between latency overhead per FETCH call,
// and not spending too much time on each FETCH call.
// We aim for a couple of seconds on each FETCH call.
const cursor = q.nextChunk();
hasRemainingData = false;
// pgwire streams rows in chunks.
// These chunks can be quite small (as little as 16KB), so we don't flush chunks automatically.
// There are typically 100-200 rows per chunk.
for await (let chunk of cursor) {
if (chunk.tag == 'RowDescription') {
continue;
}
if (chunk.rows.length > 0) {
hasRemainingData = true;
}
for (const rawRow of chunk.rows) {
const record = this.sync_rules.applyRowContext<never>(WalStream.decodeRow(rawRow, this.connections.types));
// This auto-flushes when the batch reaches its size limit
await batch.save({
tag: storage.SaveOperationTag.INSERT,
sourceTable: table,
before: undefined,
beforeReplicaId: undefined,
after: record,
afterReplicaId: getUuidReplicaIdentityBson(record, table.replicaIdColumns)
});
}
at += chunk.rows.length;
this.metrics.getCounter(ReplicationMetric.ROWS_REPLICATED).add(chunk.rows.length);
this.touch();
}
// Important: flush before marking progress
await batch.flush();
if (this.onSnapshotChunkFlushed) {
await this.onSnapshotChunkFlushed();
}
const now = performance.now();
if (now - this.lastSlotHealthCheckTime >= this.slotHealthCheckIntervalMs) {
this.lastSlotHealthCheckTime = now;
await this.checkSlotHealth();
}
if (limited == null) {
let lastKey: Uint8Array | undefined;
if (q instanceof ChunkedSnapshotQuery) {
lastKey = q.getLastKeySerialized();
}
if (lastCountTime < performance.now() - 10 * 60 * 1000) {
// Even though we're doing the snapshot inside a transaction, the transaction uses
// the default "Read Committed" isolation level. This means we can get new data
// within the transaction, so we re-estimate the count every 10 minutes when replicating
// large tables.
totalEstimatedCount = await this.estimatedCountNumber(db, table);
lastCountTime = performance.now();
}
table = await batch.updateTableProgress(table, {
lastKey: lastKey,
replicatedCount: at,
totalEstimatedCount: totalEstimatedCount
});
this.relationCache.update(table);
this.logger.info(`Replicating ${table.qualifiedName} ${table.formatSnapshotProgress()}`);
} else {
this.logger.info(`Replicating ${table.qualifiedName} ${at}/${limited.length} for resnapshot`);
}
if (this.abort_signal.aborted) {
// We only abort after flushing
throw new ReplicationAbortedError(`Initial replication interrupted`);
}
}
}
async handleRelation(options: {
batch: storage.BucketStorageBatch;
descriptor: SourceEntityDescriptor;
snapshot: boolean;
referencedTypeIds: number[];
}) {
const { batch, descriptor, snapshot, referencedTypeIds } = options;
if (!descriptor.objectId && typeof descriptor.objectId != 'number') {
throw new ReplicationAssertionError(`objectId expected, got ${typeof descriptor.objectId}`);
}
const result = await this.storage.resolveTable({
group_id: this.group_id,
connection_id: this.connection_id,
connection_tag: this.connections.connectionTag,
entity_descriptor: descriptor,
sync_rules: this.sync_rules
});
this.relationCache.update(result.table);
// Drop conflicting tables. This includes for example renamed tables.
await batch.drop(result.dropTables);
// Ensure we have a description for custom types referenced in the table.
await this.connections.types.fetchTypes(referencedTypeIds);
// Snapshot if:
// 1. Snapshot is requested (false for initial snapshot, since that process handles it elsewhere)
// 2. Snapshot is not already done, AND:
// 3. The table is used in sync rules.
const shouldSnapshot = snapshot && !result.table.snapshotComplete && result.table.syncAny;
if (shouldSnapshot) {
// Truncate this table, in case a previous snapshot was interrupted.
await batch.truncate([result.table]);
// Start the snapshot inside a transaction.
// We use a dedicated connection for this.
const db = await this.connections.snapshotConnection();
try {
const table = await this.snapshotTableInTx(batch, db, result.table);
// After the table snapshot, we wait for replication to catch up.
// To make sure there is actually something to replicate, we send a keepalive
// message.
await sendKeepAlive(db);
return table;
} finally {
await db.end();
}
}
return result.table;
}
/**
* Process rows that have missing TOAST values.
*
* This can happen during edge cases in the chunked intial snapshot process.
*
* We handle this similar to an inline table snapshot, but limited to the specific
* set of rows.
*/
private async resnapshot(batch: BucketStorageBatch, rows: MissingRow[]) {
const byTable = new Map<number, MissingRow[]>();
for (let row of rows) {
const relId = row.table.objectId as number; // always a number for postgres
if (!byTable.has(relId)) {
byTable.set(relId, []);
}
byTable.get(relId)!.push(row);
}
const db = await this.connections.snapshotConnection();
try {
for (let rows of byTable.values()) {
const table = rows[0].table;
await this.snapshotTableInTx(
batch,
db,
table,
rows.map((r) => r.key)
);
}
// Even with resnapshot, we need to wait until we get a new consistent checkpoint
// after the snapshot, so we need to send a keepalive message.
await sendKeepAlive(db);
} finally {
await db.end();
}
}
private getTable(relationId: number): storage.SourceTable {
const table = this.relationCache.get(relationId);
if (table == null) {
// We should always receive a replication message before the relation is used.
// If we can't find it, it's a bug.
throw new ReplicationAssertionError(`Missing relation cache for ${relationId}`);
}
return table;
}
private syncRulesRecord(row: SqliteInputRow): SqliteRow;
private syncRulesRecord(row: SqliteInputRow | undefined): SqliteRow | undefined;
private syncRulesRecord(row: SqliteInputRow | undefined): SqliteRow | undefined {
if (row == null) {
return undefined;
}
return this.sync_rules.applyRowContext<never>(row);
}
private toastableSyncRulesRecord(row: ToastableSqliteRow<SqliteInputValue>): ToastableSqliteRow {
return this.sync_rules.applyRowContext(row);
}
async writeChange(
batch: storage.BucketStorageBatch,
msg: pgwire.PgoutputMessage
): Promise<storage.FlushedResult | null> {
if (msg.lsn == null) {
return null;
}
if (msg.tag == 'insert' || msg.tag == 'update' || msg.tag == 'delete') {
const table = this.getTable(getRelId(msg.relation));
if (!table.syncAny) {
this.logger.debug(`Table ${table.qualifiedName} not used in sync rules - skipping`);
return null;
}
if (msg.tag == 'insert') {
this.metrics.getCounter(ReplicationMetric.ROWS_REPLICATED).add(1);
const baseRecord = this.syncRulesRecord(this.connections.types.constructAfterRecord(msg));
return await batch.save({
tag: storage.SaveOperationTag.INSERT,
sourceTable: table,
before: undefined,
beforeReplicaId: undefined,
after: baseRecord,
afterReplicaId: getUuidReplicaIdentityBson(baseRecord, table.replicaIdColumns)
});
} else if (msg.tag == 'update') {
this.metrics.getCounter(ReplicationMetric.ROWS_REPLICATED).add(1);
// "before" may be null if the replica id columns are unchanged
// It's fine to treat that the same as an insert.
const before = this.syncRulesRecord(this.connections.types.constructBeforeRecord(msg));
const after = this.toastableSyncRulesRecord(this.connections.types.constructAfterRecord(msg));
return await batch.save({
tag: storage.SaveOperationTag.UPDATE,
sourceTable: table,
before: before,
beforeReplicaId: before ? getUuidReplicaIdentityBson(before, table.replicaIdColumns) : undefined,
after: after,
afterReplicaId: getUuidReplicaIdentityBson(after, table.replicaIdColumns)
});
} else if (msg.tag == 'delete') {
this.metrics.getCounter(ReplicationMetric.ROWS_REPLICATED).add(1);
const before = this.syncRulesRecord(this.connections.types.constructBeforeRecord(msg)!);
return await batch.save({
tag: storage.SaveOperationTag.DELETE,
sourceTable: table,
before: before,
beforeReplicaId: getUuidReplicaIdentityBson(before, table.replicaIdColumns),
after: undefined,
afterReplicaId: undefined
});
}
} else if (msg.tag == 'truncate') {
let tables: storage.SourceTable[] = [];
for (let relation of msg.relations) {
const table = this.getTable(getRelId(relation));
tables.push(table);
}
return await batch.truncate(tables);
}
return null;
}
async replicate() {
try {
// If anything errors here, the entire replication process is halted, and
// all connections automatically closed, including this one.
this.initialSnapshotPromise = (async () => {
const initReplicationConnection = await this.connections.replicationConnection();
await this.initReplication(initReplicationConnection);
await initReplicationConnection.end();
})();
await this.initialSnapshotPromise;
// At this point, the above connection has often timed out, so we start a new one
const streamReplicationConnection = await this.connections.replicationConnection();
await this.streamChanges(streamReplicationConnection);
await streamReplicationConnection.end();
} catch (e) {
await this.storage.reportError(e);
throw e;
}