PostgreSQL Source Code git master
Loading...
Searching...
No Matches
server.c File Reference
#include "postgres_fe.h"
#include "common/connect.h"
#include "fe_utils/string_utils.h"
#include "libpq/pqcomm.h"
#include "pg_upgrade.h"
Include dependency graph for server.c:

Go to the source code of this file.

Functions

static PGconnget_db_conn (ClusterInfo *cluster, const char *db_name)
 
PGconnconnectToServer (ClusterInfo *cluster, const char *db_name)
 
charcluster_conn_opts (ClusterInfo *cluster)
 
PGresultexecuteQueryOrDie (PGconn *conn, const char *fmt,...)
 
static void stop_postmaster_atexit (void)
 
bool start_postmaster (ClusterInfo *cluster, bool report_and_exit_on_error)
 
void stop_postmaster (bool in_atexit)
 
void check_pghost_envvar (void)
 

Function Documentation

◆ check_pghost_envvar()

void check_pghost_envvar ( void  )

Definition at line 320 of file server.c.

321{
324
325 /* Get valid libpq env vars from the PQconndefaults function */
326
328
329 if (!start)
330 pg_fatal("out of memory");
331
332 for (option = start; option->keyword != NULL; option++)
333 {
334 if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
335 strcmp(option->envvar, "PGHOSTADDR") == 0))
336 {
337 const char *value = getenv(option->envvar);
338
339 if (value && strlen(value) > 0 &&
340 /* check for 'local' host values */
341 (strcmp(value, "localhost") != 0 && strcmp(value, "127.0.0.1") != 0 &&
342 strcmp(value, "::1") != 0 && !is_unixsock_path(value)))
343 pg_fatal("libpq environment variable %s has a non-local server value: %s",
344 option->envvar, value);
345 }
346 }
347
348 /* Free the memory that libpq allocated on our behalf */
350}
void PQconninfoFree(PQconninfoOption *connOptions)
PQconninfoOption * PQconndefaults(void)
return str start
static struct @175 value
#define pg_fatal(...)
static bool is_unixsock_path(const char *path)
Definition pqcomm.h:66
static int fb(int x)

References fb(), is_unixsock_path(), pg_fatal, PQconndefaults(), PQconninfoFree(), start, and value.

Referenced by setup().

◆ cluster_conn_opts()

char * cluster_conn_opts ( ClusterInfo cluster)

Definition at line 94 of file server.c.

95{
96 static PQExpBuffer buf;
97
98 if (buf == NULL)
100 else
102
103 if (cluster->sockdir)
104 {
105 appendPQExpBufferStr(buf, "--host ");
106 appendShellString(buf, cluster->sockdir);
108 }
109 appendPQExpBuffer(buf, "--port %d --username ", cluster->port);
111
112 return buf->data;
113}
static char buf[DEFAULT_XLOG_SEG_SIZE]
OSInfo os_info
Definition pg_upgrade.c:75
PQExpBuffer createPQExpBuffer(void)
Definition pqexpbuffer.c:72
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
void appendShellString(PQExpBuffer buf, const char *str)
char * user
Definition pg_upgrade.h:342

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendShellString(), buf, createPQExpBuffer(), fb(), os_info, resetPQExpBuffer(), and OSInfo::user.

Referenced by create_new_objects(), generate_old_dump(), prepare_new_cluster(), and prepare_new_globals().

◆ connectToServer()

PGconn * connectToServer ( ClusterInfo cluster,
const char db_name 
)

Definition at line 28 of file server.c.

29{
30 PGconn *conn = get_db_conn(cluster, db_name);
31
32 if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
33 {
35
36 if (conn)
38
39 printf(_("Failure, exiting\n"));
40 exit(1);
41 }
42
44
45 return conn;
46}
#define ALWAYS_SECURE_SEARCH_PATH_SQL
Definition connect.h:25
#define _(x)
Definition elog.c:96
ConnStatusType PQstatus(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
#define PQclear
@ CONNECTION_OK
Definition libpq-fe.h:90
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ PG_REPORT
Definition pg_upgrade.h:259
#define printf(...)
Definition port.h:267
static PGconn * get_db_conn(ClusterInfo *cluster, const char *db_name)
Definition server.c:57
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...)
Definition server.c:124
PGconn * conn
Definition streamutil.c:52

References _, ALWAYS_SECURE_SEARCH_PATH_SQL, conn, CONNECTION_OK, executeQueryOrDie(), fb(), get_db_conn(), pg_log(), PG_REPORT, PQclear, PQerrorMessage(), PQfinish(), PQstatus(), and printf.

Referenced by check_for_connection_status(), check_for_prepared_transactions(), check_for_unsupported_encodings(), check_is_install_user(), check_loadable_libraries(), check_new_cluster_replication_slots(), check_new_cluster_subscription_configuration(), check_old_cluster_global_names(), check_old_cluster_subscription_state(), create_conflict_detection_slot(), create_logical_replication_slots(), create_new_objects(), get_db_infos(), get_subscription_info(), get_tablespace_paths(), get_template0_info(), set_frozenxids(), set_locale_and_encoding(), and unicode_version_changed().

◆ executeQueryOrDie()

PGresult * executeQueryOrDie ( PGconn conn,
const char fmt,
  ... 
)

Definition at line 124 of file server.c.

125{
126 static char query[QUERY_ALLOC];
129 ExecStatusType status;
130
131 va_start(args, fmt);
132 vsnprintf(query, sizeof(query), fmt, args);
133 va_end(args);
134
135 pg_log(PG_VERBOSE, "executing: %s", query);
136 result = PQexec(conn, query);
137 status = PQresultStatus(result);
138
139 if ((status != PGRES_TUPLES_OK) && (status != PGRES_COMMAND_OK))
140 {
141 pg_log(PG_REPORT, "SQL command failed\n%s\n%s", query,
144 PQfinish(conn);
145 printf(_("Failure, exiting\n"));
146 exit(1);
147 }
148 else
149 return result;
150}
uint32 result
PGresult * PQexec(PGconn *conn, const char *query)
Definition fe-exec.c:2279
#define PQresultStatus
ExecStatusType
Definition libpq-fe.h:129
@ PGRES_COMMAND_OK
Definition libpq-fe.h:131
@ PGRES_TUPLES_OK
Definition libpq-fe.h:134
#define QUERY_ALLOC
Definition pg_upgrade.h:23
@ PG_VERBOSE
Definition pg_upgrade.h:256
#define vsnprintf
Definition port.h:260

References _, conn, fb(), pg_log(), PG_REPORT, PG_VERBOSE, PGRES_COMMAND_OK, PGRES_TUPLES_OK, PQclear, PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus, printf, QUERY_ALLOC, result, and vsnprintf.

Referenced by connectToServer().

◆ get_db_conn()

static PGconn * get_db_conn ( ClusterInfo cluster,
const char db_name 
)
static

Definition at line 57 of file server.c.

58{
60 PGconn *conn;
61
62 /* Build connection string with proper quoting */
65 appendConnStrVal(&conn_opts, db_name);
68 appendPQExpBuffer(&conn_opts, " port=%d", cluster->port);
69 if (cluster->sockdir)
70 {
73 }
75 appendPQExpBufferStr(&conn_opts, " max_protocol_version=3.0");
76
79 return conn;
80}
PGconn * PQconnectdb(const char *conninfo)
Definition fe-connect.c:830
bool protocol_negotiation_supported(const ClusterInfo *cluster)
Definition version.c:20
void initPQExpBuffer(PQExpBuffer str)
Definition pqexpbuffer.c:90
void termPQExpBuffer(PQExpBuffer str)
void appendConnStrVal(PQExpBuffer buf, const char *str)

References appendConnStrVal(), appendPQExpBuffer(), appendPQExpBufferStr(), conn, fb(), initPQExpBuffer(), os_info, PQconnectdb(), protocol_negotiation_supported(), termPQExpBuffer(), and OSInfo::user.

Referenced by connectToServer(), and start_postmaster().

◆ start_postmaster()

bool start_postmaster ( ClusterInfo cluster,
bool  report_and_exit_on_error 
)

Definition at line 161 of file server.c.

162{
163 char cmd[MAXPGPATH * 4 + 1000];
164 PGconn *conn;
165 bool pg_ctl_return = false;
166 char socket_string[MAXPGPATH + 200];
167 PQExpBufferData pgoptions;
168
169 static bool exit_hook_registered = false;
170
172 {
175 }
176
177 socket_string[0] = '\0';
178
179#if !defined(WIN32)
180 /* prevent TCP/IP connections, restrict socket access */
182 " -c listen_addresses='' -c unix_socket_permissions=0700");
183
184 /* Have a sockdir? Tell the postmaster. */
185 if (cluster->sockdir)
188 " -c %s='%s'",
189 "unix_socket_directories",
190 cluster->sockdir);
191#endif
192
193 initPQExpBuffer(&pgoptions);
194
195 /*
196 * Construct a parameter string which is passed to the server process.
197 *
198 * Turn off durability requirements to improve object creation speed, and
199 * we only modify the new cluster, so only use it there. If there is a
200 * crash, the new cluster has to be recreated anyway. fsync=off is a big
201 * win on ext4.
202 */
203 if (cluster == &new_cluster)
204 appendPQExpBufferStr(&pgoptions, " -c synchronous_commit=off -c fsync=off -c full_page_writes=off");
205
206 /*
207 * Use -b to disable autovacuum and logical replication launcher
208 * (effective in PG17 or later for the latter).
209 */
210 snprintf(cmd, sizeof(cmd),
211 "\"%s/pg_ctl\" -w -l \"%s/%s\" -D \"%s\" -o \"-p %d -b%s %s%s\" start",
212 cluster->bindir,
214 SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
215 pgoptions.data,
216 cluster->pgopts ? cluster->pgopts : "", socket_string);
217
218 termPQExpBuffer(&pgoptions);
219
220 /*
221 * Don't throw an error right away, let connecting throw the error because
222 * it might supply a reason for the failure.
223 */
225 /* pass both file names if they differ */
227 SERVER_START_LOG_FILE) != 0) ?
230 "%s", cmd);
231
232 /* Did it fail and we are just testing if the server could be started? */
234 return false;
235
236 /*
237 * We set this here to make sure atexit() shuts down the server, but only
238 * if we started the server successfully. We do it before checking for
239 * connectivity in case the server started but there is a connectivity
240 * failure. If pg_ctl did not return success, we will exit below.
241 *
242 * Pre-9.1 servers do not have PQping(), so we could be leaving the server
243 * running if authentication was misconfigured, so someday we might went
244 * to be more aggressive about doing server shutdowns even if pg_ctl
245 * fails, but now (2013-08-14) it seems prudent to be cautious. We don't
246 * want to shutdown a server that might have been accidentally started
247 * during the upgrade.
248 */
249 if (pg_ctl_return)
251
252 /*
253 * pg_ctl -w might have failed because the server couldn't be started, or
254 * there might have been a connection problem in _checking_ if the server
255 * has started. Therefore, even if pg_ctl failed, we continue and test
256 * for connectivity in case we get a connection reason for the failure.
257 */
258 if ((conn = get_db_conn(cluster, "template1")) == NULL ||
260 {
262 if (conn)
263 PQfinish(conn);
264 if (cluster == &old_cluster)
265 pg_fatal("could not connect to source postmaster started with the command:\n"
266 "%s",
267 cmd);
268 else
269 pg_fatal("could not connect to target postmaster started with the command:\n"
270 "%s",
271 cmd);
272 }
273 PQfinish(conn);
274
275 /*
276 * If pg_ctl failed, and the connection didn't fail, and
277 * report_and_exit_on_error is enabled, fail now. This could happen if
278 * the server was already running.
279 */
280 if (!pg_ctl_return)
281 {
282 if (cluster == &old_cluster)
283 pg_fatal("pg_ctl failed to start the source server, or connection failed");
284 else
285 pg_fatal("pg_ctl failed to start the target server, or connection failed");
286 }
287
288 return true;
289}
bool exec_prog(const char *log_filename, const char *opt_log_file, bool report_error, bool exit_on_error, const char *fmt,...)
Definition exec.c:77
#define false
#define MAXPGPATH
ClusterInfo new_cluster
Definition pg_upgrade.c:74
ClusterInfo old_cluster
Definition pg_upgrade.c:73
#define SERVER_START_LOG_FILE
Definition pg_upgrade.h:67
#define SERVER_LOG_FILE
Definition pg_upgrade.h:44
LogOpts log_opts
Definition util.c:17
#define snprintf
Definition port.h:261
static void stop_postmaster_atexit(void)
Definition server.c:154
char * logdir
Definition pg_upgrade.h:307
ClusterInfo * running_cluster
Definition pg_upgrade.h:346

References appendPQExpBufferStr(), conn, CONNECTION_OK, PQExpBufferData::data, exec_prog(), fb(), get_db_conn(), initPQExpBuffer(), log_opts, LogOpts::logdir, MAXPGPATH, new_cluster, old_cluster, os_info, pg_fatal, pg_log(), PG_REPORT, PQerrorMessage(), PQfinish(), PQstatus(), OSInfo::running_cluster, SERVER_LOG_FILE, SERVER_START_LOG_FILE, snprintf, stop_postmaster_atexit(), and termPQExpBuffer().

◆ stop_postmaster()

void stop_postmaster ( bool  in_atexit)

Definition at line 293 of file server.c.

294{
296
301 else
302 return; /* no cluster running */
303
305 "\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" %s stop",
306 cluster->bindir, cluster->pgconfig,
307 cluster->pgopts ? cluster->pgopts : "",
308 in_atexit ? "-m fast" : "-m smart");
309
311}
#define SERVER_STOP_LOG_FILE
Definition pg_upgrade.h:68

References exec_prog(), fb(), new_cluster, old_cluster, os_info, OSInfo::running_cluster, and SERVER_STOP_LOG_FILE.

◆ stop_postmaster_atexit()

static void stop_postmaster_atexit ( void  )
static

Definition at line 154 of file server.c.

155{
156 stop_postmaster(true);
157}
static void stop_postmaster(void)
Definition pg_regress.c:444

References stop_postmaster().

Referenced by start_postmaster().