userver: components::Postgres Class Reference
Loading...
Searching...
No Matches
components::Postgres Class Reference

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

Detailed Description

PosgreSQL client component.

Provides access to a PostgreSQL cluster.

Dynamic options:

Static configuration example:

# yaml
postgres-taxi:
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 dbconnection. If the component is configured with an alias, it will lookup connection data in Secdist.

It is a common practice to provide a database connection string via environment variables. To retrieve a value from the environment use dbconnection#env: THE_ENV_VARIABLE_WITH_CONNECTION_STRING as described in yaml_config::YamlConfig.

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.

Note that if the dbalias option is provided and components::Secdist component has update-period other than 0, then new connections are created or gracefully closed as the secdist configuration change to new value.

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",
"postgresql://host1:5432,host2:6432,host3:12000/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 of components::Postgres :

Options inherited from components::ComponentBase :

Examples
postgresql/functional_tests/basic_chaos/postgres_service.cpp, and samples/digest_auth_service/auth_digest.cpp.

Definition at line 123 of file component.hpp.

+ Inheritance 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.
 
void ExtendStatistics (utils::statistics::Writer &writer)
 Reports statistics for PostgreSQL driver.
 
ComponentHealth GetComponentHealth () const override
 
void OnLoadingCancelled () override
 
void OnAllComponentsLoaded () override
 
void OnAllComponentsAreStopping () override
 

Static Public Member Functions

static yaml_config::Schema GetStaticConfigSchema ()
 

Static Public Attributes

static constexpr size_t kDefaultShardNumber = 0
 Default shard number.
 
static constexpr storages::postgres::CommandControl kDefaultCommandControl
 Default command control.
 

Protected Types

using LoggableComponentBase = ComponentBase
 

Member Typedef Documentation

◆ LoggableComponentBase

Member Function Documentation

◆ GetComponentHealth()

ComponentHealth components::ComponentBase::GetComponentHealth ( ) const
inlineoverridevirtualinherited

Override this function to inform the world of the state of your component.

Warning
The function is called concurrently from multiple threads.

Reimplemented from components::RawComponentBase.

Reimplemented in server::handlers::Restart.

Definition at line 35 of file component_base.hpp.

◆ GetDatabase()

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

Get database object.

Definition at line 148 of file component.hpp.

◆ OnAllComponentsAreStopping()

void components::ComponentBase::OnAllComponentsAreStopping ( )
inlineoverridevirtualinherited

Component may use this function to stop doing work before the stop of the components that depend on it.

Base components may override it and make final to do some work before the derived object constructor is called. Don't use it otherwise.

Reimplemented from components::RawComponentBase.

Reimplemented in urabbitmq::ConsumerComponentBase, and components::Server.

Definition at line 58 of file component_base.hpp.

◆ OnAllComponentsLoaded()

void components::ComponentBase::OnAllComponentsLoaded ( )
inlineoverridevirtualinherited

Component may use this function to finalize registration of other components that depend on it (for example, handler components register in server component, and the latter uses OnAllComponentsLoaded() to start processing requests).

Base components may override it and make final to do some work after the derived object constructor is called. Don't use it otherwise.

Reimplemented from components::RawComponentBase.

Reimplemented in urabbitmq::ConsumerComponentBase, components::Server, and server::handlers::Ping.

Definition at line 51 of file component_base.hpp.

◆ OnLoadingCancelled()

void components::ComponentBase::OnLoadingCancelled ( )
inlineoverridevirtualinherited

Called once if the creation of any other component failed. If the current component expects some other component to take any action with the current component, this call is a signal that such action may never happen due to components loading was cancelled. Application components might not want to override it.

Reimplemented from components::RawComponentBase.

Definition at line 42 of file component_base.hpp.

Member Data Documentation

◆ kDefaultCommandControl

constexpr storages::postgres::CommandControl components::Postgres::kDefaultCommandControl
staticconstexpr
Initial value:
{
std::chrono::milliseconds{500},
std::chrono::milliseconds{250}
}

Default command control.

Definition at line 128 of file component.hpp.

◆ kDefaultShardNumber

constexpr size_t components::Postgres::kDefaultShardNumber = 0
staticconstexpr

Default shard number.

Definition at line 126 of file component.hpp.


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