|
| 1 | +import { Client, LogicalReplicationService, MppdbDecodingPlugin } from 'gaussdb-node' |
| 2 | + |
| 3 | +const slotName = 'demo_replication_slot' |
| 4 | +const tableName = 'demo_replication_table' |
| 5 | + |
| 6 | +// Connection config from environment variables or defaults |
| 7 | +const config = { |
| 8 | + host: process.env.GAUSSHOST, |
| 9 | + port: parseInt(process.env.GAUSSPORT), |
| 10 | + database: process.env.GAUSSDATABASE, |
| 11 | + user: process.env.GAUSSUSER, |
| 12 | + password: process.env.GAUSSPASSWORD, |
| 13 | +} |
| 14 | + |
| 15 | +async function checkSlotExists(client) { |
| 16 | + const res = await client.query( |
| 17 | + `SELECT 1 FROM pg_replication_slots WHERE slot_name = $1`, |
| 18 | + [slotName] |
| 19 | + ) |
| 20 | + return res.rowCount > 0 |
| 21 | +} |
| 22 | + |
| 23 | +async function createSlot(client) { |
| 24 | + await client.query( |
| 25 | + `SELECT * FROM pg_create_logical_replication_slot('${slotName}', 'mppdb_decoding')` |
| 26 | + ) |
| 27 | + console.log(`slot "${slotName}" created`) |
| 28 | +} |
| 29 | + |
| 30 | +async function dropSlot(client) { |
| 31 | + await client.query(`SELECT pg_drop_replication_slot('${slotName}')`) |
| 32 | + console.log(`slot "${slotName}" dropped`) |
| 33 | +} |
| 34 | + |
| 35 | +async function createTable(client) { |
| 36 | + await client.query(` |
| 37 | + CREATE TABLE IF NOT EXISTS ${tableName} ( |
| 38 | + id SERIAL PRIMARY KEY, |
| 39 | + name TEXT, |
| 40 | + value INT |
| 41 | + ) |
| 42 | + `) |
| 43 | + console.log(`table "${tableName}" created`) |
| 44 | +} |
| 45 | + |
| 46 | +async function dropTable(client) { |
| 47 | + await client.query(`DROP TABLE IF EXISTS ${tableName}`) |
| 48 | + console.log(`table "${tableName}" dropped`) |
| 49 | +} |
| 50 | + |
| 51 | +async function performDML(client) { |
| 52 | + console.log('performing DML operations...') |
| 53 | + |
| 54 | + await client.query(`INSERT INTO ${tableName} (name, value) VALUES ($1, $2)`, ['alice', 100]) |
| 55 | + console.log(' INSERT alice') |
| 56 | + |
| 57 | + await client.query(`INSERT INTO ${tableName} (name, value) VALUES ($1, $2)`, ['bob', 200]) |
| 58 | + console.log(' INSERT bob') |
| 59 | + |
| 60 | + await client.query(`UPDATE ${tableName} SET value = $1 WHERE name = $2`, [150, 'alice']) |
| 61 | + console.log(' UPDATE alice') |
| 62 | + |
| 63 | + await client.query(`DELETE FROM ${tableName} WHERE name = $1`, ['bob']) |
| 64 | + console.log(' DELETE bob') |
| 65 | + |
| 66 | + console.log('DML operations completed') |
| 67 | +} |
| 68 | + |
| 69 | +async function main() { |
| 70 | + let clientB = null |
| 71 | + let service = null |
| 72 | + |
| 73 | + try { |
| 74 | + clientB = new Client(config) |
| 75 | + await clientB.connect() |
| 76 | + console.log('client B connected') |
| 77 | + |
| 78 | + // setup: create table and slot if needed |
| 79 | + await createTable(clientB) |
| 80 | + |
| 81 | + const slotExists = await checkSlotExists(clientB) |
| 82 | + if (!slotExists) { |
| 83 | + await createSlot(clientB) |
| 84 | + } else { |
| 85 | + console.log(`slot "${slotName}" already exists`) |
| 86 | + } |
| 87 | + |
| 88 | + // start replication service (client A uses config, not Client instance) |
| 89 | + service = new LogicalReplicationService(config, { |
| 90 | + acknowledge: { auto: true, timeoutSeconds: 10 }, |
| 91 | + }) |
| 92 | + |
| 93 | + const plugin = new MppdbDecodingPlugin({ |
| 94 | + includeXids: false, |
| 95 | + skipEmptyXacts: true, |
| 96 | + }) |
| 97 | + |
| 98 | + const receivedMessages = [] |
| 99 | + |
| 100 | + service.on('start', () => { |
| 101 | + console.log('replication started') |
| 102 | + }) |
| 103 | + |
| 104 | + service.on('data', (lsn, msg) => { |
| 105 | + console.log('[data]', lsn, msg) |
| 106 | + receivedMessages.push(msg) |
| 107 | + }) |
| 108 | + |
| 109 | + service.on('error', (err) => { |
| 110 | + console.error('[error]', err) |
| 111 | + }) |
| 112 | + |
| 113 | + // start replication in background |
| 114 | + service.subscribe(plugin, slotName) |
| 115 | + |
| 116 | + // wait for replication to start |
| 117 | + await new Promise((resolve) => service.once('start', resolve)) |
| 118 | + |
| 119 | + // perform DML operations (client B) |
| 120 | + await performDML(clientB) |
| 121 | + |
| 122 | + // wait a bit for replication to catch up |
| 123 | + await new Promise((resolve) => setTimeout(resolve, 2000)) |
| 124 | + |
| 125 | + // stop replication first |
| 126 | + await service.stop() |
| 127 | + service = null |
| 128 | + console.log('replication stopped') |
| 129 | + |
| 130 | + console.log(`received ${receivedMessages.length} messages`) |
| 131 | + |
| 132 | + // cleanup while clientB is still connected |
| 133 | + await dropTable(clientB) |
| 134 | + await dropSlot(clientB) |
| 135 | + } catch (err) { |
| 136 | + console.error('error:', err) |
| 137 | + } finally { |
| 138 | + // stop service if still running |
| 139 | + if (service) { |
| 140 | + try { |
| 141 | + await service.stop() |
| 142 | + } catch (err) { |
| 143 | + // ignore |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // cleanup with a new connection if needed |
| 148 | + if (clientB) { |
| 149 | + try { |
| 150 | + await clientB.end() |
| 151 | + } catch (err) { |
| 152 | + // ignore |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // ensure cleanup with fresh connection |
| 157 | + const cleanupClient = new Client(config) |
| 158 | + try { |
| 159 | + await cleanupClient.connect() |
| 160 | + await cleanupClient.query(`DROP TABLE IF EXISTS ${tableName}`) |
| 161 | + await cleanupClient.query(`SELECT pg_drop_replication_slot('${slotName}')`).catch(() => {}) |
| 162 | + } catch (err) { |
| 163 | + // ignore cleanup errors |
| 164 | + } finally { |
| 165 | + await cleanupClient.end().catch(() => {}) |
| 166 | + } |
| 167 | + |
| 168 | + console.log('cleanup done') |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +main().catch((err) => { |
| 173 | + console.error(err) |
| 174 | + process.exit(1) |
| 175 | +}) |
0 commit comments