MCPcopy Create free account
hub / github.com/apache/cloudberry / ExecQueryUsingCursor

Function ExecQueryUsingCursor

src/bin/psql/common.c:1584–1836  ·  view source on GitHub ↗

* ExecQueryUsingCursor: run a SELECT-like query using a cursor * * This feature allows result sets larger than RAM to be dealt with. * * Returns true if the query executed successfully, false otherwise. * * If pset.timing is on, total query time (exclusive of result-printing) is * stored into *elapsed_msec. */

Source from the content-addressed store, hash-verified

1582 * stored into *elapsed_msec.
1583 */
1584static bool
1585ExecQueryUsingCursor(const char *query, double *elapsed_msec)
1586{
1587 bool OK = true;
1588 PGresult *results;
1589 PQExpBufferData buf;
1590 printQueryOpt my_popt = pset.popt;
1591 FILE *fout;
1592 bool is_pipe;
1593 bool is_pager = false;
1594 bool started_txn = false;
1595 int64 total_tuples = 0;
1596 int ntuples;
1597 int fetch_count;
1598 char fetch_cmd[64];
1599 instr_time before,
1600 after;
1601 int flush_error;
1602
1603 *elapsed_msec = 0;
1604
1605 /* initialize print options for partial table output */
1606 my_popt.topt.start_table = true;
1607 my_popt.topt.stop_table = false;
1608 my_popt.topt.prior_records = 0;
1609
1610 if (pset.timing)
1611 INSTR_TIME_SET_CURRENT(before);
1612
1613 /* if we're not in a transaction, start one */
1614 if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
1615 {
1616 results = PQexec(pset.db, "BEGIN");
1617 OK = AcceptResult(results) &&
1618 (PQresultStatus(results) == PGRES_COMMAND_OK);
1619 ClearOrSaveResult(results);
1620 if (!OK)
1621 return false;
1622 started_txn = true;
1623 }
1624
1625 /* Send DECLARE CURSOR */
1626 initPQExpBuffer(&buf);
1627 appendPQExpBuffer(&buf, "DECLARE _psql_cursor NO SCROLL CURSOR FOR\n%s",
1628 query);
1629
1630 results = PQexec(pset.db, buf.data);
1631 OK = AcceptResult(results) &&
1632 (PQresultStatus(results) == PGRES_COMMAND_OK);
1633 if (!OK)
1634 SetResultVariables(results, OK);
1635 ClearOrSaveResult(results);
1636 termPQExpBuffer(&buf);
1637 if (!OK)
1638 goto cleanup;
1639
1640 if (pset.timing)
1641 {

Callers 1

SendQueryFunction · 0.85

Calls 15

PQtransactionStatusFunction · 0.85
PQexecFunction · 0.85
AcceptResultFunction · 0.85
PQresultStatusFunction · 0.85
ClearOrSaveResultFunction · 0.85
initPQExpBufferFunction · 0.85
appendPQExpBufferFunction · 0.85
SetResultVariablesFunction · 0.85
termPQExpBufferFunction · 0.85
openQueryOutputFileFunction · 0.85
disable_sigpipe_trapFunction · 0.85
ClosePagerFunction · 0.85

Tested by

no test coverage detected