]> granicus.if.org Git - postgresql/blobdiff - src/bin/pg_dump/pg_backup_db.c
Create libpgcommon, and move pg_malloc et al to it
[postgresql] / src / bin / pg_dump / pg_backup_db.c
index a843eacc0c842bdda2c2bdce853f9e37e3283ce6..21c203e8e7b903732eadb69eb75e9b71a571bcca 100644 (file)
@@ -11,7 +11,6 @@
  */
 
 #include "pg_backup_db.h"
-#include "dumpmem.h"
 #include "dumputils.h"
 
 #include <unistd.h>
@@ -23,6 +22,7 @@
 
 #define DB_MAX_ERR_STMT 128
 
+/* translator: this is a module name */
 static const char *modulename = gettext_noop("archiver (db)");
 
 static void _check_database_version(ArchiveHandle *AH);
@@ -30,13 +30,13 @@ static PGconn *_connectDB(ArchiveHandle *AH, const char *newdbname, const char *
 static void notice_processor(void *arg, const char *message);
 
 static int
-_parse_version(ArchiveHandle *AH, const char *versionString)
+_parse_version(const char *versionString)
 {
        int                     v;
 
        v = parse_version(versionString);
        if (v < 0)
-               die_horribly(AH, modulename, "could not parse version string \"%s\"\n", versionString);
+               exit_horribly(modulename, "could not parse version string \"%s\"\n", versionString);
 
        return v;
 }
@@ -48,13 +48,13 @@ _check_database_version(ArchiveHandle *AH)
        const char *remoteversion_str;
        int                     remoteversion;
 
-       myversion = _parse_version(AH, PG_VERSION);
+       myversion = _parse_version(PG_VERSION);
 
        remoteversion_str = PQparameterStatus(AH->connection, "server_version");
        if (!remoteversion_str)
-               die_horribly(AH, modulename, "could not get server_version from libpq\n");
+               exit_horribly(modulename, "could not get server_version from libpq\n");
 
-       remoteversion = _parse_version(AH, remoteversion_str);
+       remoteversion = _parse_version(remoteversion_str);
 
        AH->public.remoteVersionStr = pg_strdup(remoteversion_str);
        AH->public.remoteVersion = remoteversion;
@@ -67,7 +67,7 @@ _check_database_version(ArchiveHandle *AH)
        {
                write_msg(NULL, "server version: %s; %s version: %s\n",
                                  remoteversion_str, progname, PG_VERSION);
-               die_horribly(AH, NULL, "aborting because of server version mismatch\n");
+               exit_horribly(NULL, "aborting because of server version mismatch\n");
        }
 }
 
@@ -145,7 +145,7 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
        {
                password = simple_prompt("Password: ", 100, false);
                if (password == NULL)
-                       die_horribly(AH, modulename, "out of memory\n");
+                       exit_horribly(modulename, "out of memory\n");
        }
 
        do
@@ -176,13 +176,13 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
                free(values);
 
                if (!newConn)
-                       die_horribly(AH, modulename, "failed to reconnect to database\n");
+                       exit_horribly(modulename, "failed to reconnect to database\n");
 
                if (PQstatus(newConn) == CONNECTION_BAD)
                {
                        if (!PQconnectionNeedsPassword(newConn))
-                               die_horribly(AH, modulename, "could not reconnect to database: %s",
-                                                        PQerrorMessage(newConn));
+                               exit_horribly(modulename, "could not reconnect to database: %s",
+                                                         PQerrorMessage(newConn));
                        PQfinish(newConn);
 
                        if (password)
@@ -197,10 +197,10 @@ _connectDB(ArchiveHandle *AH, const char *reqdb, const char *requser)
                        if (AH->promptPassword != TRI_NO)
                                password = simple_prompt("Password: ", 100, false);
                        else
-                               die_horribly(AH, modulename, "connection needs password\n");
+                               exit_horribly(modulename, "connection needs password\n");
 
                        if (password == NULL)
-                               die_horribly(AH, modulename, "out of memory\n");
+                               exit_horribly(modulename, "out of memory\n");
                        new_pass = true;
                }
        } while (new_pass);
@@ -238,13 +238,13 @@ ConnectDatabase(Archive *AHX,
        bool            new_pass;
 
        if (AH->connection)
-               die_horribly(AH, modulename, "already connected to a database\n");
+               exit_horribly(modulename, "already connected to a database\n");
 
        if (prompt_password == TRI_YES && password == NULL)
        {
                password = simple_prompt("Password: ", 100, false);
                if (password == NULL)
-                       die_horribly(AH, modulename, "out of memory\n");
+                       exit_horribly(modulename, "out of memory\n");
        }
        AH->promptPassword = prompt_password;
 
@@ -280,7 +280,7 @@ ConnectDatabase(Archive *AHX,
                free(values);
 
                if (!AH->connection)
-                       die_horribly(AH, modulename, "failed to connect to database\n");
+                       exit_horribly(modulename, "failed to connect to database\n");
 
                if (PQstatus(AH->connection) == CONNECTION_BAD &&
                        PQconnectionNeedsPassword(AH->connection) &&
@@ -290,7 +290,7 @@ ConnectDatabase(Archive *AHX,
                        PQfinish(AH->connection);
                        password = simple_prompt("Password: ", 100, false);
                        if (password == NULL)
-                               die_horribly(AH, modulename, "out of memory\n");
+                               exit_horribly(modulename, "out of memory\n");
                        new_pass = true;
                }
        } while (new_pass);
@@ -299,8 +299,8 @@ ConnectDatabase(Archive *AHX,
 
        /* check to see that the backend connection was successfully made */
        if (PQstatus(AH->connection) == CONNECTION_BAD)
-               die_horribly(AH, modulename, "connection to database \"%s\" failed: %s",
-                                        PQdb(AH->connection), PQerrorMessage(AH->connection));
+               exit_horribly(modulename, "connection to database \"%s\" failed: %s",
+                                         PQdb(AH->connection), PQerrorMessage(AH->connection));
 
        /* check for version mismatch */
        _check_database_version(AH);
@@ -313,7 +313,7 @@ DisconnectDatabase(Archive *AHX)
 {
        ArchiveHandle *AH = (ArchiveHandle *) AHX;
 
-       PQfinish(AH->connection);               /* noop if AH->connection is NULL */
+       PQfinish(AH->connection);       /* noop if AH->connection is NULL */
        AH->connection = NULL;
 }
 
@@ -331,11 +331,19 @@ notice_processor(void *arg, const char *message)
        write_msg(NULL, "%s", message);
 }
 
+/* Like exit_horribly(), but with a complaint about a particular query. */
+static void
+die_on_query_failure(ArchiveHandle *AH, const char *modulename, const char *query)
+{
+       write_msg(modulename, "query failed: %s",
+                         PQerrorMessage(AH->connection));
+       exit_horribly(modulename, "query was: %s\n", query);
+}
 
 void
 ExecuteSqlStatement(Archive *AHX, const char *query)
 {
-       ArchiveHandle      *AH = (ArchiveHandle *) AHX;
+       ArchiveHandle *AH = (ArchiveHandle *) AHX;
        PGresult   *res;
 
        res = PQexec(AH->connection, query);
@@ -347,7 +355,7 @@ ExecuteSqlStatement(Archive *AHX, const char *query)
 PGresult *
 ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
 {
-       ArchiveHandle      *AH = (ArchiveHandle *) AHX;
+       ArchiveHandle *AH = (ArchiveHandle *) AHX;
        PGresult   *res;
 
        res = PQexec(AH->connection, query);
@@ -393,8 +401,8 @@ ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
                                errStmt[DB_MAX_ERR_STMT - 2] = '.';
                                errStmt[DB_MAX_ERR_STMT - 1] = '\0';
                        }
-                       warn_or_die_horribly(AH, modulename, "%s: %s    Command was: %s\n",
-                                                                desc, PQerrorMessage(conn), errStmt);
+                       warn_or_exit_horribly(AH, modulename, "%s: %s    Command was: %s\n",
+                                                                 desc, PQerrorMessage(conn), errStmt);
                        break;
        }
 
@@ -428,7 +436,7 @@ ExecuteInsertCommands(ArchiveHandle *AH, const char *buf, size_t bufLen)
 
        for (; qry < eos; qry++)
        {
-               char    ch = *qry;
+               char            ch = *qry;
 
                /* For neatness, we skip any newlines between commands */
                if (!(ch == '\n' && AH->sqlparse.curCmd->len == 0))
@@ -495,8 +503,8 @@ ExecuteSqlCommandBuf(ArchiveHandle *AH, const char *buf, size_t bufLen)
                 */
                if (AH->pgCopyIn &&
                        PQputCopyData(AH->connection, buf, bufLen) <= 0)
-                       die_horribly(AH, modulename, "error returned by PQputCopyData: %s",
-                                                PQerrorMessage(AH->connection));
+                       exit_horribly(modulename, "error returned by PQputCopyData: %s",
+                                                 PQerrorMessage(AH->connection));
        }
        else if (AH->outputKind == OUTPUT_OTHERDATA)
        {
@@ -518,7 +526,7 @@ ExecuteSqlCommandBuf(ArchiveHandle *AH, const char *buf, size_t bufLen)
                        ExecuteSqlCommand(AH, buf, "could not execute query");
                else
                {
-                       char   *str = (char *) pg_malloc(bufLen + 1);
+                       char       *str = (char *) pg_malloc(bufLen + 1);
 
                        memcpy(str, buf, bufLen);
                        str[bufLen] = '\0';
@@ -541,14 +549,14 @@ EndDBCopyMode(ArchiveHandle *AH, TocEntry *te)
                PGresult   *res;
 
                if (PQputCopyEnd(AH->connection, NULL) <= 0)
-                       die_horribly(AH, modulename, "error returned by PQputCopyEnd: %s",
-                                                PQerrorMessage(AH->connection));
+                       exit_horribly(modulename, "error returned by PQputCopyEnd: %s",
+                                                 PQerrorMessage(AH->connection));
 
                /* Check command status and return to normal libpq state */
                res = PQgetResult(AH->connection);
                if (PQresultStatus(res) != PGRES_COMMAND_OK)
-                       warn_or_die_horribly(AH, modulename, "COPY failed for table \"%s\": %s",
-                                                                te->tag, PQerrorMessage(AH->connection));
+                       warn_or_exit_horribly(AH, modulename, "COPY failed for table \"%s\": %s",
+                                                                 te->tag, PQerrorMessage(AH->connection));
                PQclear(res);
 
                AH->pgCopyIn = false;