Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where is the SQLSTATE diagnostic attribute updated from ? #1240

Closed
mat100payette opened this issue Feb 17, 2021 · 3 comments
Closed

Where is the SQLSTATE diagnostic attribute updated from ? #1240

mat100payette opened this issue Feb 17, 2021 · 3 comments

Comments

@mat100payette
Copy link

In the C libs, I'm trying to figure out where the SQLSTATE code is updated from (which then maps to Postgresql errors and gives it to python).

If anyone could explain to me where that value comes from initially that'd be really appreciated.

I know that in pq_raise(...), either pgres or curs should have it, but I can't seem to trace these further.

@dvarrazzo
Copy link
Member

It comes from PQresultErrorField, if I understand correctly your question? It is read from the libpq error here and attached in this function to the Error object.

psycopg2/psycopg/pqpath.c

Lines 118 to 124 in f469331

if (pgres && *pgres) {
err = PQresultErrorMessage(*pgres);
if (err != NULL) {
Dprintf("pq_raise: PQresultErrorMessage: err=%s", err);
code = PQresultErrorField(*pgres, PG_DIAG_SQLSTATE);
}
}

@mat100payette
Copy link
Author

Ahh okok so in PQresultErrorField(*pgres, PG_DIAG_SQLSTATE) the attribute comes directly from libpq, correct ?

I'm super new to Postgresql so I didn't quite understand where the gap between your lib and theirs was.

@dvarrazzo
Copy link
Member

Yes, it comes from the libpq. The libpq receives it from the server, as part of its response. I don't know if there is any error that the libpq generates itself client-side and to which it attached a SQLSTATE code, I've never checked. Psycopg never does it: if a code is exposed there it means it came from the libpq.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants