Access last_sqlstate from libpq

From: Daniel Frey <d(dot)frey(at)gmx(dot)de>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Access last_sqlstate from libpq
Date: 2021-09-17 23:36:35
Message-ID: D0EA7D79-62C5-4733-B437-3A5C7C20746D@gmx.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I am the author of a PostgreSQL C++ client library, taoPQ (https://github.com/taocpp/taopq), wrapping the C-API of libpq.

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 );

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

* lo_* (large object functions)

Obviously, I can take the error message from PQerrorMessage and throw a generic runtime error - but it would be so much nicer if I could use the SQLSTATE to throw the correct exception class and give users more information just like I do for PGresult*.

After some research, it appears that PGconn* does have a field called last_sqlstate - it just can't be accessed.
Are there any problems adding a simple accessor to libpq? Or is there some way to access it that I'm missing?

Regards,
Daniel

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David G. Johnston 2021-09-17 23:45:25 Re: Access last_sqlstate from libpq
Previous Message Alexander Korotkov 2021-09-17 23:24:17 Re: postgres.h included from relcache.h - but removing it breaks pg_upgrade