Re: Access last_sqlstate from libpq

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Daniel Frey <d(dot)frey(at)gmx(dot)de>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Access last_sqlstate from libpq
Date: 2021-09-18 02:29:38
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Daniel Frey <d(dot)frey(at)gmx(dot)de> writes:
> In case of an error when I received a PGresult*, I can access the SQLSTATE by calling

> PGresult* pgresult = ...;
> const char* sqlstate = PQresultErrorField( pgresult, PG_DIAG_SQLSTATE );

Right ...

> However, this is not possible in a couple of other cases where I don't have a PGresult*, only the PGconn* is available:
> * PQconnectdb (and variants)
> * PQputCopyData
> * PQputCopyEnd
> * PQgetCopyData

In these cases, any error you might get is probably from libpq itself,
not from the server. libpq does not generate SQLSTATEs for its errors,
so it's likely that last_sqlstate is not relevant at all.

(Getting libpq to assign SQLSTATEs to its errors has been on the to-do
list for a couple of decades. I'm not holding my breath for somebody
to undertake that.)

> Are there any problems adding a simple accessor to libpq?

I would be strongly against that unless somebody first did the
legwork to ensure it was meaningful.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Neil Chen 2021-09-18 02:41:54 Re: psql: \dl+ to list large objects privileges
Previous Message Michael Paquier 2021-09-18 01:52:19 Re: improve pg_receivewal code