-
Notifications
You must be signed in to change notification settings - Fork 8.1k
ext/pdo_pgsql: Add Pdo\Pgsql::ATTR_CHUNK_SIZE for chunked result fetching
#23210
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
Open
KentarouTakeda
wants to merge
2
commits into
php:master
Choose a base branch
from
KentarouTakeda:pdo-pgsql-attr-chunk-size
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -285,6 +297,46 @@ static bool pgsql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t * | |
| scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR, | ||
| PDO_CURSOR_FWDONLY) == PDO_CURSOR_SCROLL; | ||
|
|
||
| S->is_unbuffered = | ||
| driver_options | ||
| && (val = zend_hash_index_find(Z_ARRVAL_P(driver_options), PDO_ATTR_PREFETCH)) | ||
| && pdo_get_long_param(&lval, val) | ||
| ? !lval | ||
| : 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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that kind of bother me but do not know why yet, I ll have a better look this week end |
||
| S->is_unbuffered = true; | ||
| } | ||
| #endif | ||
|
|
||
| if (scrollable) { | ||
| if (S->cursor_name) { | ||
| efree(S->cursor_name); | ||
|
|
@@ -310,14 +362,6 @@ static bool pgsql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t * | |
| stmt->named_rewrite_template = "$%d"; | ||
| } | ||
|
|
||
| S->is_unbuffered = | ||
| driver_options | ||
| && (val = zend_hash_index_find(Z_ARRVAL_P(driver_options), PDO_ATTR_PREFETCH)) | ||
| && pdo_get_long_param(&lval, val) | ||
| ? !lval | ||
| : H->default_fetching_laziness | ||
| ; | ||
|
|
||
| ret = pdo_parse_params(stmt, sql, &nsql); | ||
|
|
||
| if (ret == -1) { | ||
|
|
@@ -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)); | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| --TEST-- | ||
| PDO PgSQL Pdo\Pgsql::ATTR_CHUNK_SIZE splits a result set into chunks of rows | ||
| --EXTENSIONS-- | ||
| pdo_pgsql | ||
| --SKIPIF-- | ||
| <?php | ||
| require __DIR__ . '/config.inc'; | ||
| require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc'; | ||
| PDOTest::skip(); | ||
| if (!defined('Pdo\Pgsql::ATTR_CHUNK_SIZE')) die('skip libpq >= 17 required'); | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc'; | ||
| $pdo = PDOTest::test_factory(__DIR__ . '/common.phpt'); | ||
| $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
|
|
||
| // rowCount() reports the size of the chunk being consumed while fetching unbuffered | ||
| function run(PDO $pdo, string $label, array $options): void | ||
| { | ||
| $stmt = $pdo->prepare("SELECT * FROM generate_series(1, 10)", $options); | ||
| $stmt->execute(); | ||
|
|
||
| $values = []; | ||
| $sizes = []; | ||
|
|
||
| while (($row = $stmt->fetch(PDO::FETCH_NUM))) { | ||
| $values[] = $row[0]; | ||
| $sizes[] = $stmt->rowCount(); | ||
| } | ||
|
|
||
| printf("%s\n values=%s\n chunk sizes=%s\n", | ||
| $label, implode(',', $values), implode(',', $sizes)); | ||
| } | ||
|
|
||
| run($pdo, 'buffered (default)', []); | ||
| run($pdo, 'ATTR_PREFETCH => 0', [PDO::ATTR_PREFETCH => 0]); | ||
| run($pdo, 'ATTR_CHUNK_SIZE => 1', [Pdo\Pgsql::ATTR_CHUNK_SIZE => 1]); | ||
| run($pdo, 'ATTR_CHUNK_SIZE => 4 (last chunk is partial)', [Pdo\Pgsql::ATTR_CHUNK_SIZE => 4]); | ||
| run($pdo, 'ATTR_CHUNK_SIZE => 5 (divides evenly)', [Pdo\Pgsql::ATTR_CHUNK_SIZE => 5]); | ||
| run($pdo, 'ATTR_CHUNK_SIZE => 99 (larger than the result)', [Pdo\Pgsql::ATTR_CHUNK_SIZE => 99]); | ||
|
|
||
| ?> | ||
| --EXPECT-- | ||
| buffered (default) | ||
| values=1,2,3,4,5,6,7,8,9,10 | ||
| chunk sizes=10,10,10,10,10,10,10,10,10,10 | ||
| ATTR_PREFETCH => 0 | ||
| values=1,2,3,4,5,6,7,8,9,10 | ||
| chunk sizes=1,1,1,1,1,1,1,1,1,1 | ||
| ATTR_CHUNK_SIZE => 1 | ||
| values=1,2,3,4,5,6,7,8,9,10 | ||
| chunk sizes=1,1,1,1,1,1,1,1,1,1 | ||
| ATTR_CHUNK_SIZE => 4 (last chunk is partial) | ||
| values=1,2,3,4,5,6,7,8,9,10 | ||
| chunk sizes=4,4,4,4,4,4,4,4,2,2 | ||
| ATTR_CHUNK_SIZE => 5 (divides evenly) | ||
| values=1,2,3,4,5,6,7,8,9,10 | ||
| chunk sizes=5,5,5,5,5,5,5,5,5,5 | ||
| ATTR_CHUNK_SIZE => 99 (larger than the result) | ||
| values=1,2,3,4,5,6,7,8,9,10 | ||
| chunk sizes=10,10,10,10,10,10,10,10,10,10 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: there is a leak with S->cursor_name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 6bc7647. I moved the new code and the values it depends on above the
S->cursor_nameallocation. Thanks.