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

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

References ExecuteSqlCommand().

Referenced by IssueCommandPerBlob().

◆ EndDBCopyMode()

void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 498 of file pg_backup_db.c.

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

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

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

Referenced by ahwrite().

◆ ExecuteSqlQuery()

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

Definition at line 288 of file pg_backup_db.c.

289 {
290  ArchiveHandle *AH = (ArchiveHandle *) AHX;
291  PGresult *res;
292 
293  res = PQexec(AH->connection, query);
294  if (PQresultStatus(res) != status)
295  die_on_query_failure(AH, query);
296  return res;
297 }
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2262
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
Definition: pg_backup_db.c:267

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

Referenced by append_depends_on_extension(), buildMatViewRefreshDependencies(), collectBinaryUpgradeClassOids(), collectComments(), collectRoleNames(), collectSecLabels(), collectSequences(), createViewAsClause(), 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(), getCasts(), getCollations(), getConstraints(), getConversions(), getDefaultACLs(), getDependencies(), getDomainConstraints(), getEventTriggers(), getExtendedStatistics(), getExtensionMembership(), getExtensions(), getForeignDataWrappers(), getForeignServers(), getFuncs(), getIndexes(), getInherits(), getLOs(), getNamespaces(), getOpclasses(), getOperators(), getOpfamilies(), getPartitioningInfo(), getPolicies(), getProcLangs(), getPublicationNamespaces(), getPublications(), getPublicationTables(), getRules(), getSubscriptions(), getSubscriptionTables(), getTableAttrs(), getTables(), getTransforms(), getTriggers(), getTSConfigurations(), getTSDictionaries(), getTSParsers(), getTSTemplates(), getTypes(), processExtensionTables(), and set_restrict_relation_kind().

◆ ExecuteSqlQueryForSingleRow()

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

Definition at line 303 of file pg_backup_db.c.

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

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

Referenced by _check_database_version(), 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 527 of file pg_backup_db.c.

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

References ExecuteSqlCommand().

Referenced by IssueCommandPerBlob().