userver: userver/storages/postgres/options.hpp Source File
Loading...
Searching...
No Matches
options.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/postgres/options.hpp
4/// @brief Options
5
6#include <chrono>
7#include <cstdint>
8#include <iosfwd>
9#include <optional>
10#include <string>
11#include <unordered_map>
12#include <unordered_set>
13
14#include <userver/congestion_control/controllers/linear.hpp>
15#include <userver/storages/postgres/postgres_fwd.hpp>
16#include <userver/utils/impl/transparent_hash.hpp>
17#include <userver/utils/str_icase.hpp>
18#include <userver/utils/string_literal.hpp>
19
20USERVER_NAMESPACE_BEGIN
21
22namespace storages::postgres {
23
24/*! [Isolation levels] */
25/// @brief SQL transaction isolation level
26/// @see https://www.postgresql.org/docs/current/static/sql-set-transaction.html
28 kReadCommitted, //!< READ COMMITTED
29 kRepeatableRead, //!< REPEATABLE READ
30 kSerializable, //!< SERIALIZABLE
31 kReadUncommitted //!< READ UNCOMMITTED @warning In Postgres READ UNCOMMITTED
32 //!< is treated as READ COMMITTED
33};
34/*! [Isolation levels] */
35
36std::string_view ToStringView(IsolationLevel lvl);
37
38/// @brief PostgreSQL transaction options
39///
40/// A transaction can be started using all isolation levels and modes
41/// supported by PostgreSQL server as specified in it's documentation.
42///
43/// Default isolation level is READ COMMITTED, default mode is READ WRITE.
44/// @code
45/// // Read-write read committed transaction.
46/// TransactionOptions opts;
47/// @endcode
48///
49/// Transaction class provides constants Transaction::RW, Transaction::RO and
50/// Transaction::Deferrable for convenience.
51///
52/// Other variants can be created with TransactionOptions constructors
53/// that are constexpr.
54///
55/// @see https://www.postgresql.org/docs/current/static/sql-set-transaction.html
57 /*! [Transaction modes] */
58 enum Mode : std::uint16_t {
59 kReadWrite = 0,
60 kReadOnly = 1,
61 kDeferrable = 3 //!< Deferrable transaction is read only
62 };
63 /*! [Transaction modes] */
65 Mode mode = kReadWrite;
66
67 constexpr TransactionOptions() = default;
68 constexpr explicit TransactionOptions(IsolationLevel lvl)
69 : isolation_level{lvl}
70 {}
71 constexpr TransactionOptions(IsolationLevel lvl, Mode m)
72 : isolation_level{lvl},
73 mode{m}
74 {}
75 constexpr explicit TransactionOptions(Mode m)
76 : mode{m}
77 {}
78
79 bool IsReadOnly() const { return mode & kReadOnly; }
80
81 /// The deferrable property has effect only if the transaction is also
82 /// serializable and read only
84};
85
86constexpr inline bool operator==(TransactionOptions lhs, TransactionOptions rhs) {
87 return lhs.isolation_level == rhs.isolation_level && lhs.mode == rhs.mode;
88}
89USERVER_NAMESPACE::utils::StringLiteral BeginStatement(TransactionOptions opts) noexcept;
90
91/// A structure to control timeouts for PosrgreSQL queries
92///
93/// There are two parameters, `execute` and `statement`.
94///
95/// `execute` parameter controls the overall time the driver spends executing a
96/// query, that includes:
97/// * connecting to PostgreSQL server, if there are no connections available and
98/// connection pool still has space for new connections;
99/// * waiting for a connection to become idle if there are no idle connections
100/// and connection pool already has reached it's max size;
101/// * preparing a statement if the statement is run for the first time on the
102/// connection;
103/// * binding parameters and executing the statement;
104/// * waiting for the first results to arrive from the server. If the result set
105/// is big, only time to the first data packet is taken into account.
106///
107/// `statement` is rather straightforward, it's the PostgreSQL server-side
108/// parameter, and it controls the time the database backend can spend executing
109/// a single statement. It is very costly to change the statement timeout
110/// often, as it requires a roundtrip to the database to change the setting.
111/// @see https://www.postgresql.org/docs/12/runtime-config-client.html
112///
113/// `execute` timeout should always be greater than the `statement` timeout!
114///
115/// In case of a timeout, either back-end or overall, the client gets an
116/// exception and the driver tries to clean up the connection for further reuse.
118 /// Overall timeout for a command being executed
119 TimeoutDuration network_timeout_ms{};
120 /// PostgreSQL server-side timeout
121 TimeoutDuration statement_timeout_ms{};
122
123 enum class PreparedStatementsOptionOverride { kNoOverride, kEnabled, kDisabled };
124
125 PreparedStatementsOptionOverride prepared_statements_enabled{PreparedStatementsOptionOverride::kNoOverride};
126
127 constexpr CommandControl(
128 TimeoutDuration network_timeout_ms,
129 TimeoutDuration statement_timeout_ms,
130 PreparedStatementsOptionOverride prepared_statements_enabled = PreparedStatementsOptionOverride::kNoOverride
131 )
132 : network_timeout_ms(network_timeout_ms),
133 statement_timeout_ms(statement_timeout_ms),
134 prepared_statements_enabled(prepared_statements_enabled)
135 {}
136
137 constexpr CommandControl WithExecuteTimeout(TimeoutDuration n) const noexcept { return {n, statement_timeout_ms}; }
138
139 constexpr CommandControl WithStatementTimeout(TimeoutDuration s) const noexcept { return {network_timeout_ms, s}; }
140
141 bool operator==(const CommandControl& rhs) const {
143 prepared_statements_enabled == rhs.prepared_statements_enabled;
144 }
145};
146
147/// @brief storages::postgres::CommandControl that may not be set
148using OptionalCommandControl = std::optional<CommandControl>;
149
150using CommandControlByMethodMap = USERVER_NAMESPACE::utils::impl::TransparentMap<std::string, CommandControl>;
151using CommandControlByHandlerMap =
152 USERVER_NAMESPACE::utils::impl::TransparentMap<std::string, CommandControlByMethodMap>;
153using CommandControlByQueryMap = USERVER_NAMESPACE::utils::impl::TransparentMap<std::string, CommandControl>;
154
155OptionalCommandControl GetHandlerOptionalCommandControl(
156 const CommandControlByHandlerMap& map,
157 std::string_view path,
158 std::string_view method
159);
160
161OptionalCommandControl GetQueryOptionalCommandControl(const CommandControlByQueryMap& map, std::string_view query_name);
162
163/// Default initial pool connection count
164inline constexpr std::size_t kDefaultPoolMinSize = 4;
165
166/// Default maximum replication lag
167inline constexpr auto kDefaultMaxReplicationLag = std::chrono::seconds{60};
168
169/// Default pool connections limit
170inline constexpr std::size_t kDefaultPoolMaxSize = 15;
171
172/// Default size of queue for clients waiting for connections
173inline constexpr std::size_t kDefaultPoolMaxQueueSize = 200;
174
175/// Default limit for concurrent establishing connections number
176inline constexpr std::size_t kDefaultConnectingLimit = 0;
177
178/// Default minimum time between starting new connections per host in milliseconds
179inline constexpr std::size_t kDefaultConnectingIntervalMs = 0;
180
181/// @brief PostgreSQL topology options
182///
183/// Dynamic option @ref POSTGRES_TOPOLOGY_SETTINGS
185 /// Maximum replication lag. Once the replica lag exceeds this value it will be automatically disabled.
187
188 /// List of manually disabled replicas (FQDNs).
189 std::unordered_set<std::string, USERVER_NAMESPACE::utils::StrIcaseHash, USERVER_NAMESPACE::utils::StrIcaseEqual>
191};
192
193/// @brief PostgreSQL connection pool options
194///
195/// Dynamic option @ref POSTGRES_CONNECTION_POOL_SETTINGS
196struct PoolSettings final {
197 /// Number of connections created initially
199
200 /// Maximum number of created connections
202
203 /// Maximum number of clients waiting for a connection
205
206 /// Limits number of concurrent establishing connections (0 - unlimited)
208
209 /// Minimum time in milliseconds between starting new connections to each host (0 - unlimited)
211
212 bool operator==(const PoolSettings& rhs) const {
213 return min_size == rhs.min_size && max_size == rhs.max_size && max_queue_size == rhs.max_queue_size &&
215 }
216};
217
218// Configs with a suffix `Dynamic` are need to compatibility with static:
219// We must update only fields that were updated in a dynamic config (not a full config!).
220struct PoolSettingsDynamic final {
221 std::optional<std::size_t> min_size;
222 std::optional<std::size_t> max_size;
223 std::optional<std::size_t> max_queue_size;
224 std::optional<std::size_t> connecting_limit;
225 std::optional<std::size_t> connecting_interval_ms;
226};
227
228/// Default size limit for prepared statements cache
229inline constexpr std::size_t kDefaultMaxPreparedCacheSize = 200;
230
231/// Pipeline mode configuration
232///
233/// Dynamic option @ref POSTGRES_CONNECTION_PIPELINE_EXPERIMENT
234enum class PipelineMode { kDisabled, kEnabled };
235
236/// Whether to omit excessive D(escribe) message
237/// when executing prepared statements
238///
239/// Dynamic option @ref POSTGRES_OMIT_DESCRIBE_IN_EXECUTE
240enum class OmitDescribeInExecuteMode { kDisabled, kEnabled };
241
242/// PostgreSQL connection options
243///
244/// Dynamic option @ref POSTGRES_CONNECTION_SETTINGS
246 enum PreparedStatementOptions {
247 kCachePreparedStatements,
248 kNoPreparedStatements,
249 };
250 enum UserTypesOptions {
251 kUserTypesEnabled,
252 kUserTypesEnforced,
253 kPredefinedTypesOnly,
254 };
255 enum CheckQueryParamsOptions {
256 kIgnoreUnused,
257 kCheckUnused,
258 };
259 enum DiscardOnConnectOptions {
260 kDiscardNone,
261 kDiscardAll,
262 };
263 enum StatementLogMode {
264 kLogSkip,
265 kLog,
266 };
267 using SettingsVersion = std::size_t;
268
269 /// Cache prepared statements or not
270 PreparedStatementOptions prepared_statements = kCachePreparedStatements;
271
272 /// Enables the usage of user-defined types
273 UserTypesOptions user_types = kUserTypesEnabled;
274
275 /// Checks for not-NULL query params that are not used in query
276 CheckQueryParamsOptions ignore_unused_query_params = kCheckUnused;
277
278 /// Limits the size or prepared statements cache
280
281 /// Turns on connection pipeline mode
283
284 /// Enables protocol-level optimization when executing prepared statements
286
287 /// This many connection errors in 15 seconds block new connections opening
288 std::size_t recent_errors_threshold = 2;
289
290 /// The maximum lifetime of the connection after which it will be closed
291 std::optional<std::chrono::seconds> max_ttl{};
292
293 /// Execute discard all after establishing a new connection
294 DiscardOnConnectOptions discard_on_connect = kDiscardAll;
295
296 /// Statement logging in span tags
297 StatementLogMode statement_log_mode = kLog;
298
299 bool deadline_propagation_enabled = true;
300
301 /// Helps keep track of the changes in settings
302 SettingsVersion version{0U};
303
304 std::optional<std::string> application_name{};
305
306 bool operator==(const ConnectionSettings& rhs) const {
307 return !RequiresConnectionReset(rhs) && recent_errors_threshold == rhs.recent_errors_threshold;
308 }
309
310 bool RequiresConnectionReset(const ConnectionSettings& rhs) const {
311 // TODO: max_prepared_cache_size check could be relaxed
316 omit_describe_mode != rhs.omit_describe_mode || application_name != rhs.application_name;
317 }
318};
319
320struct ConnectionSettingsDynamic final {
321 std::optional<ConnectionSettings::PreparedStatementOptions> prepared_statements{};
322 std::optional<ConnectionSettings::UserTypesOptions> user_types{};
323 std::optional<std::size_t> max_prepared_cache_size{};
324 std::optional<std::size_t> recent_errors_threshold{};
325 std::optional<ConnectionSettings::CheckQueryParamsOptions> ignore_unused_query_params{};
326 std::optional<std::chrono::seconds> max_ttl{};
327 std::optional<ConnectionSettings::DiscardOnConnectOptions> discard_on_connect{};
328 std::optional<bool> deadline_propagation_enabled{};
329};
330
331/// @brief PostgreSQL statements metrics options
332///
333/// Dynamic option @ref POSTGRES_STATEMENT_METRICS_SETTINGS
334struct StatementMetricsSettings final {
335 /// Store metrics in LRU of this size
336 std::size_t max_statements{0};
337
338 bool operator==(const StatementMetricsSettings& other) const { return max_statements == other.max_statements; }
339};
340
341/// Initialization modes
342enum class InitMode {
343 kSync = 0,
344 kAsync,
345};
346
347enum class ConnlimitMode {
348 kManual = 0,
349 kAuto,
350};
351
352/// Settings for storages::postgres::Cluster
354 /// settings for statements metrics
355 StatementMetricsSettings statement_metrics_settings;
356
357 /// settings for host discovery
359
360 /// settings for connection pools
361 PoolSettings pool_settings;
362
363 /// settings for individual connections
365
366 /// initialization mode
368
369 /// database name
370 std::string db_name;
371
372 /// connection limit change mode
373 ConnlimitMode connlimit_mode = ConnlimitMode::kAuto;
374
375 /// congestion control settings
376 congestion_control::v2::LinearController::StaticConfig cc_config;
377};
378
379} // namespace storages::postgres
380
381USERVER_NAMESPACE_END