forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgsql.cpp
More file actions
268 lines (217 loc) · 7.66 KB
/
pgsql.cpp
File metadata and controls
268 lines (217 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
/* Helper functions for the postgresql connections */
#include "format.hpp"
#include "logging.hpp"
#include "pgsql.hpp"
#include "util.hpp"
#include <cassert>
#include <cstdlib>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
std::size_t pg_result_t::affected_rows() const noexcept
{
char const *const s = PQcmdTuples(m_result.get());
return std::strtoull(s, nullptr, 10);
}
std::atomic<std::uint32_t> pg_conn_t::connection_id{0};
namespace {
PGconn *open_connection(connection_params_t const &connection_params,
std::string_view context, std::uint32_t id)
{
std::vector<char const *> keywords;
std::vector<char const *> values;
for (auto const &[k, v] : connection_params) {
keywords.push_back(k.c_str());
values.push_back(v.c_str());
}
std::string const app_name{fmt::format("osm2pgsql.{}/C{}", context, id)};
keywords.push_back("fallback_application_name");
values.push_back(app_name.c_str());
keywords.push_back(nullptr);
values.push_back(nullptr);
return PQconnectdbParams(keywords.data(), values.data(), 1);
}
std::string concat_params(int num_params, char const *const *param_values)
{
util::string_joiner_t joiner{','};
for (int i = 0; i < num_params; ++i) {
joiner.add(param_values[i] ? param_values[i] : "<NULL>");
}
return joiner();
}
} // anonymous namespace
pg_conn_t::pg_conn_t(connection_params_t const &connection_params,
std::string_view context)
: m_connection_id(connection_id.fetch_add(1))
{
m_conn.reset(open_connection(connection_params, context, m_connection_id));
if (!m_conn) {
throw fmt_error("Connecting to database failed (context={}).", context);
}
if (PQstatus(m_conn.get()) != CONNECTION_OK) {
throw fmt_error("Connecting to database failed (context={}): {}.",
context, error_msg());
}
if (get_logger().log_sql()) {
auto const results = exec("SELECT pg_backend_pid()");
log_sql("(C{}) New database connection (context={}, backend_pid={})",
m_connection_id, context, results.get(0, 0));
}
// PostgreSQL sends notices in many different contexts which aren't that
// useful for the user. So we disable them for all connections.
if (!get_logger().debug_enabled()) {
exec("SET client_min_messages = WARNING");
}
// Disable synchronous_commit on all connections. For some connections it
// might not matter, especially if they read only, but then it doesn't
// hurt either.
exec("SET synchronous_commit = off");
}
void pg_conn_t::close()
{
log_sql("(C{}) Closing database connection", m_connection_id);
m_conn.reset();
}
char const *pg_conn_t::error_msg() const noexcept
{
assert(m_conn);
return PQerrorMessage(m_conn.get());
}
void pg_conn_t::set_config(char const *setting, char const *value) const
{
// Update pg_settings instead of using SET because it does not yield
// errors on older versions of PostgreSQL where the settings are not
// implemented.
exec("UPDATE pg_catalog.pg_settings SET setting = '{}' WHERE name = '{}'",
value, setting);
}
pg_result_t pg_conn_t::exec(char const *sql) const
{
assert(m_conn);
log_sql("(C{}) {}", m_connection_id, sql);
pg_result_t res{PQexec(m_conn.get(), sql)};
if (res.status() != PGRES_COMMAND_OK && res.status() != PGRES_TUPLES_OK) {
throw fmt_error("Database error: {}", error_msg());
}
return res;
}
pg_result_t pg_conn_t::exec(std::string const &sql) const
{
return exec(sql.c_str());
}
void pg_conn_t::copy_start(std::string const &sql) const
{
assert(m_conn);
log_sql("(C{}) {}", m_connection_id, sql);
pg_result_t const res{PQexec(m_conn.get(), sql.c_str())};
if (res.status() != PGRES_COPY_IN) {
throw fmt_error("Database error on COPY: {}", error_msg());
}
}
void pg_conn_t::copy_send(std::string_view data, std::string_view context) const
{
assert(m_conn);
log_sql_data("(C{}) Copy data to '{}':\n{}", m_connection_id, context,
data);
int const r = PQputCopyData(m_conn.get(), data.data(), (int)data.size());
switch (r) {
case 0: // need to wait for write ready
log_error("{} - COPY unexpectedly busy", context);
break;
case 1: // success
return;
case -1: // error occurred
log_error("{} - error on COPY: {}", context, error_msg());
break;
default: // unexpected result
break;
}
if (data.size() < 1100) {
log_error("Data: {}", data);
} else {
log_error("Data: {}\n...\n{}", data.substr(0, 500),
data.substr(data.size() - 500, 500));
}
throw std::runtime_error{"COPYing data to Postgresql."};
}
void pg_conn_t::copy_end(std::string_view context) const
{
assert(m_conn);
if (PQputCopyEnd(m_conn.get(), nullptr) != 1) {
throw fmt_error("Ending COPY mode for '{}' failed: {}.", context,
error_msg());
}
pg_result_t const res{PQgetResult(m_conn.get())};
if (res.status() != PGRES_COMMAND_OK) {
throw fmt_error("Ending COPY mode for '{}' failed: {}.", context,
error_msg());
}
}
void pg_conn_t::prepare_internal(std::string const &stmt,
std::string const &sql) const
{
if (get_logger().log_sql()) {
log_sql("(C{}) PREPARE {} AS {}", m_connection_id, stmt, sql);
}
pg_result_t const res{
PQprepare(m_conn.get(), stmt.c_str(), sql.c_str(), 0, nullptr)};
if (res.status() != PGRES_COMMAND_OK) {
throw fmt_error("Prepare failed for '{}': {}.", sql, error_msg());
}
}
pg_result_t pg_conn_t::exec_prepared_internal(char const *stmt, int num_params,
char const *const *param_values,
int *param_lengths,
int *param_formats,
int result_format) const
{
assert(m_conn);
if (get_logger().log_sql()) {
log_sql("(C{}) EXECUTE {}({})", m_connection_id, stmt,
concat_params(num_params, param_values));
}
pg_result_t res{PQexecPrepared(m_conn.get(), stmt, num_params, param_values,
param_lengths, param_formats,
result_format)};
auto const status = res.status();
if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
log_error("SQL command failed: EXECUTE {}({})", stmt,
concat_params(num_params, param_values));
throw fmt_error("Database error: {} ({})", error_msg(),
std::underlying_type_t<ExecStatusType>(status));
}
return res;
}
std::string tablespace_clause(std::string const &name)
{
std::string sql;
if (!name.empty()) {
sql += " TABLESPACE \"";
sql += name;
sql += '"';
}
return sql;
}
std::string qualified_name(std::string const &schema, std::string const &name)
{
assert(!schema.empty());
return fmt::format(R"("{}"."{}")", schema, name);
}
void check_identifier(std::string const &name, char const *in)
{
auto const pos = name.find_first_of("\"',.;$%&/()<>{}=?^*#");
if (pos == std::string::npos) {
return;
}
throw fmt_error("Special characters are not allowed in {}: '{}'.", in,
name);
}