LCOV - code coverage report
Current view: top level - src/bin/pg_basebackup - streamutil.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 247 343 72.0 %
Date: 2024-12-02 19:14:57 Functions: 17 17 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * streamutil.c - utility functions for pg_basebackup, pg_receivewal and
       4             :  *                  pg_recvlogical
       5             :  *
       6             :  * Author: Magnus Hagander <magnus@hagander.net>
       7             :  *
       8             :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *        src/bin/pg_basebackup/streamutil.c
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : 
      15             : #include "postgres_fe.h"
      16             : 
      17             : #include <sys/time.h>
      18             : #include <unistd.h>
      19             : 
      20             : #include "access/xlog_internal.h"
      21             : #include "common/connect.h"
      22             : #include "common/file_perm.h"
      23             : #include "common/logging.h"
      24             : #include "common/string.h"
      25             : #include "datatype/timestamp.h"
      26             : #include "port/pg_bswap.h"
      27             : #include "pqexpbuffer.h"
      28             : #include "streamutil.h"
      29             : 
      30             : #define ERRCODE_DUPLICATE_OBJECT  "42710"
      31             : 
      32             : int         WalSegSz;
      33             : 
      34             : static bool RetrieveDataDirCreatePerm(PGconn *conn);
      35             : static char *FindDbnameInConnParams(PQconninfoOption *conn_opts);
      36             : 
      37             : /* SHOW command for replication connection was introduced in version 10 */
      38             : #define MINIMUM_VERSION_FOR_SHOW_CMD 100000
      39             : 
      40             : /*
      41             :  * Group access is supported from version 11.
      42             :  */
      43             : #define MINIMUM_VERSION_FOR_GROUP_ACCESS 110000
      44             : 
      45             : const char *progname;
      46             : char       *connection_string = NULL;
      47             : char       *dbhost = NULL;
      48             : char       *dbuser = NULL;
      49             : char       *dbport = NULL;
      50             : char       *dbname = NULL;
      51             : int         dbgetpassword = 0;  /* 0=auto, -1=never, 1=always */
      52             : static char *password = NULL;
      53             : PGconn     *conn = NULL;
      54             : 
      55             : /*
      56             :  * Connect to the server. Returns a valid PGconn pointer if connected,
      57             :  * or NULL on non-permanent error. On permanent error, the function will
      58             :  * call exit(1) directly.
      59             :  */
      60             : PGconn *
      61         740 : GetConnection(void)
      62             : {
      63             :     PGconn     *tmpconn;
      64         740 :     int         argcount = 7;   /* dbname, replication, fallback_app_name,
      65             :                                  * host, user, port, password */
      66             :     int         i;
      67             :     const char **keywords;
      68             :     const char **values;
      69             :     const char *tmpparam;
      70             :     bool        need_password;
      71         740 :     PQconninfoOption *conn_opts = NULL;
      72             :     PQconninfoOption *conn_opt;
      73         740 :     char       *err_msg = NULL;
      74             : 
      75             :     /*
      76             :      * pg_recvlogical uses dbname only; others use connection_string only.
      77             :      * (Note: both variables will be NULL if there's no command line options.)
      78             :      */
      79             :     Assert(dbname == NULL || connection_string == NULL);
      80             : 
      81             :     /*
      82             :      * Merge the connection info inputs given in form of connection string,
      83             :      * options and default values (dbname=replication, replication=true, etc.)
      84             :      */
      85         740 :     i = 0;
      86         740 :     if (connection_string)
      87             :     {
      88           8 :         conn_opts = PQconninfoParse(connection_string, &err_msg);
      89           8 :         if (conn_opts == NULL)
      90           0 :             pg_fatal("%s", err_msg);
      91             : 
      92         336 :         for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
      93             :         {
      94         328 :             if (conn_opt->val != NULL && conn_opt->val[0] != '\0')
      95          16 :                 argcount++;
      96             :         }
      97             : 
      98           8 :         keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
      99           8 :         values = pg_malloc0((argcount + 1) * sizeof(*values));
     100             : 
     101             :         /*
     102             :          * Set dbname here already, so it can be overridden by a dbname in the
     103             :          * connection string.
     104             :          */
     105           8 :         keywords[i] = "dbname";
     106           8 :         values[i] = "replication";
     107           8 :         i++;
     108             : 
     109         336 :         for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
     110             :         {
     111         328 :             if (conn_opt->val != NULL && conn_opt->val[0] != '\0')
     112             :             {
     113          16 :                 keywords[i] = conn_opt->keyword;
     114          16 :                 values[i] = conn_opt->val;
     115          16 :                 i++;
     116             :             }
     117             :         }
     118             :     }
     119             :     else
     120             :     {
     121         732 :         keywords = pg_malloc0((argcount + 1) * sizeof(*keywords));
     122         732 :         values = pg_malloc0((argcount + 1) * sizeof(*values));
     123         732 :         keywords[i] = "dbname";
     124         732 :         values[i] = (dbname == NULL) ? "replication" : dbname;
     125         732 :         i++;
     126             :     }
     127             : 
     128         740 :     keywords[i] = "replication";
     129         740 :     values[i] = (dbname == NULL) ? "true" : "database";
     130         740 :     i++;
     131         740 :     keywords[i] = "fallback_application_name";
     132         740 :     values[i] = progname;
     133         740 :     i++;
     134             : 
     135         740 :     if (dbhost)
     136             :     {
     137         258 :         keywords[i] = "host";
     138         258 :         values[i] = dbhost;
     139         258 :         i++;
     140             :     }
     141         740 :     if (dbuser)
     142             :     {
     143          14 :         keywords[i] = "user";
     144          14 :         values[i] = dbuser;
     145          14 :         i++;
     146             :     }
     147         740 :     if (dbport)
     148             :     {
     149         258 :         keywords[i] = "port";
     150         258 :         values[i] = dbport;
     151         258 :         i++;
     152             :     }
     153             : 
     154             :     /* If -W was given, force prompt for password, but only the first time */
     155         740 :     need_password = (dbgetpassword == 1 && !password);
     156             : 
     157             :     do
     158             :     {
     159             :         /* Get a new password if appropriate */
     160         740 :         if (need_password)
     161             :         {
     162           0 :             free(password);
     163           0 :             password = simple_prompt("Password: ", false);
     164           0 :             need_password = false;
     165             :         }
     166             : 
     167             :         /* Use (or reuse, on a subsequent connection) password if we have it */
     168         740 :         if (password)
     169             :         {
     170           0 :             keywords[i] = "password";
     171           0 :             values[i] = password;
     172             :         }
     173             :         else
     174             :         {
     175         740 :             keywords[i] = NULL;
     176         740 :             values[i] = NULL;
     177             :         }
     178             : 
     179             :         /*
     180             :          * Only expand dbname when we did not already parse the argument as a
     181             :          * connection string ourselves.
     182             :          */
     183         740 :         tmpconn = PQconnectdbParams(keywords, values, !connection_string);
     184             : 
     185             :         /*
     186             :          * If there is too little memory even to allocate the PGconn object
     187             :          * and PQconnectdbParams returns NULL, we call exit(1) directly.
     188             :          */
     189         740 :         if (!tmpconn)
     190           0 :             pg_fatal("could not connect to server");
     191             : 
     192             :         /* If we need a password and -w wasn't given, loop back and get one */
     193         744 :         if (PQstatus(tmpconn) == CONNECTION_BAD &&
     194           4 :             PQconnectionNeedsPassword(tmpconn) &&
     195           0 :             dbgetpassword != -1)
     196             :         {
     197           0 :             PQfinish(tmpconn);
     198           0 :             need_password = true;
     199             :         }
     200             :     }
     201         740 :     while (need_password);
     202             : 
     203         740 :     if (PQstatus(tmpconn) != CONNECTION_OK)
     204             :     {
     205           4 :         pg_log_error("%s", PQerrorMessage(tmpconn));
     206           4 :         PQfinish(tmpconn);
     207           4 :         free(values);
     208           4 :         free(keywords);
     209           4 :         PQconninfoFree(conn_opts);
     210           4 :         return NULL;
     211             :     }
     212             : 
     213             :     /* Connection ok! */
     214         736 :     free(values);
     215         736 :     free(keywords);
     216         736 :     PQconninfoFree(conn_opts);
     217             : 
     218             :     /*
     219             :      * Set always-secure search path, so malicious users can't get control.
     220             :      * The capacity to run normal SQL queries was added in PostgreSQL 10, so
     221             :      * the search path cannot be changed (by us or attackers) on earlier
     222             :      * versions.
     223             :      */
     224         736 :     if (dbname != NULL && PQserverVersion(tmpconn) >= 100000)
     225             :     {
     226             :         PGresult   *res;
     227             : 
     228          94 :         res = PQexec(tmpconn, ALWAYS_SECURE_SEARCH_PATH_SQL);
     229          94 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
     230             :         {
     231           0 :             pg_log_error("could not clear \"search_path\": %s",
     232             :                          PQerrorMessage(tmpconn));
     233           0 :             PQclear(res);
     234           0 :             PQfinish(tmpconn);
     235           0 :             exit(1);
     236             :         }
     237          94 :         PQclear(res);
     238             :     }
     239             : 
     240             :     /*
     241             :      * Ensure we have the same value of integer_datetimes (now always "on") as
     242             :      * the server we are connecting to.
     243             :      */
     244         736 :     tmpparam = PQparameterStatus(tmpconn, "integer_datetimes");
     245         736 :     if (!tmpparam)
     246             :     {
     247           0 :         pg_log_error("could not determine server setting for \"integer_datetimes\"");
     248           0 :         PQfinish(tmpconn);
     249           0 :         exit(1);
     250             :     }
     251             : 
     252         736 :     if (strcmp(tmpparam, "on") != 0)
     253             :     {
     254           0 :         pg_log_error("\"integer_datetimes\" compile flag does not match server");
     255           0 :         PQfinish(tmpconn);
     256           0 :         exit(1);
     257             :     }
     258             : 
     259             :     /*
     260             :      * Retrieve the source data directory mode and use it to construct a umask
     261             :      * for creating directories and files.
     262             :      */
     263         736 :     if (!RetrieveDataDirCreatePerm(tmpconn))
     264             :     {
     265           0 :         PQfinish(tmpconn);
     266           0 :         exit(1);
     267             :     }
     268             : 
     269         736 :     return tmpconn;
     270             : }
     271             : 
     272             : /*
     273             :  * FindDbnameInConnParams
     274             :  *
     275             :  * This is a helper function for GetDbnameFromConnectionOptions(). Extract
     276             :  * the value of dbname from PQconninfoOption parameters, if it's present.
     277             :  * Returns a strdup'd result or NULL.
     278             :  */
     279             : static char *
     280           6 : FindDbnameInConnParams(PQconninfoOption *conn_opts)
     281             : {
     282             :     PQconninfoOption *conn_opt;
     283             : 
     284          42 :     for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
     285             :     {
     286          42 :         if (strcmp(conn_opt->keyword, "dbname") == 0 &&
     287           6 :             conn_opt->val != NULL && conn_opt->val[0] != '\0')
     288           6 :             return pg_strdup(conn_opt->val);
     289             :     }
     290           0 :     return NULL;
     291             : }
     292             : 
     293             : /*
     294             :  * GetDbnameFromConnectionOptions
     295             :  *
     296             :  * This is a special purpose function to retrieve the dbname from either the
     297             :  * connection_string specified by the user or from the environment variables.
     298             :  *
     299             :  * We follow GetConnection() to fetch the dbname from various connection
     300             :  * options.
     301             :  *
     302             :  * Returns NULL, if dbname is not specified by the user in the above
     303             :  * mentioned connection options.
     304             :  */
     305             : char *
     306           6 : GetDbnameFromConnectionOptions(void)
     307             : {
     308             :     PQconninfoOption *conn_opts;
     309           6 :     char       *err_msg = NULL;
     310             :     char       *dbname;
     311             : 
     312             :     /* First try to get the dbname from connection string. */
     313           6 :     if (connection_string)
     314             :     {
     315           2 :         conn_opts = PQconninfoParse(connection_string, &err_msg);
     316           2 :         if (conn_opts == NULL)
     317           0 :             pg_fatal("%s", err_msg);
     318             : 
     319           2 :         dbname = FindDbnameInConnParams(conn_opts);
     320             : 
     321           2 :         PQconninfoFree(conn_opts);
     322           2 :         if (dbname)
     323           2 :             return dbname;
     324             :     }
     325             : 
     326             :     /*
     327             :      * Next try to get the dbname from default values that are available from
     328             :      * the environment.
     329             :      */
     330           4 :     conn_opts = PQconndefaults();
     331           4 :     if (conn_opts == NULL)
     332           0 :         pg_fatal("out of memory");
     333             : 
     334           4 :     dbname = FindDbnameInConnParams(conn_opts);
     335             : 
     336           4 :     PQconninfoFree(conn_opts);
     337           4 :     return dbname;
     338             : }
     339             : 
     340             : /*
     341             :  * From version 10, explicitly set wal segment size using SHOW wal_segment_size
     342             :  * since ControlFile is not accessible here.
     343             :  */
     344             : bool
     345         366 : RetrieveWalSegSize(PGconn *conn)
     346             : {
     347             :     PGresult   *res;
     348             :     char        xlog_unit[3];
     349             :     int         xlog_val,
     350         366 :                 multiplier = 1;
     351             : 
     352             :     /* check connection existence */
     353             :     Assert(conn != NULL);
     354             : 
     355             :     /* for previous versions set the default xlog seg size */
     356         366 :     if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_SHOW_CMD)
     357             :     {
     358           0 :         WalSegSz = DEFAULT_XLOG_SEG_SIZE;
     359           0 :         return true;
     360             :     }
     361             : 
     362         366 :     res = PQexec(conn, "SHOW wal_segment_size");
     363         366 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
     364             :     {
     365           0 :         pg_log_error("could not send replication command \"%s\": %s",
     366             :                      "SHOW wal_segment_size", PQerrorMessage(conn));
     367             : 
     368           0 :         PQclear(res);
     369           0 :         return false;
     370             :     }
     371         366 :     if (PQntuples(res) != 1 || PQnfields(res) < 1)
     372             :     {
     373           0 :         pg_log_error("could not fetch WAL segment size: got %d rows and %d fields, expected %d rows and %d or more fields",
     374             :                      PQntuples(res), PQnfields(res), 1, 1);
     375             : 
     376           0 :         PQclear(res);
     377           0 :         return false;
     378             :     }
     379             : 
     380             :     /* fetch xlog value and unit from the result */
     381         366 :     if (sscanf(PQgetvalue(res, 0, 0), "%d%2s", &xlog_val, xlog_unit) != 2)
     382             :     {
     383           0 :         pg_log_error("WAL segment size could not be parsed");
     384           0 :         PQclear(res);
     385           0 :         return false;
     386             :     }
     387             : 
     388         366 :     PQclear(res);
     389             : 
     390             :     /* set the multiplier based on unit to convert xlog_val to bytes */
     391         366 :     if (strcmp(xlog_unit, "MB") == 0)
     392         366 :         multiplier = 1024 * 1024;
     393           0 :     else if (strcmp(xlog_unit, "GB") == 0)
     394           0 :         multiplier = 1024 * 1024 * 1024;
     395             : 
     396             :     /* convert and set WalSegSz */
     397         366 :     WalSegSz = xlog_val * multiplier;
     398             : 
     399         366 :     if (!IsValidWalSegSize(WalSegSz))
     400             :     {
     401           0 :         pg_log_error(ngettext("remote server reported invalid WAL segment size (%d byte)",
     402             :                               "remote server reported invalid WAL segment size (%d bytes)",
     403             :                               WalSegSz),
     404             :                      WalSegSz);
     405           0 :         pg_log_error_detail("The WAL segment size must be a power of two between 1 MB and 1 GB.");
     406           0 :         return false;
     407             :     }
     408             : 
     409         366 :     return true;
     410             : }
     411             : 
     412             : /*
     413             :  * RetrieveDataDirCreatePerm
     414             :  *
     415             :  * This function is used to determine the privileges on the server's PG data
     416             :  * directory and, based on that, set what the permissions will be for
     417             :  * directories and files we create.
     418             :  *
     419             :  * PG11 added support for (optionally) group read/execute rights to be set on
     420             :  * the data directory.  Prior to PG11, only the owner was allowed to have rights
     421             :  * on the data directory.
     422             :  */
     423             : static bool
     424         736 : RetrieveDataDirCreatePerm(PGconn *conn)
     425             : {
     426             :     PGresult   *res;
     427             :     int         data_directory_mode;
     428             : 
     429             :     /* check connection existence */
     430             :     Assert(conn != NULL);
     431             : 
     432             :     /* for previous versions leave the default group access */
     433         736 :     if (PQserverVersion(conn) < MINIMUM_VERSION_FOR_GROUP_ACCESS)
     434           0 :         return true;
     435             : 
     436         736 :     res = PQexec(conn, "SHOW data_directory_mode");
     437         736 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
     438             :     {
     439           0 :         pg_log_error("could not send replication command \"%s\": %s",
     440             :                      "SHOW data_directory_mode", PQerrorMessage(conn));
     441             : 
     442           0 :         PQclear(res);
     443           0 :         return false;
     444             :     }
     445         736 :     if (PQntuples(res) != 1 || PQnfields(res) < 1)
     446             :     {
     447           0 :         pg_log_error("could not fetch group access flag: got %d rows and %d fields, expected %d rows and %d or more fields",
     448             :                      PQntuples(res), PQnfields(res), 1, 1);
     449             : 
     450           0 :         PQclear(res);
     451           0 :         return false;
     452             :     }
     453             : 
     454         736 :     if (sscanf(PQgetvalue(res, 0, 0), "%o", &data_directory_mode) != 1)
     455             :     {
     456           0 :         pg_log_error("group access flag could not be parsed: %s",
     457             :                      PQgetvalue(res, 0, 0));
     458             : 
     459           0 :         PQclear(res);
     460           0 :         return false;
     461             :     }
     462             : 
     463         736 :     SetDataDirectoryCreatePerm(data_directory_mode);
     464             : 
     465         736 :     PQclear(res);
     466         736 :     return true;
     467             : }
     468             : 
     469             : /*
     470             :  * Run IDENTIFY_SYSTEM through a given connection and give back to caller
     471             :  * some result information if requested:
     472             :  * - System identifier
     473             :  * - Current timeline ID
     474             :  * - Start LSN position
     475             :  * - Database name (NULL in servers prior to 9.4)
     476             :  */
     477             : bool
     478         752 : RunIdentifySystem(PGconn *conn, char **sysid, TimeLineID *starttli,
     479             :                   XLogRecPtr *startpos, char **db_name)
     480             : {
     481             :     PGresult   *res;
     482             :     uint32      hi,
     483             :                 lo;
     484             : 
     485             :     /* Check connection existence */
     486             :     Assert(conn != NULL);
     487             : 
     488         752 :     res = PQexec(conn, "IDENTIFY_SYSTEM");
     489         752 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
     490             :     {
     491           0 :         pg_log_error("could not send replication command \"%s\": %s",
     492             :                      "IDENTIFY_SYSTEM", PQerrorMessage(conn));
     493             : 
     494           0 :         PQclear(res);
     495           0 :         return false;
     496             :     }
     497         752 :     if (PQntuples(res) != 1 || PQnfields(res) < 3)
     498             :     {
     499           0 :         pg_log_error("could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields",
     500             :                      PQntuples(res), PQnfields(res), 1, 3);
     501             : 
     502           0 :         PQclear(res);
     503           0 :         return false;
     504             :     }
     505             : 
     506             :     /* Get system identifier */
     507         752 :     if (sysid != NULL)
     508         640 :         *sysid = pg_strdup(PQgetvalue(res, 0, 0));
     509             : 
     510             :     /* Get timeline ID to start streaming from */
     511         752 :     if (starttli != NULL)
     512         640 :         *starttli = atoi(PQgetvalue(res, 0, 1));
     513             : 
     514             :     /* Get LSN start position if necessary */
     515         752 :     if (startpos != NULL)
     516             :     {
     517          14 :         if (sscanf(PQgetvalue(res, 0, 2), "%X/%X", &hi, &lo) != 2)
     518             :         {
     519           0 :             pg_log_error("could not parse write-ahead log location \"%s\"",
     520             :                          PQgetvalue(res, 0, 2));
     521             : 
     522           0 :             PQclear(res);
     523           0 :             return false;
     524             :         }
     525          14 :         *startpos = ((uint64) hi) << 32 | lo;
     526             :     }
     527             : 
     528             :     /* Get database name, only available in 9.4 and newer versions */
     529         752 :     if (db_name != NULL)
     530             :     {
     531         112 :         *db_name = NULL;
     532         112 :         if (PQserverVersion(conn) >= 90400)
     533             :         {
     534         112 :             if (PQnfields(res) < 4)
     535             :             {
     536           0 :                 pg_log_error("could not identify system: got %d rows and %d fields, expected %d rows and %d or more fields",
     537             :                              PQntuples(res), PQnfields(res), 1, 4);
     538             : 
     539           0 :                 PQclear(res);
     540           0 :                 return false;
     541             :             }
     542         112 :             if (!PQgetisnull(res, 0, 3))
     543          94 :                 *db_name = pg_strdup(PQgetvalue(res, 0, 3));
     544             :         }
     545             :     }
     546             : 
     547         752 :     PQclear(res);
     548         752 :     return true;
     549             : }
     550             : 
     551             : /*
     552             :  * Run READ_REPLICATION_SLOT through a given connection and give back to
     553             :  * caller some result information if requested for this slot:
     554             :  * - Start LSN position, InvalidXLogRecPtr if unknown.
     555             :  * - Current timeline ID, 0 if unknown.
     556             :  * Returns false on failure, and true otherwise.
     557             :  */
     558             : bool
     559           6 : GetSlotInformation(PGconn *conn, const char *slot_name,
     560             :                    XLogRecPtr *restart_lsn, TimeLineID *restart_tli)
     561             : {
     562             :     PGresult   *res;
     563             :     PQExpBuffer query;
     564           6 :     XLogRecPtr  lsn_loc = InvalidXLogRecPtr;
     565           6 :     TimeLineID  tli_loc = 0;
     566             : 
     567           6 :     if (restart_lsn)
     568           6 :         *restart_lsn = lsn_loc;
     569           6 :     if (restart_tli)
     570           6 :         *restart_tli = tli_loc;
     571             : 
     572           6 :     query = createPQExpBuffer();
     573           6 :     appendPQExpBuffer(query, "READ_REPLICATION_SLOT %s", slot_name);
     574           6 :     res = PQexec(conn, query->data);
     575           6 :     destroyPQExpBuffer(query);
     576             : 
     577           6 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
     578             :     {
     579           0 :         pg_log_error("could not send replication command \"%s\": %s",
     580             :                      "READ_REPLICATION_SLOT", PQerrorMessage(conn));
     581           0 :         PQclear(res);
     582           0 :         return false;
     583             :     }
     584             : 
     585             :     /* The command should always return precisely one tuple and three fields */
     586           6 :     if (PQntuples(res) != 1 || PQnfields(res) != 3)
     587             :     {
     588           0 :         pg_log_error("could not read replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
     589             :                      slot_name, PQntuples(res), PQnfields(res), 1, 3);
     590           0 :         PQclear(res);
     591           0 :         return false;
     592             :     }
     593             : 
     594             :     /*
     595             :      * When the slot doesn't exist, the command returns a tuple with NULL
     596             :      * values.  This checks only the slot type field.
     597             :      */
     598           6 :     if (PQgetisnull(res, 0, 0))
     599             :     {
     600           2 :         pg_log_error("replication slot \"%s\" does not exist", slot_name);
     601           2 :         PQclear(res);
     602           2 :         return false;
     603             :     }
     604             : 
     605             :     /*
     606             :      * Note that this cannot happen as READ_REPLICATION_SLOT supports only
     607             :      * physical slots, but play it safe.
     608             :      */
     609           4 :     if (strcmp(PQgetvalue(res, 0, 0), "physical") != 0)
     610             :     {
     611           0 :         pg_log_error("expected a physical replication slot, got type \"%s\" instead",
     612             :                      PQgetvalue(res, 0, 0));
     613           0 :         PQclear(res);
     614           0 :         return false;
     615             :     }
     616             : 
     617             :     /* restart LSN */
     618           4 :     if (!PQgetisnull(res, 0, 1))
     619             :     {
     620             :         uint32      hi,
     621             :                     lo;
     622             : 
     623           4 :         if (sscanf(PQgetvalue(res, 0, 1), "%X/%X", &hi, &lo) != 2)
     624             :         {
     625           0 :             pg_log_error("could not parse restart_lsn \"%s\" for replication slot \"%s\"",
     626             :                          PQgetvalue(res, 0, 1), slot_name);
     627           0 :             PQclear(res);
     628           0 :             return false;
     629             :         }
     630           4 :         lsn_loc = ((uint64) hi) << 32 | lo;
     631             :     }
     632             : 
     633             :     /* current TLI */
     634           4 :     if (!PQgetisnull(res, 0, 2))
     635           4 :         tli_loc = (TimeLineID) atoll(PQgetvalue(res, 0, 2));
     636             : 
     637           4 :     PQclear(res);
     638             : 
     639             :     /* Assign results if requested */
     640           4 :     if (restart_lsn)
     641           4 :         *restart_lsn = lsn_loc;
     642           4 :     if (restart_tli)
     643           4 :         *restart_tli = tli_loc;
     644             : 
     645           4 :     return true;
     646             : }
     647             : 
     648             : /*
     649             :  * Create a replication slot for the given connection. This function
     650             :  * returns true in case of success.
     651             :  */
     652             : bool
     653         310 : CreateReplicationSlot(PGconn *conn, const char *slot_name, const char *plugin,
     654             :                       bool is_temporary, bool is_physical, bool reserve_wal,
     655             :                       bool slot_exists_ok, bool two_phase)
     656             : {
     657             :     PQExpBuffer query;
     658             :     PGresult   *res;
     659         310 :     bool        use_new_option_syntax = (PQserverVersion(conn) >= 150000);
     660             : 
     661         310 :     query = createPQExpBuffer();
     662             : 
     663             :     Assert((is_physical && plugin == NULL) ||
     664             :            (!is_physical && plugin != NULL));
     665             :     Assert(!(two_phase && is_physical));
     666             :     Assert(slot_name != NULL);
     667             : 
     668             :     /* Build base portion of query */
     669         310 :     appendPQExpBuffer(query, "CREATE_REPLICATION_SLOT \"%s\"", slot_name);
     670         310 :     if (is_temporary)
     671         258 :         appendPQExpBufferStr(query, " TEMPORARY");
     672         310 :     if (is_physical)
     673         264 :         appendPQExpBufferStr(query, " PHYSICAL");
     674             :     else
     675          46 :         appendPQExpBuffer(query, " LOGICAL \"%s\"", plugin);
     676             : 
     677             :     /* Add any requested options */
     678         310 :     if (use_new_option_syntax)
     679         310 :         appendPQExpBufferStr(query, " (");
     680         310 :     if (is_physical)
     681             :     {
     682         264 :         if (reserve_wal)
     683         262 :             AppendPlainCommandOption(query, use_new_option_syntax,
     684             :                                      "RESERVE_WAL");
     685             :     }
     686             :     else
     687             :     {
     688          46 :         if (two_phase && PQserverVersion(conn) >= 150000)
     689           2 :             AppendPlainCommandOption(query, use_new_option_syntax,
     690             :                                      "TWO_PHASE");
     691             : 
     692          46 :         if (PQserverVersion(conn) >= 100000)
     693             :         {
     694             :             /* pg_recvlogical doesn't use an exported snapshot, so suppress */
     695          46 :             if (use_new_option_syntax)
     696          46 :                 AppendStringCommandOption(query, use_new_option_syntax,
     697             :                                           "SNAPSHOT", "nothing");
     698             :             else
     699           0 :                 AppendPlainCommandOption(query, use_new_option_syntax,
     700             :                                          "NOEXPORT_SNAPSHOT");
     701             :         }
     702             :     }
     703         310 :     if (use_new_option_syntax)
     704             :     {
     705             :         /* Suppress option list if it would be empty, otherwise terminate */
     706         310 :         if (query->data[query->len - 1] == '(')
     707             :         {
     708           2 :             query->len -= 2;
     709           2 :             query->data[query->len] = '\0';
     710             :         }
     711             :         else
     712         308 :             appendPQExpBufferChar(query, ')');
     713             :     }
     714             : 
     715             :     /* Now run the query */
     716         310 :     res = PQexec(conn, query->data);
     717         310 :     if (PQresultStatus(res) != PGRES_TUPLES_OK)
     718             :     {
     719           2 :         const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
     720             : 
     721           2 :         if (slot_exists_ok &&
     722           0 :             sqlstate &&
     723           0 :             strcmp(sqlstate, ERRCODE_DUPLICATE_OBJECT) == 0)
     724             :         {
     725           0 :             destroyPQExpBuffer(query);
     726           0 :             PQclear(res);
     727           0 :             return true;
     728             :         }
     729             :         else
     730             :         {
     731           2 :             pg_log_error("could not send replication command \"%s\": %s",
     732             :                          query->data, PQerrorMessage(conn));
     733             : 
     734           2 :             destroyPQExpBuffer(query);
     735           2 :             PQclear(res);
     736           2 :             return false;
     737             :         }
     738             :     }
     739             : 
     740         308 :     if (PQntuples(res) != 1 || PQnfields(res) != 4)
     741             :     {
     742           0 :         pg_log_error("could not create replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
     743             :                      slot_name,
     744             :                      PQntuples(res), PQnfields(res), 1, 4);
     745             : 
     746           0 :         destroyPQExpBuffer(query);
     747           0 :         PQclear(res);
     748           0 :         return false;
     749             :     }
     750             : 
     751         308 :     destroyPQExpBuffer(query);
     752         308 :     PQclear(res);
     753         308 :     return true;
     754             : }
     755             : 
     756             : /*
     757             :  * Drop a replication slot for the given connection. This function
     758             :  * returns true in case of success.
     759             :  */
     760             : bool
     761           4 : DropReplicationSlot(PGconn *conn, const char *slot_name)
     762             : {
     763             :     PQExpBuffer query;
     764             :     PGresult   *res;
     765             : 
     766             :     Assert(slot_name != NULL);
     767             : 
     768           4 :     query = createPQExpBuffer();
     769             : 
     770             :     /* Build query */
     771           4 :     appendPQExpBuffer(query, "DROP_REPLICATION_SLOT \"%s\"",
     772             :                       slot_name);
     773           4 :     res = PQexec(conn, query->data);
     774           4 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
     775             :     {
     776           0 :         pg_log_error("could not send replication command \"%s\": %s",
     777             :                      query->data, PQerrorMessage(conn));
     778             : 
     779           0 :         destroyPQExpBuffer(query);
     780           0 :         PQclear(res);
     781           0 :         return false;
     782             :     }
     783             : 
     784           4 :     if (PQntuples(res) != 0 || PQnfields(res) != 0)
     785             :     {
     786           0 :         pg_log_error("could not drop replication slot \"%s\": got %d rows and %d fields, expected %d rows and %d fields",
     787             :                      slot_name,
     788             :                      PQntuples(res), PQnfields(res), 0, 0);
     789             : 
     790           0 :         destroyPQExpBuffer(query);
     791           0 :         PQclear(res);
     792           0 :         return false;
     793             :     }
     794             : 
     795           4 :     destroyPQExpBuffer(query);
     796           4 :     PQclear(res);
     797           4 :     return true;
     798             : }
     799             : 
     800             : /*
     801             :  * Append a "plain" option - one with no value - to a server command that
     802             :  * is being constructed.
     803             :  *
     804             :  * In the old syntax, all options were parser keywords, so you could just
     805             :  * write things like SOME_COMMAND OPTION1 OPTION2 'opt2value' OPTION3 42. The
     806             :  * new syntax uses a comma-separated list surrounded by parentheses, so the
     807             :  * equivalent is SOME_COMMAND (OPTION1, OPTION2 'optvalue', OPTION3 42).
     808             :  */
     809             : void
     810        2608 : AppendPlainCommandOption(PQExpBuffer buf, bool use_new_option_syntax,
     811             :                          char *option_name)
     812             : {
     813        2608 :     if (buf->len > 0 && buf->data[buf->len - 1] != '(')
     814             :     {
     815        1950 :         if (use_new_option_syntax)
     816        1950 :             appendPQExpBufferStr(buf, ", ");
     817             :         else
     818           0 :             appendPQExpBufferChar(buf, ' ');
     819             :     }
     820             : 
     821        2608 :     appendPQExpBuffer(buf, " %s", option_name);
     822        2608 : }
     823             : 
     824             : /*
     825             :  * Append an option with an associated string value to a server command that
     826             :  * is being constructed.
     827             :  *
     828             :  * See comments for AppendPlainCommandOption, above.
     829             :  */
     830             : void
     831        1538 : AppendStringCommandOption(PQExpBuffer buf, bool use_new_option_syntax,
     832             :                           char *option_name, char *option_value)
     833             : {
     834        1538 :     AppendPlainCommandOption(buf, use_new_option_syntax, option_name);
     835             : 
     836        1538 :     if (option_value != NULL)
     837             :     {
     838        1538 :         size_t      length = strlen(option_value);
     839        1538 :         char       *escaped_value = palloc(1 + 2 * length);
     840             : 
     841        1538 :         PQescapeStringConn(conn, escaped_value, option_value, length, NULL);
     842        1538 :         appendPQExpBuffer(buf, " '%s'", escaped_value);
     843        1538 :         pfree(escaped_value);
     844             :     }
     845        1538 : }
     846             : 
     847             : /*
     848             :  * Append an option with an associated integer value to a server command
     849             :  * is being constructed.
     850             :  *
     851             :  * See comments for AppendPlainCommandOption, above.
     852             :  */
     853             : void
     854         338 : AppendIntegerCommandOption(PQExpBuffer buf, bool use_new_option_syntax,
     855             :                            char *option_name, int32 option_value)
     856             : {
     857         338 :     AppendPlainCommandOption(buf, use_new_option_syntax, option_name);
     858             : 
     859         338 :     appendPQExpBuffer(buf, " %d", option_value);
     860         338 : }
     861             : 
     862             : /*
     863             :  * Frontend version of GetCurrentTimestamp(), since we are not linked with
     864             :  * backend code.
     865             :  */
     866             : TimestampTz
     867        5324 : feGetCurrentTimestamp(void)
     868             : {
     869             :     TimestampTz result;
     870             :     struct timeval tp;
     871             : 
     872        5324 :     gettimeofday(&tp, NULL);
     873             : 
     874        5324 :     result = (TimestampTz) tp.tv_sec -
     875             :         ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
     876        5324 :     result = (result * USECS_PER_SEC) + tp.tv_usec;
     877             : 
     878        5324 :     return result;
     879             : }
     880             : 
     881             : /*
     882             :  * Frontend version of TimestampDifference(), since we are not linked with
     883             :  * backend code.
     884             :  */
     885             : void
     886        4312 : feTimestampDifference(TimestampTz start_time, TimestampTz stop_time,
     887             :                       long *secs, int *microsecs)
     888             : {
     889        4312 :     TimestampTz diff = stop_time - start_time;
     890             : 
     891        4312 :     if (diff <= 0)
     892             :     {
     893           0 :         *secs = 0;
     894           0 :         *microsecs = 0;
     895             :     }
     896             :     else
     897             :     {
     898        4312 :         *secs = (long) (diff / USECS_PER_SEC);
     899        4312 :         *microsecs = (int) (diff % USECS_PER_SEC);
     900             :     }
     901        4312 : }
     902             : 
     903             : /*
     904             :  * Frontend version of TimestampDifferenceExceeds(), since we are not
     905             :  * linked with backend code.
     906             :  */
     907             : bool
     908        6574 : feTimestampDifferenceExceeds(TimestampTz start_time,
     909             :                              TimestampTz stop_time,
     910             :                              int msec)
     911             : {
     912        6574 :     TimestampTz diff = stop_time - start_time;
     913             : 
     914        6574 :     return (diff >= msec * INT64CONST(1000));
     915             : }
     916             : 
     917             : /*
     918             :  * Converts an int64 to network byte order.
     919             :  */
     920             : void
     921        1288 : fe_sendint64(int64 i, char *buf)
     922             : {
     923        1288 :     uint64      n64 = pg_hton64(i);
     924             : 
     925        1288 :     memcpy(buf, &n64, sizeof(n64));
     926        1288 : }
     927             : 
     928             : /*
     929             :  * Converts an int64 from network byte order to native format.
     930             :  */
     931             : int64
     932        7990 : fe_recvint64(char *buf)
     933             : {
     934             :     uint64      n64;
     935             : 
     936        7990 :     memcpy(&n64, buf, sizeof(n64));
     937             : 
     938        7990 :     return pg_ntoh64(n64);
     939             : }

Generated by: LCOV version 1.14