Truy vấn thông tin đồng bộ dữ liệu Flink-CDC-PostgreSQL

Thông tin đồng bộ dữ liệu PostgreSQL

Trong PostgreSQL, bạn có thể sử dụng các view và hàm hệ thống sau để truy vấn thông tin về Publication, Replication Slot và logic replication:

1. Truy vấn thông tin Publication

-- Xem tất cả publication
SELECT * FROM pg_publication;

-- Xem các bảng trong publication
SELECT pubname, schemaname, tablename 
FROM pg_publication_tables pt
JOIN pg_tables t ON pt.tablename = t.tablename AND pt.schemaname = t.schemaname;

-- Xem chi tiết publication
SELECT p.pubname, p.pubowner::regrole, p.puballtables, p.pubinsert, p.pubupdate, p.pubdelete, p.pubtruncate
FROM pg_publication p;

2. Truy vấn thông tin Subscription

-- Xem tất cả subscription
SELECT * FROM pg_subscription;

-- Xem chi tiết subscription
SELECT subname, subconninfo, subslotname, subenabled, subpublications
FROM pg_subscription;

-- Xem trạng thái subscription
SELECT * FROM pg_stat_subscription;

3. Truy vấn thông tin Replication Slot

-- Xem tất cả replication slot
SELECT * FROM pg_replication_slots;

-- Xem chi tiết replication slot logic
SELECT slot_name, plugin, slot_type, database, active, restart_lsn, confirmed_flush_lsn
FROM pg_replication_slots
WHERE slot_type = 'logical';

-- Xem thống kê replication slot
SELECT * FROM pg_stat_replication_slots;

4. Truy vấn trạng thái logic replication

-- Xem trạng thái bên gửi
SELECT * FROM pg_stat_replication;

-- Xem trạng thái bên nhận (trên subscriber)
SELECT * FROM pg_stat_subscription;

-- Xem tiến trình WAL sender
SELECT * FROM pg_stat_wal_sender;

5. Truy vấn trạng thái đồng bộ bảng

-- Xem trạng thái đồng bộ bảng (cần quyền superuser)
SELECT * FROM pg_subscription_rel;

-- Xem trạng thái đồng bộ bảng cho subscription cụ thể
SELECT sr.subid, sr.relid, n.nspname, c.relname, 
       sr.srsubstate, sr.srsublsn, sr.srrelstate
FROM pg_subscription_rel sr
JOIN pg_class c ON sr.relid = c.oid
JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE sr.subid = (SELECT oid FROM pg_subscription WHERE subname = 'your_subscription_name');

6. Giám sát độ trễ replication

-- Tính độ trễ replication (theo byte)
SELECT 
    client_addr, 
    client_hostname, 
    sent_lsn, 
    write_lsn, 
    flush_lsn, 
    replay_lsn,
    pg_size_pretty(sent_lsn - write_lsn) AS write_delay,
    pg_size_pretty(sent_lsn - flush_lsn) AS flush_delay,
    pg_size_pretty(sent_lsn - replay_lsn) AS replay_delay
FROM pg_stat_replication;

-- Tính độ trễ từ phía subscriber
SELECT 
    subname,
    pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), latest_end_lsn)) AS replication_delay
FROM pg_stat_subscription;

7. Các hàm thường dùng

-- Tạo replication slot (trên publisher)
SELECT * FROM pg_create_logical_replication_slot('slot_name', 'plugin_name');

-- Xóa replication slot
SELECT pg_drop_replication_slot('slot_name');

-- Lấy vị trí WAL hiện tại
SELECT pg_current_wal_lsn();

-- Tính khoảng cách giữa hai LSN (theo byte)
SELECT pg_wal_lsn_diff('0/12345678', '0/12345600');

Các truy vấn này giúp bạn giám sát và quản lý cấu hình logic replication trong PostgreSQL, bao gồm trạng thái publication, subscription và replication slot.

Thẻ: PostgreSQL CDC flink-cdc Logical Replication Replication Slot

Đăng vào ngày 1 tháng 7 lúc 16:34