Skip to content
Closed
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
6 changes: 6 additions & 0 deletions ext/pdo_pgsql/pgsql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ static void pgsql_handle_closer(pdo_dbh_t *dbh) /* {{{ */
PQfinish(H->server);
H->server = NULL;
}
if (H->cached_table_name) {
H->cached_table_oid = InvalidOid;
efree(H->cached_table_name);
H->cached_table_name = NULL;
}
if (H->einfo.errmsg) {
pefree(H->einfo.errmsg, dbh->is_persistent);
H->einfo.errmsg = NULL;
Expand Down Expand Up @@ -1542,6 +1547,7 @@ static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{

H->attached = 1;
H->pgoid = -1;
H->cached_table_oid = InvalidOid;

dbh->methods = &pgsql_methods;
dbh->alloc_own_columns = 1;
Expand Down
44 changes: 36 additions & 8 deletions ext/pdo_pgsql/pgsql_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ static void pgsql_stmt_finish(pdo_pgsql_stmt *S, int fin_mode)

S->is_prepared = false;
}

if ((fin_mode & (FIN_CLOSE|FIN_ABORT)) && H->running_stmt == S) {
H->running_stmt = NULL;
}
}

static int pgsql_stmt_dtor(pdo_stmt_t *stmt)
Expand All @@ -204,10 +208,6 @@ static int pgsql_stmt_dtor(pdo_stmt_t *stmt)

pgsql_stmt_finish(S, FIN_DISCARD|(server_obj_usable ? FIN_CLOSE|FIN_ABORT : 0));

if (server_obj_usable && S->H->running_stmt == S) {
S->H->running_stmt = NULL;
}

if (S->stmt_name) {
efree(S->stmt_name);
S->stmt_name = NULL;
Expand Down Expand Up @@ -271,9 +271,8 @@ static int pgsql_stmt_execute(pdo_stmt_t *stmt)
* and returns a PGRES_FATAL_ERROR when PQgetResult gets called for stmt 2 if DEALLOCATE
* was called for stmt 1 inbetween
* (maybe it will change with pipeline mode in libpq 14?) */
if (S->is_unbuffered && H->running_stmt) {
if (H->running_stmt && H->running_stmt->is_unbuffered) {
pgsql_stmt_finish(H->running_stmt, FIN_CLOSE);
H->running_stmt = NULL;
}
/* ensure that we free any previous unfetched results */
pgsql_stmt_finish(S, 0);
Expand Down Expand Up @@ -814,12 +813,35 @@ 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 (oid == InvalidOid) {
/* InvalidOid is emitted when not selecting from a table (e.g. "SELECT 1"). No need of
* a database roundtrip.
*/
return NULL;
}
if (oid == H->cached_table_oid) {
return H->cached_table_name ? estrdup(H->cached_table_name) : 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;
}

if (H->cached_table_name) {
efree(H->cached_table_name);
H->cached_table_name = NULL;
H->cached_table_oid = InvalidOid;
}

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 All @@ -836,6 +858,8 @@ static zend_always_inline char * pdo_pgsql_translate_oid_to_table(Oid oid, PGcon
return 0;
}

H->cached_table_oid = oid;
H->cached_table_name = estrdup(table_name);
table_name = estrdup(table_name);

PQclear(tmp_res);
Expand Down Expand Up @@ -864,7 +888,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 @@ -906,6 +930,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
2 changes: 2 additions & 0 deletions ext/pdo_pgsql/php_pdo_pgsql_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ typedef struct {
unsigned _reserved:31;
pdo_pgsql_error_info einfo;
Oid pgoid;
Oid cached_table_oid;
char *cached_table_name;
unsigned int stmt_counter;
bool emulate_prepares;
bool disable_prepares;
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