pg_walstream 0.6.3

PostgreSQL logical replication protocol library - parse and handle PostgreSQL WAL streaming messages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
name: CI

on:
  push:
    branches: [ main, develop ]
    tags:
      - 'v*'
  pull_request:
    branches: [ "*" ]

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1
  MIN_COVERAGE: 90

jobs:
  test:
    name: Test Suite
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
        rust: [stable, beta]

    steps:
    - uses: actions/checkout@v4

    - name: Install Rust
      uses: dtolnay/rust-toolchain@master
      with:
        toolchain: ${{ matrix.rust }}
        components: rustfmt, clippy

    - name: Install dependencies (Ubuntu)
      if: matrix.os == 'ubuntu-latest'
      run: |
        sudo apt-get update
        sudo apt-get install -y libpq-dev pkg-config libssl-dev

    - name: Cache cargo registry
      uses: actions/cache@v4
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-cargo-

    - name: Cache target directory
      uses: actions/cache@v4
      with:
        path: target
        key: ${{ runner.os }}-${{ matrix.rust }}-target-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-${{ matrix.rust }}-target-

    - name: Check formatting
      run: cargo fmt --all -- --check

    - name: Build
      run: cargo build --verbose

    - name: Run unit tests
      run: cargo test --lib --verbose

  security:
    name: Security Audit
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: dtolnay/rust-toolchain@stable
    - name: Install cargo-audit
      run: cargo install cargo-audit
    - name: Run security audit
      run: cargo audit

  docs:
    name: Documentation
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: dtolnay/rust-toolchain@stable

    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libpq-dev pkg-config libssl-dev

    - name: Build documentation
      run: cargo doc --no-deps --all-features
      env:
        RUSTDOCFLAGS: "-D warnings"

  coverage:
    name: Code Coverage
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4

    - name: Install Rust
      uses: dtolnay/rust-toolchain@stable
      with:
        components: llvm-tools-preview

    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libpq-dev pkg-config libssl-dev

    - name: Install cargo-llvm-cov
      uses: taiki-e/install-action@cargo-llvm-cov

    - name: Cache cargo registry
      uses: actions/cache@v4
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
        key: ${{ runner.os }}-cargo-coverage-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-cargo-coverage-

    - name: Generate coverage report (lcov for Codecov)
      run: cargo llvm-cov --lib --lcov --output-path lcov.info

    - name: Generate coverage report (json for threshold check)
      run: cargo llvm-cov --lib --json --output-path coverage.json

    - name: Check coverage threshold (minimum ${{ env.MIN_COVERAGE }}%)
      run: |
        COVERAGE=$(cat coverage.json | python3 -c "
        import json, sys
        data = json.load(sys.stdin)
        totals = data['data'][0]['totals']
        lines = totals['lines']
        pct = lines['percent']
        print(f'{pct:.2f}')
        ")
        echo "Line coverage: ${COVERAGE}%"
        if python3 -c "exit(0 if float('${COVERAGE}') >= ${{ env.MIN_COVERAGE }} else 1)"; then
          echo "Coverage ${COVERAGE}% meets minimum threshold of ${{ env.MIN_COVERAGE }}%"
        else
          echo "Coverage ${COVERAGE}% is below minimum threshold of ${{ env.MIN_COVERAGE }}%"
          exit 1
        fi

    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v4
      with:
        files: lcov.info
        fail_ci_if_error: false
        token: ${{ secrets.CODECOV_TOKEN }}

  integration:
    name: Integration Tests (PG ${{ matrix.pg_version }})
    runs-on: ubuntu-latest
    needs: test
    strategy:
      fail-fast: false
      matrix:
        # pgoutput protocol v2 (streaming in-progress transactions) requires PG 15+
        pg_version: [15, 16, 17, 18]

    services:
      postgres:
        image: postgres:${{ matrix.pg_version }}
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: test_walstream
        ports:
          - 5432:5432
        options: >-
          --health-cmd "pg_isready -U postgres"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
    - uses: actions/checkout@v4

    - name: Install Rust
      uses: dtolnay/rust-toolchain@stable

    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libpq-dev pkg-config libssl-dev

    - name: Cache cargo registry
      uses: actions/cache@v4
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
        key: ${{ runner.os }}-cargo-integration-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-cargo-integration-

    - name: Configure PostgreSQL for logical replication
      env:
        PGPASSWORD: postgres
      run: |
        psql -h localhost -U postgres -d test_walstream -c "ALTER SYSTEM SET wal_level = 'logical';"
        psql -h localhost -U postgres -d test_walstream -c "ALTER SYSTEM SET max_replication_slots = 16;"
        psql -h localhost -U postgres -d test_walstream -c "ALTER SYSTEM SET max_wal_senders = 16;"
        # Restart PostgreSQL container to apply wal_level change (requires restart)
        docker restart ${{ job.services.postgres.id }}
        # Wait for PostgreSQL to be ready after restart
        for i in $(seq 1 30); do
          pg_isready -h localhost -p 5432 -U postgres && break
          echo "Waiting for PostgreSQL after restart... ($i/30)"
          sleep 2
        done

    - name: Verify PostgreSQL logical replication config
      env:
        PGPASSWORD: postgres
      run: |
        psql -h localhost -U postgres -d test_walstream -c "SHOW wal_level;"
        psql -h localhost -U postgres -d test_walstream -c "SHOW max_replication_slots;"
        psql -h localhost -U postgres -d test_walstream -c "SHOW max_wal_senders;"

    - name: Run integration tests
      env:
        DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/test_walstream?replication=database"
        DATABASE_URL_REGULAR: "postgresql://postgres:postgres@localhost:5432/test_walstream"
        RUST_BACKTRACE: 1
      run: |
        cargo test --test snapshot_export -- --ignored --nocapture --test-threads=1

        # Clean up any lingering replication slots between test suites
        PGPASSWORD=postgres psql -h localhost -U postgres -d test_walstream \
          -c "SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots WHERE slot_type = 'logical';" || true

        cargo test --test rate_limited_streaming -- --ignored --nocapture --test-threads=1

        PGPASSWORD=postgres psql -h localhost -U postgres -d test_walstream \
          -c "SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots WHERE slot_type = 'logical';" || true

        cargo test --test safe_transaction_consumer -- --ignored --nocapture --test-threads=1

        PGPASSWORD=postgres psql -h localhost -U postgres -d test_walstream \
          -c "SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots WHERE slot_type = 'logical';" || true

        cargo test --test complex_types -- --ignored --nocapture --test-threads=1

        PGPASSWORD=postgres psql -h localhost -U postgres -d test_walstream \
          -c "SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots WHERE slot_type = 'logical';" || true

        cargo test --test typed_deserialization -- --ignored --nocapture --test-threads=1

  integration-ssl:
    name: SSL Integration Tests (PG ${{ matrix.pg_version }})
    runs-on: ubuntu-latest
    needs: test
    strategy:
      fail-fast: false
      matrix:
        # pgoutput protocol v2 (streaming in-progress transactions) requires PG 15+
        pg_version: [15, 16, 17, 18]

    services:
      postgres:
        image: postgres:${{ matrix.pg_version }}
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: test_walstream
        ports:
          - 5432:5432
        options: >-
          --health-cmd "pg_isready -U postgres"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
    - uses: actions/checkout@v4

    - name: Install Rust
      uses: dtolnay/rust-toolchain@stable

    - name: Install dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y cmake pkg-config libssl-dev postgresql-client

    - name: Cache cargo registry
      uses: actions/cache@v4
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
        key: ${{ runner.os }}-cargo-ssl-${{ hashFiles('**/Cargo.lock') }}
        restore-keys: |
          ${{ runner.os }}-cargo-ssl-

    - name: Generate SSL certificates
      run: |
        mkdir -p /tmp/pg-ssl

        # Generate CA key and self-signed certificate
        openssl req -new -x509 -days 365 -nodes \
          -newkey rsa:2048 \
          -keyout /tmp/pg-ssl/ca.key \
          -out /tmp/pg-ssl/ca.crt \
          -subj "/CN=pg-walstream-test-ca"

        # Generate server key and CSR
        openssl req -new -nodes \
          -newkey rsa:2048 \
          -keyout /tmp/pg-ssl/server.key \
          -out /tmp/pg-ssl/server.csr \
          -subj "/CN=localhost"

        # Write SAN extension config to a file (avoids process substitution issues)
        echo "subjectAltName=DNS:localhost" > /tmp/pg-ssl/server.ext

        # Sign server cert with CA (SAN=DNS:localhost only, no IP SAN)
        # This allows verify-full to succeed with localhost but fail with 127.0.0.1
        openssl x509 -req -days 365 \
          -in /tmp/pg-ssl/server.csr \
          -CA /tmp/pg-ssl/ca.crt \
          -CAkey /tmp/pg-ssl/ca.key \
          -CAcreateserial \
          -out /tmp/pg-ssl/server.crt \
          -extfile /tmp/pg-ssl/server.ext

        # Generate a separate "wrong" CA for negative tests
        openssl req -new -x509 -days 365 -nodes \
          -newkey rsa:2048 \
          -keyout /tmp/pg-ssl/wrong-ca.key \
          -out /tmp/pg-ssl/wrong-ca.crt \
          -subj "/CN=wrong-ca"

    - name: Verify generated certificates
      run: |
        echo "=== CA Certificate ==="
        openssl x509 -in /tmp/pg-ssl/ca.crt -noout -subject -issuer
        echo "=== Server Certificate ==="
        openssl x509 -in /tmp/pg-ssl/server.crt -noout -subject -issuer
        echo "=== Server Certificate SAN ==="
        openssl x509 -in /tmp/pg-ssl/server.crt -noout -ext subjectAltName
        echo "=== Verify server cert against CA ==="
        openssl verify -CAfile /tmp/pg-ssl/ca.crt /tmp/pg-ssl/server.crt

    - name: Configure PostgreSQL with SSL and logical replication
      env:
        PGPASSWORD: postgres
      run: |
        CONTAINER_ID=${{ job.services.postgres.id }}

        # Copy certificates into the PostgreSQL container
        docker cp /tmp/pg-ssl/server.crt $CONTAINER_ID:/var/lib/postgresql/server.crt
        docker cp /tmp/pg-ssl/server.key $CONTAINER_ID:/var/lib/postgresql/server.key
        docker cp /tmp/pg-ssl/ca.crt $CONTAINER_ID:/var/lib/postgresql/ca.crt

        # Set proper ownership and permissions
        docker exec $CONTAINER_ID chown postgres:postgres \
          /var/lib/postgresql/server.crt \
          /var/lib/postgresql/server.key \
          /var/lib/postgresql/ca.crt
        docker exec $CONTAINER_ID chmod 0600 /var/lib/postgresql/server.key

        # Configure SSL
        psql -h localhost -U postgres -d test_walstream -c "ALTER SYSTEM SET ssl = 'on';"
        psql -h localhost -U postgres -d test_walstream -c "ALTER SYSTEM SET ssl_cert_file = '/var/lib/postgresql/server.crt';"
        psql -h localhost -U postgres -d test_walstream -c "ALTER SYSTEM SET ssl_key_file = '/var/lib/postgresql/server.key';"
        psql -h localhost -U postgres -d test_walstream -c "ALTER SYSTEM SET ssl_ca_file = '/var/lib/postgresql/ca.crt';"

        # Configure logical replication
        psql -h localhost -U postgres -d test_walstream -c "ALTER SYSTEM SET wal_level = 'logical';"
        psql -h localhost -U postgres -d test_walstream -c "ALTER SYSTEM SET max_replication_slots = 16;"
        psql -h localhost -U postgres -d test_walstream -c "ALTER SYSTEM SET max_wal_senders = 16;"

        # Restart PostgreSQL to apply changes
        docker restart $CONTAINER_ID

        # Wait for PostgreSQL to be ready after restart
        for i in $(seq 1 30); do
          pg_isready -h localhost -p 5432 -U postgres && break
          echo "Waiting for PostgreSQL after restart... ($i/30)"
          sleep 2
        done

    - name: Verify PostgreSQL SSL and replication config
      env:
        PGPASSWORD: postgres
      run: |
        psql -h localhost -U postgres -d test_walstream -c "SHOW ssl;"
        psql -h localhost -U postgres -d test_walstream -c "SHOW wal_level;"
        psql -h localhost -U postgres -d test_walstream -c "SHOW max_replication_slots;"
        psql -h localhost -U postgres -d test_walstream -c "SHOW max_wal_senders;"
        # Verify SSL is actually working from the client side
        PGSSLMODE=require psql -h localhost -U postgres -d test_walstream \
          -c "SELECT ssl, version FROM pg_stat_ssl WHERE pid = pg_backend_pid();"

    - name: Build SSL test binary
      run: cargo test --test ssl_connections --no-default-features --features rustls-tls --no-run

    - name: Run SSL integration tests
      env:
        DATABASE_URL: "postgresql://postgres:postgres@localhost:5432/test_walstream?replication=database"
        DATABASE_URL_REGULAR: "postgresql://postgres:postgres@localhost:5432/test_walstream"
        SSL_CA_CERT_PATH: "/tmp/pg-ssl/ca.crt"
        SSL_WRONG_CA_CERT_PATH: "/tmp/pg-ssl/wrong-ca.crt"
        RUST_BACKTRACE: 1
      run: |
        cargo test --test ssl_connections --no-default-features --features rustls-tls -- --ignored --nocapture --test-threads=1

        # Clean up any lingering replication slots between runs
        PGPASSWORD=postgres psql -h localhost -U postgres -d test_walstream \
          -c "SELECT pg_drop_replication_slot(slot_name) FROM pg_replication_slots WHERE slot_type = 'logical';" || true

  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: [test, integration, integration-ssl, coverage]
    if: startsWith(github.ref, 'refs/tags/v')

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Verify tag is on main branch
      run: |
        git fetch origin main
        if git merge-base --is-ancestor "$GITHUB_SHA" origin/main; then
          echo "Tag commit is on main branch — proceeding with publish."
        else
          echo "Error: Tag is NOT on the main branch. Publishing is only allowed from main."
          exit 1
        fi

    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable

    - name: Install system dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libpq-dev pkg-config libssl-dev

    - name: Extract version from tag
      id: get_version
      run: |
        TAG=${GITHUB_REF#refs/tags/}
        VERSION=${TAG#v}
        echo "version=${VERSION}" >> $GITHUB_OUTPUT
        echo "tag=${TAG}" >> $GITHUB_OUTPUT

    - name: Verify tag matches Cargo.toml version
      run: |
        CARGO_VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/.*= "//' | sed 's/".*//')
        TAG_VERSION="${{ steps.get_version.outputs.version }}"
        echo "Cargo.toml version: $CARGO_VERSION"
        echo "Git tag version: $TAG_VERSION"
        if [ "$CARGO_VERSION" != "$TAG_VERSION" ]; then
          echo "Error: Version mismatch between Cargo.toml ($CARGO_VERSION) and git tag ($TAG_VERSION)"
          exit 1
        fi

    - name: Dry run publish
      run: cargo publish --dry-run
      env:
        CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

    - name: Publish to crates.io
      run: cargo publish
      env:
        CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}