Skip to content

Commit b4e59f5

Browse files
committed
feat(prepared_statements): ttl configuration
1 parent 9087f5c commit b4e59f5

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎docs/administration/config.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ The following configuration parameters are available and their current values:
4444
| `port` [:material-link:](../configuration/pgdog.toml/general.md#port) | `6432` |
4545
| `prepared_statements` [:material-link:](../configuration/pgdog.toml/general.md#prepared_statements) | `extended` |
4646
| `prepared_statements_limit` [:material-link:](../configuration/pgdog.toml/general.md#prepared_statements_limit) | `500` |
47+
| `prepared_statements_ttl` [:material-link:](../configuration/pgdog.toml/general.md#prepared_statements_ttl) | `default` |
48+
| `prepared_statements_ttl_jitter` [:material-link:](../configuration/pgdog.toml/general.md#prepared_statements_ttl_jitter) | `30000` |
4749
| `pub_sub_channel_size` [:material-link:](../configuration/pgdog.toml/general.md#pub_sub_channel_size) | `4098` |
4850
| `query_cache_limit` | `500` |
4951
| `query_log` | `default` |

‎docs/features/connection-pooler/prepared-statements.md‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ This limit is strictly enforced on server connections: if a prepared statement n
4545

4646
Since clients re-use prepared statements, this limit isn't enforced for clients: they can prepare as many statements as they wish (and you have memory for). Each statement keeps a counter of when it's used by a client. If the counter reaches zero, i.e., all clients either closed it explicitly or disconnected, the statement is removed from the global cache.
4747

48+
## Statement TTL
49+
50+
Postgres builds an execution plan when a statement is prepared and keeps it for the lifetime of the statement. Since PgDog
51+
keeps server connections open for a long time, a plan can stay in use long after the data it was built for has changed.
52+
53+
To mitigate this, PgDog can be configured to close statements on server connections after some time. After the statement is closed PgDog will recreate the statement seamlessly when the next client tries to use the same statement again.
54+
55+
To enable TTL, extend `pgdog.toml` with the following [settings](../../configuration/pgdog.toml/general.md#prepared_statements_ttl):
56+
57+
```toml
58+
[general]
59+
prepared_statements_ttl = 300_000
60+
prepared_statements_ttl_jitter = 30_000
61+
```
62+
63+
Jitter setting is helpful to avoid all statements expiring at the same time, which could cause a noticeable spike in server load.
64+
65+
By default the TTL is disabled and statements stay prepared for the lifetime of the server connection.
66+
4867
## Tracking used statements
4968

5069
The number of prepared statements and what they are can be tracked by executing this command on the [admin database](../../administration/index.md):
@@ -56,7 +75,7 @@ The number of prepared statements and what they are can be tracked by executing
5675
```
5776
=== "Output"
5877
```
59-
name | statement | rewrite | used_by | memory_used
78+
name | statement | rewrite | used_by | memory_used
6079
-----------+-------------------------------------------------------+---------+---------+-------------
6180
__pgdog_1 | SELECT abalance FROM pgbench_accounts WHERE aid = $1; | | 4 | 144
6281
(1 row)

0 commit comments

Comments
 (0)