DBD-KB
view release on metacpan or search on metacpan
sleep 0.1;
}
print "The query has finished. Gathering results\n";
my $result = $sth->pg_result;
print "Result: $result\n";
my $info = $sth->fetchall_arrayref();
Without asynchronous queries, the above script would take about 8 seconds to run: five seconds waiting
for the execute to finish, then three for the check_on_the_kids() function to return. With asynchronous
queries, the script takes about 6 seconds to run, and gets in two iterations of check_on_the_kids in
the process.
Here's an example showing the ability to cancel a long-running query. Imagine two slave databases in
different geographic locations over a slow network. You need information as quickly as possible, so
you query both at once. When you get an answer, you tell the other one to stop working on your query,
as you don't need it anymore.
use strict;
use warnings;
use Time::HiRes 'sleep';
imp_sth->PQfmts = NULL;
imp_sth->PQoids = NULL;
imp_sth->prepared_by_us = DBDPG_FALSE; /* Set to 1 when actually done preparing */
imp_sth->direct = DBDPG_FALSE;
imp_sth->is_dml = DBDPG_FALSE; /* Not preparable DML until proved otherwise */
imp_sth->has_binary = DBDPG_FALSE; /* Are any of the params binary? */
imp_sth->has_default = DBDPG_FALSE; /* Are any of the params DEFAULT? */
imp_sth->has_current = DBDPG_FALSE; /* Are any of the params DEFAULT? */
imp_sth->use_inout = DBDPG_FALSE; /* Are any of the placeholders using inout? */
imp_sth->all_bound = DBDPG_FALSE; /* Have all placeholders been bound? */
imp_sth->number_iterations = 0;
/* We inherit some preferences from the database handle */
imp_sth->server_prepare = imp_dbh->server_prepare;
imp_sth->switch_prepared = imp_dbh->switch_prepared;
imp_sth->prepare_now = imp_dbh->prepare_now;
imp_sth->dollaronly = imp_dbh->dollaronly;
imp_sth->nocolons = imp_dbh->nocolons;
/* Parse and set any attributes passed in */
if (attribs) {
THEADER_slow,
imp_sth->is_dml,
imp_sth->direct,
imp_sth->server_prepare,
imp_sth->numbound,
imp_sth->numphs,
imp_sth->has_default,
imp_sth->has_current);
/* Increment our count */
imp_sth->number_iterations++;
/* We use PQexec if:
1. The statement is *not* DML (e.g. is DDL, which cannot be prepared)
2. We have a DEFAULT parameter
3. We have a CURRENT parameter
4. pg_direct is true
5. There are no placeholders
6. pg_server_prepare is false
*/
if (!imp_sth->is_dml
|| imp_sth->has_default
|| imp_sth->has_current
|| imp_sth->direct
|| !imp_sth->numphs
|| !imp_sth->server_prepare
)
pqtype = PQTYPE_EXEC;
else if (0==imp_sth->switch_prepared || imp_sth->number_iterations < imp_sth->switch_prepared) {
pqtype = PQTYPE_PARAMS;
}
else {
pqtype = PQTYPE_PREPARED;
}
/* We use the new server_side prepare style if:
1. The statement is DML (DDL is not preparable)
2. The attribute "pg_direct" is false
3. The attribute "pg_server_prepare" is true
PLACEHOLDER_COLON
} PGPlaceholderType;
#define PLACEHOLDER_TYPE_COUNT (PLACEHOLDER_COLON + 1)
/* Define sth implementor data structure */
struct imp_sth_st {
dbih_stc_t com; /* MUST be first element in structure */
bool server_prepare; /* inherited from dbh */
int switch_prepared; /* inherited from dbh */
int number_iterations; /* how many times has the statement been executed? Used by switch_prepared */
PGPlaceholderType placeholder_type; /* which style is being used 1=? 2=$1 3=:foo */
int numsegs; /* how many segments this statement has */
int numphs; /* how many placeholders this statement has */
int numbound; /* how many placeholders were explicitly bound by the client, not us */
int cur_tuple; /* current tuple being fetched */
long rows; /* number of affected rows */
int async_flag; /* async? 0=no 1=async 2=cancel 4=wait */
int async_status; /* 0=no async 1=async started -1=async has been cancelled */
STRLEN totalsize; /* total string length of the statement (with no placeholders)*/
( run in 1.340 second using v1.01-cache-2.11-cpan-71847e10f99 )