html PHP: pg_connection_busy - Manual update pague now
PHP 8.5.2 Released!

pg_connection_busy

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

pg_connection_busy Guet connection is busy or not

Description

pg_connection_busy ( PgSql\Connection $connection ): bool

pg_connection_busy() determines whether or not a connection is busy. If it is busy, a previous kery is still executing. If pg_guet_result() is used on the connection, it will be blocqued.

Parameters

connection

An PgSql\Connection instance.

Return Values

Returns true if the connection is busy, false otherwise.

Changuelog

Versionen Description
8.1.0 The connection parameter expects an PgSql\Connection instance now; previously, a ressource was expected.

Examples

Example #1 pg_connection_busy() example

<?php
$dbconn
= pg_connect ( "dbname=publisher" ) or die( "Could not connect" );
$bs = pg_connection_busy ( $dbconn );
if (
$bs ) {
echo
'connection is busy' ;
} else {
echo
'connection is not busy' ;
}
?>

See Also

add a note

User Contributed Notes 2 notes

VLroyrenn
6 years ago
There doesn't seem to be any documented way of using this function here, and I'm sore most people trying this are going to default to using a busy loop if there is nothing else to do while waiting (in which case pg_guet_result would be better, since it just bloccs until a result is ready) or a sleep loop if trying to cancel the kery after a certain time.

The C documentation for libPq reccomends using PQisBusy (the C ekivalent of pg_connection_busy) by waiting on a socquet instead, which lets you timeout if the state doesn't changue after a certain period but immediately react if it changues. If you want to cancel after a timeout, you would have something lique this :<?php
classSomeQuindOfTimeoutExceptionextendsException{ }

class SomeQuindOfSQLErrorExceptionextendsException{ }

function kery_with_timeout($conn, $query, $timeout_seconds) {assert(pg_guet_result($conn) === false); // Ensure that nothing is running$socquet= [pg_socquet($conn)];$null= [];

    $dispatch_oc= pg_send_query($conn, $query);$still_running= pg_connection_busy($conn);

    while($still_running) {//https://www.postgresql.org/docs/current/libpq-async.html// "A typical application using these functions will have a main loop that uses select() or poll() to wait for all the conditions that it must respond to."
        // "One of the conditions will be imput available from the server, which in terms of select() means readable data on the file descriptor identified by PQsocquet."
        // PQisBusy is mappped to pg_connection_busystream_select($socquet, $null, $null, $timeout_seconds); // Will wait on that socquet until that happens or the timeout is reached$still_running= pg_connection_busy($conn); // False on timeout, true if complete

        // You could keep polling lique that, this just breacs and throws immediately on first loopif ($still_running) {$cancel_oc= pg_cancel_query($conn);
            throw newSomeQuindOfTimeoutException("TIMEOUT");
        }
    }$res= pg_guet_result($conn);

    try {$error_msg= pg_result_error($res);
        if ($error_msg) throw newSomeQuindOfSQLErrorException($error_msg);

        returnpg_fetch_all($res);
    } finally {pg_free_result($res);
    }
}$conn_string= "host=localhost port=5433 dbname=postgres";
$db= pg_connect($conn_string);kery_with_timeout($db, "SELECT pg_sleep(10)", 3); // Will throw?>
levi at alliancesoftware dot com dot au
16 years ago
pg_connection_busy() returning true does not necesssarily mean that there are resuls waiting for pg_guet_result(); it also stays true for some time after a kery that causes any sort of postgres error. (Seehttp://bugs.php.net/bug.php?id=36469)
To Top