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

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

References ExecuteSqlCommand().

◆ EndDBCopyMode()

void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 499 of file pg_backup_db.c.

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

References _archiveHandle::connection, 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 444 of file pg_backup_db.c.

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

References buf, _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().

◆ ExecuteSqlQuery()

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

Definition at line 289 of file pg_backup_db.c.

290 {
291  ArchiveHandle *AH = (ArchiveHandle *) AHX;
292  PGresult *res;
293 
294  res = PQexec(AH->connection, query);
295  if (PQresultStatus(res) != status)
296  die_on_query_failure(AH, query);
297  return res;
298 }
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193
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:229

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

Referenced by append_depends_on_extension(), buildMatViewRefreshDependencies(), collectComments(), collectRoleNames(), collectSecLabels(), createViewAsClause(), dumpBlobs(), dumpCompositeType(), dumpDatabase(), dumpDatabaseConfig(), dumpEnumType(), 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(), getBlobs(), getCasts(), getCollations(), getConstraints(), getConversions(), getDefaultACLs(), getDependencies(), getDomainConstraints(), getEventTriggers(), getExtendedStatistics(), getExtensionMembership(), getExtensions(), getForeignDataWrappers(), getForeignServers(), getFuncs(), getIndexes(), getInherits(), 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 304 of file pg_backup_db.c.

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

References ExecuteSqlQuery(), fatal, ngettext, 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 528 of file pg_backup_db.c.

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

References ExecuteSqlCommand().