Github   Telegram
No Matches
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
components::Postgres Class Reference

PosgreSQL client component. More...

#include <userver/storages/postgres/component.hpp>

+ Inheritance diagram for components::Postgres:
+ Collaboration diagram for components::Postgres:

Public Member Functions

 Postgres (const ComponentConfig &, const ComponentContext &)
 Component constructor.
 ~Postgres () override
 Component destructor.
storages::postgres::ClusterPtr GetCluster () const
 Cluster accessor for default shard number.
storages::postgres::ClusterPtr GetClusterForShard (size_t shard) const
 Cluster accessor for specific shard number.
size_t GetShardCount () const
 Get total shard count.
storages::postgres::DatabasePtr GetDatabase () const
 Get database object. More...
void ExtendStatistics (utils::statistics::Writer &writer)
 Reports statistics for PostgreSQL driver.
- Public Member Functions inherited from components::LoggableComponentBase
 LoggableComponentBase (const ComponentConfig &, const ComponentContext &)
 LoggableComponentBase (LoggableComponentBase &&)=delete
 LoggableComponentBase (const LoggableComponentBase &)=delete
 ~LoggableComponentBase () override=default
ComponentHealth GetComponentHealth () const override
void OnLoadingCancelled () override
void OnAllComponentsLoaded () override
void OnAllComponentsAreStopping () override

Static Public Member Functions

static yaml_config::Schema GetStaticConfigSchema ()
- Static Public Member Functions inherited from components::LoggableComponentBase
static yaml_config::Schema GetStaticConfigSchema ()

Static Public Attributes

static constexpr auto kDefaultMaxReplicationLag = std::chrono::seconds{60}
static constexpr size_t kDefaultShardNumber = 0
 Default shard number. More...
static constexpr storages::postgres::CommandControl kDefaultCommandControl
 Default command control. More...

Detailed Description

PosgreSQL client component.

Provides access to a PostgreSQL cluster.

Dynamic options:

Static configuration example:

# yaml
dbalias: taxi
blocking_task_processor: task-processor-name
max_replication_lag: 60s
min_pool_size: 4
max_pool_size: 15
max_queue_size: 200
max_statement_metrics: 50

You must specify either dbalias or conn_info. If the component is configured with an alias, it will lookup connection data in secdist.json

You must specify blocking_task_processor as well.

max_replication_lag can be used to tune replication lag limit for replicas. Once the replica lag exceeds this value it will be automatically disabled. Note, however, that client-size lag detection is not precise in nature and can only provide the precision of couple seconds.

Secdist format

A PosgreSQL alias in secdist is described as a JSON array of objects containing a single cluster description. There are two formats of describing a cluster, the first one assigns predefined roles to DSNs, the second one is just a list of DSNs and the Postgres component takes care of discovering the cluster's topology itself.

Predefined roles

In predefined roles format the component requires single-host connection strings.

"shard_number" : 0,
"master": "host=localhost dbname=example",
"sync_slave": "host=localhost dbname=example",
"slaves": [
"host=localhost dbname=example"

The predefined roles format is deprecated and the support will be removed soon.

Automatic discovery

In automatic discovery format the connection strings are any valid PostgreSQL connection strings including multi-host ones with the exception of target_session_attrs which will be ignored.

"shard_number" : 0,
"hosts": [
"host=host1,host2,host3 dbname=example",

The shard_number parameter is required in both formats and must match the index of cluster description object in the alias array.

Please see PostgreSQL documentation on connection strings.

Static options:

Name Description Default value
dbalias name of the database in secdist config (if available)
dbconnection connection DSN string (used if no dbalias specified)
blocking_task_processor name of task processor for background blocking operations
max_replication_lag replication lag limit for usable slaves 60s
min_pool_size number of connections created initially 4
max_pool_size limit of connections count 15
sync-start perform initial connections synchronously false
dns_resolver server hostname resolver type (getaddrinfo or async) 'async'
persistent-prepared-statements cache prepared statements or not true
user-types-enabled disabling will disallow use of user-defined types true
ignore_unused_query_params disable check for not-NULL query params that are not used in query false
monitoring-dbalias name of the database for monitorings calculated from dbalias or dbconnection options
max_prepared_cache_size prepared statements cache size limit 5000
max_statement_metrics limit of exported metrics for named statements 0
min_pool_size number of connections created initially 4
max_pool_size maximum number of created connections 15
max_queue_size maximum number of clients waiting for a connection 200
connecting_limit limit for concurrent establishing connections number per pool (0 - unlimited) 0
connlimit_mode max_connections setup mode (manual or auto) auto
error-injection artificial error injection settings, error_injection::Settings

Definition at line 138 of file component.hpp.

Member Function Documentation

◆ GetDatabase()

storages::postgres::DatabasePtr components::Postgres::GetDatabase ( ) const

Get database object.

Definition at line 164 of file component.hpp.

Member Data Documentation

◆ kDefaultCommandControl

constexpr storages::postgres::CommandControl components::Postgres::kDefaultCommandControl
Initial value:

Default command control.

Definition at line 144 of file component.hpp.

◆ kDefaultMaxReplicationLag

constexpr auto components::Postgres::kDefaultMaxReplicationLag = std::chrono::seconds{60}

Definition at line 140 of file component.hpp.

◆ kDefaultShardNumber

constexpr size_t components::Postgres::kDefaultShardNumber = 0

Default shard number.

Definition at line 142 of file component.hpp.

The documentation for this class was generated from the following file: