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

Conversation

KentarouTakeda
Copy link
Contributor

Retrieve the memory usage of the query result resource.

Returns the value of PQresultMemorySize() provided by the client library libpq as is. The memory here is allocated internally by libpq, so until now there was no way for the user to directly check it.

Is an RFC necessary to introduce this feature? Although pg_set_error_context_visibility() added in #11395 did not require an RFC, please confirm if one is needed for this case.

@@ -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)

@devnexen
Copy link
Member

Retrieve the memory usage of the query result resource.

Returns the value of PQresultMemorySize() provided by the client library libpq as is. The memory here is allocated internally by libpq, so until now there was no way for the user to directly check it.

Is an RFC necessary to introduce this feature? Although pg_set_error_context_visibility() added in #11395 did not require an RFC, please confirm if one is needed for this case.

No, it s not necessary in this case neither.

@devnexen
Copy link
Member

Looks pretty straightforward and correct, will look again later today.


$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.)

Copy link
Member

@devnexen devnexen left a comment

Choose a reason for hiding this comment

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

Looking good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants