Skip to content

ext/pgsql: adding pg_result_memory_size. #14214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
1 change: 1 addition & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ PHP 8.4 UPGRADE NOTES
3-parameter signature with a null $row parameter instead.
. Calling pg_field_is_null() with 2 arguments is deprecated. Use the
3-parameter signature with a null $row parameter instead.
. Added pg_result_memory_size to get the visibility the memory used by a query result.

- Reflection:
. Calling ReflectionMethod::__construct() with 1 argument is deprecated.
Expand Down
1 change: 1 addition & 0 deletions ext/pgsql/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ if test "$PHP_PGSQL" != "no"; then
AC_CHECK_LIB(pq, pg_encoding_to_char,AC_DEFINE(HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT,1,[Whether libpq is compiled with --enable-multibyte]))
AC_CHECK_LIB(pq, lo_truncate64, AC_DEFINE(HAVE_PG_LO64,1,[PostgreSQL 9.3 or later]))
AC_CHECK_LIB(pq, PQsetErrorContextVisibility, AC_DEFINE(HAVE_PG_CONTEXT_VISIBILITY,1,[PostgreSQL 9.6 or later]))
AC_CHECK_LIB(pq, PQresultMemorySize, AC_DEFINE(HAVE_PG_RESULT_MEMORY_SIZE,1,[PostgreSQL 12 or later]))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE:
PostgreSQL: Documentation: 12: E.20. Release 12
https://www.postgresql.org/docs/12/release-12.html#id-1.11.6.24.5.9

E.20.3.7. Client Interfaces

  • Add libpq function PQresultMemorySize() to report the memory used by a query result (Lars Kanis, Tom Lane)

LIBS=$old_LIBS
LDFLAGS=$old_LDFLAGS

Expand Down
17 changes: 17 additions & 0 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -2911,6 +2911,23 @@ PHP_FUNCTION(pg_set_error_context_visibility)
}
#endif

#ifdef HAVE_PG_RESULT_MEMORY_SIZE
PHP_FUNCTION(pg_result_memory_size)
{
zval *result;
pgsql_result_handle *pg_result;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &result, pgsql_result_ce) == FAILURE) {
RETURN_THROWS();
}

pg_result = Z_PGSQL_RESULT_P(result);
CHECK_PGSQL_RESULT(pg_result);

RETURN_LONG(PQresultMemorySize(pg_result->result));
}
#endif

/* {{{ Set client encoding */
PHP_FUNCTION(pg_set_client_encoding)
{
Expand Down
4 changes: 4 additions & 0 deletions ext/pgsql/pgsql.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,10 @@ function pg_select(PgSql\Connection $connection, string $table_name, array $cond
#ifdef HAVE_PG_CONTEXT_VISIBILITY
function pg_set_error_context_visibility(PgSql\Connection $connection, int $visibility): int {}
#endif

#ifdef HAVE_PG_RESULT_MEMORY_SIZE
function pg_result_memory_size(PgSql\Result $result): int {}
#endif
}

namespace PgSql {
Expand Down
14 changes: 13 additions & 1 deletion ext/pgsql/pgsql_arginfo.h

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

29 changes: 29 additions & 0 deletions ext/pgsql/tests/pg_result_memory_size.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
pg_result_memory_size
--EXTENSIONS--
pgsql
--SKIPIF--
<?php
include("inc/skipif.inc");
if (!function_exists('pg_result_memory_size')) die('skip function pg_result_memory_size() does not exist');
?>
--FILE--
<?php
include('inc/config.inc');

$db = pg_connect($conn_str);

$result = pg_query($db, 'select 1');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: is the memory usage deterministic right ? If that s the case if I redo this query it should be the same amount correct ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The memory usage returned by PQresultMemorySize() for the same query is generally consistent, but this cannot be guaranteed in all cases due to several factors:

  • Version of libpq: Although significant differences are unlikely, the lack of documented specifications of libpq means we cannot ensure identical behavior across all versions.
  • CPU architecture: Differences in architecture could potentially influence memory usage.
  • Query purity: Non-deterministic elements, such as the output of VOLATILE functions or results from INSERT INTO .. RETURNING ... / UPDATE ... RETURNING, can affect the memory used.

Based on some tests with simple queries, the memory usage has been consistent when none of these variables are at play. However, I must note that I am not deeply familiar with the internal implementation of libpq, so while my response is based on research and tests, it might not cover all possible scenarios.

(For the above reasons, I did not write the output size directly in the test, but instead tested by comparing the two results)

Copy link
Member

@devnexen devnexen May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detail but what I meant if within your test if I do

$result = pg_query($db, 'select 1');
$size_3 = pg_result_memory_size($result);
var_dump($size_1 == $size_3);

it should be true then ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, true in that case.
(Although not guaranteed, I don't think this behavior will change.)

$size_1 = pg_result_memory_size($result);

$result = pg_query($db, "select generate_series(1, 10000) as i, repeat('string', 100)");
$size_2 = pg_result_memory_size($result);

var_dump($size_1);
var_dump($size_2);
var_dump($size_1 < $size_2);
?>
--EXPECTF--
int(%d)
int(%d)
bool(true)
Loading