Skip to content

Commit

Permalink
Postgres components: Adds queryExecMode option (#3103)
Browse files Browse the repository at this point in the history
Signed-off-by: Bernd Verst <github@bernd.dev>
  • Loading branch information
berndverst committed Aug 25, 2023
1 parent 247b08a commit 81539d0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
13 changes: 13 additions & 0 deletions bindings/postgres/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,16 @@ metadata:
sensitive: true
example: |
"user=dapr password=secret host=dapr.example.com port=5432 dbname=dapr sslmode=verify-ca"
- name: queryExecMode
required: false
description: |
Controls the default mode for executing queries. By default Dapr uses the extended protocol and automatically prepares and caches prepared statements.
However, this may be incompatible with proxies such as PGBouncer. In this case it may be preferrable to use `exec` or `simple_protocol`.
allowedValues:
- "cache_statement"
- "cache_describe"
- "describe_exec"
- "exec"
- "simple_protocol"
example: "cache_describe"
default: ""
15 changes: 14 additions & 1 deletion configuration/postgres/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,17 @@ metadata:
description: |
Deprecated alias for 'connectionMaxIdleTime'.
example: "5m"
type: duration
type: duration
- name: queryExecMode
required: false
description: |
Controls the default mode for executing queries. By default Dapr uses the extended protocol and automatically prepares and caches prepared statements.
However, this may be incompatible with proxies such as PGBouncer. In this case it may be preferrable to use `exec` or `simple_protocol`.
allowedValues:
- "cache_statement"
- "cache_describe"
- "describe_exec"
- "exec"
- "simple_protocol"
example: "cache_describe"
default: ""
17 changes: 17 additions & 0 deletions internal/authentication/postgresql/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PostgresAuthMetadata struct {
ConnectionMaxIdleTime time.Duration `mapstructure:"connectionMaxIdleTime"`
MaxConns int `mapstructure:"maxConns"`
UseAzureAD bool `mapstructure:"useAzureAD"`
QueryExecMode string `mapstructure:"queryExecMode"`

azureEnv azure.EnvironmentSettings
}
Expand All @@ -42,6 +43,7 @@ func (m *PostgresAuthMetadata) Reset() {
m.ConnectionMaxIdleTime = 0
m.MaxConns = 0
m.UseAzureAD = false
m.QueryExecMode = ""
}

// InitWithMetadata inits the object with metadata from the user.
Expand Down Expand Up @@ -81,6 +83,21 @@ func (m *PostgresAuthMetadata) GetPgxPoolConfig() (*pgxpool.Config, error) {
config.MaxConns = int32(m.MaxConns)
}

if m.QueryExecMode != "" {
queryExecModes := map[string]pgx.QueryExecMode{
"cache_statement": pgx.QueryExecModeCacheStatement,
"cache_describe": pgx.QueryExecModeCacheDescribe,
"describe_exec": pgx.QueryExecModeDescribeExec,
"exec": pgx.QueryExecModeExec,
"simple_protocol": pgx.QueryExecModeSimpleProtocol,
}
if mode, ok := queryExecModes[m.QueryExecMode]; ok {
config.ConnConfig.DefaultQueryExecMode = mode
} else {
return nil, fmt.Errorf("invalid queryExecMode metadata value: %s", m.QueryExecMode)
}
}

// Check if we should use Azure AD
if m.UseAzureAD {
tokenCred, errToken := m.azureEnv.GetTokenCredential()
Expand Down
13 changes: 13 additions & 0 deletions state/postgresql/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,16 @@ metadata:
database driver to choose.
example: "5m"
type: duration
- name: queryExecMode
required: false
description: |
Controls the default mode for executing queries. By default Dapr uses the extended protocol and automatically prepares and caches prepared statements.
However, this may be incompatible with proxies such as PGBouncer. In this case it may be preferrable to use `exec` or `simple_protocol`.
allowedValues:
- "cache_statement"
- "cache_describe"
- "describe_exec"
- "exec"
- "simple_protocol"
example: "cache_describe"
default: ""

0 comments on commit 81539d0

Please sign in to comment.