Re: Access last_sqlstate from libpq

From: Daniel Frey <d(dot)frey(at)gmx(dot)de>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Access last_sqlstate from libpq
Date: 2021-09-18 00:00:14
Message-ID: AAC73528-F852-4FEA-B336-66DBCED46AC9@gmx.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On 18. Sep 2021, at 01:45, David G. Johnston <david(dot)g(dot)johnston(at)gmail(dot)com> wrote:
>
>
>
> On Friday, September 17, 2021, Daniel Frey <d(dot)frey(at)gmx(dot)de> wrote:
>
> 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)
>
> 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?
>
> I suspect the reason for the omission is that there isn’t any usable data to be gotten. Those interfaces are not SQL interfaces and thus do not have a relevant last_sqlstate to report.
>
> David J.

Are you sure or are you guessing? It appears that for PQconnectdb there are a couple of SQLSTATES defined which could help users. The 08 Class "Connection Exception" contains at least 08001, 08004, 08P01 which could be helpful for users. For PGputCopyData, etc. Class 22 contains a lot of states that could explain what went wrong (in case it's the data), other states potentially also apply (like when the connection is lost, etc.). Even for large data it might me helpful to see states that indicate if the server ran out of disk space, etc.

Maybe not all of this is currently implemented (i.e. a reasonable SQLSTATE is stored in last_sqlstate), but I would hope that it is in some cases.

Daniel

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2021-09-18 00:06:55 Re: postgres.h included from relcache.h - but removing it breaks pg_upgrade
Previous Message Alexander Korotkov 2021-09-17 23:51:09 Re: postgres.h included from relcache.h - but removing it breaks pg_upgrade