|
| 1 | +--- |
| 2 | +icon: material/traffic-light |
| 3 | +--- |
| 4 | + |
| 5 | +# Quality of service |
| 6 | + |
| 7 | +Postgres lacks the ability to filter or prioritize queries. Any client can run as many queries as they want, potentially blocking others from using the database. |
| 8 | + |
| 9 | +PgDog implements traffic filtering and prioritization at the proxy. By tagging and assigning queries a budget, it can automatically regulate how much of database resources are available to each query, blocking or throttling queries that exceed their allocation. |
| 10 | + |
| 11 | +## How it works |
| 12 | + |
| 13 | +To make QoS work, tag your queries with the appropriate unit name using query comments. For example: |
| 14 | + |
| 15 | +```postgresql |
| 16 | +/* pgdog_qos: unit=api.users.read */ |
| 17 | +SELECT * FROM users WHERE email = $1 |
| 18 | +``` |
| 19 | + |
| 20 | +The query comment must appear at the beginning of the query, must start with the `pgdog_qos: ` tag, and supports the following attributes: |
| 21 | + |
| 22 | +| Attribute | Description | Example | |
| 23 | +|-|-|-| |
| 24 | +| `unit` | A unique name identifying the type / grouping the query belongs to. | `unit=api.users.create` | |
| 25 | + |
| 26 | +The `unit` attribute is arbitrary and PgDog supports an unlimited amount of unit names. It can identify a type of query (e.g., reads that fetch user information), an API endpoint in the application (e.g., fetch real-time location of IoT devices), or any other arbitrary classification. |
| 27 | + |
| 28 | +### Configuration |
| 29 | + |
| 30 | +QoS is disabled by default and can be enabled in [`pgdog.toml`](../configuration/pgdog.toml/general.md): |
| 31 | + |
| 32 | +=== "pgdog.toml" |
| 33 | + ```toml |
| 34 | + [qos] |
| 35 | + enabled = true |
| 36 | + max_entries = 10_000 |
| 37 | + ``` |
| 38 | +=== "Helm chart" |
| 39 | + ```yaml |
| 40 | + qos: |
| 41 | + enabled: true |
| 42 | + maxEntries: 10000 |
| 43 | + ``` |
| 44 | + |
| 45 | +| Setting | Description | Example | |
| 46 | +|-|-|-| |
| 47 | +| `enabled` | Enable/disable QoS. | `true` | |
| 48 | +| `max_entries` | Maximum number of units that will be recorded by PgDog. | `10_000` | |
| 49 | + |
| 50 | +Recording more entries only uses more memory and doesn't impact query performance. When an entry exceeds the max, the oldest / least used entry is removed and won't be tracked until a query using that unit is received by PgDog again. |
| 51 | + |
| 52 | +### Admin database |
| 53 | + |
| 54 | +The QoS measurements are viewable in the [admin database](../administration/index.md), by running the `SHOW QOS` command: |
| 55 | + |
| 56 | +=== "Command" |
| 57 | + ``` |
| 58 | + SHOW QOS; |
| 59 | + ``` |
| 60 | +=== "Output" |
| 61 | + ``` |
| 62 | + unit | executed | blocked | failed | total_time | state |
| 63 | + -----------------+----------+---------+--------+------------+------- |
| 64 | + api.users.read | 79 | 0 | 0 | 23.601 | pass |
| 65 | + api.users.write | 15 | 0 | 0 | 4.502 | pass |
| 66 | + ``` |
| 67 | + |
| 68 | +| Column | Description | |
| 69 | +|-|-| |
| 70 | +| `unit` | The unit name extracted from the `pgdog_qos: unit=...` query comment. | |
| 71 | +| `executed` | Total number of queries that completed execution successfully. | |
| 72 | +| `blocked` | Total number of queries blocked before reaching the backend. | |
| 73 | +| `failed` | Total number of queries that failed or were aborted before producing a response. | |
| 74 | +| `total_time` | Cumulative execution time in milliseconds across all queries for this unit. | |
| 75 | +| `state` | Administrative state of the unit: `pass` (queries flow normally) or `blocked` (queries are rejected). | |
| 76 | + |
| 77 | +### Control plane |
| 78 | + |
| 79 | +The QoS measurements are uploaded to the [control plane](control_plane/index.md) on a configurable basis, controlled by the `stats_interval` setting: |
| 80 | + |
| 81 | +=== "pgdog.toml" |
| 82 | + ```toml |
| 83 | + [control] |
| 84 | + endpoint = "https://control-plane-endpoint.cloud.pgdog.dev" |
| 85 | + token = "cff57e5c-7c4f-4ca0-b81c-c8ed22cf873d" |
| 86 | + stats_interval = 5_000 |
| 87 | + ``` |
| 88 | +=== "Helm chart" |
| 89 | + ```yaml |
| 90 | + control: |
| 91 | + endpoint: https://control-plane-endpoint.cloud.pgdog.dev |
| 92 | + token: cff57e5c-7c4f-4ca0-b81c-c8ed22cf873d |
| 93 | + statsInterval: 5000 |
| 94 | + ``` |
0 commit comments