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

References ExecuteSqlCommand().

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

◆ EndDBCopyMode()

void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 611 of file pg_backup_db.c.

References _archiveHandle::connection, fatal, pg_log_warning, _archiveHandle::pgCopyIn, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQgetResult(), PQputCopyEnd(), PQresultStatus(), and warn_or_exit_horribly().

Referenced by restore_toc_entry().

612 {
613  ArchiveHandle *AH = (ArchiveHandle *) AHX;
614 
615  if (AH->pgCopyIn)
616  {
617  PGresult *res;
618 
619  if (PQputCopyEnd(AH->connection, NULL) <= 0)
620  fatal("error returned by PQputCopyEnd: %s",
622 
623  /* Check command status and return to normal libpq state */
624  res = PQgetResult(AH->connection);
625  if (PQresultStatus(res) != PGRES_COMMAND_OK)
626  warn_or_exit_horribly(AH, "COPY failed for table \"%s\": %s",
627  tocEntryTag, PQerrorMessage(AH->connection));
628  PQclear(res);
629 
630  /* Do this to ensure we've pumped libpq back to idle state */
631  if (PQgetResult(AH->connection) != NULL)
632  pg_log_warning("unexpected extra results during COPY of table \"%s\"",
633  tocEntryTag);
634 
635  AH->pgCopyIn = false;
636  }
637 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6623
void warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...)
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2384
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
void PQclear(PGresult *res)
Definition: fe-exec.c:694
#define fatal(...)
#define pg_log_warning(...)
Definition: pgfnames.c:24
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:1778

◆ ExecuteSqlCommandBuf()

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

Definition at line 556 of file pg_backup_db.c.

References _archiveHandle::connection, ExecuteSimpleCommands(), ExecuteSqlCommand(), fatal, free, OUTPUT_COPYDATA, OUTPUT_OTHERDATA, _archiveHandle::outputKind, pg_malloc(), _archiveHandle::pgCopyIn, PQerrorMessage(), PQputCopyData(), and generate_unaccent_rules::str.

Referenced by ahwrite().

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

◆ ExecuteSqlQuery()

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

Definition at line 401 of file pg_backup_db.c.

References _archiveHandle::connection, die_on_query_failure(), 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().

402 {
403  ArchiveHandle *AH = (ArchiveHandle *) AHX;
404  PGresult *res;
405 
406  res = PQexec(AH->connection, query);
407  if (PQresultStatus(res) != status)
408  die_on_query_failure(AH, query);
409  return res;
410 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
Definition: pg_backup_db.c:381
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:226

◆ ExecuteSqlQueryForSingleRow()

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

Definition at line 416 of file pg_backup_db.c.

References ExecuteSqlQuery(), fatal, 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().

417 {
418  PGresult *res;
419  int ntups;
420 
421  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
422 
423  /* Expecting a single result only */
424  ntups = PQntuples(res);
425  if (ntups != 1)
426  fatal(ngettext("query returned %d row instead of one: %s",
427  "query returned %d rows instead of one: %s",
428  ntups),
429  ntups, query);
430 
431  return res;
432 }
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
#define ngettext(s, p, n)
Definition: c.h:1134
#define fatal(...)
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:401

◆ ExecuteSqlStatement()

void ExecuteSqlStatement ( Archive AHX,
const char *  query 
)

Definition at line 389 of file pg_backup_db.c.

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

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

390 {
391  ArchiveHandle *AH = (ArchiveHandle *) AHX;
392  PGresult *res;
393 
394  res = PQexec(AH->connection, query);
395  if (PQresultStatus(res) != PGRES_COMMAND_OK)
396  die_on_query_failure(AH, query);
397  PQclear(res);
398 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2692
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
Definition: pg_backup_db.c:381
void PQclear(PGresult *res)
Definition: fe-exec.c:694
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1939

◆ StartTransaction()

void StartTransaction ( Archive AHX)

Definition at line 640 of file pg_backup_db.c.

References ExecuteSqlCommand().

641 {
642  ArchiveHandle *AH = (ArchiveHandle *) AHX;
643 
644  ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
645 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:439