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/// @brief PostgreSQL topology options
179///
180/// Dynamic option @ref POSTGRES_TOPOLOGY_SETTINGS
182 /// Maximum replication lag. Once the replica lag exceeds this value it will be automatically disabled.
184
185 /// List of manually disabled replicas (FQDNs).
186 std::unordered_set<std::string, USERVER_NAMESPACE::utils::StrIcaseHash, USERVER_NAMESPACE::utils::StrIcaseEqual>
188};
189
190/// @brief PostgreSQL connection pool options
191///
192/// Dynamic option @ref POSTGRES_CONNECTION_POOL_SETTINGS
193struct PoolSettings final {
194 /// Number of connections created initially
196
197 /// Maximum number of created connections
199
200 /// Maximum number of clients waiting for a connection
202
203 /// Limits number of concurrent establishing connections (0 - unlimited)
205
206 bool operator==(const PoolSettings& rhs) const {
207 return min_size == rhs.min_size && max_size == rhs.max_size && max_queue_size == rhs.max_queue_size &&
209 }
210};
211
212// Configs with a suffix `Dynamic` are need to compatibility with static:
213// We must update only fields that were updated in a dynamic config (not a full config!).
214struct PoolSettingsDynamic final {
215 std::optional<std::size_t> min_size;
216 std::optional<std::size_t> max_size;
217 std::optional<std::size_t> max_queue_size;
218 std::optional<std::size_t> connecting_limit;
219};
220
221/// Default size limit for prepared statements cache
222inline constexpr std::size_t kDefaultMaxPreparedCacheSize = 200;
223
224/// Pipeline mode configuration
225///
226/// Dynamic option @ref POSTGRES_CONNECTION_PIPELINE_EXPERIMENT
227enum class PipelineMode { kDisabled, kEnabled };
228
229/// Whether to omit excessive D(escribe) message
230/// when executing prepared statements
231///
232/// Dynamic option @ref POSTGRES_OMIT_DESCRIBE_IN_EXECUTE
233enum class OmitDescribeInExecuteMode { kDisabled, kEnabled };
234
235/// PostgreSQL connection options
236///
237/// Dynamic option @ref POSTGRES_CONNECTION_SETTINGS
239 enum PreparedStatementOptions {
240 kCachePreparedStatements,
241 kNoPreparedStatements,
242 };
243 enum UserTypesOptions {
244 kUserTypesEnabled,
245 kUserTypesEnforced,
246 kPredefinedTypesOnly,
247 };
248 enum CheckQueryParamsOptions {
249 kIgnoreUnused,
250 kCheckUnused,
251 };
252 enum DiscardOnConnectOptions {
253 kDiscardNone,
254 kDiscardAll,
255 };
256 enum StatementLogMode {
257 kLogSkip,
258 kLog,
259 };
260 using SettingsVersion = std::size_t;
261
262 /// Cache prepared statements or not
263 PreparedStatementOptions prepared_statements = kCachePreparedStatements;
264
265 /// Enables the usage of user-defined types
266 UserTypesOptions user_types = kUserTypesEnabled;
267
268 /// Checks for not-NULL query params that are not used in query
269 CheckQueryParamsOptions ignore_unused_query_params = kCheckUnused;
270
271 /// Limits the size or prepared statements cache
273
274 /// Turns on connection pipeline mode
276
277 /// Enables protocol-level optimization when executing prepared statements
279
280 /// This many connection errors in 15 seconds block new connections opening
281 std::size_t recent_errors_threshold = 2;
282
283 /// The maximum lifetime of the connection after which it will be closed
284 std::optional<std::chrono::seconds> max_ttl{};
285
286 /// Execute discard all after establishing a new connection
287 DiscardOnConnectOptions discard_on_connect = kDiscardAll;
288
289 /// Statement logging in span tags
290 StatementLogMode statement_log_mode = kLog;
291
292 bool deadline_propagation_enabled = true;
293
294 /// Helps keep track of the changes in settings
295 SettingsVersion version{0U};
296
297 std::optional<std::string> application_name{};
298
299 bool operator==(const ConnectionSettings& rhs) const {
300 return !RequiresConnectionReset(rhs) && recent_errors_threshold == rhs.recent_errors_threshold;
301 }
302
303 bool RequiresConnectionReset(const ConnectionSettings& rhs) const {
304 // TODO: max_prepared_cache_size check could be relaxed
309 omit_describe_mode != rhs.omit_describe_mode || application_name != rhs.application_name;
310 }
311};
312
313struct ConnectionSettingsDynamic final {
314 std::optional<ConnectionSettings::PreparedStatementOptions> prepared_statements{};
315 std::optional<ConnectionSettings::UserTypesOptions> user_types{};
316 std::optional<std::size_t> max_prepared_cache_size{};
317 std::optional<std::size_t> recent_errors_threshold{};
318 std::optional<ConnectionSettings::CheckQueryParamsOptions> ignore_unused_query_params{};
319 std::optional<std::chrono::seconds> max_ttl{};
320 std::optional<ConnectionSettings::DiscardOnConnectOptions> discard_on_connect{};
321 std::optional<bool> deadline_propagation_enabled{};
322};
323
324/// @brief PostgreSQL statements metrics options
325///
326/// Dynamic option @ref POSTGRES_STATEMENT_METRICS_SETTINGS
327struct StatementMetricsSettings final {
328 /// Store metrics in LRU of this size
329 std::size_t max_statements{0};
330
331 bool operator==(const StatementMetricsSettings& other) const { return max_statements == other.max_statements; }
332};
333
334/// Initialization modes
335enum class InitMode {
336 kSync = 0,
337 kAsync,
338};
339
340enum class ConnlimitMode {
341 kManual = 0,
342 kAuto,
343};
344
345/// Settings for storages::postgres::Cluster
347 /// settings for statements metrics
348 StatementMetricsSettings statement_metrics_settings;
349
350 /// settings for host discovery
352
353 /// settings for connection pools
354 PoolSettings pool_settings;
355
356 /// settings for individual connections
358
359 /// initialization mode
361
362 /// database name
363 std::string db_name;
364
365 /// connection limit change mode
366 ConnlimitMode connlimit_mode = ConnlimitMode::kAuto;
367
368 /// congestion control settings
369 congestion_control::v2::LinearController::StaticConfig cc_config;
370};
371
372} // namespace storages::postgres
373
374USERVER_NAMESPACE_END