Skip to content

Commit 0d9b5a2

Browse files
authored
Update docs for PostgreSQL state store (#3033)
Update the component due to changes introduced in dapr/components-contrib#2302 and dapr/components-contrib#2379 Fixes #2966 Signed-off-by: ItalyPaleAle <[email protected]> Signed-off-by: ItalyPaleAle <[email protected]>
1 parent de0bdd8 commit 0d9b5a2

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

daprdocs/content/en/reference/components-reference/supported-state-stores/setup-postgresql.md

+55-16
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ aliases:
77
- "/operations/components/setup-state-store/supported-state-stores/setup-postgresql/"
88
---
99

10+
This component allows using PostgreSQL (Postgres) as state store for Dapr.
11+
1012
## Create a Dapr component
1113

12-
Create a file called `postgres.yaml`, paste the following and replace the `<CONNECTION STRING>` value with your connection string. The connection string is a standard PostgreSQL connection string. For example, `"host=localhost user=postgres password=example port=5432 connect_timeout=10 database=dapr_test"`. See the PostgreSQL [documentation on database connections](https://www.postgresql.org/docs/current/libpq-connect.html), specifically Keyword/Value Connection Strings, for information on how to define a connection string.
14+
Create a file called `postgres.yaml`, paste the following and replace the `<CONNECTION STRING>` value with your connection string. The connection string is a standard PostgreSQL connection string. For example, `"host=localhost user=postgres password=example port=5432 connect_timeout=10 database=dapr_test"`. See the PostgreSQL [documentation on database connections](https://www.postgresql.org/docs/current/libpq-connect.html) for information on how to define a connection string.
1315

14-
If you want to also configure PostgreSQL to store actors, add the `actorStateStore` configuration element shown below.
16+
If you want to also configure PostgreSQL to store actors, add the `actorStateStore` option as in the example below.
1517

1618
```yaml
1719
apiVersion: dapr.io/v1alpha1
@@ -22,30 +24,45 @@ spec:
2224
type: state.postgresql
2325
version: v1
2426
metadata:
27+
# Connection string
2528
- name: connectionString
2629
value: "<CONNECTION STRING>"
30+
# Timeout for database operations, in seconds (optional)
31+
#- name: timeoutInSeconds
32+
# value: 20
33+
# Name of the table where to store the state (optional)
34+
#- name: tableName
35+
# value: "state"
36+
# Name of the table where to store metadata used by Dapr (optional)
37+
#- name: metadataTableName
38+
# value: "dapr_metadata"
39+
# Cleanup interval in seconds, to remove expired rows (optional)
40+
#- name: cleanupIntervalInSeconds
41+
# value: 3600
42+
# Max idle time for connections before they're closed (optional)
43+
#- name: connectionMaxIdleTime
44+
# value: 0
45+
# Uncomment this if you wish to use PostgreSQL as a state store for actors (optional)
46+
#- name: actorStateStore
47+
# value: "true"
2748
```
2849
{{% alert title="Warning" color="warning" %}}
2950
The above example uses secrets as plain strings. It is recommended to use a secret store for the secrets as described [here]({{< ref component-secrets.md >}}).
3051
{{% /alert %}}
3152

3253
## Spec metadata fields
3354

34-
| Field | Required | Details | Example |
55+
| Field | Required | Details | Example |
3556
|--------------------|:--------:|---------|---------|
36-
| connectionString | Y | The connection string for PostgreSQL | `"host=localhost user=postgres password=example port=5432 connect_timeout=10 database=dapr_test"`
37-
| actorStateStore | N | Consider this state store for actors. Defaults to `"false"` | `"true"`, `"false"`
38-
39-
40-
If you wish to use PostgreSQL as an actor store, append the following to the yaml.
57+
| `connectionString` | Y | The connection string for the PostgreSQL database | `"host=localhost user=postgres password=example port=5432 connect_timeout=10 database=dapr_test"`
58+
| `timeoutInSeconds` | N | Timeout, in seconds, for all database operations. Defaults to `20` | `30`
59+
| `tableName` | N | Name of the table where the data is stored. Defaults to `state`. Can optionally have the schema name as prefix, such as `public.state` | `"state"`, `"public.state"`
60+
| `metadataTableName` | N | Name of the table Dapr uses to store a few metadata properties. Defaults to `dapr_metadata`. Can optionally have the schema name as prefix, such as `public.dapr_metadata` | `"dapr_metadata"`, `"public.dapr_metadata"`
61+
| `cleanupIntervalInSeconds` | N | Interval, in seconds, to clean up rows with an expired TTL. Default: `3600` (i.e. 1 hour). Setting this to values <=0 disables the periodic cleanup. | `1800`, `-1`
62+
| `connectionMaxIdleTime` | N | Max idle time before unused connections are automatically closed in the connection pool. By default, there's no value and this is left to the database driver to choose. | `"5m"`
63+
| `actorStateStore` | N | Consider this state store for actors. Defaults to `"false"` | `"true"`, `"false"`
4164

42-
```yaml
43-
- name: actorStateStore
44-
value: "true"
45-
```
46-
47-
48-
## Create PostgreSQL
65+
## Setup PostgreSQL
4966

5067
{{< tabs "Self-Hosted" >}}
5168

@@ -65,13 +82,35 @@ Either the default "postgres" database can be used, or create a new database for
6582
To create a new database in PostgreSQL, run the following SQL command:
6683

6784
```SQL
68-
create database dapr_test
85+
CREATE DATABASE dapr_test;
6986
```
7087
{{% /codetab %}}
7188

7289
{{% /tabs %}}
7390

91+
## Advanced
92+
93+
### TTLs and cleanups
94+
95+
This state store supports [Time-To-Live (TTL)](https://docs.dapr.io/developing-applications/building-blocks/state-management/state-store-ttl/) for records stored with Dapr. When storing data using Dapr, you can set the `ttlInSeconds` metadata property to indicate after how many seconds the data should be considered "expired".
96+
97+
Because PostgreSQL doesn't have built-in support for TTLs, this is implemented in Dapr by adding a column in the state table indicating when the data is to be considered "expired". Records that are "expired" are not returned to the caller, even if they're still physically stored in the database. A background "garbage collector" periodically scans the state table for expired rows and deletes them.
98+
99+
The interval at which the deletion of expired records happens is set with the `cleanupIntervalInSeconds` metadata property, which defaults to 3600 seconds (that is, 1 hour).
100+
101+
- Longer intervals require less frequent scans for expired rows, but can require storing expired records for longer, potentially requiring more storage space. If you plan to store many records in your state table, with short TTLs, consider setting `cleanupIntervalInSeconds` to a smaller value, for example `300` (300 seconds, or 5 minutes).
102+
- If you do not plan to use TTLs with Dapr and the PostgreSQL state store, you should consider setting `cleanupIntervalInSeconds` to a value <= 0 (e.g. `0` or `-1`) to disable the periodic cleanup and reduce the load on the database.
103+
104+
The column in the state table where the expiration date for records is stored in, `expiredate`, **does not have an index by default**, so each periodic cleanup must perform a full-table scan. If you have a table with a very large number of records, and only some of them use a TTL, you may find it useful to create an index on that column. Assuming that your state table name is `state` (the default), you can use this query:
105+
106+
```sql
107+
CREATE INDEX expiredate_idx
108+
ON state
109+
USING btree (expiredate ASC NULLS LAST);
110+
```
111+
74112
## Related links
113+
75114
- [Basic schema for a Dapr component]({{< ref component-schema >}})
76115
- Read [this guide]({{< ref "howto-get-save-state.md#step-2-save-and-retrieve-a-single-state" >}}) for instructions on configuring state store components
77116
- [State management building block]({{< ref state-management >}})

daprdocs/data/components/state_stores/generic.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
crud: true
140140
transactions: true
141141
etag: true
142-
ttl: false
142+
ttl: true
143143
query: true
144144
- component: Redis
145145
link: setup-redis

0 commit comments

Comments
 (0)