Skip to content

Commit 2a16121

Browse files
author
Etsuro Fujita
committed
postgres_fdw: Remove redundancy in postgresAcquireSampleRowsFunc().
Previously, in the loop in postgresAcquireSampleRowsFunc() to iterate fetching rows from a given remote table, we redundantly 1) determined the fetch size by parsing the table's server/table-level options and then 2) constructed the fetch command; remove that redundancy. Author: Etsuro Fujita Reviewed-by: Julien Rouhaud Discussion: https://postgr.es/m/CAPmGK17_urk9qkLV65_iYMFw64z5qhdfhY=tMVV6Jg4KNYx8+w@mail.gmail.com
1 parent e72489e commit 2a16121

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4490,20 +4490,51 @@ postgresAcquireSampleRowsFunc(Relation relation, int elevel,
44904490
/* In what follows, do not risk leaking any PGresults. */
44914491
PG_TRY();
44924492
{
4493+
char fetch_sql[64];
4494+
int fetch_size;
4495+
ListCell *lc;
4496+
44934497
res = pgfdw_exec_query(conn, sql.data);
44944498
if (PQresultStatus(res) != PGRES_COMMAND_OK)
44954499
pgfdw_report_error(ERROR, res, conn, false, sql.data);
44964500
PQclear(res);
44974501
res = NULL;
44984502

4503+
/*
4504+
* Determine the fetch size. The default is arbitrary, but shouldn't
4505+
* be enormous.
4506+
*/
4507+
fetch_size = 100;
4508+
foreach(lc, server->options)
4509+
{
4510+
DefElem *def = (DefElem *) lfirst(lc);
4511+
4512+
if (strcmp(def->defname, "fetch_size") == 0)
4513+
{
4514+
fetch_size = strtol(defGetString(def), NULL, 10);
4515+
break;
4516+
}
4517+
}
4518+
foreach(lc, table->options)
4519+
{
4520+
DefElem *def = (DefElem *) lfirst(lc);
4521+
4522+
if (strcmp(def->defname, "fetch_size") == 0)
4523+
{
4524+
fetch_size = strtol(defGetString(def), NULL, 10);
4525+
break;
4526+
}
4527+
}
4528+
4529+
/* Construct command to fetch rows from remote. */
4530+
snprintf(fetch_sql, sizeof(fetch_sql), "FETCH %d FROM c%u",
4531+
fetch_size, cursor_number);
4532+
44994533
/* Retrieve and process rows a batch at a time. */
45004534
for (;;)
45014535
{
4502-
char fetch_sql[64];
4503-
int fetch_size;
45044536
int numrows;
45054537
int i;
4506-
ListCell *lc;
45074538

45084539
/* Allow users to cancel long query */
45094540
CHECK_FOR_INTERRUPTS();
@@ -4514,33 +4545,7 @@ postgresAcquireSampleRowsFunc(Relation relation, int elevel,
45144545
* then just adjust rowstoskip and samplerows appropriately.
45154546
*/
45164547

4517-
/* The fetch size is arbitrary, but shouldn't be enormous. */
4518-
fetch_size = 100;
4519-
foreach(lc, server->options)
4520-
{
4521-
DefElem *def = (DefElem *) lfirst(lc);
4522-
4523-
if (strcmp(def->defname, "fetch_size") == 0)
4524-
{
4525-
fetch_size = strtol(defGetString(def), NULL, 10);
4526-
break;
4527-
}
4528-
}
4529-
foreach(lc, table->options)
4530-
{
4531-
DefElem *def = (DefElem *) lfirst(lc);
4532-
4533-
if (strcmp(def->defname, "fetch_size") == 0)
4534-
{
4535-
fetch_size = strtol(defGetString(def), NULL, 10);
4536-
break;
4537-
}
4538-
}
4539-
45404548
/* Fetch some rows */
4541-
snprintf(fetch_sql, sizeof(fetch_sql), "FETCH %d FROM c%u",
4542-
fetch_size, cursor_number);
4543-
45444549
res = pgfdw_exec_query(conn, fetch_sql);
45454550
/* On error, report the original query, not the FETCH. */
45464551
if (PQresultStatus(res) != PGRES_TUPLES_OK)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy