Skip to content

Feature Request: Add SQLSTATE return when the pg_connect() failed to the error text #18335

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

Open
EntertainmentOne opened this issue Apr 16, 2025 · 9 comments

Comments

@EntertainmentOne
Copy link

Description

Now, we only get the string desprection, no SQLSTATE.
I must get the SQLSTATE to handle the errors,
Caused by the pg_connect() returns false, I cannot get SQLSTATE by general methods.
Please add this feature to pgsql extension.

@SakiTakamachi
Copy link
Member

SakiTakamachi commented Apr 16, 2025

Since changing existing behavior would be a BC break, an RFC would be required.

However, if we’re just adding a new function for making connections, an RFC might not be necessary.

@devnexen
Could I get your thoughts on this?

edit:
Since ext/odbc follows the same design, it would make sense to update it as well if we’re making any changes.

cc: @NattyNarwhal

@devnexen
Copy link
Member

It is definitively a fair request. I ll put some more thoughts whether we should add an optional parameter to pg_(*)connect or something else.

@SakiTakamachi
Copy link
Member

One option would be to give PgSql\Connection a constructor and have it throw an exception when a connection attempt is made through it.

@NattyNarwhal
Copy link
Member

You should be able to get the SQLSTATE of a failed connection in the procedural ODBC extension by not specifying a connection. (I don't know if Pg does this, but it seems to be a common pattern with pre-PDO extensions; ibm_db2 also does this.)

<?php

odbc_connect("BOGUS", "user", "hunter2");

var_dump(odbc_error());
% php82 odbc_error.php

Warning: odbc_connect(): SQL error: [unixODBC][Driver Manager]Data source name not found and no default driver specified, SQL state IM002 in SQLConnect in /Users/calvin/src/odbc_error.php on line 3
string(5) "IM002"

@SakiTakamachi
Copy link
Member

@NattyNarwhal

I didn’t know that, thanks. It looks like pgsql doesn’t currently have an equivalent feature, but I think it could be a good option.

@devnexen
Copy link
Member

devnexen commented Apr 16, 2025

You should be able to get the SQLSTATE of a failed connection in the procedural ODBC extension by not specifying a connection. (I don't know if Pg does this, but it seems to be a common pattern with pre-PDO extensions; ibm_db2 also does this.)

I think there is a difference, pgsql still supports a default connection (which ought to be deprecated at some point ... likely for PHP 9), I do not think you re going to get a proper sqlstate until you have at least a successful connection. if you try a similar use case you did with odbc for pgsql, since you failed the connection pgsql last error will try to fallback to the default connection and failing at it. I ll have a better look later.

@devnexen
Copy link
Member

devnexen commented Apr 16, 2025

Description

Now, we only get the string desprection, no SQLSTATE. I must get the SQLSTATE to handle the errors, Caused by the pg_connect() returns false, I cannot get SQLSTATE by general methods. Please add this feature to pgsql extension.

@EntertainmentOne ; I spent time to dig into libpq, while there is definitively what we call proper SQLSTATE in a query level, there is no such thing in a connection level though. Would you be able to clarify what you are expecting more precisely ?

@EntertainmentOne
Copy link
Author

描述

现在,我们只能获取字符串 desprection,而没有 SQLSTATE。我必须获取 SQLSTATE 来处理错误,这是由于 pg_connect() 返回 false 造成的,我无法通过常规方法获取 SQLSTATE。请将此功能添加到 pgsql 扩展中。

@EntertainmentOne 我花了一些时间深入研究 libpq,虽然查询级别确实存在所谓的正确 SQLSTATE,但在连接级别却没有。您能更准确地解释一下您的期望吗?

[Resend 2]
I was read the document of PostgreSQL, I saw the SQLSTATE includes some connection-layer problems (eg, 28D01 Wrong Password), I think the libpq gave our a interface to get the SQLSTATE, I dont know the implention of libpq, but I think it have a interface to get it.

@devnexen
Copy link
Member

I report here a message from the libpq devs themselves

No, libpq generally doesn't assign SQLSTATE codes to errors generated
within libpq.  (Adding that has been on the TODO list for a long time,
but apparently few people care.)

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

No branches or pull requests

4 participants