Re: Is there public API to fetch errcode?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Sergey Fukanchik <s(dot)fukanchik(at)postgrespro(dot)ru>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Is there public API to fetch errcode?
Date: 2025-10-12 20:09:19
Message-ID: 50916.1760299759@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Sergey Fukanchik <s(dot)fukanchik(at)postgrespro(dot)ru> writes:
> sqlstate is saved into conn->last_sqlstate along with the error message
> in pqGetErrorNotice3:
>         if (id == PG_DIAG_SQLSTATE)
>             strlcpy(conn->last_sqlstate, workBuf.data,
>                     sizeof(conn->last_sqlstate));
> is this usable? Can it be extracted somehow?

It is not exposed, and if it were you'd have the same problem of lack
of context: you don't know which connection attempt set the value,
nor whether that's the most interesting failure.

If we wanted to up our game in this area, I'd envision making it
possible to extract a PGresult or PGresult-like structure for each
connection attempt that libpq made. It'd need to carry the connection
details (server address, encryption options, etc) as well as the
failure info. Not a trivial project, but doubtless do-able if
someone cared to put in the effort.

Of course, that just begs the question of what an application
would do with this data if it had it.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Smith 2025-10-12 22:28:40 Re: [PROPOSAL] Termination of Background Workers for ALTER/DROP DATABASE
Previous Message Sergey Fukanchik 2025-10-12 19:41:58 Re: Is there public API to fetch errcode?