Skip to contents

When the server reports an error, RPostgres raises an rlang::abort() condition that carries PostgreSQL's SQLSTATE code and the other diagnostic fields reported by libpq. The class vector of the condition is c("RPostgres_error_<SQLSTATE>", "RPostgres_error", "rlang_error", "error", "condition"), so handlers can dispatch on a specific code or on all RPostgres errors. Branch on the code rather than on the message, which the server may translate according to its lc_messages setting.

Details

An error raised while a statement runs names the DBI method that ran it, such as DBI::dbSendQuery(), DBI::dbFetch(), DBI::dbBind() or DBI::dbAppendTable(). That can be a method DBI calls internally: an error in DBI::dbGetQuery() or DBI::dbExecute() names dbSendQuery(), or dbBind() when params are given.

Message

The first line of the message is the step that failed, such as "Failed to fetch row", followed by the server's primary message. Each field below that is set follows as an i bullet, in place of the DETAIL:, HINT:, QUERY: and CONTEXT: lines of libpq's own text:

Error in `dbSendQuery()`:
! Failed to fetch row : duplicate key value violates unique constraint "t_pkey"
i SQLSTATE: 23505
i Detail: Key (id)=(1) already exists.
i Schema: public
i Table: t
i Constraint: t_pkey

A position is followed by the line of the statement it points into and a caret under the character it points to, like the LINE n: excerpt of libpq's text. A line wider than 60 columns is cut around the caret. An error in a statement that the server runs on behalf of the statement sent, such as the string run by EXECUTE in PL/pgSQL, shows that internal statement and the excerpt for its position:

Error in `dbSendQuery()`:
! Failed to fetch row : relation "nope" does not exist
i SQLSTATE: 42P01
i Internal query: SELECT * FROM nope
i Internal position: 15
  LINE 1: SELECT * FROM nope
                        ^
i Context: PL/pgSQL function inline_code_block line 1 at EXECUTE

conditionMessage() returns all of these lines.

Fields

The following fields are attached to the condition object, each NULL when libpq did not supply a value: sqlstate, detail, hint, position, internal_query, internal_position, context, schema, table, column, datatype, and constraint. They map to the corresponding PG_DIAG_* fields of libpq's PQresultErrorField(); position is the 1-based character offset into the statement, as a string, and internal_position the one into internal_query. See https://www.postgresql.org/docs/current/errcodes-appendix.html for the codes.

A warning that arises while the failing call runs, such as a failed attempt to cancel the previous query, is not signaled but travels with an RPostgres_error, also one without a code, so that a warning handler or options(warn = 2) cannot replace the error. An ordinary error, listed below, drops such warnings. Such warnings are in the warnings field, a character vector or NULL, and follow the other bullets as i Warning: bullets.

Errors without a code

An error detected by libpq rather than reported by the server, such as a lost connection, still carries the RPostgres_error class but has no sqlstate. Its message is the step that failed followed by libpq's text, with no bullets other than those for warnings. A query interrupted while waiting for the server ("Interrupted.") and a failure to wait on the connection's socket are classed the same way, without libpq's text. A lost connection detected outside of running a statement, for instance while quoting, has no call. Errors raised by DBI::dbConnect(), by postgresImportLargeObject() and postgresExportLargeObject(), by using a connection after DBI::dbDisconnect(), and by checks of the arguments in R or C++ are ordinary errors without these classes and fields.

Examples

if (FALSE) { # \dontrun{
# Dispatch on a specific SQLSTATE
tryCatch(
  dbGetQuery(con, "SELECT * FROM no_such_table"),
  RPostgres_error_42P01 = function(e) e$sqlstate
)

# Read the SQLSTATE programmatically
cnd <- tryCatch(
  dbGetQuery(con, "SELECT * FROM no_such_table"),
  RPostgres_error = identity
)
cnd$sqlstate
} # }