Feature: Improve peerDB for pg-to-pg migrations #3678
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add Index and Trigger Migration Support for Postgres-to-Postgres Replication
🎯 Overview
This PR implements comprehensive schema object migration support for PostgreSQL-to-PostgreSQL replication in PeerDB, specifically adding the ability to migrate indexes and triggers alongside data. This enhancement ensures that destination tables maintain the same performance characteristics and business logic enforcement as source tables.
📊 Impact Summary
🚀 What's New
Core Features
LOWER(email))🏗️ Architecture
New Components
1. Protocol Buffer Definitions (
protos/flow.proto)Why these fields?
index_def: Full CREATE INDEX statement frompg_get_indexdef()- ensures exact recreationtimingandevents: Critical for understanding trigger behaviorfor_each: Determines trigger execution frequency (per row vs per statement)2. Core Implementation (
schema_delta_indexes_triggers.go)Four main functions:
Key Implementation Details:
Why this approach?
pg_get_indexdef()returns the exact DDL used to create the indexWhy decode tgtype?
tgtypetgisinternalfilterWhy not compare by name?
idx_emailvsidx_user_email- same structure, different names3. Enhanced ReplayTableSchemaDeltas (
postgres.go)Operation Order (Critical!):
Why this order?
Table Name Replacement Logic:
Why simple string replace?
pg_get_indexdef()always formats: "... ON schema.table ..."🧪 Testing Strategy
Test Coverage (12 scenarios)
Basic Operations (6 tests)
Advanced Scenarios (6 tests)
LOWER(email))Test Isolation
Benefits:
E2E Test
Located in
postgres_indexes_triggers_test.go:📈 Performance Analysis
Query Performance
Index Extraction:
Trigger Extraction:
Migration Performance
Single Index Creation:
Optimization Opportunity:
Transaction Overhead
Before (columns only):
After (with indexes/triggers):
Impact:
Memory Footprint
Per Table:
For 1000 tables:
🔍 Technical Deep Dive
Challenge 1: Index Name Conflicts
Problem:
Solution:
Challenge 2: Structure vs Name Comparison
Problem:
Solution:
Challenge 3: Table Name in DDL
Problem:
Solution:
🛡️ Safety & Reliability
Transactional Guarantees
Benefits:
Error Handling
Every operation wrapped with:
Provides:
Logging
Helps with:
📋 Usage Examples
Example 1: Migrating a User Table
Source Table:
PeerDB Automatically Replicates:
idx_emailindex ✅ (NEW!)idx_createdindex ✅ (NEW!)set_created_attrigger ✅ (NEW!)Example 2: Complex Indexes
Source:
All migrated with exact definitions!
🔄 Migration Path
Backward Compatibility
Existing Flows:
New Flows:
Future Enhancements
Possible Improvements:
📊 Test Results
Unit Tests (12/12 Passed)
Compilation
🎯 Requirements Checklist
Original Requirement:
Delivered:
TableSchemaDeltawith index/trigger fields🔐 Security Considerations
SQL Injection Prevention
Safe:
Also Safe:
Permission Requirements
Source Database:
SELECTon system catalogs (pg_index,pg_trigger)Destination Database:
CREATEon indexes (usually inherited from table owner)CREATE TRIGGERpermission📝 Code Quality
Metrics
Design Principles
🚦 Breaking Changes
None! This is a pure addition.
📚 References
👥 Reviewer Notes
Files to Focus On
protos/flow.proto(19 lines)schema_delta_indexes_triggers.go(237 lines)postgres.go(96 lines changed)Testing Locally
Questions to Consider
Ready to merge! 🚀