简体   繁体   中英

Strange behaviour of PHP script performing SELECT query

I've a PHP script that has been working well for a long time. It basically performs a query to a remote database; using postgreSQL API:

SELECT id,command FROM tablet_sync WHERE id>$certainNumber

Id field is a int4 type; and command is a text type.

As I said, it has been working well for a long time. But recently, I found that sometimes the command field could be a little (but not extremly) large; about 10000 characters or more.

Well, when that query find a row wich its field command is larger than certain number of characters (about 9000); it does not work.

  • Apache call never ends.
  • No error message is noticed (PHP/Apache log files, client and server).

I should tell that if this query is performed in a PostgreSQL client, let's say, pgAdmin, it works perfectly (of course, 10000 bytes of data is not a large amount of data!). But it does not work if I make it from my PHP script.

I give you all other information that could be usefull to solve the problem:

  • Server and client have same Apache amd PHP versions: 2.2 , and 5.3.0; respectively.
  • Client and server are NOT located in the same machine
  • If I limit the amount of data to recover when perfmorming the query in PHP script file; it works! . (for ex: SELECT id,( CAST command AS varchar(9000) FROM tablet_sync WHERE id>$certainNumber)

* Updated * I could figure that PostgreSQL remote database server is throwing these errors:

  • LOG: could not receive data from client: Connection timed out
  • LOG: unexpected EOF on client connection

Despite these errors, PHP call still does not end...

* 04/08 Updated *

Thank you for your answer @regilero. I've done some tests:

  1. Changing timeout value has not worked in any way.

  2. I've found, thanks to SHOW AL command, that some configuration fields are diferent in Client and Server side: all locales (lc_collate, lc_message etc;); are set in UTF-8 in server side, meanwhile in client side are set to English_United States.1252. max_fsm_pages is set to 153600; less than client side. I found that postgreSQL version are different too in client and server side: 8.3.1 and 8.3.9, respectively. shared_buffer value is set different too: 32MB and 24MB; client and server, respectively. No more relevant differences have been found.

  3. I use PHP API connector to PostgreSQL and has been working well since now. PHP error log file is not showing anything.

It seems to be a timeout problem.

On the postgresql session side you can try to add theses commands before your query:

set statement_timeout to 10000;

It's a value in ms, you could also try to set it to 0 (infinite).

If it comes from a tcp problem on the session you could adjust theses 3 settings:

set tcp_keepalives_count to 10;
set tcp_keepalives_idle to 2;
set tcp_keepalives_interval to 1;

or other values (here is just a random try). Here it means, try to make a 'tcp keepalive ping' every 2s, retry every 1s if failed, and allow 10 retry before dying (0 means system's default).

Now the problem could also be on the client side, which postgresql connector do you use? What are the optioinnal settings use with this connection (connection string, default conf values,...). A " SHOW ALL; " will list every settings, you should try to perform this query from your PHP client and store the result somewhere, then examine every settings in this list, and check the difference with the same query from pgadmin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM