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
12 changes: 12 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ PHP NEWS
. Fixed bug GH-23016 (NULL values in long columns come back as garbage
binary strings). (Calvin Buckley, iliaal)

- PDO_PGSQL:
. Fixed an infinite loop when cleaning up a lazy fetch
(PDO::ATTR_PREFETCH => 0) left in a COPY. (KentarouTakeda)
. Fixed a use-after-free when a lazy statement with emulated or disabled
prepares is destroyed. (KentarouTakeda)
. Fixed a lazy fetch with emulated or disabled prepares leaving the
connection busy for the next one. (KentarouTakeda)
. Fixed a lazy fetch returning a row of NULLs after another statement
took over the connection. (KentarouTakeda)
. Added Pdo\Pgsql::ATTR_CHUNK_SIZE to fetch a result set in chunks of the
given number of rows. (KentarouTakeda)

- Readline:
. Fixed the interactive shell not waiting for the pager process to exit.
(Weilin Du)
Expand Down
5 changes: 5 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,11 @@ PHP 8.6 UPGRADE NOTES
outcome is reported as 'accepted', 'rejected' or 'not_sent' in the
early_data key of the crypto stream_get_meta_data() array.

- PDO_PGSQL:
. Added Pdo\Pgsql::ATTR_CHUNK_SIZE, the number of rows a statement fetches
per chunk. A value of 1 or more enters the lazy fetch mode of
PDO::ATTR_PREFETCH => 0. Requires libpq 17 or later.

- Phar:
. Overriding the getMTime() and getPathname() methods of SplFileInfo now
influences the result of the phar buildFrom family of functions.
Expand Down
14 changes: 14 additions & 0 deletions ext/pdo_pgsql/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ if test "$PHP_PDO_PGSQL" != "no"; then
or later).])],,
[$PGSQL_LIBS])

old_CFLAGS=$CFLAGS
CFLAGS="$CFLAGS $PGSQL_CFLAGS"

AC_CHECK_DECL([PGRES_TUPLES_CHUNK],
PHP_CHECK_LIBRARY([pq], [PQsetChunkedRowsMode],
[AC_DEFINE([HAVE_PG_SET_CHUNKED_ROWS_SIZE], [1],
[Define to 1 if libpq has the 'PQsetChunkedRowsMode' function (PostgreSQL
17 or later).])],,
[$PGSQL_LIBS]),,
[#include <libpq-fe.h>]
)

CFLAGS=$old_CFLAGS

PHP_CHECK_PDO_INCLUDES

PHP_NEW_EXTENSION([pdo_pgsql],
Expand Down
5 changes: 5 additions & 0 deletions ext/pdo_pgsql/pdo_pgsql.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class Pgsql extends \PDO
public const int ATTR_RESULT_MEMORY_SIZE = UNKNOWN;
#endif

#ifdef HAVE_PG_SET_CHUNKED_ROWS_SIZE
/** @cvalue PDO_PGSQL_ATTR_CHUNK_SIZE */
public const int ATTR_CHUNK_SIZE = UNKNOWN;
#endif

/** @cvalue PGSQL_TRANSACTION_IDLE */
#[\Deprecated(since: "8.5", message: "as it has no effect")]
public const int TRANSACTION_IDLE = UNKNOWN;
Expand Down
10 changes: 9 additions & 1 deletion ext/pdo_pgsql/pdo_pgsql_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions ext/pdo_pgsql/pgsql_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,18 @@ static void pgsql_handle_closer(pdo_dbh_t *dbh) /* {{{ */
}
/* }}} */

#ifdef HAVE_PG_SET_CHUNKED_ROWS_SIZE
static bool pdo_pgsql_check_chunk_size(zend_long size)
{
if (size < 0 || ZEND_LONG_EXCEEDS_INT(size)) {
zend_value_error("Pdo\\Pgsql::ATTR_CHUNK_SIZE must be between 0 and %d", INT_MAX);
return false;
}

return true;
}
#endif

static bool pgsql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
{
pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
Expand Down Expand Up @@ -318,6 +330,38 @@ static bool pgsql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *
: H->default_fetching_laziness
;

#ifdef HAVE_PG_SET_CHUNKED_ROWS_SIZE
bool chunk_size_given = driver_options
&& (val = zend_hash_index_find(Z_ARRVAL_P(driver_options), PDO_PGSQL_ATTR_CHUNK_SIZE));

if (chunk_size_given) {
if (!pdo_get_long_param(&lval, val)) {
return false;
}
S->chunk_size = lval;
} else {
S->chunk_size = H->default_chunk_size;
}

if (!pdo_pgsql_check_chunk_size(S->chunk_size)) {
return false;
}

if (S->chunk_size >= 1 && scrollable) {
if (chunk_size_given) {
zend_value_error("Pdo\\Pgsql::ATTR_CHUNK_SIZE cannot be combined with "
"PDO::ATTR_CURSOR set to PDO::CURSOR_SCROLL");
return false;
}

S->chunk_size = 0;
}

if (S->chunk_size >= 1) {
S->is_unbuffered = true;
}
#endif

ret = pdo_parse_params(stmt, sql, &nsql);

if (ret == -1) {
Expand Down Expand Up @@ -473,6 +517,12 @@ static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_
ZVAL_BOOL(return_value, H->disable_prepares);
break;

#ifdef HAVE_PG_SET_CHUNKED_ROWS_SIZE
case PDO_PGSQL_ATTR_CHUNK_SIZE:
ZVAL_LONG(return_value, H->default_chunk_size);
break;
#endif

case PDO_ATTR_CLIENT_VERSION: {
char buf[16];
pdo_libpq_version(buf, sizeof(buf));
Expand Down Expand Up @@ -1377,6 +1427,20 @@ static bool pdo_pgsql_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
}
H->default_fetching_laziness = !bval;
return true;
#ifdef HAVE_PG_SET_CHUNKED_ROWS_SIZE
case PDO_PGSQL_ATTR_CHUNK_SIZE: {
zend_long lval;

if (!pdo_get_long_param(&lval, val)) {
return false;
}
if (!pdo_pgsql_check_chunk_size(lval)) {
return false;
}
H->default_chunk_size = lval;
return true;
}
#endif
default:
return false;
}
Expand Down
79 changes: 68 additions & 11 deletions ext/pdo_pgsql/pgsql_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,33 @@
#define FIN_CLOSE 0x2
#define FIN_ABORT 0x4

static bool pgsql_result_status_ok(ExecStatusType status)
{
switch (status) {
case PGRES_COMMAND_OK:
case PGRES_TUPLES_OK:
case PGRES_SINGLE_TUPLE:
#ifdef HAVE_PG_SET_CHUNKED_ROWS_SIZE
case PGRES_TUPLES_CHUNK:
#endif
return true;
default:
return false;
}
}



static void pgsql_stmt_finish(pdo_pgsql_stmt *S, int fin_mode)
{
pdo_pgsql_db_handle *H = S->H;

if (S->is_running_unbuffered && S->result && (fin_mode & FIN_ABORT)) {
/* a buffered query may have already drained this statement's stream */
if (S->is_running_unbuffered && H->running_stmt == S && S->result && (fin_mode & FIN_ABORT)) {
PGcancel *cancel = PQgetCancel(H->server);
char errbuf[256];
PQcancel(cancel, errbuf, 256);
PQfreeCancel(cancel);
S->is_running_unbuffered = false;
}

if (S->result) {
Expand All @@ -78,7 +93,7 @@ static void pgsql_stmt_finish(pdo_pgsql_stmt *S, int fin_mode)
S->result = NULL;
}

if (S->is_running_unbuffered) {
if (S->is_running_unbuffered && H->running_stmt == S) {
/* https://postgresql.org/docs/current/libpq-async.html:
* "PQsendQuery cannot be called again until PQgetResult has returned NULL"
* And as all single-row functions are connection-wise instead of statement-wise,
Expand All @@ -88,8 +103,35 @@ static void pgsql_stmt_finish(pdo_pgsql_stmt *S, int fin_mode)
// instead of discarding results we could store them to their statement
// so that their fetch() will get them (albeit not in lazy mode anymore).
while ((S->result = PQgetResult(H->server))) {
ExecStatusType status = PQresultStatus(S->result);

PQclear(S->result);
S->result = NULL;

/* PQgetResult() keeps handing out the same result while the
* connection is copying: only these calls can end it */
if (status == PGRES_COPY_IN || status == PGRES_COPY_BOTH) {
/* fail a copy in, so that abandoning a statement cannot
* commit it; a replication stream only accepts a clean end */
const char *error = status == PGRES_COPY_IN
? "COPY terminated by PDO"
: NULL;

if (PQputCopyEnd(H->server, error) < 0) {
break;
}
}
if (status == PGRES_COPY_OUT || status == PGRES_COPY_BOTH) {
char *buf;
int nbytes;

while ((nbytes = PQgetCopyData(H->server, &buf, 0)) > 0) {
PQfreemem(buf);
}
if (nbytes < -1) {
break;
}
}
}
S->is_running_unbuffered = false;
}
Expand All @@ -111,9 +153,6 @@ static void pgsql_stmt_finish(pdo_pgsql_stmt *S, int fin_mode)
}

S->is_prepared = false;
if (H->running_stmt == S) {
H->running_stmt = NULL;
}
}
}

Expand All @@ -124,6 +163,10 @@ 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 @@ -326,16 +369,24 @@ static int pgsql_stmt_execute(pdo_stmt_t *stmt)
return 0;
}
S->is_running_unbuffered = true;
/* no matter if they return 0: PQ then transparently fallbacks to full result fetching */
#ifdef HAVE_PG_SET_CHUNKED_ROWS_SIZE
if (S->chunk_size >= 1) {
(void)PQsetChunkedRowsMode(H->server, (int)S->chunk_size);
} else {
(void)PQsetSingleRowMode(H->server);
}
#else
(void)PQsetSingleRowMode(H->server);
/* no matter if it returns 0: PQ then transparently fallbacks to full result fetching */
#endif

/* try a first fetch to at least have column names and so on */
S->result = PQgetResult(S->H->server);
}

status = PQresultStatus(S->result);

if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK && status != PGRES_SINGLE_TUPLE) {
if (!pgsql_result_status_ok(status)) {
pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
return 0;
}
Expand Down Expand Up @@ -579,7 +630,7 @@ static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
}
status = PQresultStatus(S->result);

if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK && status != PGRES_SINGLE_TUPLE) {
if (!pgsql_result_status_ok(status)) {
pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
return 0;
}
Expand All @@ -588,12 +639,12 @@ static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
S->current_row = 0;

if (!stmt->row_count) {
S->is_running_unbuffered = false;
/* libpq requires looping until getResult returns null */
pgsql_stmt_finish(S, 0);
}
}
if (S->current_row < stmt->row_count) {
/* another statement may have taken over and freed the result */
if (S->result && S->current_row < stmt->row_count) {
S->current_row++;
return 1;
} else {
Expand Down Expand Up @@ -856,6 +907,12 @@ static int pgsql_stmt_get_attr(pdo_stmt_t *stmt, zend_long attr, zval *val)
return 1;
#endif

#ifdef HAVE_PG_SET_CHUNKED_ROWS_SIZE
case PDO_PGSQL_ATTR_CHUNK_SIZE:
ZVAL_LONG(val, S->chunk_size);
return 1;
#endif

default:
(void)S;
return 0;
Expand Down
3 changes: 3 additions & 0 deletions ext/pdo_pgsql/php_pdo_pgsql_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef struct {
HashTable *lob_streams;
zend_fcall_info_cache *notice_callback;
bool default_fetching_laziness;
zend_long default_chunk_size;
pdo_pgsql_stmt *running_stmt;
} pdo_pgsql_db_handle;

Expand All @@ -66,6 +67,7 @@ struct pdo_pgsql_stmt {
int *param_formats;
Oid *param_types;
int current_row;
zend_long chunk_size;
bool is_prepared;
bool is_unbuffered;
bool is_running_unbuffered;
Expand Down Expand Up @@ -93,6 +95,7 @@ extern const struct pdo_stmt_methods pgsql_stmt_methods;
enum {
PDO_PGSQL_ATTR_DISABLE_PREPARES = PDO_ATTR_DRIVER_SPECIFIC,
PDO_PGSQL_ATTR_RESULT_MEMORY_SIZE,
PDO_PGSQL_ATTR_CHUNK_SIZE,
};

struct pdo_pgsql_lob_self {
Expand Down
Loading
Loading