Checks whether an asynchronous PostgreSQL query is in progress.
The objective is to understand the behavior and operational limits, not only the method name.
What It Does
Checks whether an asynchronous PostgreSQL query is in progress.
Syntax and Return Value
pg_connection_busy(...)
The return value and exceptional cases depend on the call described here: Use pg_consume_input when handling asynchronous results before relying on this state.
Requirements: install and enable the corresponding PHP driver or extension and configure the service before running the examples.
Example 1: Basic Use
Requires a reachable PostgreSQL server and the pgsql PHP extension; asynchronous requests must be drained with pg_get_result.
<?php
$host = getenv('DB_HOST'); $user = getenv('DB_USER'); $password = getenv('DB_PASSWORD'); $database = getenv('DB_NAME');
if (!$host || !$user || $password === false || !$database) throw new RuntimeException('Missing PostgreSQL configuration');
$pg = pg_connect('host=' . $host . ' dbname=' . $database . ' user=' . $user . ' password=' . $password);
if (!$pg) { throw new RuntimeException('PostgreSQL connection failed'); }
pg_send_query($pg, "SELECT pg_sleep(1)");
pg_consume_input($pg);
echo pg_connection_busy($pg) ? "query pending" : "query finished";
while (pg_get_result($pg) !== false) {}
Example 2: A Second Scenario
Requires a reachable PostgreSQL server and the pgsql PHP extension; asynchronous requests must be drained with pg_get_result. This additional check distinguishes the method's result from connection or statement state.
<?php
$host = getenv('DB_HOST'); $user = getenv('DB_USER'); $password = getenv('DB_PASSWORD'); $database = getenv('DB_NAME');
if (!$host || !$user || $password === false || !$database) throw new RuntimeException('Missing PostgreSQL configuration');
$pg = pg_connect('host=' . $host . ' dbname=' . $database . ' user=' . $user . ' password=' . $password);
if (!$pg) { throw new RuntimeException('PostgreSQL connection failed'); }
pg_send_query($pg, "SELECT pg_sleep(1)");
pg_consume_input($pg);
echo pg_connection_busy($pg) ? "query pending" : "query finished";
while (pg_get_result($pg) !== false) {}
echo pg_connection_busy($pg) ? "still busy" : "ready for next query";
Example 3: Practical Checks
Requires a reachable PostgreSQL server and the pgsql PHP extension; asynchronous requests must be drained with pg_get_result. This additional check distinguishes the method's result from connection or statement state.
<?php
$host = getenv('DB_HOST'); $user = getenv('DB_USER'); $password = getenv('DB_PASSWORD'); $database = getenv('DB_NAME');
if (!$host || !$user || $password === false || !$database) throw new RuntimeException('Missing PostgreSQL configuration');
$pg = pg_connect('host=' . $host . ' dbname=' . $database . ' user=' . $user . ' password=' . $password);
if (!$pg) { throw new RuntimeException('PostgreSQL connection failed'); }
pg_send_query($pg, "SELECT pg_sleep(1)");
pg_consume_input($pg);
echo pg_connection_busy($pg) ? "query pending" : "query finished";
while (pg_get_result($pg) !== false) {}
echo "Status: " . pg_connection_status($pg);
Important Behavior and Edge Cases
Use pg_consume_input when handling asynchronous results before relying on this state.
For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.
Comparison with Related Approaches
| Approach | Important distinction |
|---|---|
| pg_connection_busy | Checks whether an asynchronous PostgreSQL query is in progress. |
| Related API / pattern | pg_get_result retrieves a completed asynchronous query result. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Assuming the happy path covers edge cases | Use pg_consume_input when handling asynchronous results before relying on this state. |
| Using a related API interchangeably | pg_get_result retrieves a completed asynchronous query result. |
Key Takeaways
- Checks whether an asynchronous PostgreSQL query is in progress.
- Use pg_consume_input when handling asynchronous results before relying on this state.
- pg_get_result retrieves a completed asynchronous query result.
Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.