You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This component allows using PostgreSQL (Postgres) as state store for Dapr.
11
+
10
12
## Create a Dapr component
11
13
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.
13
15
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.
15
17
16
18
```yaml
17
19
apiVersion: dapr.io/v1alpha1
@@ -22,30 +24,45 @@ spec:
22
24
type: state.postgresql
23
25
version: v1
24
26
metadata:
27
+
# Connection string
25
28
- name: connectionString
26
29
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"
27
48
```
28
49
{{% alert title="Warning" color="warning" %}}
29
50
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 >}}).
| 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"`
41
64
42
-
```yaml
43
-
- name: actorStateStore
44
-
value: "true"
45
-
```
46
-
47
-
48
-
## Create PostgreSQL
65
+
## Setup PostgreSQL
49
66
50
67
{{< tabs "Self-Hosted" >}}
51
68
@@ -65,13 +82,35 @@ Either the default "postgres" database can be used, or create a new database for
65
82
To create a new database in PostgreSQL, run the following SQL command:
66
83
67
84
```SQL
68
-
create database dapr_test
85
+
CREATE DATABASE dapr_test;
69
86
```
70
87
{{% /codetab %}}
71
88
72
89
{{% /tabs %}}
73
90
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 forexpired rows, but can require storing expired records for longer, potentially requiring more storage space. If you plan to store many recordsin 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
+
74
112
## Related links
113
+
75
114
- [Basic schema for a Dapr component]({{< ref component-schema >}})
76
115
- Read [this guide]({{< ref "howto-get-save-state.md#step-2-save-and-retrieve-a-single-state">}}) for instructions on configuring state store components
77
116
- [State management building block]({{< ref state-management >}})
0 commit comments