Skip to content
Open
11 changes: 10 additions & 1 deletion .schema/pgdog.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,15 @@
"description": "Database settings configure which databases PgDog is managing. This is a TOML list of hosts, ports, and other settings like database roles (primary or replica).\n\n<https://docs.pgdog.dev/configuration/pgdog.toml/databases/>",
"type": "object",
"properties": {
"client_idle_timeout": {
"description": "Overrides the `client_idle_timeout` setting for this logical database. Client connections to this database that haven't sent any queries for this long will be disconnected.\n\nAll shards and replicas with the same `name` share one frontend timeout. The first configured non-`None` value is used, and conflicting values produce a warning. Set to `0` to exempt clients of this database from the client idle timeout entirely, e.g. for `LISTEN`/`NOTIFY` subscribers that are expected to stay quiet for long periods.\n\n<https://docs.pgdog.dev/configuration/pgdog.toml/databases/#client_idle_timeout>",
"type": [
"integer",
"null"
],
"format": "uint64",
"minimum": 0
},
"database_name": {
"description": "Name of the PostgreSQL database on the server PgDog will connect to. If not set, this defaults to `name`.\n\n<https://docs.pgdog.dev/configuration/pgdog.toml/databases/#database_name>",
"type": [
Expand Down Expand Up @@ -721,7 +730,7 @@
"minimum": 0
},
"client_idle_timeout": {
"description": "Close client connections that have been idle, i.e., haven't sent any queries, for this amount of time.\n\n<https://docs.pgdog.dev/configuration/pgdog.toml/general/#client_idle_timeout>",
"description": "Close client connections that have been idle, i.e., haven't sent any queries, for this amount of time.\n\n**Note:** Set to `0` to disable the client idle timeout entirely. Can be overridden per-user or per-database.\n\n<https://docs.pgdog.dev/configuration/pgdog.toml/general/#client_idle_timeout>",
"type": "integer",
"format": "uint64",
"default": 9223372036854775807,
Expand Down
9 changes: 9 additions & 0 deletions .schema/users.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@
"type": "boolean",
"default": false
},
"client_idle_timeout": {
"description": "Overrides [`client_idle_timeout`](https://docs.pgdog.dev/configuration/pgdog.toml/general/#client_idle_timeout) for this user. Client connections that haven't sent any queries for this long will be disconnected.\n\n**Note:** Set to `0` to exempt this user from the client idle timeout entirely, e.g. for `LISTEN`/`NOTIFY` subscribers that are expected to stay quiet for long periods. If multiple entries for this user match through `database`, `databases`, or `all_databases`, the last matching entry that configures this setting is used; entries without an override do not erase an earlier one.\n\n<https://docs.pgdog.dev/configuration/users.toml/users/#client_idle_timeout>",
"type": [
"integer",
"null"
],
"format": "uint64",
"minimum": 0
},
"cross_shard_disabled": {
"description": "Disable cross-shard queries for this user.",
"type": [
Expand Down
12 changes: 12 additions & 0 deletions docs/CLIENT_CONNECTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ flowchart TD

`self.buffer(client_state)` reads bytes from the client socket into a `ClientRequest` ([`frontend/client_request.rs`](../pgdog/src/frontend/client_request.rs)). A request is complete (`ClientRequest::is_complete()`) when the last message code is one of `{H, S, Q, c, f, F}`, or when a `CopyData` chunk reaches 4 KB. `'X'` (Terminate) triggers a graceful disconnect.

### Client idle timeout

At the start of each `buffer()` invocation, the client checks the cached `client_idle_timeout` for its authenticated startup user and logical database. Resolution precedence is:

1. The last matching `[[users]]` entry that configures `client_idle_timeout`, including matches through `databases` or `all_databases`. A later matching entry without the setting does not erase an earlier override.
2. The first configured non-`None` value among `[[databases]]` entries with the logical database name. Shards and replicas share this frontend policy, and conflicting values produce a configuration warning.
3. `[general].client_idle_timeout`.

A value of `0` at the selected level disables the timeout for that client. This can exempt intentionally quiet sessions such as `LISTEN`/`NOTIFY` subscribers without disabling idle-client protection globally. Authenticated admin sessions always use the general timeout because the admin database is virtual and has no user or backend database configuration.

The resolved timeout is cached with a weak identity handle to the configuration snapshot. A reload is applied on the next `buffer()` invocation; a socket read already waiting when the reload occurs keeps its current deadline. The full configuration snapshot is released before awaiting the frontend socket, so an indefinitely idle client does not retain obsolete configuration data.

### Maintenance mode

Before dispatching, `client_messages()` checks `maintenance_mode::waiter(&database)` ([`backend/maintenance_mode.rs`](../pgdog/src/backend/maintenance_mode.rs)). If a waiter is active and the client is not in a transaction, the client parks until `maintenance_mode::stop()` fires.
Expand Down
7 changes: 7 additions & 0 deletions example.pgdog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ idle_timeout = 60_000
# Client idle timeout. How long to wait for clients to send another transaction
# before disconnecting them.
#
# Set to 0 to disable. This can also be overridden for one logical database
# or user in the corresponding configuration entry.
#
# Default: unlimited
client_idle_timeout = 60_000
# Size of the mirror queue. Queries that don't fit are dropped.
Expand Down Expand Up @@ -340,6 +343,10 @@ port = 5432
# - replica
#
role = "primary"
# Optional client idle timeout for this logical database. All entries with the
# same name (shards and replicas) share the first configured value. Set to 0 to
# disable the timeout, for example for intentionally quiet LISTEN clients.
# client_idle_timeout = 0

#
# Add a replica and automatically load balance queries.
Expand Down
3 changes: 3 additions & 0 deletions example.users.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
name = "pgdog"
database = "pgdog"
password = "pgdog"
# Optional client idle timeout for this user. Set to 0 to exempt intentionally
# quiet sessions such as LISTEN/NOTIFY subscribers.
# client_idle_timeout = 0

[[users]]
name = "pgdog"
Expand Down
Loading