LCOV - differential code coverage report
Current view: top level - src/bin/pg_dump - pg_backup_db.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: ba12a202ce1b5581dc0ed149cf3f637d7897ad5d vs 2866d8c7dbfc9d882a7d80fef93fbbe763709932 Lines: 66.4 % 229 152 6 71 3 149 4 3
Current Date: 2026-08-27 14:31:44 +0300 Functions: 89.5 % 19 17 2 4 13
Baseline: lcov-20260827-baseline Branches: 45.5 % 123 56 67 56
Baseline Date: 2026-08-27 14:31:58 +0300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 33.3 % 9 3 6 3
(360..) days: 67.7 % 220 149 71 149
Function coverage date bins:
(360..) days: 89.5 % 19 17 2 4 13
Branch coverage date bins:
(360..) days: 45.5 % 123 56 67 56

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pg_backup_db.c
                                  4                 :                :  *
                                  5                 :                :  *  Implements the basic DB functions used by the archiver.
                                  6                 :                :  *
                                  7                 :                :  * IDENTIFICATION
                                  8                 :                :  *    src/bin/pg_dump/pg_backup_db.c
                                  9                 :                :  *
                                 10                 :                :  *-------------------------------------------------------------------------
                                 11                 :                :  */
                                 12                 :                : #include "postgres_fe.h"
                                 13                 :                : 
                                 14                 :                : #include <unistd.h>
                                 15                 :                : #include <ctype.h>
                                 16                 :                : #ifdef HAVE_TERMIOS_H
                                 17                 :                : #include <termios.h>
                                 18                 :                : #endif
                                 19                 :                : 
                                 20                 :                : #include "common/connect.h"
                                 21                 :                : #include "common/string.h"
                                 22                 :                : #include "connectdb.h"
                                 23                 :                : #include "parallel.h"
                                 24                 :                : #include "pg_backup_archiver.h"
                                 25                 :                : #include "pg_backup_db.h"
                                 26                 :                : #include "pg_backup_utils.h"
                                 27                 :                : 
                                 28                 :                : static void _check_database_version(ArchiveHandle *AH);
                                 29                 :                : static void notice_processor(void *arg, const char *message);
                                 30                 :                : 
                                 31                 :                : static void
 6710 tgl@sss.pgh.pa.us          32                 :CBC         341 : _check_database_version(ArchiveHandle *AH)
                                 33                 :                : {
                                 34                 :                :     const char *remoteversion_str;
                                 35                 :                :     int         remoteversion;
                                 36                 :                :     PGresult   *res;
                                 37                 :                : 
 8467                            38                 :            341 :     remoteversion_str = PQparameterStatus(AH->connection, "server_version");
 4902 heikki.linnakangas@i       39                 :            341 :     remoteversion = PQserverVersion(AH->connection);
                                 40   [ +  -  -  + ]:            341 :     if (remoteversion == 0 || !remoteversion_str)
  722 michael@paquier.xyz        41                 :UBC           0 :         pg_fatal("could not get \"server_version\" from libpq");
                                 42                 :                : 
 5389 bruce@momjian.us           43                 :CBC         341 :     AH->public.remoteVersionStr = pg_strdup(remoteversion_str);
 9255 pjw@rhyme.com.au           44                 :            341 :     AH->public.remoteVersion = remoteversion;
 6028 tgl@sss.pgh.pa.us          45         [ +  + ]:            341 :     if (!AH->archiveRemoteVersion)
                                 46                 :            210 :         AH->archiveRemoteVersion = AH->public.remoteVersionStr;
                                 47                 :                : 
 4902 heikki.linnakangas@i       48         [ -  + ]:            341 :     if (remoteversion != PG_VERSION_NUM
 8467 tgl@sss.pgh.pa.us          49         [ #  # ]:UBC           0 :         && (remoteversion < AH->public.minRemoteVersion ||
                                 50         [ #  # ]:              0 :             remoteversion > AH->public.maxRemoteVersion))
                                 51                 :                :     {
 1602                            52                 :              0 :         pg_log_error("aborting because of server version mismatch");
                                 53                 :              0 :         pg_log_error_detail("server version: %s; %s version: %s",
                                 54                 :                :                             remoteversion_str, progname, PG_VERSION);
                                 55                 :              0 :         exit(1);
                                 56                 :                :     }
                                 57                 :                : 
                                 58                 :                :     /*
                                 59                 :                :      * Check if server is in recovery mode, which means we are on a hot
                                 60                 :                :      * standby.
                                 61                 :                :      */
 1715 tgl@sss.pgh.pa.us          62                 :CBC         341 :     res = ExecuteSqlQueryForSingleRow((Archive *) AH,
                                 63                 :                :                                       "SELECT pg_catalog.pg_is_in_recovery()");
                                 64                 :            341 :     AH->public.isStandby = (strcmp(PQgetvalue(res, 0, 0), "t") == 0);
                                 65                 :            341 :     PQclear(res);
 9533 pjw@rhyme.com.au           66                 :            341 : }
                                 67                 :                : 
                                 68                 :                : /*
                                 69                 :                :  * Reconnect to the server.  If dbname is not NULL, use that database,
                                 70                 :                :  * else the one associated with the archive handle.
                                 71                 :                :  */
                                 72                 :                : void
 2163 tgl@sss.pgh.pa.us          73                 :             65 : ReconnectToServer(ArchiveHandle *AH, const char *dbname)
                                 74                 :                : {
                                 75                 :             65 :     PGconn     *oldConn = AH->connection;
                                 76                 :             65 :     RestoreOptions *ropt = AH->public.ropt;
                                 77                 :                : 
                                 78                 :                :     /*
                                 79                 :                :      * Save the dbname, if given, in override_dbname so that it will also
                                 80                 :                :      * affect any later reconnection attempt.
                                 81                 :                :      */
                                 82         [ +  - ]:             65 :     if (dbname)
                                 83                 :             65 :         ropt->cparams.override_dbname = pg_strdup(dbname);
                                 84                 :                : 
                                 85                 :                :     /*
                                 86                 :                :      * Note: we want to establish the new connection, and in particular update
                                 87                 :                :      * ArchiveHandle's connCancel, before closing old connection.  Otherwise
                                 88                 :                :      * an ill-timed SIGINT could try to access a dead connection.
                                 89                 :                :      */
  510 andrew@dunslane.net        90                 :             65 :     AH->connection = NULL;       /* dodge error check in ConnectDatabaseAhx */
                                 91                 :                : 
                                 92                 :             65 :     ConnectDatabaseAhx((Archive *) AH, &ropt->cparams, true);
                                 93                 :                : 
 2163 tgl@sss.pgh.pa.us          94                 :             65 :     PQfinish(oldConn);
 9530 pjw@rhyme.com.au           95                 :             65 : }
                                 96                 :                : 
                                 97                 :                : /*
                                 98                 :                :  * Make, or remake, a database connection with the given parameters.
                                 99                 :                :  *
                                100                 :                :  * The resulting connection handle is stored in AHX->connection.
                                101                 :                :  *
                                102                 :                :  * An interactive password prompt is automatically issued if required.
                                103                 :                :  * We store the results of that in AHX->savedPassword.
                                104                 :                :  * Note: it's not really all that sensible to use a single-entry password
                                105                 :                :  * cache if the username keeps changing.  In current usage, however, the
                                106                 :                :  * username never does change, so one savedPassword is sufficient.
                                107                 :                :  */
                                108                 :                : void
  510 andrew@dunslane.net       109                 :            343 : ConnectDatabaseAhx(Archive *AHX,
                                110                 :                :                    const ConnParams *cparams,
                                111                 :                :                    bool isReconnect)
                                112                 :                : {
 9289 bruce@momjian.us          113                 :            343 :     ArchiveHandle *AH = (ArchiveHandle *) AHX;
                                114                 :                :     trivalue    prompt_password;
                                115                 :                :     char       *password;
                                116                 :                : 
 9533 pjw@rhyme.com.au          117         [ -  + ]:            343 :     if (AH->connection)
 1602 tgl@sss.pgh.pa.us         118                 :UBC           0 :         pg_fatal("already connected to a database");
                                119                 :                : 
                                120                 :                :     /* Never prompt for a password during a reconnection */
 2163 tgl@sss.pgh.pa.us         121         [ +  + ]:CBC         343 :     prompt_password = isReconnect ? TRI_NO : cparams->promptPassword;
                                122                 :                : 
 3649                           123                 :            343 :     password = AH->savedPassword;
                                124                 :                : 
 6391 peter_e@gmx.net           125   [ -  +  -  - ]:            343 :     if (prompt_password == TRI_YES && password == NULL)
 2184 tgl@sss.pgh.pa.us         126                 :UBC           0 :         password = simple_prompt("Password: ", false);
                                127                 :                : 
  510 andrew@dunslane.net       128                 :CBC         684 :     AH->connection = ConnectDatabase(cparams->dbname, NULL, cparams->pghost,
                                129                 :            343 :                                      cparams->pgport, cparams->username,
                                130                 :                :                                      prompt_password, true,
                                131                 :            343 :                                      progname, NULL, NULL, password, cparams->override_dbname);
                                132                 :                : 
                                133                 :                :     /* Start strict; later phases may override this. */
 3104 noah@leadboat.com         134                 :            341 :     PQclear(ExecuteSqlQueryForSingleRow((Archive *) AH,
                                135                 :                :                                         ALWAYS_SECURE_SEARCH_PATH_SQL));
                                136                 :                : 
 2184 tgl@sss.pgh.pa.us         137   [ -  +  -  - ]:            341 :     if (password && password != AH->savedPassword)
 2184 tgl@sss.pgh.pa.us         138                 :UBC           0 :         free(password);
                                139                 :                : 
                                140                 :                :     /*
                                141                 :                :      * We want to remember connection's actual password, whether or not we got
                                142                 :                :      * it by prompting.  So we don't just store the password variable.
                                143                 :                :      */
 3900 tgl@sss.pgh.pa.us         144         [ -  + ]:CBC         341 :     if (PQconnectionUsedPassword(AH->connection))
                                145                 :                :     {
   57 peter@eisentraut.org      146                 :UNC           0 :         pg_free(AH->savedPassword);
 3900 tgl@sss.pgh.pa.us         147                 :UBC           0 :         AH->savedPassword = pg_strdup(PQpass(AH->connection));
                                148                 :                :     }
                                149                 :                : 
                                150                 :                :     /* check for version mismatch */
 6710 tgl@sss.pgh.pa.us         151                 :CBC         341 :     _check_database_version(AH);
                                152                 :                : 
 9146 peter_e@gmx.net           153                 :            341 :     PQsetNoticeProcessor(AH->connection, notice_processor, NULL);
                                154                 :                : 
                                155                 :                :     /* arrange for SIGINT to issue a query cancel on this connection */
 3738 tgl@sss.pgh.pa.us         156                 :            341 :     set_archive_cancel_info(AH, AH->connection);
 9533 pjw@rhyme.com.au          157                 :            341 : }
                                158                 :                : 
                                159                 :                : /*
                                160                 :                :  * Close the connection to the database and also cancel off the query if we
                                161                 :                :  * have one running.
                                162                 :                :  */
                                163                 :                : void
 5306 rhaas@postgresql.org      164                 :            278 : DisconnectDatabase(Archive *AHX)
                                165                 :                : {
                                166                 :            278 :     ArchiveHandle *AH = (ArchiveHandle *) AHX;
                                167                 :                :     char        errbuf[1];
                                168                 :                : 
 4904 andrew@dunslane.net       169         [ +  + ]:            278 :     if (!AH->connection)
                                170                 :              2 :         return;
                                171                 :                : 
 3738 tgl@sss.pgh.pa.us         172         [ +  - ]:            276 :     if (AH->connCancel)
                                173                 :                :     {
                                174                 :                :         /*
                                175                 :                :          * If we have an active query, send a cancel before closing, ignoring
                                176                 :                :          * any errors.  This is of no use for a normal exit, but might be
                                177                 :                :          * helpful during pg_fatal().
                                178                 :                :          */
                                179         [ -  + ]:            276 :         if (PQtransactionStatus(AH->connection) == PQTRANS_ACTIVE)
 3397 alvherre@alvh.no-ip.      180                 :UBC           0 :             (void) PQcancel(AH->connCancel, errbuf, sizeof(errbuf));
                                181                 :                : 
                                182                 :                :         /*
                                183                 :                :          * Prevent signal handler from sending a cancel after this.
                                184                 :                :          */
 3738 tgl@sss.pgh.pa.us         185                 :CBC         276 :         set_archive_cancel_info(AH, NULL);
                                186                 :                :     }
                                187                 :                : 
 4904 andrew@dunslane.net       188                 :            276 :     PQfinish(AH->connection);
 5306 rhaas@postgresql.org      189                 :            276 :     AH->connection = NULL;
                                190                 :                : }
                                191                 :                : 
                                192                 :                : PGconn *
                                193                 :           5003 : GetConnection(Archive *AHX)
                                194                 :                : {
                                195                 :           5003 :     ArchiveHandle *AH = (ArchiveHandle *) AHX;
                                196                 :                : 
                                197                 :           5003 :     return AH->connection;
                                198                 :                : }
                                199                 :                : 
                                200                 :                : static void
 9072 bruce@momjian.us          201                 :             10 : notice_processor(void *arg, const char *message)
                                202                 :                : {
 1602 tgl@sss.pgh.pa.us         203                 :             10 :     pg_log_info("%s", message);
 9146 peter_e@gmx.net           204                 :             10 : }
                                205                 :                : 
                                206                 :                : /* Like pg_fatal(), but with a complaint about a particular query. */
                                207                 :                : static void
 2705 peter@eisentraut.org      208                 :              2 : die_on_query_failure(ArchiveHandle *AH, const char *query)
                                209                 :                : {
                                210                 :                :     if (!is_cancel_in_progress())
                                211                 :                :     {
   50 heikki.linnakangas@i      212                 :GNC           2 :         pg_log_error("query failed: %s",
                                213                 :                :                      PQerrorMessage(AH->connection));
                                214                 :              2 :         pg_log_error_detail("Query was: %s", query);
                                215                 :                :     }
                                216                 :                : 
                                217                 :              2 :     exit_nicely(1);
                                218                 :                : }
                                219                 :                : 
                                220                 :                : void
 5315 rhaas@postgresql.org      221                 :CBC        3858 : ExecuteSqlStatement(Archive *AHX, const char *query)
                                222                 :                : {
 5191 bruce@momjian.us          223                 :           3858 :     ArchiveHandle *AH = (ArchiveHandle *) AHX;
                                224                 :                :     PGresult   *res;
                                225                 :                : 
 5315 rhaas@postgresql.org      226                 :           3858 :     res = PQexec(AH->connection, query);
                                227         [ +  + ]:           3858 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
 2705 peter@eisentraut.org      228                 :              1 :         die_on_query_failure(AH, query);
 5315 rhaas@postgresql.org      229                 :           3857 :     PQclear(res);
                                230                 :           3857 : }
                                231                 :                : 
                                232                 :                : PGresult *
                                233                 :          33821 : ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
                                234                 :                : {
 5191 bruce@momjian.us          235                 :          33821 :     ArchiveHandle *AH = (ArchiveHandle *) AHX;
                                236                 :                :     PGresult   *res;
                                237                 :                : 
 5315 rhaas@postgresql.org      238                 :          33821 :     res = PQexec(AH->connection, query);
                                239         [ +  + ]:          33821 :     if (PQresultStatus(res) != status)
 2705 peter@eisentraut.org      240                 :              1 :         die_on_query_failure(AH, query);
 5315 rhaas@postgresql.org      241                 :          33820 :     return res;
                                242                 :                : }
                                243                 :                : 
                                244                 :                : /*
                                245                 :                :  * Execute an SQL query and verify that we got exactly one row back.
                                246                 :                :  */
                                247                 :                : PGresult *
 3222 peter_e@gmx.net           248                 :          14095 : ExecuteSqlQueryForSingleRow(Archive *fout, const char *query)
                                249                 :                : {
                                250                 :                :     PGresult   *res;
                                251                 :                :     int         ntups;
                                252                 :                : 
 3745 magnus@hagander.net       253                 :          14095 :     res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
                                254                 :                : 
                                255                 :                :     /* Expecting a single result only */
                                256                 :          14095 :     ntups = PQntuples(res);
                                257         [ -  + ]:          14095 :     if (ntups != 1)
 1602 tgl@sss.pgh.pa.us         258                 :UBC           0 :         pg_fatal(ngettext("query returned %d row instead of one: %s",
                                259                 :                :                           "query returned %d rows instead of one: %s",
                                260                 :                :                           ntups),
                                261                 :                :                  ntups, query);
                                262                 :                : 
 3745 magnus@hagander.net       263                 :CBC       14095 :     return res;
                                264                 :                : }
                                265                 :                : 
                                266                 :                : /*
                                267                 :                :  * Convenience function to send a query.
                                268                 :                :  * Monitors result to detect COPY statements
                                269                 :                :  */
                                270                 :                : static void
 6585 tgl@sss.pgh.pa.us         271                 :          10245 : ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
                                272                 :                : {
 7737                           273                 :          10245 :     PGconn     *conn = AH->connection;
                                274                 :                :     PGresult   *res;
                                275                 :                : 
                                276                 :                : #ifdef NOT_USED
                                277                 :                :     fprintf(stderr, "Executing: '%s'\n\n", qry);
                                278                 :                : #endif
 6585                           279                 :          10245 :     res = PQexec(conn, qry);
                                280                 :                : 
                                281      [ +  +  - ]:          10245 :     switch (PQresultStatus(res))
                                282                 :                :     {
                                283                 :          10204 :         case PGRES_COMMAND_OK:
                                284                 :                :         case PGRES_TUPLES_OK:
                                285                 :                :         case PGRES_EMPTY_QUERY:
                                286                 :                :             /* A-OK */
                                287                 :          10204 :             break;
                                288                 :             41 :         case PGRES_COPY_IN:
                                289                 :                :             /* Assume this is an expected result */
 7508                           290                 :             41 :             AH->pgCopyIn = true;
 6585                           291                 :             41 :             break;
 6585 tgl@sss.pgh.pa.us         292                 :UBC           0 :         default:
                                293                 :                :             /* trouble */
 2705 peter@eisentraut.org      294                 :              0 :             warn_or_exit_horribly(AH, "%s: %sCommand was: %s",
                                295                 :                :                                   desc, PQerrorMessage(conn), qry);
 6585 tgl@sss.pgh.pa.us         296                 :              0 :             break;
                                297                 :                :     }
                                298                 :                : 
 9533 pjw@rhyme.com.au          299                 :CBC       10245 :     PQclear(res);
                                300                 :          10245 : }
                                301                 :                : 
                                302                 :                : 
                                303                 :                : /*
                                304                 :                :  * Process non-COPY table data (that is, INSERT commands).
                                305                 :                :  *
                                306                 :                :  * The commands have been run together as one long string for compressibility,
                                307                 :                :  * and we are receiving them in bufferloads with arbitrary boundaries, so we
                                308                 :                :  * have to locate command boundaries and save partial commands across calls.
                                309                 :                :  * All state must be kept in AH->sqlparse, not in local variables of this
                                310                 :                :  * routine.  We assume that AH->sqlparse was filled with zeroes when created.
                                311                 :                :  *
                                312                 :                :  * We have to lex the data to the extent of identifying literals and quoted
                                313                 :                :  * identifiers, so that we can recognize statement-terminating semicolons.
                                314                 :                :  * We assume that INSERT data will not contain SQL comments, E'' literals,
                                315                 :                :  * or dollar-quoted strings, so this is much simpler than a full SQL lexer.
                                316                 :                :  *
                                317                 :                :  * Note: when restoring from a pre-9.0 dump file, this code is also used to
                                318                 :                :  * process BLOB COMMENTS data, which has the same problem of containing
                                319                 :                :  * multiple SQL commands that might be split across bufferloads.  Fortunately,
                                320                 :                :  * that data won't contain anything complicated to lex either.
                                321                 :                :  */
                                322                 :                : static void
 4459 tgl@sss.pgh.pa.us         323                 :              7 : ExecuteSimpleCommands(ArchiveHandle *AH, const char *buf, size_t bufLen)
                                324                 :                : {
 5347                           325                 :              7 :     const char *qry = buf;
                                326                 :              7 :     const char *eos = buf + bufLen;
                                327                 :                : 
                                328                 :                :     /* initialize command buffer if first time through */
                                329         [ +  + ]:              7 :     if (AH->sqlparse.curCmd == NULL)
                                330                 :              3 :         AH->sqlparse.curCmd = createPQExpBuffer();
                                331                 :                : 
                                332         [ +  + ]:         129700 :     for (; qry < eos; qry++)
                                333                 :                :     {
 5191 bruce@momjian.us          334                 :         129693 :         char        ch = *qry;
                                335                 :                : 
                                336                 :                :         /* For neatness, we skip any newlines between commands */
 5347 tgl@sss.pgh.pa.us         337   [ +  +  -  + ]:         129693 :         if (!(ch == '\n' && AH->sqlparse.curCmd->len == 0))
                                338                 :         126679 :             appendPQExpBufferChar(AH->sqlparse.curCmd, ch);
                                339                 :                : 
                                340   [ +  +  -  - ]:         129693 :         switch (AH->sqlparse.state)
                                341                 :                :         {
                                342                 :         125693 :             case SQL_SCAN:      /* Default state == 0, set in _allocAH */
                                343         [ +  + ]:         125693 :                 if (ch == ';')
                                344                 :                :                 {
                                345                 :                :                     /*
                                346                 :                :                      * We've found the end of a statement. Send it and reset
                                347                 :                :                      * the buffer.
                                348                 :                :                      */
                                349                 :           3000 :                     ExecuteSqlCommand(AH, AH->sqlparse.curCmd->data,
                                350                 :                :                                       "could not execute query");
                                351                 :           3000 :                     resetPQExpBuffer(AH->sqlparse.curCmd);
                                352                 :                :                 }
                                353         [ +  + ]:         122693 :                 else if (ch == '\'')
                                354                 :                :                 {
                                355                 :           2000 :                     AH->sqlparse.state = SQL_IN_SINGLE_QUOTE;
                                356                 :           2000 :                     AH->sqlparse.backSlash = false;
                                357                 :                :                 }
                                358         [ -  + ]:         120693 :                 else if (ch == '"')
                                359                 :                :                 {
 5347 tgl@sss.pgh.pa.us         360                 :UBC           0 :                     AH->sqlparse.state = SQL_IN_DOUBLE_QUOTE;
                                361                 :                :                 }
 5347 tgl@sss.pgh.pa.us         362                 :CBC      125693 :                 break;
                                363                 :                : 
                                364                 :           4000 :             case SQL_IN_SINGLE_QUOTE:
                                365                 :                :                 /* We needn't handle '' specially */
                                366   [ +  +  +  - ]:           4000 :                 if (ch == '\'' && !AH->sqlparse.backSlash)
                                367                 :           2000 :                     AH->sqlparse.state = SQL_SCAN;
                                368   [ -  +  -  - ]:           2000 :                 else if (ch == '\\' && !AH->public.std_strings)
 5347 tgl@sss.pgh.pa.us         369                 :UBC           0 :                     AH->sqlparse.backSlash = !AH->sqlparse.backSlash;
                                370                 :                :                 else
 5347 tgl@sss.pgh.pa.us         371                 :CBC        2000 :                     AH->sqlparse.backSlash = false;
                                372                 :           4000 :                 break;
                                373                 :                : 
 5347 tgl@sss.pgh.pa.us         374                 :UBC           0 :             case SQL_IN_DOUBLE_QUOTE:
                                375                 :                :                 /* We needn't handle "" specially */
                                376         [ #  # ]:              0 :                 if (ch == '"')
                                377                 :              0 :                     AH->sqlparse.state = SQL_SCAN;
                                378                 :              0 :                 break;
                                379                 :                :         }
                                380                 :                :     }
 5347 tgl@sss.pgh.pa.us         381                 :CBC           7 : }
                                382                 :                : 
                                383                 :                : 
                                384                 :                : /*
                                385                 :                :  * Implement ahwrite() for direct-to-DB restore
                                386                 :                :  */
                                387                 :                : int
 4335 alvherre@alvh.no-ip.      388                 :           6979 : ExecuteSqlCommandBuf(Archive *AHX, const char *buf, size_t bufLen)
                                389                 :                : {
                                390                 :           6979 :     ArchiveHandle *AH = (ArchiveHandle *) AHX;
                                391                 :                : 
 5347 tgl@sss.pgh.pa.us         392         [ +  + ]:           6979 :     if (AH->outputKind == OUTPUT_COPYDATA)
                                393                 :                :     {
                                394                 :                :         /*
                                395                 :                :          * COPY data.
                                396                 :                :          *
                                397                 :                :          * We drop the data on the floor if libpq has failed to enter COPY
                                398                 :                :          * mode; this allows us to behave reasonably when trying to continue
                                399                 :                :          * after an error in a COPY command.
                                400                 :                :          */
 5509                           401   [ +  -  -  + ]:             82 :         if (AH->pgCopyIn &&
                                402                 :             41 :             PQputCopyData(AH->connection, buf, bufLen) <= 0)
                                403                 :                :         {
                                404                 :                :             /* Stay quiet if this is a result of our own cancellation. */
                                405                 :                :             if (!is_cancel_in_progress())
   50 heikki.linnakangas@i      406                 :UNC           0 :                 pg_log_error("error returned by PQputCopyData: %s",
                                407                 :                :                              PQerrorMessage(AH->connection));
                                408                 :              0 :             exit_nicely(1);
                                409                 :                :         }
                                410                 :                :     }
 5347 tgl@sss.pgh.pa.us         411         [ +  + ]:CBC        6938 :     else if (AH->outputKind == OUTPUT_OTHERDATA)
                                412                 :                :     {
                                413                 :                :         /*
                                414                 :                :          * Table data expressed as INSERT commands; or, in old dump files,
                                415                 :                :          * BLOB COMMENTS data (which is expressed as COMMENT ON commands).
                                416                 :                :          */
 4459                           417                 :              7 :         ExecuteSimpleCommands(AH, buf, bufLen);
                                418                 :                :     }
                                419                 :                :     else
                                420                 :                :     {
                                421                 :                :         /*
                                422                 :                :          * General SQL commands; we assume that commands will not be split
                                423                 :                :          * across calls.
                                424                 :                :          *
                                425                 :                :          * In most cases the data passed to us will be a null-terminated
                                426                 :                :          * string, but if it's not, we have to add a trailing null.
                                427                 :                :          */
 5509                           428         [ +  - ]:           6931 :         if (buf[bufLen] == '\0')
                                429                 :           6931 :             ExecuteSqlCommand(AH, buf, "could not execute query");
                                430                 :                :         else
                                431                 :                :         {
 5191 bruce@momjian.us          432                 :UBC           0 :             char       *str = (char *) pg_malloc(bufLen + 1);
                                433                 :                : 
 5509 tgl@sss.pgh.pa.us         434                 :              0 :             memcpy(str, buf, bufLen);
                                435                 :              0 :             str[bufLen] = '\0';
                                436                 :              0 :             ExecuteSqlCommand(AH, str, "could not execute query");
   57 peter@eisentraut.org      437                 :UNC           0 :             pg_free(str);
                                438                 :                :         }
                                439                 :                :     }
                                440                 :                : 
 4497 bruce@momjian.us          441                 :CBC        6979 :     return bufLen;
                                442                 :                : }
                                443                 :                : 
                                444                 :                : /*
                                445                 :                :  * Terminate a COPY operation during direct-to-DB restore
                                446                 :                :  */
                                447                 :                : void
 4335 alvherre@alvh.no-ip.      448                 :             41 : EndDBCopyMode(Archive *AHX, const char *tocEntryTag)
                                449                 :                : {
                                450                 :             41 :     ArchiveHandle *AH = (ArchiveHandle *) AHX;
                                451                 :                : 
 5509 tgl@sss.pgh.pa.us         452         [ +  - ]:             41 :     if (AH->pgCopyIn)
                                453                 :                :     {
                                454                 :                :         PGresult   *res;
                                455                 :                : 
 7482                           456         [ -  + ]:             41 :         if (PQputCopyEnd(AH->connection, NULL) <= 0)
                                457                 :                :         {
                                458                 :                :             /* Stay quiet if this is a result of our own cancellation. */
                                459                 :                :             if (!is_cancel_in_progress())
   50 heikki.linnakangas@i      460                 :UNC           0 :                 pg_log_error("error returned by PQputCopyEnd: %s",
                                461                 :                :                              PQerrorMessage(AH->connection));
                                462                 :              0 :             exit_nicely(1);
                                463                 :                :         }
                                464                 :                : 
                                465                 :                :         /* Check command status and return to normal libpq state */
 7482 tgl@sss.pgh.pa.us         466                 :CBC          41 :         res = PQgetResult(AH->connection);
                                467         [ -  + ]:             41 :         if (PQresultStatus(res) != PGRES_COMMAND_OK)
 2705 peter@eisentraut.org      468                 :UBC           0 :             warn_or_exit_horribly(AH, "COPY failed for table \"%s\": %s",
 3354 tgl@sss.pgh.pa.us         469                 :              0 :                                   tocEntryTag, PQerrorMessage(AH->connection));
 7482 tgl@sss.pgh.pa.us         470                 :CBC          41 :         PQclear(res);
                                471                 :                : 
                                472                 :                :         /* Do this to ensure we've pumped libpq back to idle state */
 3738                           473         [ -  + ]:             41 :         if (PQgetResult(AH->connection) != NULL)
 2705 peter@eisentraut.org      474                 :UBC           0 :             pg_log_warning("unexpected extra results during COPY of table \"%s\"",
                                475                 :                :                            tocEntryTag);
                                476                 :                : 
 7508 tgl@sss.pgh.pa.us         477                 :CBC          41 :         AH->pgCopyIn = false;
                                478                 :                :     }
 9533 pjw@rhyme.com.au          479                 :             41 : }
                                480                 :                : 
                                481                 :                : void
 4335 alvherre@alvh.no-ip.      482                 :            157 : StartTransaction(Archive *AHX)
                                483                 :                : {
                                484                 :            157 :     ArchiveHandle *AH = (ArchiveHandle *) AHX;
                                485                 :                : 
 6585 tgl@sss.pgh.pa.us         486                 :            157 :     ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
 9533 pjw@rhyme.com.au          487                 :            157 : }
                                488                 :                : 
                                489                 :                : void
 4335 alvherre@alvh.no-ip.      490                 :            157 : CommitTransaction(Archive *AHX)
                                491                 :                : {
                                492                 :            157 :     ArchiveHandle *AH = (ArchiveHandle *) AHX;
                                493                 :                : 
 6585 tgl@sss.pgh.pa.us         494                 :            157 :     ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
 9431 pjw@rhyme.com.au          495                 :            157 : }
                                496                 :                : 
                                497                 :                : /*
                                498                 :                :  * Issue per-blob commands for the large object(s) listed in the TocEntry
                                499                 :                :  *
                                500                 :                :  * The TocEntry's defn string is assumed to consist of large object OIDs,
                                501                 :                :  * one per line.  Wrap these in the given SQL command fragments and issue
                                502                 :                :  * the commands.  (cmdEnd need not include a semicolon.)
                                503                 :                :  */
                                504                 :                : void
  878 tgl@sss.pgh.pa.us         505                 :            150 : IssueCommandPerBlob(ArchiveHandle *AH, TocEntry *te,
                                506                 :                :                     const char *cmdBegin, const char *cmdEnd)
                                507                 :                : {
                                508                 :                :     /* Make a writable copy of the command string */
                                509                 :            150 :     char       *buf = pg_strdup(te->defn);
                                510                 :            150 :     RestoreOptions *ropt = AH->public.ropt;
                                511                 :                :     char       *st;
                                512                 :                :     char       *en;
                                513                 :                : 
                                514                 :            150 :     st = buf;
                                515         [ +  + ]:            316 :     while ((en = strchr(st, '\n')) != NULL)
                                516                 :                :     {
                                517                 :            166 :         *en++ = '\0';
                                518                 :            166 :         ahprintf(AH, "%s%s%s;\n", cmdBegin, st, cmdEnd);
                                519                 :                : 
                                520                 :                :         /* In --transaction-size mode, count each command as an action */
                                521   [ +  -  -  + ]:            166 :         if (ropt && ropt->txn_size > 0)
                                522                 :                :         {
  878 tgl@sss.pgh.pa.us         523         [ #  # ]:UBC           0 :             if (++AH->txnCount >= ropt->txn_size)
                                524                 :                :             {
                                525         [ #  # ]:              0 :                 if (AH->connection)
                                526                 :                :                 {
                                527                 :              0 :                     CommitTransaction(&AH->public);
                                528                 :              0 :                     StartTransaction(&AH->public);
                                529                 :                :                 }
                                530                 :                :                 else
                                531                 :              0 :                     ahprintf(AH, "COMMIT;\nBEGIN;\n\n");
                                532                 :              0 :                 AH->txnCount = 0;
                                533                 :                :             }
                                534                 :                :         }
                                535                 :                : 
  878 tgl@sss.pgh.pa.us         536                 :CBC         166 :         st = en;
                                537                 :                :     }
                                538                 :            150 :     ahprintf(AH, "\n");
                                539                 :            150 :     pg_free(buf);
                                540                 :            150 : }
                                541                 :                : 
                                542                 :                : /*
                                543                 :                :  * Process a "LARGE OBJECTS" ACL TocEntry.
                                544                 :                :  *
                                545                 :                :  * To save space in the dump file, the TocEntry contains only one copy
                                546                 :                :  * of the required GRANT/REVOKE commands, written to apply to the first
                                547                 :                :  * blob in the group (although we do not depend on that detail here).
                                548                 :                :  * We must expand the text to generate commands for all the blobs listed
                                549                 :                :  * in the associated BLOB METADATA entry.
                                550                 :                :  */
                                551                 :                : void
  878 tgl@sss.pgh.pa.us         552                 :UBC           0 : IssueACLPerBlob(ArchiveHandle *AH, TocEntry *te)
                                553                 :                : {
                                554                 :              0 :     TocEntry   *blobte = getTocEntryByDumpId(AH, te->dependencies[0]);
                                555                 :                :     char       *buf;
                                556                 :                :     char       *st;
                                557                 :                :     char       *st2;
                                558                 :                :     char       *en;
                                559                 :                :     bool        inquotes;
                                560                 :                : 
                                561         [ #  # ]:              0 :     if (!blobte)
                                562                 :              0 :         pg_fatal("could not find entry for ID %d", te->dependencies[0]);
                                563         [ #  # ]:              0 :     Assert(strcmp(blobte->desc, "BLOB METADATA") == 0);
                                564                 :                : 
                                565                 :                :     /* Make a writable copy of the ACL commands string */
                                566                 :              0 :     buf = pg_strdup(te->defn);
                                567                 :                : 
                                568                 :                :     /*
                                569                 :                :      * We have to parse out the commands sufficiently to locate the blob OIDs
                                570                 :                :      * and find the command-ending semicolons.  The commands should not
                                571                 :                :      * contain anything hard to parse except for double-quoted role names,
                                572                 :                :      * which are easy to ignore.  Once we've split apart the first and second
                                573                 :                :      * halves of a command, apply IssueCommandPerBlob.  (This means the
                                574                 :                :      * updates on the blobs are interleaved if there's multiple commands, but
                                575                 :                :      * that should cause no trouble.)
                                576                 :                :      */
                                577                 :              0 :     inquotes = false;
                                578                 :              0 :     st = en = buf;
                                579                 :              0 :     st2 = NULL;
                                580         [ #  # ]:              0 :     while (*en)
                                581                 :                :     {
                                582                 :                :         /* Ignore double-quoted material */
                                583         [ #  # ]:              0 :         if (*en == '"')
                                584                 :              0 :             inquotes = !inquotes;
                                585         [ #  # ]:              0 :         if (inquotes)
                                586                 :                :         {
                                587                 :              0 :             en++;
                                588                 :              0 :             continue;
                                589                 :                :         }
                                590                 :                :         /* If we found "LARGE OBJECT", that's the end of the first half */
                                591         [ #  # ]:              0 :         if (strncmp(en, "LARGE OBJECT ", 13) == 0)
                                592                 :                :         {
                                593                 :                :             /* Terminate the first-half string */
                                594                 :              0 :             en += 13;
                                595         [ #  # ]:              0 :             Assert(isdigit((unsigned char) *en));
                                596                 :              0 :             *en++ = '\0';
                                597                 :                :             /* Skip the rest of the blob OID */
                                598         [ #  # ]:              0 :             while (isdigit((unsigned char) *en))
                                599                 :              0 :                 en++;
                                600                 :                :             /* Second half starts here */
                                601         [ #  # ]:              0 :             Assert(st2 == NULL);
                                602                 :              0 :             st2 = en;
                                603                 :                :         }
                                604                 :                :         /* If we found semicolon, that's the end of the second half */
                                605         [ #  # ]:              0 :         else if (*en == ';')
                                606                 :                :         {
                                607                 :                :             /* Terminate the second-half string */
                                608                 :              0 :             *en++ = '\0';
                                609         [ #  # ]:              0 :             Assert(st2 != NULL);
                                610                 :                :             /* Issue this command for each blob */
                                611                 :              0 :             IssueCommandPerBlob(AH, blobte, st, st2);
                                612                 :                :             /* For neatness, skip whitespace before the next command */
                                613         [ #  # ]:              0 :             while (isspace((unsigned char) *en))
                                614                 :              0 :                 en++;
                                615                 :                :             /* Reset for new command */
                                616                 :              0 :             st = en;
                                617                 :              0 :             st2 = NULL;
                                618                 :                :         }
                                619                 :                :         else
                                620                 :              0 :             en++;
                                621                 :                :     }
                                622                 :              0 :     pg_free(buf);
                                623                 :              0 : }
                                624                 :                : 
                                625                 :                : void
                                626                 :              0 : DropLOIfExists(ArchiveHandle *AH, Oid oid)
                                627                 :                : {
                                628                 :              0 :     ahprintf(AH,
                                629                 :                :              "SELECT pg_catalog.lo_unlink(oid) "
                                630                 :                :              "FROM pg_catalog.pg_largeobject_metadata "
                                631                 :                :              "WHERE oid = '%u';\n",
                                632                 :                :              oid);
 6100 itagaki.takahiro@gma      633                 :              0 : }
        

Generated by: LCOV version 2.0-1