Knotrel Docs

PostgreSQL reference feeder: protocol, assumptions and correctness

Status: design for roadmap items 0.9.1 and 0.9.2, written before the implementation and corrected where testing showed a gap (§4.3 recovery, §8.9 barrier under load); implemented in integrations/postgres-feeder/. It is a reference integration: one schema, one source, reproducible, with stated limits. It is not a general PostgreSQL connector, and nothing here runs inside the Knotrel server.

The feeder is a separate program (integrations/postgres-feeder/, its own Cargo workspace and lockfile). It reads one PostgreSQL database with logical replication and drives a Knotrel server in source-managed mode through the integration contract and the feeder routes of the HTTP API.

1. What it guarantees, in short

2. Components

 application ──writes──▶ PostgreSQL ──logical replication (pgoutput)──▶ feeder ──HTTP──▶ Knotrel (source-managed)
                         (authority)        slot + publication                          ▲
                                                                                         └── console (diagnostics)

3. Assumptions

4. Source schema and mapping

4.1 Schema

The reference schema (sql/schema.sql):

CREATE TABLE knotrel_graph.nodes (
    id     bigint  PRIMARY KEY CHECK (id >= 0),
    active boolean NOT NULL DEFAULT true
);

CREATE TABLE knotrel_graph.edges (
    source  bigint  NOT NULL REFERENCES knotrel_graph.nodes (id) ON DELETE CASCADE,
    target  bigint  NOT NULL REFERENCES knotrel_graph.nodes (id) ON DELETE CASCADE,
    enabled boolean NOT NULL DEFAULT true,
    PRIMARY KEY (source, target),
    CHECK (source < target)
);

Applications may add other columns; the feeder ignores them. Schema and table names are configurable.

4.2 Mapping

PostgreSQL Knotrel
nodes.id (bigint, 0 ≤ id ≤ 2^63−1) node ID, the same integer as a decimal string. Knotrel node IDs above 2^63−1 are not reachable from this schema.
nodes.active node lifecycle flag active
row of edges (source, target) the undirected edge {source, target}. CHECK (source < target) makes each undirected edge one row and excludes self-loops, which Knotrel rejects.
edges.enabled edge lifecycle flag enabled

The graph is the complete registered topology: every node row (including isolated and inactive nodes) and every edge row (including disabled edges). Connectivity in Knotrel is computed on the effective graph (active nodes, enabled edges between active nodes), as in topology-lifecycle.md.

Changes are translated row by row, from the change record alone (no feeder state), so that the same transaction always produces the same events:

Change Events, in order
INSERT nodes (id, active) add_node(id); then set_node_active(id, false) if active is false
UPDATE nodes, same id set_node_active(id, active) (a no-op in Knotrel if unchanged)
UPDATE nodes, id changed remove_node(old), add_node(new), then set_node_active(new, false) if inactive. With the reference foreign keys (NO ACTION on update) an id with edges cannot change; with ON UPDATE CASCADE the cascaded edge updates follow and are translated below
DELETE nodes remove_node(id) (removes its edges in Knotrel)
INSERT edges (s, t, enabled) link(s, t); then set_edge_enabled(s, t, false) if enabled is false
UPDATE edges, same key set_edge_enabled(s, t, enabled)
UPDATE edges, key changed cut(old_s, old_t), link(s, t), then set_edge_enabled(s, t, false) if disabled
DELETE edges cut(s, t)
TRUNCATE of either table no events: the feeder rebuilds from a new snapshot (§8.7)

Why this is exact under Knotrel’s rules:

4.3 Validation and unsupported changes

At startup the feeder checks, and refuses to start with an error naming the problem:

While streaming, PostgreSQL sends a relation description before the first change after any DDL. The feeder validates it the same way. A dropped, renamed or retyped required column, a renamed table, or a changed replica identity stops the feeder with exit code 3 and a message such as:

unsupported schema change on knotrel_graph.nodes: column "active" has type integer (OID 23), expected boolean

The feeder confirms nothing to PostgreSQL past the last applied change, so Knotrel keeps an exact prefix and the slot keeps every later change. Those later changes cannot simply be resumed, though: logical decoding describes each change with the catalog as it was when the change was written, so changes made under the unsupported schema decode the same way again. The recovery is a rebuild:

  1. fix or revert the schema;
  2. with the feeder stopped, run knotrel-pg-feeder drop-slots;
  3. start the feeder. The slot named in Knotrel’s position is gone, so it declares a gap and rebuilds from a fresh snapshot (§8.8).

Added columns are accepted and ignored. A value outside the mapping (a negative id, a NULL flag, a self-loop) is reported the same way, should a schema without the reference constraints let it through.

5. Bootstrap: one slot, one snapshot, no gap

  1. On the replication connection: IDENTIFY_SYSTEM (system identifier, timeline), then CREATE_REPLICATION_SLOT <slot> LOGICAL pgoutput (SNAPSHOT 'export'). PostgreSQL returns the slot’s consistent point L0 and an exported snapshot name.
  2. On the normal connection, before any other command on the replication connection: BEGIN ISOLATION LEVEL REPEATABLE READ READ ONLY; SET TRANSACTION SNAPSHOT '<name>'; then read both tables.
  3. Import the image into Knotrel at source position S = (slot, L0) (§6.1): create_only on a new instance, replace with If-Match otherwise.
  4. START_REPLICATION on the slot.

No-gap argument. PostgreSQL guarantees that the exported snapshot contains exactly the transactions committed before the slot’s consistent point, and that the slot decodes exactly the transactions committing after it. The image (step 2) and the stream (step 4) therefore partition the committed history at L0: no transaction is in both, none in neither. Writes during the bootstrap are simply streamed after the import. Rolled-back transactions are never decoded and never reach Knotrel.

6. Identities and positions

6.1 Tokens sent to Knotrel

Knotrel field Value
source_id pg;sys=<system identifier>;db=<database>;pub=<publication>
source_epoch pg1;sys=<system identifier>;tli=<timeline>;slot=<slot name>
source_position <source_epoch>;lsn=<commit LSN>;i=<n> plus ;end on the last event of a transaction; the snapshot position is <source_epoch>;lsn=<L0>;i=0;end
delivery_sequence 1, 2, 3, … after each import, as the contract requires
event_id <commit LSN>.<n> (unique in the stream; Knotrel resets its deduplication window on import)
feeder_session_id pgfeeder-<random>, new for each process
feeder_epoch wall-clock milliseconds at registration, strictly increasing in a process (A4)

Slot names are <prefix>_<suffix> with a fresh random suffix per bootstrap (prefix knotrel_feeder by default). Because the slot name is part of source_epoch, every bootstrap starts a new source lineage, and an event from an older lineage can never be accepted by a graph imported from a newer one (source_epoch_mismatch).

Knotrel treats positions as opaque. The feeder parses its own positions: in one lineage, (commit LSN, n) is totally ordered, because logical decoding emits transactions in commit order and commit LSNs increase.

6.2 Events per transaction

A transaction with commit LSN C that touches the graph tables becomes the events (C, 1) … (C, k) in the order of its changes. Transactions with no graph change produce no event. The feeder sends events in batches of at most 1024 (and under Knotrel’s 1 MiB request limit), each event carrying its predecessor’s position, so Knotrel can verify continuity across batches.

6.3 What a reader can rely on

Knotrel applies events one at a time under its graph lock. Every response carries the applied position (X-Knotrel-Source-Position, ETag) captured under the same lock as the answer. Therefore:

There is no multi-event atomicity in Knotrel’s feeder protocol, so the feeder cannot offer more. Applications that need transaction-consistent answers can check the position marker and retry.

7. Confirming progress to PostgreSQL

The feeder reports a flushed position to the slot (standby status update) only under this rule:

R1. Report F only if every graph change of every transaction whose commit end LSN is at most F has been acknowledged by Knotrel (a 200 applied or duplicate response, or a frontier read showing it applied), in the current Knotrel instance and generation.

In practice the feeder reports the commit end LSN of a transaction after Knotrel acknowledged its last event, and, when nothing is pending, the wal_end of keepalives so an idle graph does not pin WAL. It never reports a position for a transaction it has only partly sent.

Consequence: the slot’s confirmed position never passes Knotrel’s applied frontier in the same lineage. After any disconnection, PostgreSQL resends from the confirmed position; the feeder skips what Knotrel already holds (§8.2), so nothing is lost and nothing is applied twice.

R1 does not make the slot a record of what Knotrel holds: a restarted Knotrel has lost everything regardless of the slot (§8.4).

8. Failures and recovery

8.1 Knotrel request fails or its answer is lost

A batch request that times out or loses its connection has an uncertain outcome. The feeder does not resend blindly: it re-registers the same session and epoch (idempotent), whose response reports Knotrel’s applied delivery sequence and position, and resends only the events after that frontier. An exact resend of an applied batch would also be answered duplicate by Knotrel’s window; the feeder does not rely on that.

Knotrel answer Feeder action
200 applied / duplicate advance; confirm finished transactions (R1)
503 busy / import_in_progress, timeout, connection error back off (bounded exponential), reconcile frontier, resend the rest
409 stale_instance_id Knotrel restarted: rebuild (§8.4)
409 fenced_feeder_session another feeder took over: stop (§8.6)
409 lease_expired register a new epoch, reconcile, continue; barrier later
409 out_of_order_sequence, source_predecessor_mismatch, conflicting_duplicate, graph_desynchronized, source_epoch_mismatch divergence: rebuild (replace import)
422 limit_exceeded configuration error (Knotrel capacity): stop with exit code 4, confirm nothing
422 other domain errors (unknown_edge, …) divergence: declare a gap, rebuild

8.2 PostgreSQL connection lost

The feeder reconnects with bounded backoff and starts replication on the same slot. PostgreSQL resumes at the slot’s confirmed position, which by R1 is at or before Knotrel’s frontier. The feeder regenerates each resent transaction’s events (§4.2 is deterministic) and skips every event at or before Knotrel’s applied position (C, n); transactions entirely before it are skipped whole.

8.3 Feeder crash

The new process registers a session with a higher epoch. The response gives Knotrel’s generation, applied sequence D, applied position P and status. The feeder resumes on the slot named in P only if all of these hold:

  1. Knotrel’s status is catching_up, ready or stale and its generation is at least 1;
  2. P parses as this feeder’s format, and its system identifier and timeline match IDENTIFY_SYSTEM;
  3. the slot named in P exists, belongs to this database, uses pgoutput, is not invalidated (wal_status <> 'lost').

Otherwise it rebuilds. When it resumes, it streams from the slot and continues at D + 1 with predecessor P, skipping events at or before P as in §8.2. By R1, the slot cannot have advanced past what Knotrel applied.

8.4 Knotrel restart (state lost)

Any 409 stale_instance_id, or a different instance_id in GET /v1/info, means the graph is gone. The feeder always rebuilds: new slot, new exported snapshot, create_only import on the new instance, then streaming and a barrier. The old slot’s position is not reused, even though it is valid, because it describes what the old process applied.

8.5 Lease expiry

The feeder heartbeats at a third of the lease, also during a bootstrap (reading and importing a large image can take longer than the lease). If Knotrel reports stale (lease expired during a pause), the feeder registers a higher epoch, catches up and declares a new barrier, as §4.3 of the contract requires. A heartbeat answer for an earlier epoch, arriving after the new one was registered, is ignored.

8.6 Takeover

409 fenced_feeder_session means a feeder with a higher epoch owns the server. The feeder stops (exit code 5) without confirming anything further to PostgreSQL; it does not fight for the lease.

8.7 Rebuild

Used after a Knotrel restart, a divergence, a TRUNCATE, a lost slot or a lineage mismatch:

  1. stop streaming (the old slot is released);
  2. bootstrap a new slot with an exported snapshot (§5);
  3. import with create_only (generation 0) or replace with the current ETag as If-Match;
  4. drop the feeder’s other slots (§9), stream, barrier.

During a replacement Knotrel keeps serving the previous graph until the new one is published. If the feeder dies in the middle, the next start either resumes the old lineage (if Knotrel still holds it and its slot still exists) or rebuilds again; an unused new slot is removed by the next cleanup.

8.8 History no longer available

If the slot is invalidated (its WAL exceeded max_slot_wal_keep_size and was removed), PostgreSQL refuses to stream from it. The feeder then declares a gap to Knotrel (POST /v1/feeder/gap, Knotrel becomes desynchronized and answers queries with 503 rather than serve a graph that misses changes) and rebuilds. The same applies when the slot named in Knotrel’s position is missing, or the timeline changed.

8.9 Readiness

After an import, a new epoch or a lease recovery, the feeder declares a barrier at Knotrel’s applied frontier once it is caught up: no transaction in progress, every received event applied, and either

The first condition is reached under continuous writes, where the stream is never idle. Knotrel then becomes ready. After registering a new epoch the feeder expects stale until this barrier and does not treat it as a new lapse.

9. Slots, retained WAL and limits

10. Privileges and credentials

11. Correctness criteria

Each criterion is checked by tests against a real PostgreSQL server (§12).

12. Tests

Integration tests (integrations/postgres-feeder/tests/) start a real knotrel-server binary and the feeder binary as child processes against a PostgreSQL server provided by the environment (a container in CI and in the local script). They connect as an administrator to create the schema, the roles and the publication, and run the feeder with the least-privileged role. Fault points are injected with an environment variable that makes the feeder abort at a named point, or discard a Knotrel answer as if the connection had dropped (compiled in all builds, inert unless set).

Test (tests/) Criteria
initial_load_with_concurrent_writers: four writers before, during and after the bootstrap C1, C2
rolled_back_and_mixed_transactions: aborted transactions, savepoints, one 3000-change transaction in batches of 37 C1, C3
steady_streaming_with_concurrent_writers: six writers with lifecycle changes, cascades, node and edge key updates; deferred foreign keys C1
feeder_crash_at_each_fault_point: crash after slot creation, after the snapshot read, after the import, before a batch, after a batch was applied, after a confirmation, and inside a transaction; then restart C1, C4
disconnections_and_lost_answers: replication connection terminated three times under writes; a Knotrel answer discarded C1, C5
knotrel_restart_rebuilds: Knotrel killed and restarted under writes C1, C6, C8
slot_invalidated_declares_gap_and_rebuilds: 200 MB of WAL with a 64 MB bound while the feeder is stopped C7, C1
truncate_rebuilds C1
foreign_slots_untouched_and_slot_count_bounded, unbounded_retention_is_refused C8
retyped_column_stops_then_recovers_by_rebuild, dropped_column_stops_with_clear_error, startup_rejects_unsupported_schema C9

Comparison method. The tests stop the writers, read pg_current_wal_lsn(), wait until the feeder has confirmed at least that position and Knotrel is ready, then read the full registered topology from Knotrel’s inspection routes (KNOTREL_INSPECTION=true) and the tables from PostgreSQL, and compare them. Because no write happens after that LSN, the tables are the projection at the verified position.

The CI job runs these tests on PostgreSQL 15, 16, 17, 18 and 19.

13. Limits