LCOV - differential code coverage report
Current view: top level - src/bin/pg_upgrade - server.c (source / functions) Coverage Total Hit UBC CBC DUB DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 79.8 % 114 91 23 91 3 10
Current Date: 2026-05-05 10:23:31 +0900 Functions: 100.0 % 8 8 8 1
Baseline: lcov-20260505-025707-baseline Branches: 59.2 % 76 45 31 45 4 4
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 50.0 % 2 1 1 1
(360..) days: 80.4 % 112 90 22 90
Function coverage date bins:
(360..) days: 100.0 % 8 8 8
Branch coverage date bins:
(30,360] days: 50.0 % 2 1 1 1
(360..) days: 59.5 % 74 44 30 44

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  *  server.c
                                  3                 :                :  *
                                  4                 :                :  *  database server functions
                                  5                 :                :  *
                                  6                 :                :  *  Copyright (c) 2010-2026, PostgreSQL Global Development Group
                                  7                 :                :  *  src/bin/pg_upgrade/server.c
                                  8                 :                :  */
                                  9                 :                : 
                                 10                 :                : #include "postgres_fe.h"
                                 11                 :                : 
                                 12                 :                : #include "common/connect.h"
                                 13                 :                : #include "fe_utils/string_utils.h"
                                 14                 :                : #include "libpq/pqcomm.h"
                                 15                 :                : #include "pg_upgrade.h"
                                 16                 :                : 
                                 17                 :                : static PGconn *get_db_conn(ClusterInfo *cluster, const char *db_name);
                                 18                 :                : 
                                 19                 :                : 
                                 20                 :                : /*
                                 21                 :                :  * connectToServer()
                                 22                 :                :  *
                                 23                 :                :  *  Connects to the desired database on the designated server.
                                 24                 :                :  *  If the connection attempt fails, this function logs an error
                                 25                 :                :  *  message and calls exit() to kill the program.
                                 26                 :                :  */
                                 27                 :                : PGconn *
 5603 bruce@momjian.us           28                 :CBC         328 : connectToServer(ClusterInfo *cluster, const char *db_name)
                                 29                 :                : {
 5488                            30                 :            328 :     PGconn     *conn = get_db_conn(cluster, db_name);
                                 31                 :                : 
 5837                            32   [ +  -  -  + ]:            328 :     if (conn == NULL || PQstatus(conn) != CONNECTION_OK)
                                 33                 :                :     {
 1929 tgl@sss.pgh.pa.us          34                 :UBC           0 :         pg_log(PG_REPORT, "%s", PQerrorMessage(conn));
                                 35                 :                : 
 5837 bruce@momjian.us           36         [ #  # ]:              0 :         if (conn)
                                 37                 :              0 :             PQfinish(conn);
                                 38                 :                : 
 3490 peter_e@gmx.net            39                 :              0 :         printf(_("Failure, exiting\n"));
 5508                            40                 :              0 :         exit(1);
                                 41                 :                :     }
                                 42                 :                : 
 2990 noah@leadboat.com          43                 :CBC         328 :     PQclear(executeQueryOrDie(conn, ALWAYS_SECURE_SEARCH_PATH_SQL));
                                 44                 :                : 
 5837 bruce@momjian.us           45                 :            328 :     return conn;
                                 46                 :                : }
                                 47                 :                : 
                                 48                 :                : 
                                 49                 :                : /*
                                 50                 :                :  * get_db_conn()
                                 51                 :                :  *
                                 52                 :                :  * get database connection, using named database + standard params for cluster
                                 53                 :                :  *
                                 54                 :                :  * Caller must check for connection failure!
                                 55                 :                :  */
                                 56                 :                : static PGconn *
 5488                            57                 :            383 : get_db_conn(ClusterInfo *cluster, const char *db_name)
                                 58                 :                : {
                                 59                 :                :     PQExpBufferData conn_opts;
                                 60                 :                :     PGconn     *conn;
                                 61                 :                : 
                                 62                 :                :     /* Build connection string with proper quoting */
 3557 noah@leadboat.com          63                 :            383 :     initPQExpBuffer(&conn_opts);
                                 64                 :            383 :     appendPQExpBufferStr(&conn_opts, "dbname=");
                                 65                 :            383 :     appendConnStrVal(&conn_opts, db_name);
                                 66                 :            383 :     appendPQExpBufferStr(&conn_opts, " user=");
                                 67                 :            383 :     appendConnStrVal(&conn_opts, os_info.user);
                                 68                 :            383 :     appendPQExpBuffer(&conn_opts, " port=%d", cluster->port);
 4992 tgl@sss.pgh.pa.us          69         [ +  - ]:            383 :     if (cluster->sockdir)
                                 70                 :                :     {
 3557 noah@leadboat.com          71                 :            383 :         appendPQExpBufferStr(&conn_opts, " host=");
                                 72                 :            383 :         appendConnStrVal(&conn_opts, cluster->sockdir);
                                 73                 :                :     }
   70 jchampion@postgresql       74         [ -  + ]:            383 :     if (!protocol_negotiation_supported(cluster))
   70 jchampion@postgresql       75                 :UBC           0 :         appendPQExpBufferStr(&conn_opts, " max_protocol_version=3.0");
                                 76                 :                : 
 3557 noah@leadboat.com          77                 :CBC         383 :     conn = PQconnectdb(conn_opts.data);
                                 78                 :            383 :     termPQExpBuffer(&conn_opts);
                                 79                 :            383 :     return conn;
                                 80                 :                : }
                                 81                 :                : 
                                 82                 :                : 
                                 83                 :                : /*
                                 84                 :                :  * cluster_conn_opts()
                                 85                 :                :  *
                                 86                 :                :  * Return standard command-line options for connecting to this cluster when
                                 87                 :                :  * using psql, pg_dump, etc.  Ideally this would match what get_db_conn()
                                 88                 :                :  * sets, but the utilities we need aren't very consistent about the treatment
                                 89                 :                :  * of database name options, so we leave that out.
                                 90                 :                :  *
                                 91                 :                :  * Result is valid until the next call to this function.
                                 92                 :                :  */
                                 93                 :                : char *
 4992 tgl@sss.pgh.pa.us          94                 :            112 : cluster_conn_opts(ClusterInfo *cluster)
                                 95                 :                : {
                                 96                 :                :     static PQExpBuffer buf;
                                 97                 :                : 
 3557 noah@leadboat.com          98         [ +  + ]:            112 :     if (buf == NULL)
                                 99                 :             12 :         buf = createPQExpBuffer();
                                100                 :                :     else
                                101                 :            100 :         resetPQExpBuffer(buf);
                                102                 :                : 
                                103         [ +  - ]:            112 :     if (cluster->sockdir)
                                104                 :                :     {
                                105                 :            112 :         appendPQExpBufferStr(buf, "--host ");
                                106                 :            112 :         appendShellString(buf, cluster->sockdir);
                                107                 :            112 :         appendPQExpBufferChar(buf, ' ');
                                108                 :                :     }
                                109                 :            112 :     appendPQExpBuffer(buf, "--port %d --username ", cluster->port);
                                110                 :            112 :     appendShellString(buf, os_info.user);
                                111                 :                : 
                                112                 :            112 :     return buf->data;
                                113                 :                : }
                                114                 :                : 
                                115                 :                : 
                                116                 :                : /*
                                117                 :                :  * executeQueryOrDie()
                                118                 :                :  *
                                119                 :                :  *  Formats a query string from the given arguments and executes the
                                120                 :                :  *  resulting query.  If the query fails, this function logs an error
                                121                 :                :  *  message and calls exit() to kill the program.
                                122                 :                :  */
                                123                 :                : PGresult *
 5677 bruce@momjian.us          124                 :            746 : executeQueryOrDie(PGconn *conn, const char *fmt,...)
                                125                 :                : {
                                126                 :                :     static char query[QUERY_ALLOC];
                                127                 :                :     va_list     args;
                                128                 :                :     PGresult   *result;
                                129                 :                :     ExecStatusType status;
                                130                 :                : 
 5837                           131                 :            746 :     va_start(args, fmt);
 4276                           132                 :            746 :     vsnprintf(query, sizeof(query), fmt, args);
 5837                           133                 :            746 :     va_end(args);
                                134                 :                : 
 1393 tgl@sss.pgh.pa.us         135                 :            746 :     pg_log(PG_VERBOSE, "executing: %s", query);
 4276 bruce@momjian.us          136                 :            746 :     result = PQexec(conn, query);
 5837                           137                 :            746 :     status = PQresultStatus(result);
                                138                 :                : 
                                139   [ +  +  -  + ]:            746 :     if ((status != PGRES_TUPLES_OK) && (status != PGRES_COMMAND_OK))
                                140                 :                :     {
 3178 peter_e@gmx.net           141                 :UBC           0 :         pg_log(PG_REPORT, "SQL command failed\n%s\n%s", query,
                                142                 :                :                PQerrorMessage(conn));
 5837 bruce@momjian.us          143                 :              0 :         PQclear(result);
                                144                 :              0 :         PQfinish(conn);
 3490 peter_e@gmx.net           145                 :              0 :         printf(_("Failure, exiting\n"));
 5508                           146                 :              0 :         exit(1);
                                147                 :                :     }
                                148                 :                :     else
 5837 bruce@momjian.us          149                 :CBC         746 :         return result;
                                150                 :                : }
                                151                 :                : 
                                152                 :                : 
                                153                 :                : static void
 5508 peter_e@gmx.net           154                 :             18 : stop_postmaster_atexit(void)
                                155                 :                : {
 5489 bruce@momjian.us          156                 :             18 :     stop_postmaster(true);
 5508 peter_e@gmx.net           157                 :             18 : }
                                158                 :                : 
                                159                 :                : 
                                160                 :                : bool
 3039 bruce@momjian.us          161                 :             55 : start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error)
                                162                 :                : {
                                163                 :                :     char        cmd[MAXPGPATH * 4 + 1000];
                                164                 :                :     PGconn     *conn;
 4999 alvherre@alvh.no-ip.      165                 :             55 :     bool        pg_ctl_return = false;
                                166                 :                :     char        socket_string[MAXPGPATH + 200];
                                167                 :                :     PQExpBufferData pgoptions;
                                168                 :                : 
                                169                 :                :     static bool exit_hook_registered = false;
                                170                 :                : 
 5508 peter_e@gmx.net           171         [ +  + ]:             55 :     if (!exit_hook_registered)
                                172                 :                :     {
                                173                 :             18 :         atexit(stop_postmaster_atexit);
                                174                 :             18 :         exit_hook_registered = true;
                                175                 :                :     }
                                176                 :                : 
 2276 michael@paquier.xyz       177                 :             55 :     socket_string[0] = '\0';
                                178                 :                : 
                                179                 :                : #if !defined(WIN32)
                                180                 :                :     /* prevent TCP/IP connections, restrict socket access */
                                181                 :             55 :     strcat(socket_string,
                                182                 :                :            " -c listen_addresses='' -c unix_socket_permissions=0700");
                                183                 :                : 
                                184                 :                :     /* Have a sockdir?  Tell the postmaster. */
                                185         [ +  - ]:             55 :     if (cluster->sockdir)
                                186                 :             55 :         snprintf(socket_string + strlen(socket_string),
                                187                 :             55 :                  sizeof(socket_string) - strlen(socket_string),
                                188                 :                :                  " -c %s='%s'",
 2037 bruce@momjian.us          189         [ -  + ]:             55 :                  (GET_MAJOR_VERSION(cluster->major_version) <= 902) ?
                                190                 :                :                  "unix_socket_directory" : "unix_socket_directories",
                                191                 :                :                  cluster->sockdir);
                                192                 :                : #endif
                                193                 :                : 
  922 akapila@postgresql.o      194                 :             55 :     initPQExpBuffer(&pgoptions);
                                195                 :                : 
                                196                 :                :     /*
                                197                 :                :      * Construct a parameter string which is passed to the server process.
                                198                 :                :      *
                                199                 :                :      * Turn off durability requirements to improve object creation speed, and
                                200                 :                :      * we only modify the new cluster, so only use it there.  If there is a
                                201                 :                :      * crash, the new cluster has to be recreated anyway.  fsync=off is a big
                                202                 :                :      * win on ext4.
                                203                 :                :      */
                                204         [ +  + ]:             55 :     if (cluster == &new_cluster)
                                205                 :             37 :         appendPQExpBufferStr(&pgoptions, " -c synchronous_commit=off -c fsync=off -c full_page_writes=off");
                                206                 :                : 
                                207                 :                :     /*
                                208                 :                :      * Use -b to disable autovacuum and logical replication launcher
                                209                 :                :      * (effective in PG17 or later for the latter).
                                210                 :                :      */
 2276 michael@paquier.xyz       211                 :             55 :     snprintf(cmd, sizeof(cmd),
                                212                 :                :              "\"%s/pg_ctl\" -w -l \"%s/%s\" -D \"%s\" -o \"-p %d -b%s %s%s\" start",
                                213                 :                :              cluster->bindir,
                                214                 :                :              log_opts.logdir,
 1549                           215                 :             55 :              SERVER_LOG_FILE, cluster->pgconfig, cluster->port,
                                216                 :                :              pgoptions.data,
 2276                           217         [ -  + ]:             55 :              cluster->pgopts ? cluster->pgopts : "", socket_string);
                                218                 :                : 
  922 akapila@postgresql.o      219                 :             55 :     termPQExpBuffer(&pgoptions);
                                220                 :                : 
                                221                 :                :     /*
                                222                 :                :      * Don't throw an error right away, let connecting throw the error because
                                223                 :                :      * it might supply a reason for the failure.
                                224                 :                :      */
 4999 alvherre@alvh.no-ip.      225                 :             55 :     pg_ctl_return = exec_prog(SERVER_START_LOG_FILE,
                                226                 :                :     /* pass both file names if they differ */
                                227                 :                :                               (strcmp(SERVER_LOG_FILE,
                                228                 :                :                                       SERVER_START_LOG_FILE) != 0) ?
                                229                 :                :                               SERVER_LOG_FILE : NULL,
                                230                 :                :                               report_and_exit_on_error, false,
                                231                 :                :                               "%s", cmd);
                                232                 :                : 
                                233                 :                :     /* Did it fail and we are just testing if the server could be started? */
 3039 bruce@momjian.us          234   [ -  +  -  - ]:             55 :     if (!pg_ctl_return && !report_and_exit_on_error)
 4849 bruce@momjian.us          235                 :UBC           0 :         return false;
                                236                 :                : 
                                237                 :                :     /*
                                238                 :                :      * We set this here to make sure atexit() shuts down the server, but only
                                239                 :                :      * if we started the server successfully.  We do it before checking for
                                240                 :                :      * connectivity in case the server started but there is a connectivity
                                241                 :                :      * failure.  If pg_ctl did not return success, we will exit below.
                                242                 :                :      *
                                243                 :                :      * Pre-9.1 servers do not have PQping(), so we could be leaving the server
                                244                 :                :      * running if authentication was misconfigured, so someday we might went
                                245                 :                :      * to be more aggressive about doing server shutdowns even if pg_ctl
                                246                 :                :      * fails, but now (2013-08-14) it seems prudent to be cautious.  We don't
                                247                 :                :      * want to shutdown a server that might have been accidentally started
                                248                 :                :      * during the upgrade.
                                249                 :                :      */
 4645 bruce@momjian.us          250         [ +  - ]:CBC          55 :     if (pg_ctl_return)
                                251                 :             55 :         os_info.running_cluster = cluster;
                                252                 :                : 
                                253                 :                :     /*
                                254                 :                :      * pg_ctl -w might have failed because the server couldn't be started, or
                                255                 :                :      * there might have been a connection problem in _checking_ if the server
                                256                 :                :      * has started.  Therefore, even if pg_ctl failed, we continue and test
                                257                 :                :      * for connectivity in case we get a connection reason for the failure.
                                258                 :                :      */
 5488                           259   [ +  -  -  + ]:            110 :     if ((conn = get_db_conn(cluster, "template1")) == NULL ||
                                260                 :             55 :         PQstatus(conn) != CONNECTION_OK)
                                261                 :                :     {
 1929 tgl@sss.pgh.pa.us         262                 :UBC           0 :         pg_log(PG_REPORT, "\n%s", PQerrorMessage(conn));
 5444 bruce@momjian.us          263         [ #  # ]:              0 :         if (conn)
 5488                           264                 :              0 :             PQfinish(conn);
 3217 alvherre@alvh.no-ip.      265         [ #  # ]:              0 :         if (cluster == &old_cluster)
                                266                 :              0 :             pg_fatal("could not connect to source postmaster started with the command:\n"
                                267                 :                :                      "%s",
                                268                 :                :                      cmd);
                                269                 :                :         else
                                270                 :              0 :             pg_fatal("could not connect to target postmaster started with the command:\n"
                                271                 :                :                      "%s",
                                272                 :                :                      cmd);
                                273                 :                :     }
 5488 bruce@momjian.us          274                 :CBC          55 :     PQfinish(conn);
                                275                 :                : 
                                276                 :                :     /*
                                277                 :                :      * If pg_ctl failed, and the connection didn't fail, and
                                278                 :                :      * report_and_exit_on_error is enabled, fail now.  This could happen if
                                279                 :                :      * the server was already running.
                                280                 :                :      */
 4999 alvherre@alvh.no-ip.      281         [ -  + ]:             55 :     if (!pg_ctl_return)
                                282                 :                :     {
 3042 bruce@momjian.us          283         [ #  # ]:UBC           0 :         if (cluster == &old_cluster)
 1393 tgl@sss.pgh.pa.us         284                 :              0 :             pg_fatal("pg_ctl failed to start the source server, or connection failed");
                                285                 :                :         else
                                286                 :              0 :             pg_fatal("pg_ctl failed to start the target server, or connection failed");
                                287                 :                :     }
                                288                 :                : 
 4849 bruce@momjian.us          289                 :CBC          55 :     return true;
                                290                 :                : }
                                291                 :                : 
                                292                 :                : 
                                293                 :                : void
 3039                           294                 :             66 : stop_postmaster(bool in_atexit)
                                295                 :                : {
                                296                 :                :     ClusterInfo *cluster;
                                297                 :                : 
 5603                           298         [ +  + ]:             66 :     if (os_info.running_cluster == &old_cluster)
 5321                           299                 :             18 :         cluster = &old_cluster;
 5603                           300         [ +  + ]:             48 :     else if (os_info.running_cluster == &new_cluster)
 5321                           301                 :             37 :         cluster = &new_cluster;
                                302                 :                :     else
 5077                           303                 :             11 :         return;                 /* no cluster running */
                                304                 :                : 
 3039                           305         [ +  + ]:            110 :     exec_prog(SERVER_STOP_LOG_FILE, NULL, !in_atexit, !in_atexit,
                                306                 :                :               "\"%s/pg_ctl\" -w -D \"%s\" -o \"%s\" %s stop",
                                307                 :                :               cluster->bindir, cluster->pgconfig,
 4999 alvherre@alvh.no-ip.      308         [ -  + ]:             55 :               cluster->pgopts ? cluster->pgopts : "",
 3039 bruce@momjian.us          309                 :             55 :               in_atexit ? "-m fast" : "-m smart");
                                310                 :                : 
 5603                           311                 :             55 :     os_info.running_cluster = NULL;
                                312                 :                : }
                                313                 :                : 
                                314                 :                : 
                                315                 :                : /*
                                316                 :                :  * check_pghost_envvar()
                                317                 :                :  *
                                318                 :                :  * Tests that PGHOST does not point to a non-local server
                                319                 :                :  */
                                320                 :                : void
 5468                           321                 :             20 : check_pghost_envvar(void)
                                322                 :                : {
                                323                 :                :     PQconninfoOption *option;
                                324                 :                :     PQconninfoOption *start;
                                325                 :                : 
                                326                 :                :     /* Get valid libpq env vars from the PQconndefaults function */
                                327                 :                : 
 5507 peter_e@gmx.net           328                 :             20 :     start = PQconndefaults();
                                329                 :                : 
 4536 bruce@momjian.us          330         [ -  + ]:             20 :     if (!start)
 1393 tgl@sss.pgh.pa.us         331                 :UBC           0 :         pg_fatal("out of memory");
                                332                 :                : 
 5507 peter_e@gmx.net           333         [ +  + ]:CBC        1060 :     for (option = start; option->keyword != NULL; option++)
                                334                 :                :     {
 5468 bruce@momjian.us          335   [ +  +  +  + ]:           1040 :         if (option->envvar && (strcmp(option->envvar, "PGHOST") == 0 ||
 5444                           336         [ +  + ]:            720 :                                strcmp(option->envvar, "PGHOSTADDR") == 0))
                                337                 :                :         {
 5468                           338                 :             40 :             const char *value = getenv(option->envvar);
                                339                 :                : 
                                340   [ +  +  +  - ]:             40 :             if (value && strlen(value) > 0 &&
                                341                 :                :             /* check for 'local' host values */
                                342   [ +  -  +  - ]:             20 :                 (strcmp(value, "localhost") != 0 && strcmp(value, "127.0.0.1") != 0 &&
 1525 michael@paquier.xyz       343   [ +  -  -  + ]:             20 :                  strcmp(value, "::1") != 0 && !is_unixsock_path(value)))
 1393 tgl@sss.pgh.pa.us         344                 :UBC           0 :                 pg_fatal("libpq environment variable %s has a non-local server value: %s",
                                345                 :                :                          option->envvar, value);
                                346                 :                :         }
                                347                 :                :     }
                                348                 :                : 
                                349                 :                :     /* Free the memory that libpq allocated on our behalf */
 5837 bruce@momjian.us          350                 :CBC          20 :     PQconninfoFree(start);
                                351                 :             20 : }
        

Generated by: LCOV version 2.5.0-beta