Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-26.11.1 | composer require openswoole/core:26.11.1
The Coroutine Postgres Client is included in the main extension since version v4.8.0.
Coroutine PHP PostgreSQL Client, support for using Postgres with OpenSwoole and coroutines.
You can use the provided Postgres client within a coroutine context without blocking the process.
You can enable Coroutine PHP PostgreSQL Client with option: --with-postgres[=DIR].
If you are using Open Swoole < v4.8.0, you can install ext-postgresql.
libpq is required.Ubuntu
You may use sudo apt-get install libpq-dev.
CentOS
You may use yum install postgresql10-devel.
Other
You may specify a custom location for libpq with ./configure --with-postgres[=DIR].
Once you have OpenSwoole installed, you can compile and install the ext-postgresql client:
git clone [email protected]:openswoole/ext-postgresql.git
phpize
./configure
make && make install
You must choose a version from the release page which is compatible with OpenSwoole's version
The client is split into two classes:
OpenSwoole\Coroutine\PostgreSQL: the connection. It connects to the server, runs queries, prepares statements, escapes values and manages large objects.OpenSwoole\Coroutine\PostgreSQLStatement: the result of query() or prepare(). Rows are fetched from the statement, and prepared statements are run with execute().query() and prepare() return a PostgreSQLStatement object on success or false on failure.
General Example
<?php
use OpenSwoole\Coroutine\PostgreSQL;
co::run(function()
{
$pg = new PostgreSQL();
$conn = $pg->connect("host=127.0.0.1 port=5432 dbname=test user=postgres password=***");
if(!$conn)
{
var_dump($pg->error);
return;
}
$stmt = $pg->query('SELECT * FROM test;');
$arr = $stmt->fetchAll();
var_dump($arr);
});
Prepared Statement
<?php
use OpenSwoole\Coroutine\PostgreSQL;
co::run(function()
{
$pg = new PostgreSQL();
$pg->connect("host=127.0.0.1 port=5432 dbname=test user=postgres password=***");
$stmt = $pg->prepare('SELECT * FROM test WHERE id > $1 AND id < $2');
$stmt->execute([1, 3]);
var_dump($stmt->fetchAll());
});
Transaction Processing
<?php
use OpenSwoole\Coroutine\PostgreSQL;
co::run(function()
{
$pg = new PostgreSQL();
$pg->connect("host=127.0.0.1 port=5432 dbname=test user=postgres password=***");
$pg->query('BEGIN');
$stmt = $pg->query('SELECT * FROM test');
$arr = $stmt->fetchAll();
$pg->query('COMMIT');
var_dump($arr);
});
OpenSwoole\Coroutine\PostgreSQL
OpenSwoole\Coroutine\PostgreSQL->connect(...)OpenSwoole\Coroutine\PostgreSQL->query(...)OpenSwoole\Coroutine\PostgreSQL->prepare(...)OpenSwoole\Coroutine\PostgreSQL->escape(...)OpenSwoole\Coroutine\PostgreSQL->escapeLiteral(...)OpenSwoole\Coroutine\PostgreSQL->escapeIdentifier(...)OpenSwoole\Coroutine\PostgreSQL->metaData(...)OpenSwoole\Coroutine\PostgreSQL->createLOB()OpenSwoole\Coroutine\PostgreSQL->openLOB(...)OpenSwoole\Coroutine\PostgreSQL->unlinkLOB(...)OpenSwoole\Coroutine\PostgreSQL->status()OpenSwoole\Coroutine\PostgreSQL->reset(...)OpenSwoole\Coroutine\PostgreSQLStatement
OpenSwoole\Coroutine\PostgreSQLStatement->execute(...)OpenSwoole\Coroutine\PostgreSQLStatement->fetchAll(...)OpenSwoole\Coroutine\PostgreSQLStatement->fetchRow(...)OpenSwoole\Coroutine\PostgreSQLStatement->fetchAssoc(...)OpenSwoole\Coroutine\PostgreSQLStatement->fetchArray(...)OpenSwoole\Coroutine\PostgreSQLStatement->fetchObject(...)OpenSwoole\Coroutine\PostgreSQLStatement->affectedRows()OpenSwoole\Coroutine\PostgreSQLStatement->numRows()OpenSwoole\Coroutine\PostgreSQLStatement->fieldCount()Both PostgreSQL and PostgreSQLStatement objects have these public properties, updated after each request:
error: the last error message, or null when the last request succeedederrCode: the error code reported by escape() when it failsresultStatus: the result status of the last query, one of the OPENSWOOLE_PGRES_* constants belowresultDiag: an array of diagnostic fields (severity, sqlstate, message_primary, message_detail, message_hint, ...) when the last query failed, otherwise nullnotices: on the PostgreSQL object, an array of notice messages sent by the serverThe following query result status constants are available:
OPENSWOOLE_PGRES_EMPTY_QUERYOPENSWOOLE_PGRES_COMMAND_OKOPENSWOOLE_PGRES_TUPLES_OKOPENSWOOLE_PGRES_BAD_RESPONSEOPENSWOOLE_PGRES_NONFATAL_ERROROPENSWOOLE_PGRES_FATAL_ERRORYou can check the result status with the resultStatus property of the client or the statement.
<?php
use OpenSwoole\Coroutine\PostgreSQL;
co::run(function()
{
$pg = new PostgreSQL();
$pg->connect("host=127.0.0.1 port=5432 dbname=test user=postgres password=***");
$stmt = $pg->query('SELECT 1');
var_dump($pg->resultStatus);
if (!in_array($pg->resultStatus, [OPENSWOOLE_PGRES_COMMAND_OK, OPENSWOOLE_PGRES_TUPLES_OK])) {
throw new Exception("PG client error: " . $pg->error);
}
});
<?php
use OpenSwoole\Coroutine\PostgreSQL;
co::run(function()
{
$pg = new PostgreSQL();
$conn = $pg->connect("host=127.0.0.1 port=5432 dbname=test user=postgres password=***");
if(!$conn)
{
var_dump($pg->error);
return;
}
$stmt = $pg->query('SELECT * FROM weather;');
if(!$stmt)
{
var_dump($pg->error, $pg->resultDiag);
return;
}
$arr = $stmt->fetchAll();
var_dump($arr);
$stmt = $pg->prepare('SELECT * FROM weather WHERE id = $1');
if(!$stmt)
{
var_dump($pg->error);
return;
}
if(!$stmt->execute([1]))
{
var_dump($stmt->error, $stmt->resultDiag);
return;
}
var_dump($stmt->fetchAll());
});
From the example we can use $pg->error and $stmt->error to output any problems we have and check what went wrong.
All network requests (establish a connection, send data and receive data) may time out.
As each OpenSwoole client is written using coroutines, their timeouts are set the same way, refer to the timeout guide to understand how to setup timeouts.