Skip to content

Commit 77fa4c0

Browse files
KentarouTakedadevnexen
authored andcommitted
ext/pgsql: add pg_result_memory_size
Close GH-14214
1 parent 44ed17c commit 77fa4c0

File tree

7 files changed

+67
-1
lines changed

7 files changed

+67
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ PHP NEWS
183183
. Added the possibility to have no conditions for pg_select. (OmarEmaraDev)
184184
. Persistent connections support the PGSQL_CONNECT_FORCE_RENEW flag.
185185
(David Carlier)
186+
. Added pg_result_memory_size to get the query result memory usage.
187+
(KentarouTakeda)
186188

187189
- Phar:
188190
. Fixed bug GH-12532 (PharData created from zip has incorrect timestamp).

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ PHP 8.4 UPGRADE NOTES
339339
3-parameter signature with a null $row parameter instead.
340340
. Calling pg_field_is_null() with 2 arguments is deprecated. Use the
341341
3-parameter signature with a null $row parameter instead.
342+
. Added pg_result_memory_size to get the visibility the memory used by a query result.
342343

343344
- Reflection:
344345
. Calling ReflectionMethod::__construct() with 1 argument is deprecated.

ext/pgsql/config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ if test "$PHP_PGSQL" != "no"; then
6666
AC_CHECK_LIB(pq, pg_encoding_to_char,AC_DEFINE(HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT,1,[Whether libpq is compiled with --enable-multibyte]))
6767
AC_CHECK_LIB(pq, lo_truncate64, AC_DEFINE(HAVE_PG_LO64,1,[PostgreSQL 9.3 or later]))
6868
AC_CHECK_LIB(pq, PQsetErrorContextVisibility, AC_DEFINE(HAVE_PG_CONTEXT_VISIBILITY,1,[PostgreSQL 9.6 or later]))
69+
AC_CHECK_LIB(pq, PQresultMemorySize, AC_DEFINE(HAVE_PG_RESULT_MEMORY_SIZE,1,[PostgreSQL 12 or later]))
6970
LIBS=$old_LIBS
7071
LDFLAGS=$old_LDFLAGS
7172

ext/pgsql/pgsql.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2911,6 +2911,23 @@ PHP_FUNCTION(pg_set_error_context_visibility)
29112911
}
29122912
#endif
29132913

2914+
#ifdef HAVE_PG_RESULT_MEMORY_SIZE
2915+
PHP_FUNCTION(pg_result_memory_size)
2916+
{
2917+
zval *result;
2918+
pgsql_result_handle *pg_result;
2919+
2920+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &result, pgsql_result_ce) == FAILURE) {
2921+
RETURN_THROWS();
2922+
}
2923+
2924+
pg_result = Z_PGSQL_RESULT_P(result);
2925+
CHECK_PGSQL_RESULT(pg_result);
2926+
2927+
RETURN_LONG(PQresultMemorySize(pg_result->result));
2928+
}
2929+
#endif
2930+
29142931
/* {{{ Set client encoding */
29152932
PHP_FUNCTION(pg_set_client_encoding)
29162933
{

ext/pgsql/pgsql.stub.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,10 @@ function pg_select(PgSql\Connection $connection, string $table_name, array $cond
943943
#ifdef HAVE_PG_CONTEXT_VISIBILITY
944944
function pg_set_error_context_visibility(PgSql\Connection $connection, int $visibility): int {}
945945
#endif
946+
947+
#ifdef HAVE_PG_RESULT_MEMORY_SIZE
948+
function pg_result_memory_size(PgSql\Result $result): int {}
949+
#endif
946950
}
947951

948952
namespace PgSql {

ext/pgsql/pgsql_arginfo.h

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
pg_result_memory_size
3+
--EXTENSIONS--
4+
pgsql
5+
--SKIPIF--
6+
<?php
7+
include("inc/skipif.inc");
8+
if (!function_exists('pg_result_memory_size')) die('skip function pg_result_memory_size() does not exist');
9+
?>
10+
--FILE--
11+
<?php
12+
include('inc/config.inc');
13+
14+
$db = pg_connect($conn_str);
15+
16+
$result = pg_query($db, 'select 1');
17+
$size_1 = pg_result_memory_size($result);
18+
19+
$result = pg_query($db, "select generate_series(1, 10000) as i, repeat('string', 100)");
20+
$size_2 = pg_result_memory_size($result);
21+
22+
var_dump($size_1);
23+
var_dump($size_2);
24+
var_dump($size_1 < $size_2);
25+
?>
26+
--EXPECTF--
27+
int(%d)
28+
int(%d)
29+
bool(true)

0 commit comments

Comments
 (0)