PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_backup_db.h File Reference
#include "pg_backup.h"
Include dependency graph for pg_backup_db.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int ExecuteSqlCommandBuf (Archive *AHX, const char *buf, size_t bufLen)
 
void ExecuteSqlStatement (Archive *AHX, const char *query)
 
PGresultExecuteSqlQuery (Archive *AHX, const char *query, ExecStatusType status)
 
PGresultExecuteSqlQueryForSingleRow (Archive *fout, char *query)
 
void EndDBCopyMode (Archive *AHX, const char *tocEntryTag)
 
void StartTransaction (Archive *AHX)
 
void CommitTransaction (Archive *AHX)
 

Function Documentation

void CommitTransaction ( Archive AHX)

Definition at line 655 of file pg_backup_db.c.

References ExecuteSqlCommand().

656 {
657  ArchiveHandle *AH = (ArchiveHandle *) AHX;
658 
659  ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
660 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:446
void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 618 of file pg_backup_db.c.

References _archiveHandle::connection, exit_horribly(), modulename, NULL, _archiveHandle::pgCopyIn, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQgetResult(), PQputCopyEnd(), PQresultStatus(), warn_or_exit_horribly(), and write_msg().

Referenced by restore_toc_entry().

619 {
620  ArchiveHandle *AH = (ArchiveHandle *) AHX;
621 
622  if (AH->pgCopyIn)
623  {
624  PGresult *res;
625 
626  if (PQputCopyEnd(AH->connection, NULL) <= 0)
627  exit_horribly(modulename, "error returned by PQputCopyEnd: %s",
629 
630  /* Check command status and return to normal libpq state */
631  res = PQgetResult(AH->connection);
632  if (PQresultStatus(res) != PGRES_COMMAND_OK)
633  warn_or_exit_horribly(AH, modulename, "COPY failed for table \"%s\": %s",
634  tocEntryTag, PQerrorMessage(AH->connection));
635  PQclear(res);
636 
637  /* Do this to ensure we've pumped libpq back to idle state */
638  if (PQgetResult(AH->connection) != NULL)
639  write_msg(NULL, "WARNING: unexpected extra results during COPY of table \"%s\"\n",
640  tocEntryTag);
641 
642  AH->pgCopyIn = false;
643  }
644 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5988
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2288
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
void warn_or_exit_horribly(ArchiveHandle *AH, const char *modulename, const char *fmt,...)
static const char * modulename
Definition: pg_backup_db.c:29
void PQclear(PGresult *res)
Definition: fe-exec.c:650
void write_msg(const char *modulename, const char *fmt,...)
#define NULL
Definition: c.h:229
void exit_horribly(const char *modulename, const char *fmt,...)
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1702
int ExecuteSqlCommandBuf ( Archive AHX,
const char *  buf,
size_t  bufLen 
)

Definition at line 563 of file pg_backup_db.c.

References _archiveHandle::connection, ExecuteSimpleCommands(), ExecuteSqlCommand(), exit_horribly(), free, modulename, OUTPUT_COPYDATA, OUTPUT_OTHERDATA, _archiveHandle::outputKind, pg_malloc(), _archiveHandle::pgCopyIn, PQerrorMessage(), and PQputCopyData().

Referenced by ahwrite().

564 {
565  ArchiveHandle *AH = (ArchiveHandle *) AHX;
566 
567  if (AH->outputKind == OUTPUT_COPYDATA)
568  {
569  /*
570  * COPY data.
571  *
572  * We drop the data on the floor if libpq has failed to enter COPY
573  * mode; this allows us to behave reasonably when trying to continue
574  * after an error in a COPY command.
575  */
576  if (AH->pgCopyIn &&
577  PQputCopyData(AH->connection, buf, bufLen) <= 0)
578  exit_horribly(modulename, "error returned by PQputCopyData: %s",
580  }
581  else if (AH->outputKind == OUTPUT_OTHERDATA)
582  {
583  /*
584  * Table data expressed as INSERT commands; or, in old dump files,
585  * BLOB COMMENTS data (which is expressed as COMMENT ON commands).
586  */
587  ExecuteSimpleCommands(AH, buf, bufLen);
588  }
589  else
590  {
591  /*
592  * General SQL commands; we assume that commands will not be split
593  * across calls.
594  *
595  * In most cases the data passed to us will be a null-terminated
596  * string, but if it's not, we have to add a trailing null.
597  */
598  if (buf[bufLen] == '\0')
599  ExecuteSqlCommand(AH, buf, "could not execute query");
600  else
601  {
602  char *str = (char *) pg_malloc(bufLen + 1);
603 
604  memcpy(str, buf, bufLen);
605  str[bufLen] = '\0';
606  ExecuteSqlCommand(AH, str, "could not execute query");
607  free(str);
608  }
609  }
610 
611  return bufLen;
612 }
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2221
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5988
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:446
static char * buf
Definition: pg_test_fsync.c:66
static const char * modulename
Definition: pg_backup_db.c:29
static void ExecuteSimpleCommands(ArchiveHandle *AH, const char *buf, size_t bufLen)
Definition: pg_backup_db.c:498
#define free(a)
Definition: header.h:65
ArchiverOutput outputKind
void exit_horribly(const char *modulename, const char *fmt,...)
PGresult* ExecuteSqlQuery ( Archive AHX,
const char *  query,
ExecStatusType  status 
)

Definition at line 407 of file pg_backup_db.c.

References _archiveHandle::connection, die_on_query_failure(), modulename, PQexec(), and PQresultStatus().

Referenced by buildMatViewRefreshDependencies(), collectComments(), collectSecLabels(), createViewAsClause(), dumpBlobs(), dumpCompositeType(), dumpCompositeTypeColComments(), dumpDatabase(), dumpEnumType(), dumpOpclass(), dumpOpfamily(), dumpRule(), dumpSequence(), dumpSequenceData(), dumpTable(), dumpTableData_copy(), dumpTableData_insert(), dumpTSConfig(), dumpUserMappings(), ExecuteSqlQueryForSingleRow(), expand_schema_name_patterns(), expand_table_name_patterns(), getAccessMethods(), getAggregates(), getBlobs(), getCasts(), getCollations(), getConstraints(), getConversions(), getDefaultACLs(), getDependencies(), getDomainConstraints(), getEventTriggers(), getExtendedStatistics(), getExtensionMembership(), getExtensions(), getForeignDataWrappers(), getForeignServers(), getFuncs(), getIndexes(), getInherits(), getNamespaces(), getOpclasses(), getOperators(), getOpfamilies(), getPolicies(), getProcLangs(), getPublications(), getPublicationTables(), getRules(), getSubscriptions(), getTableAttrs(), getTables(), getTransforms(), getTriggers(), getTSConfigurations(), getTSDictionaries(), getTSParsers(), getTSTemplates(), getTypes(), and processExtensionTables().

408 {
409  ArchiveHandle *AH = (ArchiveHandle *) AHX;
410  PGresult *res;
411 
412  res = PQexec(AH->connection, query);
413  if (PQresultStatus(res) != status)
414  die_on_query_failure(AH, modulename, query);
415  return res;
416 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static const char * modulename
Definition: pg_backup_db.c:29
static void die_on_query_failure(ArchiveHandle *AH, const char *modulename, const char *query)
Definition: pg_backup_db.c:387
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
PGresult* ExecuteSqlQueryForSingleRow ( Archive fout,
char *  query 
)

Definition at line 422 of file pg_backup_db.c.

References ExecuteSqlQuery(), exit_horribly(), ngettext, NULL, PGRES_TUPLES_OK, and PQntuples().

Referenced by _check_database_version(), binary_upgrade_set_pg_class_oids(), binary_upgrade_set_type_oids_by_rel_oid(), binary_upgrade_set_type_oids_by_type_oid(), convertTSFunction(), dumpAgg(), dumpBaseType(), dumpCollation(), dumpConversion(), dumpDatabase(), dumpDomain(), dumpForeignServer(), dumpFunc(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpRangeType(), dumpTableSchema(), dumpTSConfig(), dumpTSDictionary(), findLastBuiltinOid_V71(), get_language_name(), get_synchronized_snapshot(), and getFormattedTypeName().

423 {
424  PGresult *res;
425  int ntups;
426 
427  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
428 
429  /* Expecting a single result only */
430  ntups = PQntuples(res);
431  if (ntups != 1)
433  ngettext("query returned %d row instead of one: %s\n",
434  "query returned %d rows instead of one: %s\n",
435  ntups),
436  ntups, query);
437 
438  return res;
439 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2673
#define ngettext(s, p, n)
Definition: c.h:127
#define NULL
Definition: c.h:229
void exit_horribly(const char *modulename, const char *fmt,...)
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:407
void ExecuteSqlStatement ( Archive AHX,
const char *  query 
)

Definition at line 395 of file pg_backup_db.c.

References _archiveHandle::connection, die_on_query_failure(), modulename, PGRES_COMMAND_OK, PQclear(), PQexec(), and PQresultStatus().

Referenced by dumpBlobs(), dumpTableData_insert(), getTables(), selectSourceSchema(), and setup_connection().

396 {
397  ArchiveHandle *AH = (ArchiveHandle *) AHX;
398  PGresult *res;
399 
400  res = PQexec(AH->connection, query);
401  if (PQresultStatus(res) != PGRES_COMMAND_OK)
402  die_on_query_failure(AH, modulename, query);
403  PQclear(res);
404 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static const char * modulename
Definition: pg_backup_db.c:29
static void die_on_query_failure(ArchiveHandle *AH, const char *modulename, const char *query)
Definition: pg_backup_db.c:387
void PQclear(PGresult *res)
Definition: fe-exec.c:650
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1846
void StartTransaction ( Archive AHX)

Definition at line 647 of file pg_backup_db.c.

References ExecuteSqlCommand().

648 {
649  ArchiveHandle *AH = (ArchiveHandle *) AHX;
650 
651  ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
652 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:446