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

538 {
539  ArchiveHandle *AH = (ArchiveHandle *) AHX;
540 
541  ExecuteSqlCommand(AH, "COMMIT", "could not commit database transaction");
542 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:328

References ExecuteSqlCommand().

◆ EndDBCopyMode()

void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 500 of file pg_backup_db.c.

501 {
502  ArchiveHandle *AH = (ArchiveHandle *) AHX;
503 
504  if (AH->pgCopyIn)
505  {
506  PGresult *res;
507 
508  if (PQputCopyEnd(AH->connection, NULL) <= 0)
509  pg_fatal("error returned by PQputCopyEnd: %s",
511 
512  /* Check command status and return to normal libpq state */
513  res = PQgetResult(AH->connection);
515  warn_or_exit_horribly(AH, "COPY failed for table \"%s\": %s",
516  tocEntryTag, PQerrorMessage(AH->connection));
517  PQclear(res);
518 
519  /* Do this to ensure we've pumped libpq back to idle state */
520  if (PQgetResult(AH->connection) != NULL)
521  pg_log_warning("unexpected extra results during COPY of table \"%s\"",
522  tocEntryTag);
523 
524  AH->pgCopyIn = false;
525  }
526 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6743
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3240
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2631
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2031
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:97
void warn_or_exit_horribly(ArchiveHandle *AH, const char *fmt,...)
#define pg_fatal(...)
#define pg_log_warning(...)
Definition: pgfnames.c:24

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

Referenced by restore_toc_entry().

◆ ExecuteSqlCommandBuf()

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

Definition at line 445 of file pg_backup_db.c.

446 {
447  ArchiveHandle *AH = (ArchiveHandle *) AHX;
448 
449  if (AH->outputKind == OUTPUT_COPYDATA)
450  {
451  /*
452  * COPY data.
453  *
454  * We drop the data on the floor if libpq has failed to enter COPY
455  * mode; this allows us to behave reasonably when trying to continue
456  * after an error in a COPY command.
457  */
458  if (AH->pgCopyIn &&
459  PQputCopyData(AH->connection, buf, bufLen) <= 0)
460  pg_fatal("error returned by PQputCopyData: %s",
462  }
463  else if (AH->outputKind == OUTPUT_OTHERDATA)
464  {
465  /*
466  * Table data expressed as INSERT commands; or, in old dump files,
467  * BLOB COMMENTS data (which is expressed as COMMENT ON commands).
468  */
469  ExecuteSimpleCommands(AH, buf, bufLen);
470  }
471  else
472  {
473  /*
474  * General SQL commands; we assume that commands will not be split
475  * across calls.
476  *
477  * In most cases the data passed to us will be a null-terminated
478  * string, but if it's not, we have to add a trailing null.
479  */
480  if (buf[bufLen] == '\0')
481  ExecuteSqlCommand(AH, buf, "could not execute query");
482  else
483  {
484  char *str = (char *) pg_malloc(bufLen + 1);
485 
486  memcpy(str, buf, bufLen);
487  str[bufLen] = '\0';
488  ExecuteSqlCommand(AH, str, "could not execute query");
489  free(str);
490  }
491  }
492 
493  return bufLen;
494 }
int PQputCopyData(PGconn *conn, const char *buffer, int nbytes)
Definition: fe-exec.c:2576
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define free(a)
Definition: header.h:65
@ OUTPUT_COPYDATA
@ OUTPUT_OTHERDATA
static void ExecuteSimpleCommands(ArchiveHandle *AH, const char *buf, size_t bufLen)
Definition: pg_backup_db.c:380
static char * buf
Definition: pg_test_fsync.c:67
ArchiverOutput outputKind

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

Referenced by ahwrite().

◆ ExecuteSqlQuery()

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

Definition at line 290 of file pg_backup_db.c.

291 {
292  ArchiveHandle *AH = (ArchiveHandle *) AHX;
293  PGresult *res;
294 
295  res = PQexec(AH->connection, query);
296  if (PQresultStatus(res) != status)
297  die_on_query_failure(AH, query);
298  return res;
299 }
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2225
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
Definition: pg_backup_db.c:269
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224

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

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

◆ ExecuteSqlQueryForSingleRow()

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

Definition at line 305 of file pg_backup_db.c.

306 {
307  PGresult *res;
308  int ntups;
309 
310  res = ExecuteSqlQuery(fout, query, PGRES_TUPLES_OK);
311 
312  /* Expecting a single result only */
313  ntups = PQntuples(res);
314  if (ntups != 1)
315  pg_fatal(ngettext("query returned %d row instead of one: %s",
316  "query returned %d rows instead of one: %s",
317  ntups),
318  ntups, query);
319 
320  return res;
321 }
#define ngettext(s, p, n)
Definition: c.h:1171
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3310
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:100
PGresult * ExecuteSqlQuery(Archive *AHX, const char *query, ExecStatusType status)
Definition: pg_backup_db.c:290

References ExecuteSqlQuery(), ngettext, pg_fatal, PGRES_TUPLES_OK, PQntuples(), and res.

Referenced by _check_database_version(), binary_upgrade_set_pg_class_oids(), binary_upgrade_set_type_oids_by_type_oid(), ConnectDatabase(), convertTSFunction(), dumpAgg(), dumpBaseType(), dumpCollation(), dumpConversion(), dumpDatabase(), dumpDomain(), dumpForeignServer(), dumpFunc(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpRangeType(), dumpSearchPath(), dumpStatisticsExt(), dumpTableAttach(), dumpTableSchema(), dumpTSConfig(), dumpTSDictionary(), expand_table_name_patterns(), get_language_name(), get_next_possible_free_pg_type_oid(), get_synchronized_snapshot(), getFormattedTypeName(), and setup_connection().

◆ ExecuteSqlStatement()

◆ StartTransaction()

void StartTransaction ( Archive AHX)

Definition at line 529 of file pg_backup_db.c.

530 {
531  ArchiveHandle *AH = (ArchiveHandle *) AHX;
532 
533  ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
534 }

References ExecuteSqlCommand().