PostgreSQL Source Code  git master
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, const char *query)
 
void EndDBCopyMode (Archive *AHX, const char *tocEntryTag)
 
void StartTransaction (Archive *AHX)
 
void CommitTransaction (Archive *AHX)
 

Function Documentation

◆ CommitTransaction()

void CommitTransaction ( Archive AHX)

Definition at line 653 of file pg_backup_db.c.

References ExecuteSqlCommand().

654 {
655  ArchiveHandle *AH = (ArchiveHandle *) AHX;
656 
657  ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
658 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:444

◆ EndDBCopyMode()

void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 616 of file pg_backup_db.c.

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

Referenced by restore_toc_entry().

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

◆ ExecuteSqlCommandBuf()

int ExecuteSqlCommandBuf ( Archive AHX,
const char *  buf,
size_t  bufLen 
)

Definition at line 561 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(), PQputCopyData(), and generate_unaccent_rules::str.

Referenced by ahwrite().

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

◆ ExecuteSqlQuery()

PGresult* ExecuteSqlQuery ( Archive AHX,
const char *  query,
ExecStatusType  status 
)

Definition at line 405 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(), dumpDatabaseConfig(), 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().

406 {
407  ArchiveHandle *AH = (ArchiveHandle *) AHX;
408  PGresult *res;
409 
410  res = PQexec(AH->connection, query);
411  if (PQresultStatus(res) != status)
412  die_on_query_failure(AH, modulename, query);
413  return res;
414 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
static const char * modulename
Definition: pg_backup_db.c:30
static void die_on_query_failure(ArchiveHandle *AH, const char *modulename, const char *query)
Definition: pg_backup_db.c:385
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ ExecuteSqlQueryForSingleRow()

PGresult* ExecuteSqlQueryForSingleRow ( Archive fout,
const char *  query 
)

Definition at line 420 of file pg_backup_db.c.

References ExecuteSqlQuery(), exit_horribly(), ngettext, 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(), ConnectDatabase(), convertTSFunction(), dumpAgg(), dumpBaseType(), dumpCollation(), dumpConversion(), dumpDatabase(), dumpDomain(), dumpForeignServer(), dumpFunc(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpRangeType(), dumpSearchPath(), dumpStatisticsExt(), dumpTableSchema(), dumpTSConfig(), dumpTSDictionary(), expand_table_name_patterns(), findLastBuiltinOid_V71(), get_language_name(), get_synchronized_snapshot(), getFormattedTypeName(), ReconnectToServer(), and setup_connection().

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

◆ ExecuteSqlStatement()

void ExecuteSqlStatement ( Archive AHX,
const char *  query 
)

Definition at line 393 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(), expand_table_name_patterns(), getTables(), and setup_connection().

394 {
395  ArchiveHandle *AH = (ArchiveHandle *) AHX;
396  PGresult *res;
397 
398  res = PQexec(AH->connection, query);
399  if (PQresultStatus(res) != PGRES_COMMAND_OK)
400  die_on_query_failure(AH, modulename, query);
401  PQclear(res);
402 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
static const char * modulename
Definition: pg_backup_db.c:30
static void die_on_query_failure(ArchiveHandle *AH, const char *modulename, const char *query)
Definition: pg_backup_db.c:385
void PQclear(PGresult *res)
Definition: fe-exec.c:671
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897

◆ StartTransaction()

void StartTransaction ( Archive AHX)

Definition at line 645 of file pg_backup_db.c.

References ExecuteSqlCommand().

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