Header And Logo

| The world's most advanced open source database.


server.c File Reference

#include "postgres_fe.h"
#include "pg_upgrade.h"
Include dependency graph for server.c:

Go to the source code of this file.


static PGconnget_db_conn (ClusterInfo *cluster, const char *db_name)
PGconnconnectToServer (ClusterInfo *cluster, const char *db_name)
char * cluster_conn_opts (ClusterInfo *cluster)
PGresultexecuteQueryOrDie (PGconn *conn, const char *fmt,...)
uint32 get_major_server_version (ClusterInfo *cluster)
static void stop_postmaster_atexit (void)
bool start_postmaster (ClusterInfo *cluster, bool throw_error)
void stop_postmaster (bool fast)
void check_pghost_envvar (void)

Function Documentation

void check_pghost_envvar ( void   ) 

Definition at line 295 of file server.c.

References _PQconninfoOption::envvar, _PQconninfoOption::keyword, PG_FATAL, pg_log(), PQconndefaults(), PQconninfoFree(), and value.

Referenced by setup().

    PQconninfoOption *option;
    PQconninfoOption *start;

    /* Get valid libpq env vars from the PQconndefaults function */

    start = PQconndefaults();

    for (option = start; option->keyword != NULL; option++)
        if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
                               strcmp(option->envvar, "PGHOSTADDR") == 0))
            const char *value = getenv(option->envvar);

            if (value && strlen(value) > 0 &&
            /* check for 'local' host values */
                (strcmp(value, "localhost") != 0 && strcmp(value, "") != 0 &&
                 strcmp(value, "::1") != 0 && value[0] != '/'))
                       "libpq environment variable %s has a non-local server value: %s\n",
                       option->envvar, value);

    /* Free the memory that libpq allocated on our behalf */

char* cluster_conn_opts ( ClusterInfo cluster  ) 

Definition at line 80 of file server.c.

References MAXPGPATH, NAMEDATALEN, os_info, ClusterInfo::port, snprintf(), ClusterInfo::sockdir, and OSInfo::user.

Referenced by create_new_objects(), generate_old_dump(), issue_warnings(), prepare_new_cluster(), and prepare_new_databases().

    static char conn_opts[MAXPGPATH + NAMEDATALEN + 100];

    if (cluster->sockdir)
        snprintf(conn_opts, sizeof(conn_opts),
                 "--host \"%s\" --port %d --username \"%s\"",
                 cluster->sockdir, cluster->port, os_info.user);
        snprintf(conn_opts, sizeof(conn_opts),
                 "--port %d --username \"%s\"",
                 cluster->port, os_info.user);

    return conn_opts;

PGconn* connectToServer ( ClusterInfo cluster,
const char *  db_name 
PGresult* executeQueryOrDie ( PGconn conn,
const char *  fmt,

Definition at line 105 of file server.c.

References pg_log(), PG_REPORT, PG_VERBOSE, PGRES_COMMAND_OK, PGRES_TUPLES_OK, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), PQresultStatus(), and vsnprintf().

    static char command[8192];
    va_list     args;
    PGresult   *result;
    ExecStatusType status;

    va_start(args, fmt);
    vsnprintf(command, sizeof(command), fmt, args);

    pg_log(PG_VERBOSE, "executing: %s\n", command);
    result = PQexec(conn, command);
    status = PQresultStatus(result);

    if ((status != PGRES_TUPLES_OK) && (status != PGRES_COMMAND_OK))
        pg_log(PG_REPORT, "SQL command failed\n%s\n%s\n", command,
        printf("Failure, exiting\n");
        return result;

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

Definition at line 52 of file server.c.

References MAXPGPATH, NAMEDATALEN, os_info, ClusterInfo::port, PQconnectdb(), snprintf(), ClusterInfo::sockdir, and OSInfo::user.

Referenced by connectToServer(), and start_postmaster().

    char        conn_opts[2 * NAMEDATALEN + MAXPGPATH + 100];

    if (cluster->sockdir)
        snprintf(conn_opts, sizeof(conn_opts),
                 "dbname = '%s' user = '%s' host = '%s' port = %d",
                 db_name, os_info.user, cluster->sockdir, cluster->port);
        snprintf(conn_opts, sizeof(conn_opts),
                 "dbname = '%s' user = '%s' port = %d",
                 db_name, os_info.user, cluster->port);

    return PQconnectdb(conn_opts);

uint32 get_major_server_version ( ClusterInfo cluster  ) 

Definition at line 142 of file server.c.

References ClusterInfo::major_version_str, NULL, PG_FATAL, pg_log(), ClusterInfo::pgdata, and snprintf().

Referenced by check_cluster_versions().

    FILE       *version_fd;
    char        ver_filename[MAXPGPATH];
    int         integer_version = 0;
    int         fractional_version = 0;

    snprintf(ver_filename, sizeof(ver_filename), "%s/PG_VERSION",
    if ((version_fd = fopen(ver_filename, "r")) == NULL)
        pg_log(PG_FATAL, "could not open version file: %s\n", ver_filename);

    if (fscanf(version_fd, "%63s", cluster->major_version_str) == 0 ||
        sscanf(cluster->major_version_str, "%d.%d", &integer_version,
               &fractional_version) != 2)
        pg_log(PG_FATAL, "could not get version from %s\n", cluster->pgdata);


    return (100 * integer_version + fractional_version) * 100;

bool start_postmaster ( ClusterInfo cluster,
bool  throw_error 

Definition at line 174 of file server.c.

References BINARY_UPGRADE_SERVER_FLAG_CAT_VER, ClusterInfo::bindir, ControlData::cat_ver, CLUSTER_NAME, conn, CONNECTION_OK, ClusterInfo::controldata, exec_prog(), get_db_conn(), GET_MAJOR_VERSION, ClusterInfo::major_version, MAXPGPATH, new_cluster, NULL, os_info, PG_FATAL, pg_log(), PG_REPORT, ClusterInfo::pgconfig, ClusterInfo::pgopts, ClusterInfo::port, PQerrorMessage(), PQfinish(), PQstatus(), OSInfo::running_cluster, SERVER_LOG_FILE, SERVER_START_LOG_FILE, snprintf(), ClusterInfo::sockdir, and stop_postmaster_atexit().

Referenced by check_and_dump_old_cluster(), do_start(), issue_warnings(), main(), and setup().

    char        cmd[MAXPGPATH * 4 + 1000];
    PGconn     *conn;
    bool        exit_hook_registered = false;
    bool        pg_ctl_return = false;
    char        socket_string[MAXPGPATH + 200];

    if (!exit_hook_registered)
        exit_hook_registered = true;

    socket_string[0] = '\0';

    /* prevent TCP/IP connections, restrict socket access */
           " -c listen_addresses='' -c unix_socket_permissions=0700");

    /* Have a sockdir?  Tell the postmaster. */
    if (cluster->sockdir)
        snprintf(socket_string + strlen(socket_string),
                 sizeof(socket_string) - strlen(socket_string),
                 " -c %s='%s'",
                 (GET_MAJOR_VERSION(cluster->major_version) < 903) ?
                 "unix_socket_directory" : "unix_socket_directories",

     * Using autovacuum=off disables cleanup vacuum and analyze, but freeze
     * vacuums can still happen, so we set autovacuum_freeze_max_age to its
     * maximum.  We assume all datfrozenxid and relfrozen values are less than
     * a gap of 2000000000 from the current xid counter, so autovacuum will
     * not touch them.
     * Turn off durability requirements to improve object creation speed, and
     * we only modify the new cluster, so only use it there.  If there is a
     * crash, the new cluster has to be recreated anyway.  fsync=off is a big
     * win on ext4.
    snprintf(cmd, sizeof(cmd),
             "\"%s/pg_ctl\" -w -l \"%s\" -D \"%s\" -o \"-p %d%s%s %s%s\" start",
          cluster->bindir, SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
             (cluster->controldata.cat_ver >=
              BINARY_UPGRADE_SERVER_FLAG_CAT_VER) ? " -b" :
             " -c autovacuum=off -c autovacuum_freeze_max_age=2000000000",
             (cluster == &new_cluster) ?
                " -c synchronous_commit=off -c fsync=off -c full_page_writes=off" : "",
             cluster->pgopts ? cluster->pgopts : "", socket_string);

     * Don't throw an error right away, let connecting throw the error because
     * it might supply a reason for the failure.
    pg_ctl_return = exec_prog(SERVER_START_LOG_FILE,
                              /* pass both file names if they differ */
                                      SERVER_START_LOG_FILE) != 0) ?
                              SERVER_LOG_FILE : NULL,
                              "%s", cmd);

    if (!pg_ctl_return && !throw_error)
        return false;
    /* Check to see if we can connect to the server; if not, report it. */
    if ((conn = get_db_conn(cluster, "template1")) == NULL ||
        PQstatus(conn) != CONNECTION_OK)
        pg_log(PG_REPORT, "\nconnection to database failed: %s\n",
        if (conn)
        pg_log(PG_FATAL, "could not connect to %s postmaster started with the command:\n"
               CLUSTER_NAME(cluster), cmd);

    /* If the connection didn't fail, fail now */
    if (!pg_ctl_return)
        pg_log(PG_FATAL, "pg_ctl failed to start the %s server, or connection failed\n",

    os_info.running_cluster = cluster;

    return true;

void stop_postmaster ( bool  fast  ) 

Definition at line 268 of file server.c.

References ClusterInfo::bindir, exec_prog(), new_cluster, NULL, old_cluster, os_info, ClusterInfo::pgconfig, ClusterInfo::pgopts, OSInfo::running_cluster, and SERVER_STOP_LOG_FILE.

Referenced by check_and_dump_old_cluster(), issue_warnings(), main(), regression_main(), report_clusters_compatible(), setup(), and stop_postmaster_atexit().

    ClusterInfo *cluster;

    if (os_info.running_cluster == &old_cluster)
        cluster = &old_cluster;
    else if (os_info.running_cluster == &new_cluster)
        cluster = &new_cluster;
        return;                 /* no cluster running */

    exec_prog(SERVER_STOP_LOG_FILE, NULL, !fast,
              "\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" %s stop",
              cluster->bindir, cluster->pgconfig,
              cluster->pgopts ? cluster->pgopts : "",
              fast ? "-m fast" : "");

    os_info.running_cluster = NULL;

static void stop_postmaster_atexit ( void   )  [static]

Definition at line 166 of file server.c.

References stop_postmaster().

Referenced by start_postmaster().