Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ rmp-serde = "1.3.1"
tracing-subscriber = "0.3.23"

# Model checking of `SharedLsnFeedback` (`RUSTFLAGS='--cfg loom'`); never built otherwise.
[target.'cfg(loom)'.dependencies]
# A dev-dependency, not a dependency: a target-specific dependency never compiles downstream but
# still lands in every consumer's Cargo.lock (loom, generator, tracing-subscriber, windows-*, ...).
[target.'cfg(loom)'.dev-dependencies]
loom = "0.7.3"

[lints.rust]
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ struct User { /* fields as above */ }
| Format | Crate | Transaction | Read across versions | Notes |
|--------|-------|------------:|----------------------|-------|
| JSON | `serde_json` | 1174 B (100%) | yes, bytes are unchanged | readable, any language; the one to store long-term |
| CBOR | `ciborium` | 933 B (79%) | no, see [Upgrading](#upgrading-from-09) | self-describing, writes field names |
| MessagePack | `rmp-serde` | 498 B (42%) | no, see [Upgrading](#upgrading-from-09) | `to_vec` writes fields as an array, `to_vec_named` keeps field names |
| CBOR | `ciborium` | 933 B (79%) | no, see [Upgrading](docs/upgrading/0.10-to-0.11.md#serde-formats) | self-describing, writes field names |
| MessagePack | `rmp-serde` | 498 B (42%) | no, see [Upgrading](docs/upgrading/0.10-to-0.11.md#serde-formats) | `to_vec` writes fields as an array, `to_vec_named` keeps field names |
| postcard | `postcard` | 349 B (30%) | same version on both ends | smallest, `no_std` friendly, Rust to Rust |
| bincode 2 | `bincode` | 361 B (31%) | same version on both ends | Rust to Rust; the crate is unmaintained, so prefer postcard in new code |

Expand Down Expand Up @@ -446,6 +446,10 @@ The gap is OpenSSL's per-call bookkeeping, not crypto: with TLS, libpq spends ab

- **The minimum supported Rust version is 1.88.**

## Upgrading

Upgrade notes are in the [`docs/upgrading`](docs/upgrading) folder, one file per version step, named `<from>-to-<to>.md`. Before moving to a new version, read the file for each step between the version you run and the one you want; for example, [0.10 to 0.11](docs/upgrading/0.10-to-0.11.md).

## Resources

- [PostgreSQL Logical Replication Documentation](https://www.postgresql.org/docs/current/logical-replication.html)
Expand Down
109 changes: 109 additions & 0 deletions docs/upgrading/0.10-to-0.11.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Upgrading from 0.10 to 0.11

What changed since `v0.10.0` that can break a build or change what a running consumer sees. Additions that break nothing (stream metrics, Unix-domain sockets, `type_catalog`, ...) are in the [README](../../README.md).

| Change | It affects you if | Section |
|--------|-------------------|---------|
| `ReplicationError` variants carry the server's `ErrorResponse` | you `match` on or build `Protocol`, `TransientConnection`, `PermanentConnection`, `Authentication` or `ReplicationSlot` | [Error type](#error-type) |
| Minimum Rust is 1.88 | you build with an older toolchain | [Minimum Rust version](#minimum-rust-version) |
| Non-JSON serde bytes changed | you store or ship `ChangeEvent`s as CBOR, MessagePack, postcard or bincode | [Serde formats](#serde-formats) |
| A `Relation` event for every re-announcement | you read `EventType::Relation` as "the schema changed" | [Relation events](#relation-events) |
| The connection runs on the runtime that created it | native backend, and you create a stream inside a current-thread runtime and read it from another | [Runtime affinity](#runtime-affinity) |
| `hostaddr`, `sslcert`, `sslkey`, `application_name` and `options` take effect | native backend, and your connection string sets any of them | [Connection strings](#connection-strings-native-backend) |

## Error type

`Protocol`, `TransientConnection`, `PermanentConnection`, `Authentication` and `ReplicationSlot` went from `(String)` to `(Box<str>, Option<Box<ServerError>>)`. The second field is the server's `ErrorResponse` when the error came from one.

```rust,ignore
// 0.10
Err(ReplicationError::PermanentConnection(msg)) => log(msg),
// 0.11
Err(ReplicationError::PermanentConnection(msg, _)) => log(msg),
```

- Build errors with the constructors (`ReplicationError::protocol(msg)`, `permanent_connection(msg)`, ...), which are unchanged.
- `ReplicationError::server_error()` returns the `ServerError` (SQLSTATE, detail, hint, routine) and `sqlstate()` returns just the code.
- Which errors are retried did not change, with one rename: a server error `42704` while starting up is a `PermanentConnection`, no longer a `ReplicationSlot`. Both are permanent.

## Minimum Rust version

1.88, up from 1.87: the `sqlstate` crate behind `ReplicationError::sqlstate()` needs it.

## Serde formats

`ChangeEvent` and `ColumnValue` now write the same fields in a different layout in formats that are not human-readable. JSON bytes are unchanged.

| Format | 0.10 bytes read by 0.11 | 0.11 bytes read by 0.10 |
|--------|------------------------|-------------------------|
| JSON | yes | yes |
| CBOR | no: `unknown variant`, expected `Text` or `Binary` | no: `unknown key "Text"` |
| MessagePack (`to_vec` and `to_vec_named`) | no, the same errors | no, the same errors |
| postcard | no | no |
| bincode 2 | no | no |

0.10 could write postcard and bincode but never read them back, not even its own bytes (`deserialize_any` is not supported there). 0.11 reads what it writes.

What to do:

- Drain or re-encode anything stored or queued in one of those formats before the consumer that reads it moves to 0.11, and upgrade producer and consumer together.
- Keep events you store long-term as JSON. The other formats are for the same version on both ends; see [Shipping Events Between Processes](../../README.md#shipping-events-between-processes).

## Relation events

Since 0.11 every pgoutput `Relation` message becomes an `EventType::Relation` event, including one identical to the definition already cached. In 0.10 an identical one was dropped, so the event meant "first seen, or the columns changed". Now it means "pgoutput announced this relation again".

PostgreSQL re-sends a `Relation` on the next change to a published table after any invalidation of that table's relcache entry, so you will now see one after:

- `ANALYZE`, `VACUUM FULL` and autovacuum, `CREATE`/`DROP INDEX`, `REINDEX`;
- `GRANT`, `ALTER POLICY`, `ALTER TABLE ... OWNER`, `SET (fillfactor)`, `ALTER PUBLICATION ... SET (publish = ...)`;
- an automatic reconnect, and once per streamed top-level transaction (`StreamingMode::On`).

This is deliberate ([PR #173](https://github.com/isdaniel/pg-walstream/pull/173)): a policy or grant change re-sends a definition identical to the cached one, so that repeat is the only point in the stream where a consumer can re-read its rules in commit order. A spurious re-read after maintenance costs one catalog query; a missed revoke keeps delivering rows the new policy hides.

If you treated `Relation` as "the schema changed", compare it yourself:

```rust,ignore
if let EventType::Relation { relation_id, columns, .. } = &event.event_type {
if seen.insert(*relation_id, columns.clone()).as_ref() != Some(columns) {
// first sight or a column change
}
}
```

`columns` carries names, type OIDs, modifiers and key flags. It does not tell a column dropped and re-added under the same name and type from an unchanged one.

`event.lsn` of a `Relation` event is `0/0`: pgoutput sends these frames with no WAL position. That is not new, only how often it appears. `SharedLsnFeedback` and `EventStream::for_each_event` ignore a zero LSN, but a checkpoint you write yourself from `event.lsn` must skip `Relation` events or it will move backwards.

## Runtime affinity

On a current-thread runtime 0.10 gave the native backend's connection a worker thread of its own. 0.11 runs it on the runtime that created the stream, which measured about 30% less CPU per event there and one thread fewer. The cost is that the socket belongs to that runtime's reactor, and a current-thread runtime turns its reactor only while it is running.

So a stream created inside `rt.block_on(..)` on a current-thread runtime and read from another runtime while `rt` is idle makes no progress. The first such read logs this warning:

```text
this stream's connection was created on a current-thread runtime and is being read from another runtime (or outside one): it makes progress only while the creating runtime is running. ...
```

Either read the stream on the runtime that created it, or give the connection its own thread as 0.10 did:

```rust,ignore
let config = ReplicationStreamConfig::builder("slot", "publication")
.with_dedicated_io_thread(true);
```

A multi-thread runtime drives its own reactor from its workers, so streams created on one can be read from anywhere. The libpq backend is not affected.

## Connection strings (native backend)

0.10 accepted and ignored several options the 0.11 native backend acts on. A string that connected before can fail now, with the same meaning it has in libpq.

- **`hostaddr`** is where to connect and **`host`** is the name the server's certificate is checked against, as in libpq. In 0.10 whichever came last in the string was both. `hostaddr=127.0.0.1 host=localhost sslmode=verify-full` needs a certificate for `localhost`. A non-empty `hostaddr` wins over a `host` socket directory, and an empty `hostaddr=` is ignored. A URI host is percent-decoded (`postgresql://%2Ftmp/db`).
- **`sslcert` and `sslkey`** are now loaded and presented to a server that asks for a client certificate ([Client certificates](../../README.md#client-certificates)). This can fail where 0.10 connected:
- a pair that exists but cannot be used (an encrypted key, a key that does not match, an unreadable file) is an error under every `sslmode` except `disable`, `prefer` and `allow` included, even against a server that never asks for a certificate;
- a lone `sslcert=` is a `Config` error when the string is parsed, which is not retried;
- the other errors count as connection failures, so they are retried (five attempts, about 15 s) before they are returned;
- `sslmode=allow` tries TLS first here, so it reads the files where libpq, which tries plaintext first, would not.

Remove the options, decrypt the key, or add `sslcertmode=disable` or `sslmode=disable` to keep the old behavior.
- **`application_name`, `fallback_application_name` and `options`** now reach the server's startup message ([Session Parameters](../../README.md#session-parameters)). A string that carried them for the libpq backend now behaves the same on the native one.
Loading