# Neon

> Connect AI agents to a hosted Neon Postgres database through Osevel and its pooler, with every change recorded and undoable.

Source: https://osevel.com/docs/guides/neon/

## Status

- **Tested against hosted Neon** (Postgres 18.7, eu-central-1) by `e2e/hosted/neon.test.ts`:
  - `init` and `verify`;
  - an agent write through the executor over the pooler, with its journal entry and rows;
  - no session state from the pooler reaches an agent transaction;
  - `undo`;
  - the agent registry read by the dashboard role.
- Everything runs in a throwaway schema with a random role prefix, and is uninstalled at the end.
- Postgres 18 is in the container matrix (15–18 are tested).

```sh
AR_HOSTED_NEON_URL='postgresql://neondb_owner:…@ep-….neon.tech/neondb?sslmode=require' \
  pnpm vitest run --project e2e e2e/hosted/neon.test.ts
```

Use a **throwaway branch or project**. The test passes with the direct or the `-pooler`
connection string: `init` records the direct endpoint either way.

## The pooler (`-pooler`)

Neon's pooler is PgBouncer in transaction mode: session state (`SET`, `LISTEN`, session advisory
locks) does not survive between transactions. osevel therefore splits the connections:

- **The agent** (many short connections) goes through the pooler. `init` sets this up by itself
  for a Neon host (`agentHost` in the project config).
- **Everything else** (install, journal, approvals, undo with its advisory lock, the bridge,
  the auditor) uses the direct endpoint.

On a pooled connection the reset an agent connection gets on release (`DISCARD ALL`) can reach
another server connection, so every agent transaction starts by clearing what earlier agent SQL
could have left on its own (`RESET ALL`, `pg_advisory_unlock_all()`). The hosted test leaves
session state from another client on the pooler and checks that the agent never sees it (it does
without this reset).

```sh
osevel projects pooler <project> on    # Neon: the agent via -pooler
osevel projects pooler <project> off   # the agent connects directly again
osevel projects pooler <project> on --host <pooler-host>   # another transaction pooler
```

## What is different from plain Postgres

| Question                  | Observed on Neon                                                                                                                                                       | What osevel does                                                                                                                                                                                                                                       |
| ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Is the owner a superuser? | No. `neondb_owner` has `CREATEROLE`, `CREATEDB`, `BYPASSRLS` and `REPLICATION`, and is a member of `neon_superuser`.                                                   | Same path as Supabase's `postgres`: creates the roles, hands the journal to `<prefix>_owner`, and takes membership in it.                                                                                                                              |
| Event triggers?           | Allowed for `neon_superuser` members, including `ENABLE ALWAYS`.                                                                                                       | Installed as on plain Postgres; no `no_event_trigger` fallback.                                                                                                                                                                                        |
| Role passwords            | The control plane refuses SCRAM verifiers, and only at `COMMIT` ("Neon only supports being given plaintext passwords"). By then the whole install transaction is lost. | Detected up front (the `neon_superuser` role exists). The generated passwords are then sent as plaintext over the TLS connection, for `init` and for the password rotation of daemon adoption. Everywhere else, only SCRAM verifiers reach the server. |
| TLS                       | Public CA chain.                                                                                                                                                       | `--tls verify-full` works with the system CAs. `sslmode` and `channel_binding` in the URL are ignored; use `--tls`.                                                                                                                                    |

## What this means for you

With the plaintext path, the role passwords reach Neon's servers (as they would with any
`ALTER ROLE … PASSWORD 'x'`). Neon handles them like the passwords you set in its console.
They are random, 43 characters long, and stored on your machine in the OS keychain, as
everywhere else.
