> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgreSQL relation source

> Observe a PostgreSQL table from your app.

This app reads one PostgreSQL table with a primary key of one to eight
`bigint`, `text`, or `varchar` columns. It
publishes the newest observation per key and the next position in the same
commit at the mount's `data` label.

For changes from a dedicated PostgreSQL slot, use [logical replication](https://docs.bijection.com/integrations/native-postgres-logical.md).

The same app has profiles for [MySQL](https://docs.bijection.com/integrations/native-mysql.md),
[MariaDB](https://docs.bijection.com/integrations/native-mariadb.md), [SQL Server](https://docs.bijection.com/integrations/native-sql-server.md),
[Oracle](https://docs.bijection.com/integrations/native-oracle.md), [BigQuery](https://docs.bijection.com/integrations/native-bigquery.md),
[Snowflake](https://docs.bijection.com/integrations/native-snowflake.md), [Redshift](https://docs.bijection.com/integrations/native-redshift.md),
[Databricks SQL](https://docs.bijection.com/integrations/native-databricks-sql.md), [ClickHouse](https://docs.bijection.com/integrations/native-clickhouse.md),
[Exasol](https://docs.bijection.com/integrations/native-exasol.md), [Netezza](https://docs.bijection.com/integrations/native-netezza.md), [SAP ASE](https://docs.bijection.com/integrations/native-sap-ase.md)
and [Vertica](https://docs.bijection.com/integrations/native-vertica.md).

The mount's clock starts a due pass at its next ten-second turn. Each page
progresses through a continuation of the SQL receiver's task.

## Mount and bind

The SQL app is a mountable app: `sql({ data, provider, writer })` returns the
app to place at a path of your tree. Bind the SQL receiver (`SqlReceiver`) with one of the
package's binders, declare its credential with `secret()`, and place all three
in the same tree.

```ts app.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app, label, secret } from "@bijectionhq/bijection";
import sql, { postgresRelation } from "@bijectionhq/app-sql";

const inventory = label();
const login = secret();
const relation = postgresRelation("inventory.example:5432", login, inventory);

export default app({
  inventory,
  login,
  relation,
  sql: sql({ data: inventory, provider: relation }),
});
```

Every relation and writer binder takes the endpoint, the credential and the
label as its last, optional argument. The mount's records, positions and task
results live under `data`, and the receiver answers under the same label. The
host reserves clock and send capacity from the label's declared quotas.

The mount declares its `operator` role, which clears `data`. The operator runs
the mount's clocks and is the only role its functions allow. Set the
credential, grant the role and start the source:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection secret set login --from-file login.json
bijection mutate sql.source.initialize '{}'
```

`sql.operator` is granted from code: `ctx.grant(sql.operator, { to, under })` in one of your
mutations ([Grants](https://docs.bijection.com/access/grants.md)).

Use a read-only account limited to the chosen table. Pass the revision from
`sql.source.health` to `poll`, `pause` and `resume`.

```json theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
{
  "username": "inventory_reader",
  "password": "<database password>",
  "database": "inventory",
  "schema": "publishing",
  "table": "items",
  "key": ["account", "id"]
}
```

The driver requires TLS and checks the endpoint against platform trust. Use a
UTF-8 database and list the complete primary key in declaration order. Pages
compare the full tuple, using numeric order for `bigint` and byte order for
text. Record keys are JSON arrays of strings, such as `["EMEA","9007199254740993"]`,
and may use up to 512 encoded bytes. Payloads are PostgreSQL row JSON text.

Each bounded read checks relation and key identity in its read-only snapshot.
A page carries up to 32 rows and uses at most 48 KiB after encoding the result
for the task result. The app can request 128 pages per polling turn and resumes
after the last published tuple when it yields. A single row that cannot fit
blocks progress at that key. A retained relation identity change stops the
source and requires an explicit reset at the health revision and gap instant.

Each page observes its own PostgreSQL snapshot. Concurrent writes can affect
which keys a traversal sees. Later traversals refresh observed keys and keep
keys that disappear.

## Read consistency by mode

Every read mode retains its receiver reply and publishes records and positions
at the mount's `data` label. Each accepted page advances the position in
the same commit as its changes.

| Mode | What a published position covers |
| - | - |
| PostgreSQL, MySQL, MariaDB and SQL Server observations | One read-only page snapshot, ordered by the configured key. A traversal can span concurrent writes. Keys absent from observations remain in the mount. |
| Oracle Net, Oracle ORDS, BigQuery, Snowflake and Databricks observations | One provider statement view. Continuations use the key from the last page, and a traversal can span concurrent writes. Missing keys remain in the mount. |
| ClickHouse and Exasol observations | One local table statement view ordered by the complete installer-maintained key. Missing keys remain in the mount. |
| Netezza, Redshift, SAP ASE and Vertica observations | One read-only transaction over the configured relation. The installer maintains key uniqueness. Missing keys remain in the mount. |
| PostgreSQL logical replication and MySQL binlog | Complete provider transactions after the installation head, including reported deletes. An unavailable cursor stops advancement. |
| SQL Server change tracking | An initial snapshot, then the latest state of keys changed through one tracking version, including deletes. Each baseline or tracked window must fit one task result. |
| SQL Server CDC | One complete captured source commit's net effect by key after the installation head. The retained LSN and capture identity detect lost history. |
| Oracle LogMiner | The net keyed effects of one complete archived commit SCN, including deletes. The retained SCN and restart sample bind continuity to the source incarnation and archive coverage. |
| Snowflake stream changes | One bounded net-change window from the stream offset or prior Time Travel marker. The window can span multiple source transactions. The mount publishes end-state images and deletes together. |

## Parameterized SQL writes

Pass a writer receiver as `writer`, bound at the same `data` label. Without
`writer` the mount has no `writes` subtree. The operator calls
`sql.writes.dml` and `sql.writes.get`. The writer uses the installed dialect
and a sealed credential with a declared relation, ordered key and writable
projection.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import sql, { postgresRelation, postgresWriter } from "@bijectionhq/app-sql";

const writeLogin = secret();
const writer = postgresWriter("inventory.example:5432", writeLogin, inventory);
// in the tree: { writeLogin, writer, sql: sql({ data: inventory, provider: relation, writer }) }
```

| Dialect | Writer binder | Relation path |
| - | - | - |
| PostgreSQL | `postgresWriter(endpoint, credential, label)` | Schema and table |
| MySQL | `mysqlWriter(endpoint, credential, label)` | Database and table |
| MariaDB | `mariadbWriter(endpoint, credential, label)` | Database and table |
| SQL Server | `sqlServerWriter(endpoint, credential, label)` | Schema and table |
| Redshift | `redshiftWriter(endpoint, credential, label)` | Schema and table |
| Snowflake | `snowflakeWriter(origin, credential, label)` | Database, schema and table |
| BigQuery | `bigQueryWriter(credential, label)` | Project, dataset and table |

Socket writer credentials take `username`, `password`, `database` and
`relation`. Warehouse writer credentials take `token`, `relation` and
`settings`. Use the settings from the matching reader guide. Authorize the
account for the declared table, its metadata, required locks and DML.
Use a principal restricted to the intended table and operation permissions.

```json theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
{
  "path": ["publishing", "items"],
  "key": [{"name": "warehouse", "kind": "text"}, {"name": "id", "kind": "bigint"}],
  "columns": [{"name": "description", "kind": "text"}]
}
```

The relation accepts one to eight key columns and up to sixteen distinct
writable columns. Supported kinds are `text` and `bigint`. Supply the key
tuple in declaration order, using the same ordered key coordinates as the
PostgreSQL read profile. Bigints are canonical signed decimal strings.
Text preserves its UTF-8 contents, and key equality follows the
declared provider column’s comparison semantics. The encoded key is
bounded to 512 bytes, and supplied non-key values total at most 16 KiB.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
{
  key: "erp-4912",
  command: { operation: "update", key: ["eu-west", "879"], values: { description: "in stock" } },
}
```

Insert supplies the entire key and any projected values. Omitted insert
fields use provider defaults. Update must name at least one projected
field. Delete supplies an empty `values` object. Explicit `null` means SQL
NULL for a projected column. Use a new command key for a different request:
the same key with other inputs refuses `KeyReused`, and an invalid command
refuses `InvalidCommand`. The app submits bound values and fixed DML once per
command key, with one send per task.

PostgreSQL, MySQL, MariaDB and SQL Server require the complete provider
primary key. MySQL and MariaDB require InnoDB. Redshift serializes the
guarded statement under a table lock. BigQuery and Snowflake check the
declared key in the statement snapshot. Coordinate independent inserts
at those warehouses if uniqueness must hold across concurrent writers.

`writes.get({ key })` reports `committed` with the provider’s direct
affected-row count, 0 or 1. A refused task is reported as `refused` with
the refusal value in `refusal`. Socket writers require a COMMIT acknowledgement.
The warehouse writers require a completed statement or job and its DML
count. A zero count can reflect a missing key, a guard refusing a duplicate,
or the provider’s changed-row semantics. Triggers, cascades, later writes
and replica visibility have their own outcomes. A lost or ambiguous
acknowledgement settles the task `Unknown`. Writes leave the read cursor intact.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.