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

References ExecuteSqlCommand().

667 {
668  ArchiveHandle *AH = (ArchiveHandle *) AHX;
669 
670  ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
671 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:448
void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

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

630 {
631  ArchiveHandle *AH = (ArchiveHandle *) AHX;
632 
633  if (AH->pgCopyIn)
634  {
635  PGresult *res;
636 
637  if (PQputCopyEnd(AH->connection, NULL) <= 0)
638  exit_horribly(modulename, "error returned by PQputCopyEnd: %s",
640 
641  /* Check command status and return to normal libpq state */
642  res = PQgetResult(AH->connection);
643  if (PQresultStatus(res) != PGRES_COMMAND_OK)
644  warn_or_exit_horribly(AH, modulename, "COPY failed for table \"%s\": %s",
645  tocEntryTag, PQerrorMessage(AH->connection));
646  PQclear(res);
647 
648  /* Do this to ensure we've pumped libpq back to idle state */
649  if (PQgetResult(AH->connection) != NULL)
650  write_msg(NULL, "WARNING: unexpected extra results during COPY of table \"%s\"\n",
651  tocEntryTag);
652 
653  AH->pgCopyIn = false;
654  }
655 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6011
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:31
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 574 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().

575 {
576  ArchiveHandle *AH = (ArchiveHandle *) AHX;
577 
578  if (AH->outputKind == OUTPUT_COPYDATA)
579  {
580  /*
581  * COPY data.
582  *
583  * We drop the data on the floor if libpq has failed to enter COPY
584  * mode; this allows us to behave reasonably when trying to continue
585  * after an error in a COPY command.
586  */
587  if (AH->pgCopyIn &&
588  PQputCopyData(AH->connection, buf, bufLen) <= 0)
589  exit_horribly(modulename, "error returned by PQputCopyData: %s",
591  }
592  else if (AH->outputKind == OUTPUT_OTHERDATA)
593  {
594  /*
595  * Table data expressed as INSERT commands; or, in old dump files,
596  * BLOB COMMENTS data (which is expressed as COMMENT ON commands).
597  */
598  ExecuteSimpleCommands(AH, buf, bufLen);
599  }
600  else
601  {
602  /*
603  * General SQL commands; we assume that commands will not be split
604  * across calls.
605  *
606  * In most cases the data passed to us will be a null-terminated
607  * string, but if it's not, we have to add a trailing null.
608  */
609  if (buf[bufLen] == '\0')
610  ExecuteSqlCommand(AH, buf, "could not execute query");
611  else
612  {
613  char *str = (char *) pg_malloc(bufLen + 1);
614 
615  memcpy(str, buf, bufLen);
616  str[bufLen] = '\0';
617  ExecuteSqlCommand(AH, str, "could not execute query");
618  free(str);
619  }
620  }
621 
622  return bufLen;
623 }
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2221
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6011
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:448
static char * buf
Definition: pg_test_fsync.c:65
static const char * modulename
Definition: pg_backup_db.c:31
static void ExecuteSimpleCommands(ArchiveHandle *AH, const char *buf, size_t bufLen)
Definition: pg_backup_db.c:509
#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 409 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(), getExtensionMembership(), getExtensions(), getForeignDataWrappers(), getForeignServers(), getFuncs(), getIndexes(), getInherits(), getNamespaces(), getOpclasses(), getOperators(), getOpfamilies(), getPartitions(), getPolicies(), getProcLangs(), getPublications(), getPublicationTables(), getRules(), getSubscriptions(), getTableAttrs(), getTablePartitionKeyInfo(), getTables(), getTransforms(), getTriggers(), getTSConfigurations(), getTSDictionaries(), getTSParsers(), getTSTemplates(), getTypes(), and processExtensionTables().

410 {
411  ArchiveHandle *AH = (ArchiveHandle *) AHX;
412  PGresult *res;
413 
414  res = PQexec(AH->connection, query);
415  if (PQresultStatus(res) != status)
416  die_on_query_failure(AH, modulename, query);
417  return res;
418 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static const char * modulename
Definition: pg_backup_db.c:31
static void die_on_query_failure(ArchiveHandle *AH, const char *modulename, const char *query)
Definition: pg_backup_db.c:389
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 424 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().

425 {
426  PGresult *res;
427  int ntups;
428 
429  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
430 
431  /* Expecting a single result only */
432  ntups = PQntuples(res);
433  if (ntups != 1)
435  ngettext("query returned %d row instead of one: %s\n",
436  "query returned %d rows instead of one: %s\n",
437  ntups),
438  ntups, query);
439 
440  return res;
441 }
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:409
void ExecuteSqlStatement ( Archive AHX,
const char *  query 
)

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

398 {
399  ArchiveHandle *AH = (ArchiveHandle *) AHX;
400  PGresult *res;
401 
402  res = PQexec(AH->connection, query);
403  if (PQresultStatus(res) != PGRES_COMMAND_OK)
404  die_on_query_failure(AH, modulename, query);
405  PQclear(res);
406 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2596
static const char * modulename
Definition: pg_backup_db.c:31
static void die_on_query_failure(ArchiveHandle *AH, const char *modulename, const char *query)
Definition: pg_backup_db.c:389
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 658 of file pg_backup_db.c.

References ExecuteSqlCommand().

659 {
660  ArchiveHandle *AH = (ArchiveHandle *) AHX;
661 
662  ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
663 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:448