1 # Incremental SQL Backup System Using PostgreSQL Logical Replication
3 **PostgreSQL Version**: This design is based on PostgreSQL 18.0 documentation. While most features (logical replication, event triggers, pg_recvlogical) are available in earlier versions (PostgreSQL 10+), verify specific parameter availability (e.g., `max_slot_wal_keep_size` requires PostgreSQL 13+) for your target version.
7 This document details the design for a PostgreSQL backup system that produces human-readable, plain SQL incremental backups using logical replication. The system creates backups that remain readable and restorable for 10+ years while supporting online operation and crash safety.
9 **Design Decision**: Use `pg_recvlogical` with the `decoder_raw` plugin for DML capture, combined with event triggers using `pg_logical_emit_message()` for DDL tracking and periodic `pg_dumpall --globals-only` for shared objects.
12 - **Built-in tooling handles complexity**: `pg_recvlogical` provides streaming infrastructure, crash recovery, and position tracking
13 - **No transformation layer needed**: `decoder_raw` produces production-ready SQL directly
14 - **Complete coverage**: Event triggers + `pg_logical_emit_message()` + `pg_dumpall --globals-only` captures all DDL at correct chronological positions
15 - **Long-term readability**: Plain SQL format that can be executed years later
16 - **Correct DDL/DML ordering**: DDL messages appear in replication stream at exact time of execution
19 1. **DDL tracking** - Event triggers emit DDL via `pg_logical_emit_message()`; `pg_dumpall --globals-only` handles shared objects
20 2. **Replica identity configuration** - All tables need proper configuration for UPDATE/DELETE
21 3. **Aggressive monitoring** - Replication slots must be monitored to prevent operational issues
22 4. **decoder_raw extension** - Third-party plugin must be extended with TRUNCATE support and `message_cb` callback
24 ## Architecture Overview
29 ┌─────────────────────────────────────────────────────────────┐
30 │ PostgreSQL Database │
32 │ ┌────────────────┐ ┌──────────────────┐ │
33 │ │ Regular Tables│────────▶│ WAL (Write-Ahead│ │
34 │ │ (DML Changes) │ │ Log) │ │
35 │ └────────────────┘ └──────────────────┘ │
38 │ ┌─────────────────────────┐ │
39 │ │ Logical Decoding Process│ │
40 │ │ (decoder_raw plugin) │ │
41 │ └─────────────────────────┘ │
43 └──────────────────────────────────────┼───────────────────────┘
46 ┌─────────────────────────────┐
48 │ (Tracks position, durable) │
49 └─────────────────────────────┘
52 ┌─────────────────────────────┐
53 │ pg_recvlogical Tool │
54 │ (Built-in PostgreSQL util) │
55 └─────────────────────────────┘
57 ┌──────────────┴──────────────┐
59 ┌─────────────────────┐ ┌─────────────────────┐
60 │ Incremental Files │ │ Full pg_dump │
61 │ (SQL Changes) │ │ (Periodic) │
62 │ - 2024-01-01.sql │ │ - base-2024-01.sql │
63 │ - 2024-01-02.sql │ │ - base-2024-02.sql │
65 └─────────────────────┘ └─────────────────────┘
70 1. **Logical Replication Slot**: Durable position tracker in PostgreSQL
71 2. **decoder_raw Plugin**: Transforms binary WAL to executable SQL
72 3. **pg_recvlogical**: Built-in PostgreSQL tool that streams logical decoding output
73 4. **Base Backup System**: Regular full `pg_dump` backups with `--snapshot` for consistency
74 5. **Schema Tracking System**: Event triggers + `pg_dumpall --globals-only` for DDL changes
78 ### DML Capture via Logical Replication
80 PostgreSQL's logical replication decodes the Write-Ahead Log (WAL) into logical changes. The `decoder_raw` plugin outputs these directly as executable SQL:
84 INSERT INTO public.users (id, name, email) VALUES (1, 'Alice', 'alice@example.com');
85 UPDATE public.users SET name = 'Alice Smith' WHERE id = 1;
86 DELETE FROM public.orders WHERE id = 42;
91 - **Crash-safe**: Replication slots persist position across crashes (positions persisted at checkpoint intervals; after crash, slot may return to earlier LSN causing recent changes to be replayed)
92 - **Consistent**: Transaction boundaries are preserved
93 - **Online**: Runs without blocking database operations
94 - **Idempotent positioning**: Can restart from last known position (clients responsible for handling duplicate messages)
96 ### DDL Capture via Event Triggers and Logical Messages
98 Logical replication does **not** capture DDL (CREATE TABLE, ALTER TABLE, etc.). We solve this by emitting DDL commands directly into the logical replication stream using PostgreSQL's `pg_logical_emit_message()` function:
101 -- Create event trigger function that emits DDL into replication stream
102 CREATE OR REPLACE FUNCTION emit_ddl_to_stream()
103 RETURNS event_trigger AS $$
105 -- Emit DDL command directly into logical replication stream
106 -- The 'true' parameter makes this transactional (part of current transaction)
107 -- The 'ddl' prefix allows the output plugin to identify these messages
108 PERFORM pg_logical_emit_message(
109 true, -- transactional
110 'ddl', -- prefix for identification
111 current_query()::text -- the actual DDL SQL command
116 -- Register the event trigger
117 CREATE EVENT TRIGGER emit_ddl_to_stream ON ddl_command_end
118 EXECUTE FUNCTION emit_ddl_to_stream();
122 1. Event trigger fires on all DDL commands
123 2. `pg_logical_emit_message()` writes DDL into WAL as a logical decoding message
124 3. Message appears in replication stream at exact chronological position relative to DML
125 4. Output plugin's `message_cb` callback outputs DDL as executable SQL
126 5. Restore is simple: just execute the incremental backup file sequentially
128 **Example incremental backup output**:
131 INSERT INTO public.users (id, name, email) VALUES (1, 'Alice', 'alice@example.com');
134 -- DDL message appears at exact time of execution
135 ALTER TABLE public.users DROP COLUMN email;
138 -- Subsequent DML only references remaining columns
139 INSERT INTO public.users (id, name) VALUES (2, 'Bob');
144 - ✅ **Perfect chronological ordering**: DDL appears exactly when it was executed
145 - ✅ **Transactional integrity**: DDL message commits with its transaction
146 - ✅ **Simple restore**: Execute backup file sequentially with `psql -f`
147 - ✅ **PostgreSQL built-in**: `pg_logical_emit_message()` available since PostgreSQL 9.6
150 - Event triggers don't fire for shared objects: databases, roles, tablespaces, parameter privileges, and ALTER SYSTEM commands
151 - Solution: Use periodic `pg_dumpall --globals-only` to capture shared objects
153 ## Implementation Components
155 ### 1. Initial Setup Script
157 **Purpose**: Bootstrap the backup system
160 - Install decoder_raw plugin (compile from source)
161 - Create logical replication slot with snapshot export (see detailed procedure below)
162 - Capture the exported snapshot identifier
163 - Take initial base backup using the exported snapshot
164 - Set up event triggers for DDL capture
165 - Create initial `pg_dumpall --globals-only` backup
166 - Configure `REPLICA IDENTITY` on tables without primary keys
167 - Document PostgreSQL version and installed extensions
169 **Critical Detail - Initial Snapshot Consistency**:
171 From PostgreSQL documentation (Section 47.2.5):
172 > When a new replication slot is created using the streaming replication interface, a snapshot is exported which will show exactly the state of the database after which all changes will be included in the change stream.
174 This ensures the base backup and incremental stream are perfectly aligned with no gaps or overlaps.
176 **Step-by-Step Setup Procedure**:
178 1. **Create replication slot with snapshot export**:
179 - Use `pg_recvlogical --create-slot` with `--if-not-exists` flag for idempotency
180 - Command outputs snapshot identifier in format: `snapshot: 00000003-00000001-1`
181 - Capture this identifier from stdout/stderr (appears on stderr with "snapshot:" prefix)
183 2. **Extract and validate snapshot identifier**:
184 - Parse the snapshot identifier from slot creation output
185 - Verify identifier was successfully captured before proceeding
186 - Snapshot is only valid during the creating session/connection
188 3. **Take synchronized base backup**:
189 - Use `pg_dump --snapshot=<identifier> --file=backup.sql` with captured snapshot
190 - This guarantees perfect alignment between base backup and incremental stream
191 - Use `--compress=zstd:9` for large databases to reduce storage requirements
193 4. **Capture globals and metadata**:
194 - Run `pg_dumpall --globals-only` for roles, tablespaces
195 - Document PostgreSQL version, extensions, encoding in metadata file
197 **Critical Considerations**:
198 - **Snapshot validity**: Exported snapshot only valid until the session that created it disconnects; must use immediately or maintain connection
199 - **Idempotency**: The `--if-not-exists` flag allows safe re-execution of setup scripts
200 - **Timing**: Entire sequence (create slot → capture snapshot → run pg_dump) must complete while snapshot remains valid
202 ### 2. Incremental Backup Collection
204 **Tool**: Built-in `pg_recvlogical` utility with `decoder_raw` plugin
206 **Key Configuration**:
208 Run `pg_recvlogical --start` with the following important parameters:
210 - **Status interval** (`--status-interval`): Controls how often client reports position back to server (recommended: 10 seconds)
211 - Lower values (5-10s) allow server to advance slot position and free WAL more quickly
212 - Too high risks slot lag and WAL accumulation
213 - Balance between slot health and network overhead
215 - **Fsync interval** (`--fsync-interval`): Controls disk write safety (recommended: 10 seconds)
216 - Frequency of forced disk synchronization for crash safety on backup client
217 - 0 disables fsync (faster but risks data loss if client crashes)
218 - Higher values improve performance but increase window of potential loss
220 - **Plugin options** (`--option`): Pass `include_transaction=on` to decoder_raw
221 - Includes BEGIN/COMMIT statements in output
222 - Essential for maintaining transaction boundaries during restore
224 **What pg_recvlogical provides**:
225 - Streams decoded changes continuously from the replication slot
226 - Handles connection failures and automatic reconnection
227 - Tracks LSN positions with status updates to the server
228 - Supports file rotation via SIGHUP signal (for log rotation without stopping stream)
230 **What decoder_raw provides**:
231 - ✅ Schema-qualified table names: `INSERT INTO public.users (...)`
232 - ✅ Proper column name quoting with `quote_identifier()`
233 - ✅ Transaction boundaries via `include_transaction=on` option (BEGIN/COMMIT)
234 - ✅ Intelligent replica identity handling (DEFAULT, INDEX, FULL, NOTHING)
235 - ✅ Comprehensive data type support (booleans, NaN, Infinity, NULL, strings, bit strings)
236 - ✅ TOAST optimization (skips unchanged TOAST columns in UPDATEs)
237 - ✅ Production-quality memory management
239 **What decoder_raw needs (via extension)**:
240 - ⚠️ **`message_cb` callback** to handle DDL messages from `pg_logical_emit_message()`
241 - ⚠️ **`truncate_cb` callback** to handle TRUNCATE operations
243 **Custom wrapper script tasks**:
244 - File rotation and timestamping
245 - Coordinate with monitoring system
246 - Metadata file generation (PostgreSQL version, extensions, encoding, collation)
248 ### 3. Periodic Full Backup Script
250 **Purpose**: Take regular full backups as restore points
253 - Execute `pg_dump --file=backup.sql` to create full database backup in plain SQL format
254 - Execute `pg_dumpall --globals-only` to capture shared objects (databases, roles, tablespaces)
255 - Compress backups to save space
256 - Implement retention policy (delete old backups)
259 - Use `pg_dump --file=backup.sql` for plain SQL output
260 - Maintains human-readable format consistent with design goals
261 - Single connection to database
262 - Universal compatibility across PostgreSQL versions
265 - Use `pg_dump --compress=<method>` for built-in compression (e.g., `--compress=zstd:9`)
266 - Or compress after creation with external tools (gzip, zstd)
267 - Compression significantly reduces storage requirements while maintaining recoverability
269 ### 4. Restore Script
271 **Purpose**: Restore database to latest captured state from base + incremental backups
274 1. Locate most recent full backup
275 2. Find all incremental backups since that full backup
276 3. Create new target database
277 4. Restore `pg_dumpall --globals-only` (shared objects: roles, tablespaces)
278 5. Restore full `pg_dump` backup using `psql -f backup.sql`
279 6. Apply all incremental SQL backups in chronological order using `psql -f incremental-*.sql`
280 - DDL and DML are already interleaved in correct chronological order
281 - No separate DDL extraction or ordering step needed
282 7. Synchronize sequence values using `setval()` with max values from tables
283 8. Verify data integrity (row counts, application smoke tests)
285 **Sequence Synchronization**:
286 - After applying all changes, sequences may be behind actual max values
287 - Query `information_schema.sequences` to find all sequences
288 - For each sequence: `SELECT setval('sequence_name', COALESCE(MAX(id_column), 1)) FROM table`
289 - Can be automated by generating setval queries from schema metadata
291 **Handling Duplicate Transactions**:
293 After PostgreSQL crash, replication slot may return to earlier LSN, causing some transactions to be streamed again. The restore process handles this naturally through idempotent operations:
295 - Most SQL operations are idempotent or fail safely:
296 - INSERT fails on duplicate primary key (acceptable during restore)
297 - UPDATE reapplies same values (idempotent)
298 - DELETE succeeds or reports row not found (acceptable)
299 - Transaction boundaries (BEGIN/COMMIT from `include_transaction=on`) preserve consistency
300 - Simply apply all incremental files in order; duplicates will be handled correctly
301 - No additional LSN tracking infrastructure required
303 ### 5. Monitoring and Health Check Script
305 **Purpose**: Prevent operational issues from inactive replication slots
307 **Critical Metrics**:
309 -- Check replication slot health
315 pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) as replication_lag,
316 pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), confirmed_flush_lsn)) as confirmed_lag
317 FROM pg_replication_slots
318 WHERE slot_type = 'logical';
322 - **Critical alert** when `restart_lsn` falls more than 1GB behind
323 - **Emergency alert** when slot lag exceeds 10GB or age exceeds 24 hours
324 - **Emergency procedure** documented to drop slot if it threatens database availability
327 - **Prometheus + postgres_exporter + Grafana**: Open-source monitoring stack
328 - **pgDash** (https://pgdash.io/): Commercial PostgreSQL monitoring
329 - **check_postgres**: Nagios/Icinga/Zabbix integration
330 - **Built-in views**: `pg_replication_slots`, `pg_stat_replication_slots`
332 ## decoder_raw Plugin Details
334 **Source**: https://github.com/michaelpq/pg_plugins/tree/main/decoder_raw
335 **License**: PostgreSQL License (permissive, production-ready)
336 **Compatibility**: PostgreSQL 9.4+
340 # Install PostgreSQL development headers
341 apt-get install postgresql-server-dev-XX # Debian/Ubuntu
342 yum install postgresql-devel # RHEL/CentOS
345 git clone https://github.com/michaelpq/pg_plugins.git
346 cd pg_plugins/decoder_raw
350 # Verify installation
351 ls $(pg_config --pkglibdir)/decoder_raw.so
354 **Why decoder_raw is essential**:
355 - Eliminates the entire SQL transformation layer
356 - Handles all data type escaping correctly (strings, NULL, NaN, Infinity, booleans)
357 - Produces production-ready SQL that can be executed with `psql -f changes.sql`
358 - Mature codebase with comprehensive test suite
359 - Clean code structure makes it straightforward to extend with additional callbacks
361 **Required Extensions to decoder_raw**:
363 The stock `decoder_raw` plugin does not implement two optional callbacks required for this design:
365 **1. `message_cb` Callback for DDL Capture**:
366 - Required to handle messages from `pg_logical_emit_message()` (documented in Section 47.6.4.8)
367 - Filter messages by prefix: only output messages with prefix `'ddl'`
368 - Output the message content as executable SQL (already valid DDL)
369 - Example output: `ALTER TABLE public.users DROP COLUMN email;`
370 - Straightforward implementation (~30-50 lines)
372 **2. `truncate_cb` Callback for TRUNCATE Operations**:
373 - The stock plugin silently ignores TRUNCATE operations during logical decoding
374 - Event triggers for `ddl_command_end` only fire for DDL commands (CREATE, ALTER, DROP, etc.)
375 - TRUNCATE is a DML operation, not DDL, and does NOT trigger event triggers
376 - Must be captured via logical replication using `truncate_cb` (documented in Section 47.6.4.6)
377 - The callback receives an array of relations since TRUNCATE can affect multiple tables via foreign keys
378 - Should output: `TRUNCATE TABLE schema.table1, schema.table2;`
379 - Straightforward implementation following existing patterns in decoder_raw.c (~50-100 lines)
380 - Alternative: Enforce organizational policy against using TRUNCATE
382 ## Key Challenges and Solutions
384 ### 1. Replica Identity Required for UPDATE/DELETE
386 **Problem**: Tables need replica identity for UPDATE/DELETE operations.
388 From PostgreSQL documentation (Section 29.1.1):
389 > Tables with a replica identity defined as `NOTHING`, `DEFAULT` without a primary key, or `USING INDEX` with a dropped index, **cannot support UPDATE or DELETE operations**. **Attempting such operations will result in an error on the publisher.**
391 This means UPDATE/DELETE will **fail on the source database**, not just during restore!
393 **Solution**: Ensure all tables have one of:
394 - A primary key (automatic replica identity)
395 - A unique index configured via `REPLICA IDENTITY USING INDEX index_name`
396 - Explicit `REPLICA IDENTITY FULL` setting (inefficient, last resort)
400 -- Table without primary key will error on UPDATE/DELETE
401 CREATE TABLE logs (timestamp TIMESTAMPTZ, message TEXT);
403 -- Fix: Set replica identity to FULL
404 ALTER TABLE logs REPLICA IDENTITY FULL;
407 ### 2. Replication Slots Prevent WAL Cleanup
409 **Problem**: Inactive replication slots prevent WAL cleanup, leading to:
410 1. Disk fills up (WAL files not cleaned)
411 2. Table bloat (VACUUM cannot clean old row versions)
412 3. **Database shutdown** (transaction ID wraparound)
414 From PostgreSQL documentation (Section 47.2.2):
415 > In extreme cases this could cause the database to shut down to prevent transaction ID wraparound.
418 - **Monitor slot lag aggressively** (see monitoring section)
419 - Set `max_slot_wal_keep_size` parameter (PostgreSQL 13+) to limit WAL retention
420 - Have documented emergency procedure to drop slot if needed
421 - Consider `pg_replication_slot_advance()` to skip ahead (loses backup coverage)
423 ### 3. Sequences Are Not Replicated
425 **Problem**: Sequence values are not captured in logical replication.
428 - Use `pg_dump --sequence-data` (enabled by default) in periodic full dumps
429 - After restore, synchronize sequences:
431 SELECT setval('users_id_seq', (SELECT MAX(id) FROM users));
434 ### 4. Large Objects Are Not Replicated
436 **Problem**: PostgreSQL large objects are not captured in logical replication.
439 - **Preferred**: Use `BYTEA` columns instead (these ARE replicated)
440 - **Alternative**: Use `pg_dump --large-objects` in periodic full backups
441 - Note: Incremental changes to large objects NOT captured between full backups
443 ### 5. Crash Recovery and Duplicate Handling
445 **Problem**: After database crash, slot position may roll back, causing duplicate changes.
447 From PostgreSQL documentation (Section 47.2.2):
448 > The current position of each slot is persisted only at checkpoint, so in the case of a crash the slot might return to an earlier LSN, which will then cause recent changes to be sent again when the server restarts.
450 **Solution**: The restore process handles duplicates naturally through idempotent operations. Per PostgreSQL documentation (Section 47.2.2): "Logical decoding clients are responsible for avoiding ill effects from handling the same message more than once."
453 - Most SQL operations in backup files are naturally idempotent:
454 - INSERT will fail on duplicate primary key (acceptable during restore)
455 - UPDATE will reapply same values (idempotent)
456 - DELETE will succeed or report row not found (acceptable)
457 - Transaction boundaries (BEGIN/COMMIT from `include_transaction=on`) ensure consistency
458 - Simply apply all incremental files in chronological order
459 - No additional LSN tracking infrastructure required
460 - See Restore Script section (Section 4) for implementation details
463 - Test crash scenarios with `pg_ctl stop -m immediate` to verify duplicate handling
464 - Monitor `confirmed_flush_lsn` lag during normal operations (see Monitoring section)
466 ### 6. Long-Term Readability
469 - PostgreSQL syntax may change between major versions (rare)
470 - Extension dependencies may not exist in future systems
471 - Encoding/collation definitions may change
473 **Solution**: Include metadata file with each backup:
474 - PostgreSQL version (full version string)
475 - All installed extension names and versions
477 - Locale and collation settings
478 - Custom data types and enums
480 Periodically test restoring old backups on current PostgreSQL versions.
482 ## Prerequisites and Configuration
484 ### PostgreSQL Configuration
489 # Required: Set WAL level to logical
492 # Required: Allow at least one replication slot
493 max_replication_slots = 10
495 # Recommended: Allow replication connections
498 # Recommended: Keep more WAL for safety
501 # Recommended: Limit WAL retention for safety (PostgreSQL 13+)
502 max_slot_wal_keep_size = 10GB
504 # Optional: Tune checkpoint frequency to persist slot positions more often
505 checkpoint_timeout = 5min
508 ### Client Requirements
510 - PostgreSQL client utilities installed (`pg_recvlogical`, `pg_dump`, `pg_dumpall`)
511 - Superuser or role with `REPLICATION` privilege
512 - Permission to create replication slots
513 - decoder_raw plugin compiled and installed
515 ## Operational Procedures
520 - **Incremental**: Continuously streaming via `pg_recvlogical`
521 - **Full backup**: Daily at 2 AM
522 - **Globals backup**: Daily (`pg_dumpall --globals-only`)
523 - **Metadata export**: Daily (PostgreSQL version, extensions, encoding)
527 - **Incremental backups**: Keep 7 days
528 - **Full backups**: Keep 30 days, then one per month for 1 year
529 - **Monitor disk space**: Alert if backup directory exceeds 80% capacity
531 ### Disaster Recovery Runbook
533 1. **Stop application** to prevent new writes during restore
534 2. **Create new database** (don't overwrite production)
535 3. **Restore shared objects**: `psql -f globals-YYYY-MM-DD.sql`
536 4. **Restore full backup**: `psql dbname < base-YYYY-MM-DD.sql`
537 5. **Apply all incremental backups**: `for f in incremental-*.sql; do psql dbname < "$f"; done`
538 - DDL and DML are already interleaved in correct chronological order
539 6. **Sync sequences**: Run `setval()` for all sequences to match table max values
540 7. **Verify data integrity**: Check row counts, run application smoke tests
541 8. **Test application** against restored database
542 9. **Switch over** application to restored database
544 **See Section 4 (Restore Script)** for detailed procedures including sequence synchronization and duplicate transaction handling.
548 ### 1. Basic Functionality Test
551 -- Create test database and setup
552 CREATE DATABASE backup_test;
556 CREATE TABLE test_users (id SERIAL PRIMARY KEY, name TEXT, created_at TIMESTAMP DEFAULT now());
558 -- Generate data and schema changes to test DDL/DML ordering
559 INSERT INTO test_users (name) VALUES ('Alice'), ('Bob'), ('Charlie');
560 UPDATE test_users SET name = 'Alice Smith' WHERE id = 1;
561 DELETE FROM test_users WHERE id = 3;
563 -- Add column - DDL message should appear in stream here
564 ALTER TABLE test_users ADD COLUMN email TEXT;
566 -- Use the new column - should work because DDL already executed
567 UPDATE test_users SET email = 'alice@example.com' WHERE id = 1;
569 -- Drop column - DDL message should appear in stream here
570 ALTER TABLE test_users DROP COLUMN created_at;
572 -- Subsequent inserts should work without the dropped column
573 INSERT INTO test_users (name, email) VALUES ('David', 'david@example.com');
575 -- Restore and verify:
576 -- 1. All operations should replay successfully
577 -- 2. DML before column add should not reference email column
578 -- 3. DML after column add should reference email column
579 -- 4. DML after column drop should not reference created_at column
582 ### 2. Crash Recovery Test
585 # Start collecting incrementals
586 # Generate load with pgbench
587 # Simulate crash: pg_ctl stop -m immediate
589 # Verify no data loss and duplicates handled correctly
593 ### 3. Long-Term Storage Test
598 # Wait (or simulate) years passing
599 # Restore on modern PostgreSQL version
600 # Verify SQL is still readable and executable
603 ### 4. Replica Identity Test
606 -- Create table without primary key
607 CREATE TABLE test_no_pk (col1 TEXT, col2 INT);
609 -- Attempt UPDATE (should fail with replica identity error)
610 UPDATE test_no_pk SET col2 = 5 WHERE col1 = 'test';
612 -- Fix with REPLICA IDENTITY FULL
613 ALTER TABLE test_no_pk REPLICA IDENTITY FULL;
615 -- Retry UPDATE (should succeed)
618 ### 5. TRUNCATE Handling Test
622 CREATE TABLE test_truncate (id INT);
623 INSERT INTO test_truncate VALUES (1), (2), (3);
626 TRUNCATE test_truncate;
628 -- Verify: Check if decoder_raw incremental backup captured TRUNCATE
629 -- Expected: SHOULD be captured by extended decoder_raw with truncate_cb
630 -- Look for: TRUNCATE TABLE public.test_truncate;
631 -- Note: Event triggers do NOT capture TRUNCATE (it's DML, not DDL)
633 -- Test TRUNCATE with multiple tables (foreign key cascade)
634 CREATE TABLE parent_table (id INT PRIMARY KEY);
635 CREATE TABLE child_table (parent_id INT REFERENCES parent_table(id));
636 INSERT INTO parent_table VALUES (1), (2);
637 INSERT INTO child_table VALUES (1), (2);
639 -- TRUNCATE CASCADE should capture both tables
640 TRUNCATE parent_table, child_table;
641 -- Expected output: TRUNCATE TABLE public.parent_table, public.child_table;
644 ## Performance Considerations
646 **Write Amplification**:
647 - WAL must be written (normal)
648 - WAL must be decoded into logical format (additional CPU)
649 - Event triggers fire on every DDL operation (minimal overhead)
652 - Additional WAL volume retained by replication slots
653 - More frequent checkpoint I/O if checkpoint_timeout is tuned
656 - Benchmark overhead on test system with production-like workload
657 - Monitor CPU usage of WAL sender processes
658 - Monitor disk usage for WAL and backup directories
660 ## Next Steps for Proof of Concept
662 1. **Extend and install decoder_raw**
663 - Clone pg_plugins repository
664 - Install PostgreSQL development headers
665 - Add `message_cb` callback to decoder_raw.c for DDL messages
666 - Add `truncate_cb` callback to decoder_raw.c for TRUNCATE operations
667 - Compile and install modified decoder_raw
668 - Test both DDL message handling and TRUNCATE support
671 - Create replication slot with extended decoder_raw
672 - Set up event triggers using `pg_logical_emit_message()` for DDL capture
673 - Take initial synchronized base backup
675 3. **Streaming Collection**
676 - Test `pg_recvlogical` with extended decoder_raw
677 - Verify output is immediately executable SQL
678 - Test with various data types and operations
681 - Test event trigger emits DDL messages correctly via `pg_logical_emit_message()`
682 - Verify DDL appears in incremental backup stream at correct chronological position
683 - Test DDL/DML interleaving (e.g., add column, insert with new column, drop column, insert without column)
684 - Test `pg_dumpall --globals-only` captures shared objects
685 - Verify simple sequential restore works correctly
687 5. **Monitoring Setup**
688 - Configure replication slot monitoring
689 - Set up critical alerting
690 - Document emergency procedures
692 6. **Restore Process**
693 - Build restore scripts
694 - Test point-in-time recovery
695 - Verify sequence synchronization
697 7. **Crash Recovery**
698 - Test duplicate handling with `pg_ctl stop -m immediate`
699 - Verify idempotent restore behavior
701 8. **Performance Testing**
702 - Measure storage overhead
703 - Measure CPU overhead
704 - Benchmark restore time
708 ### PostgreSQL Documentation
709 - PostgreSQL Documentation: Chapter 25 - Backup and Restore
710 - PostgreSQL Documentation: Chapter 29 - Logical Replication
711 - PostgreSQL Documentation: Chapter 47 - Logical Decoding
712 - PostgreSQL Documentation: Section 29.8 - Logical Replication Restrictions
713 - PostgreSQL Documentation: Section 47.6.4.8 - Generic Message Callback (message_cb)
714 - PostgreSQL Documentation: Section 9.28.6 - `pg_logical_emit_message()` function
715 - PostgreSQL Documentation: `pg_recvlogical` man page
716 - PostgreSQL Documentation: `pg_dump` man page
717 - PostgreSQL Documentation: `pg_dumpall` man page
720 - **decoder_raw**: SQL output plugin for logical decoding
721 - Source: https://github.com/michaelpq/pg_plugins/tree/main/decoder_raw
722 - **CRITICAL COMPONENT**: Eliminates output transformation layer
723 - License: PostgreSQL License (production-ready)
724 - Compatibility: PostgreSQL 9.4+
727 - **Prometheus + postgres_exporter + Grafana**: Open-source monitoring stack
728 - **pgDash**: PostgreSQL monitoring - https://pgdash.io/
729 - **check_postgres**: Nagios/Icinga/Zabbix integration
730 - **pg_stat_replication_slots**: Built-in PostgreSQL monitoring view