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 644 of file pg_backup_db.c.

References ExecuteSqlCommand().

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

◆ EndDBCopyMode()

void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 607 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().

608 {
609  ArchiveHandle *AH = (ArchiveHandle *) AHX;
610 
611  if (AH->pgCopyIn)
612  {
613  PGresult *res;
614 
615  if (PQputCopyEnd(AH->connection, NULL) <= 0)
616  exit_horribly(modulename, "error returned by PQputCopyEnd: %s",
618 
619  /* Check command status and return to normal libpq state */
620  res = PQgetResult(AH->connection);
621  if (PQresultStatus(res) != PGRES_COMMAND_OK)
622  warn_or_exit_horribly(AH, modulename, "COPY failed for table \"%s\": %s",
623  tocEntryTag, PQerrorMessage(AH->connection));
624  PQclear(res);
625 
626  /* Do this to ensure we've pumped libpq back to idle state */
627  if (PQgetResult(AH->connection) != NULL)
628  write_msg(NULL, "WARNING: unexpected extra results during COPY of table \"%s\"\n",
629  tocEntryTag);
630 
631  AH->pgCopyIn = false;
632  }
633 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6115
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:29
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 552 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().

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

397 {
398  ArchiveHandle *AH = (ArchiveHandle *) AHX;
399  PGresult *res;
400 
401  res = PQexec(AH->connection, query);
402  if (PQresultStatus(res) != status)
403  die_on_query_failure(AH, modulename, query);
404  return res;
405 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
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:376
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 411 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(), convertTSFunction(), dumpAgg(), dumpBaseType(), dumpCollation(), dumpConversion(), dumpDatabase(), dumpDomain(), dumpForeignServer(), dumpFunc(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpRangeType(), dumpStatisticsExt(), dumpTableSchema(), dumpTSConfig(), dumpTSDictionary(), findLastBuiltinOid_V71(), get_language_name(), get_synchronized_snapshot(), and getFormattedTypeName().

412 {
413  PGresult *res;
414  int ntups;
415 
416  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
417 
418  /* Expecting a single result only */
419  ntups = PQntuples(res);
420  if (ntups != 1)
421  exit_horribly(NULL,
422  ngettext("query returned %d row instead of one: %s\n",
423  "query returned %d rows instead of one: %s\n",
424  ntups),
425  ntups, query);
426 
427  return res;
428 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2724
#define ngettext(s, p, n)
Definition: c.h:1011
void exit_horribly(const char *modulename, const char *fmt,...)
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:396

◆ ExecuteSqlStatement()

void ExecuteSqlStatement ( Archive AHX,
const char *  query 
)

Definition at line 384 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().

385 {
386  ArchiveHandle *AH = (ArchiveHandle *) AHX;
387  PGresult *res;
388 
389  res = PQexec(AH->connection, query);
390  if (PQresultStatus(res) != PGRES_COMMAND_OK)
391  die_on_query_failure(AH, modulename, query);
392  PQclear(res);
393 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
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:376
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 636 of file pg_backup_db.c.

References ExecuteSqlCommand().

637 {
638  ArchiveHandle *AH = (ArchiveHandle *) AHX;
639 
640  ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
641 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:435