Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions ext/pdo_pgsql/pgsql_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,12 +793,19 @@ static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, zval *result, enum pd
return 1;
}

static zend_always_inline char * pdo_pgsql_translate_oid_to_table(Oid oid, PGconn *conn)
static zend_always_inline char * pdo_pgsql_translate_oid_to_table(Oid oid, pdo_pgsql_db_handle *H)
{
PGconn *conn = H->server;
char *table_name = NULL;
PGresult *tmp_res;
char *querystr = NULL;

if (H->running_stmt && H->running_stmt->is_unbuffered) {
/* in single-row mode, libpq forbids passing a new query
* while we're still flushing the current one's result */
return NULL;
}

spprintf(&querystr, 0, "SELECT RELNAME FROM PG_CLASS WHERE OID=%d", oid);

if ((tmp_res = PQexec(conn, querystr)) == NULL || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) {
Expand Down Expand Up @@ -843,7 +850,7 @@ static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *r

table_oid = PQftable(S->result, colno);
add_assoc_long(return_value, "pgsql:table_oid", table_oid);
table_name = pdo_pgsql_translate_oid_to_table(table_oid, S->H->server);
table_name = pdo_pgsql_translate_oid_to_table(table_oid, S->H);
if (table_name) {
add_assoc_string(return_value, "table", table_name);
efree(table_name);
Expand Down Expand Up @@ -885,6 +892,10 @@ static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *r
break;
default:
/* Fetch metadata from Postgres system catalogue */
if (S->H->running_stmt && S->H->running_stmt->is_unbuffered) {
/* libpq forbids calling a query while we're still reading the preceding one's */
break;
}
spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%u", S->cols[colno].pgsql_type);
res = PQexec(S->H->server, q);
efree(q);
Expand Down
15 changes: 15 additions & 0 deletions ext/pdo_pgsql/tests/gh15287.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ $res = []; while (($re = $stmt->fetch())) $res[] = $re; display($res);
$stmt->execute([ 0 ]);
$res = []; for ($i = -1; ++$i < 2;) $res[] = $stmt->fetch(); display($res);
display($pdo->query("select * from t2")->fetchAll());

// Metadata calls the server for some operations (notably table oid-to-name conversion).
// This will break libpq (that forbids a second PQexec before we consumed the first one).
// Instead of either letting libpq return an error, or blindly forbid this call, we expect
// being transparently provided at least attributes which do not require a server roundtrip.
// And good news: column name is one of those "local" attributes.
echo "=== meta ===\n";
$stmt = $pdo->query("select * from t limit 2");
echo "Starting with column " . $stmt->getColumnMeta(0)['name'] . ":\n";
display($stmt->fetchAll());

?>
--EXPECTF--
=== non regression ===
Expand Down Expand Up @@ -181,3 +192,7 @@ multiple calls to the same prepared statement, some interrupted before having re
0
1
678 ok
=== meta ===
Starting with column n:
0 original
1 non original
Loading