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

References ExecuteSqlCommand().

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

◆ EndDBCopyMode()

void EndDBCopyMode ( Archive AHX,
const char *  tocEntryTag 
)

Definition at line 504 of file pg_backup_db.c.

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

Referenced by restore_toc_entry().

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

◆ ExecuteSqlCommandBuf()

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

Definition at line 449 of file pg_backup_db.c.

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

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

◆ ExecuteSqlQuery()

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

Definition at line 294 of file pg_backup_db.c.

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

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

295 {
296  ArchiveHandle *AH = (ArchiveHandle *) AHX;
297  PGresult *res;
298 
299  res = PQexec(AH->connection, query);
300  if (PQresultStatus(res) != status)
301  die_on_query_failure(AH, query);
302  return res;
303 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
Definition: pg_backup_db.c:274
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ ExecuteSqlQueryForSingleRow()

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

Definition at line 309 of file pg_backup_db.c.

References ExecuteSqlQuery(), fatal, ngettext, 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(), ConnectDatabase(), convertTSFunction(), dumpAgg(), dumpBaseType(), dumpCollation(), dumpConversion(), dumpDatabase(), dumpDomain(), dumpForeignServer(), dumpFunc(), dumpOpclass(), dumpOpfamily(), dumpOpr(), dumpRangeType(), dumpSearchPath(), dumpStatisticsExt(), dumpTableSchema(), dumpTSConfig(), dumpTSDictionary(), expand_table_name_patterns(), findLastBuiltinOid_V71(), get_language_name(), get_next_possible_free_pg_type_oid(), get_synchronized_snapshot(), getFormattedTypeName(), and setup_connection().

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

◆ ExecuteSqlStatement()

void ExecuteSqlStatement ( Archive AHX,
const char *  query 
)

Definition at line 282 of file pg_backup_db.c.

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

Referenced by dumpBlobs(), dumpTableData_insert(), expand_table_name_patterns(), getTables(), and setup_connection().

283 {
284  ArchiveHandle *AH = (ArchiveHandle *) AHX;
285  PGresult *res;
286 
287  res = PQexec(AH->connection, query);
288  if (PQresultStatus(res) != PGRES_COMMAND_OK)
289  die_on_query_failure(AH, query);
290  PQclear(res);
291 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3178
static void die_on_query_failure(ArchiveHandle *AH, const char *query)
Definition: pg_backup_db.c:274
void PQclear(PGresult *res)
Definition: fe-exec.c:694
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2193

◆ StartTransaction()

void StartTransaction ( Archive AHX)

Definition at line 533 of file pg_backup_db.c.

References ExecuteSqlCommand().

534 {
535  ArchiveHandle *AH = (ArchiveHandle *) AHX;
536 
537  ExecuteSqlCommand(AH, "BEGIN", "could not start database transaction");
538 }
static void ExecuteSqlCommand(ArchiveHandle *AH, const char *qry, const char *desc)
Definition: pg_backup_db.c:332