pg: Add query tracing & prepare/execute time measurements

What I'd really like, in addition to this, is a way to extract a query
from an $st object that can be run in the psql CLI. VNDB has a debugging
feature for that, but it's less trivial to make that work with binary
query parameters.
This commit is contained in:
Yorhel 2025-02-22 15:14:51 +01:00
parent a5f9584b02
commit b2d676b1ed
6 changed files with 269 additions and 54 deletions

View file

@ -401,6 +401,46 @@ subtest 'Prepared statement cache', sub {
ok !defined numexec('SELECT 2');
};
subtest 'Tracing', sub {
my @log;
$conn->query_trace(sub($st) { push @log, $st });
is_deeply $conn->q('SELECT 1 AS a, $1 AS b', 123)->rowa, [ 1, 123 ];
is scalar @log, 1;
my $st = shift @log;
is ref $st, 'FU::Pg::st';
is_deeply $st->param_types, [ 25 ];
is_deeply $st->param_values, [ 123 ];
is_deeply $st->columns, [{ name => 'a', oid => 23 }, { name => 'b', oid => 25 }];
is $st->nrows, 1;
is $st->query, 'SELECT 1 AS a, $1 AS b';
ok $st->exec_time > 0 && $st->exec_time < 1;
ok $st->prepare_time > 0 && $st->prepare_time < 1;
ok !$st->get_cache;
ok $st->text_params;
ok $st->text_results;
$conn->exec('SET client_encoding=UTF8');
is scalar @log, 1;
$st = shift @log;
is ref $st, 'FU::Pg::st';
is_deeply $st->param_types, [];
is_deeply $st->param_values, [];
is_deeply $st->columns, [];
ok !defined $st->nrows;
is $st->query, 'SET client_encoding=UTF8';
ok $st->exec_time > 0 && $st->exec_time < 1;
ok !defined $st->prepare_time;
ok !$st->get_cache;
ok $st->text_params;
ok $st->text_results;
$conn->query_trace(undef);
$conn->exec('SELECT 1');
is scalar @log, 0;
};
{
my $st = $conn->q("SELECT 1");
undef $conn; # statement keeps the connection alive