Skip to content

Commit bd7e7c3

Browse files
committed
Further tweaks to postgres compatibility fallbacks.
1 parent bc36974 commit bd7e7c3

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

modules/module-postgres/src/replication/WalStream.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,10 @@ export class WalStream {
294294
this.logger.info(`Initial replication already done`);
295295
}
296296

297-
let invalidationReason = (await this.checkInvalidationReasonSupport()) ? 'invalidation_reason' : `'unknown'`;
298297
// Check if replication slot exists
299298
const slot = pgwire.pgwireRows(
300299
await this.connections.pool.query({
301-
statement: `SELECT wal_status, ${invalidationReason} as invalidation_reason FROM pg_replication_slots WHERE slot_name = $1`,
300+
statement: await this.getSlotQuery(),
302301
params: [{ type: 'varchar', value: slotName }]
303302
})
304303
)[0];
@@ -394,7 +393,7 @@ export class WalStream {
394393
// Postgres 17 - exceeded max_slot_wal_keep_size
395394
/can no longer get changes from replication slot/.test(e.message)
396395
) {
397-
// Fatal error. In most cases, the `wal_status == 'lost'` check should pick this up, but this
396+
// Fatal error. In most cases since Postgres 13+, the `wal_status == 'lost'` check should pick this up, but this
398397
// works as a fallback.
399398

400399
container.reporter.captureException(e, {
@@ -1155,9 +1154,19 @@ WHERE oid = $1::regclass`,
11551154
return version ? version.compareMain('14.0.0') >= 0 : false;
11561155
}
11571156

1158-
protected async checkInvalidationReasonSupport() {
1157+
/**
1158+
* The pg_replication_slots definition added new debugging features in postgres 13 and in 17, so we selectively use that.
1159+
*/
1160+
protected async getSlotQuery() {
11591161
const version = await this.connections.getServerVersion();
1160-
return version ? version.compareMain('16.0.0') >= 0 : false;
1162+
1163+
if (version!.compareMain('17.0.0') >= 0) {
1164+
return `SELECT wal_status, invalidation_reason FROM pg_replication_slots WHERE slot_name = $1`;
1165+
} else if (version!.compareMain('13.0.0') >= 0) {
1166+
return `SELECT wal_status, 'unknown' as invalidation_reason FROM pg_replication_slots WHERE slot_name = $1`;
1167+
} else {
1168+
return `SELECT 'unknown' as wal_status, 'unknown' as invalidation_reason FROM pg_replication_slots WHERE slot_name = $1`;
1169+
}
11611170
}
11621171

11631172
async getReplicationLagMillis(): Promise<number | undefined> {

0 commit comments

Comments
 (0)