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

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

References ExecuteSqlCommand().

◆ EndDBCopyMode()

void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 501 of file pg_backup_db.c.

502 {
503  ArchiveHandle *AH = (ArchiveHandle *) AHX;
504 
505  if (AH->pgCopyIn)
506  {
507  PGresult *res;
508 
509  if (PQputCopyEnd(AH->connection, NULL) <= 0)
510  pg_fatal("error returned by PQputCopyEnd: %s",
512 
513  /* Check command status and return to normal libpq state */
514  res = PQgetResult(AH->connection);
516  warn_or_exit_horribly(AH, "COPY failed for table \"%s\": %s",
517  tocEntryTag, PQerrorMessage(AH->connection));
518  PQclear(res);
519 
520  /* Do this to ensure we've pumped libpq back to idle state */
521  if (PQgetResult(AH->connection) != NULL)
522  pg_log_warning("unexpected extra results during COPY of table \"%s\"",
523  tocEntryTag);
524 
525  AH->pgCopyIn = false;
526  }
527 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6908
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3270
void PQclear(PGresult *res)
Definition: fe-exec.c:718
int PQputCopyEnd(PGconn *conn, const char *errormsg)
Definition: fe-exec.c:2683
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2082
@ 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 446 of file pg_backup_db.c.

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

292 {
293  ArchiveHandle *AH = (ArchiveHandle *) AHX;
294  PGresult *res;
295 
296  res = PQexec(AH->connection, query);
297  if (PQresultStatus(res) != status)
298  die_on_query_failure(AH, query);
299  return res;
300 }
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2273
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
Definition: pg_backup_db.c:270
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 306 of file pg_backup_db.c.

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

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

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

References ExecuteSqlCommand().