File Coverage

blib/lib/Pg/Explain/FromText.pm
Criterion Covered Total %
statement 211 216 97.6
branch 98 110 89.0
condition 3 3 100.0
subroutine 18 18 100.0
pod 4 4 100.0
total 334 351 95.1


line stmt bran cond sub pod time code
1             package Pg::Explain::FromText;
2              
3             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
4 72     72   811 use v5.18;
  72         208  
5 72     72   335 use strict;
  72         122  
  72         1316  
6 72     72   290 use warnings;
  72         132  
  72         1854  
7 72     72   304 use warnings qw( FATAL utf8 );
  72         105  
  72         1934  
8 72     72   306 use utf8;
  72         111  
  72         345  
9 72     72   1497 use open qw( :std :utf8 );
  72         108  
  72         341  
10 72     72   8042 use Unicode::Normalize qw( NFC );
  72         127  
  72         2887  
11 72     72   358 use Unicode::Collate;
  72         109  
  72         1365  
12 72     72   274 use Encode qw( decode );
  72         116  
  72         2481  
13 72     72   29339 use English qw( -no_match_vars );
  72         207835  
  72         355  
14              
15             if ( grep /\P{ASCII}/ => @ARGV ) {
16             @ARGV = map { decode( 'UTF-8', $_ ) } @ARGV;
17             }
18              
19             # UTF8 boilerplace, per http://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/
20              
21 72     72   35795 use Carp;
  72         126  
  72         3536  
22 72     72   34730 use Pg::Explain::Node;
  72         159  
  72         2889  
23 72     72   31091 use Pg::Explain::Buffers;
  72         162  
  72         2324  
24 72     72   31537 use Pg::Explain::JIT;
  72         165  
  72         188265  
25              
26             =head1 NAME
27              
28             Pg::Explain::FromText - Parser for text based explains
29              
30             =head1 VERSION
31              
32             Version 2.2
33              
34             =cut
35              
36             our $VERSION = '2.2';
37              
38             =head1 SYNOPSIS
39              
40             It's internal class to wrap some work. It should be used by Pg::Explain, and not directly.
41              
42             =head1 FUNCTIONS
43              
44             =head2 new
45              
46             Object constructor.
47              
48             This is not really useful in this particular class, but it's to have the same API for all Pg::Explain::From* classes.
49              
50             =cut
51              
52             sub new {
53 299     299 1 674 my $class = shift;
54 299         688 my $self = bless {}, $class;
55 299         665 return $self;
56             }
57              
58             =head2 explain
59              
60             Get/Set master explain object.
61              
62             =cut
63              
64 1566 100   1566 1 2173 sub explain { my $self = shift; $self->{ 'explain' } = $_[ 0 ] if 0 < scalar @_; return $self->{ 'explain' }; }
  1566         4235  
  1566         4581  
65              
66             =head2 split_into_lines
67              
68             Splits source into lines, while fixing (well, trying to fix) cases where input has been force-wrapped to some length.
69              
70             =cut
71              
72             sub split_into_lines {
73 299     299 1 453 my $self = shift;
74 299         438 my $source = shift;
75              
76 299         4167 my @lines = split /\r?\n/, $source;
77              
78 299         675 my @out = ();
79 299         633 for my $l ( @lines ) {
80              
81             # Ignore certain lines
82 2371 100       4996 next if $l =~ m{\A \s* \( \d+ \s+ rows? \) \s* \z}xms;
83 2370 50       5690 next if $l =~ m{\A \s* query \s plan \s* \z}xmsi;
84 2370 100       7022 next if $l =~ m{\A \s* (?: -+ | ─+ ) \s* \z}xms;
85              
86 2368 100       6863 if ( $l =~ m{ \A Trigger \s+ }xms ) {
    100          
    100          
87 18         34 push @out, $l;
88             }
89             elsif ( $l =~ m{ \A (?: Total \s+ runtime | Planning \s+ time | Execution \s+ time | Time | Filter | Output | JIT | Planning | Settings ): }xmsi ) {
90 163         378 push @out, $l;
91             }
92             elsif ( $l =~ m{\A\S} ) {
93 230 100       578 if ( 0 < scalar @out ) {
94 25         77 $out[ -1 ] .= $l;
95             }
96             else {
97 205         456 push @out, $l;
98             }
99             }
100             else {
101 1957         3108 push @out, $l;
102             }
103             }
104              
105 299         1154 return @out;
106             }
107              
108             =head2 parse_source
109              
110             Function which parses actual plan, and constructs Pg::Explain::Node objects
111             which represent it.
112              
113             Returns Top node of query plan.
114              
115             =cut
116              
117             sub parse_source {
118 299     299 1 521 my $self = shift;
119 299         495 my $source = shift;
120              
121             # Store jit text info, and flag whether we're in JIT parsing phase
122 299         465 my $jit = undef;
123 299         457 my $in_jit = undef;
124              
125             # Store information about planning buffers
126 299         431 my $planning_buffers = undef;
127 299         427 my $in_planning = undef;
128              
129 299         432 my $top_node = undef;
130 299         560 my %element_at_depth = (); # element is hashref, contains 2 keys: node (Pg::Explain::Node) and subelement-type, which can be: subnode, initplan or subplan.
131              
132 299         833 my @lines = $self->split_into_lines( $source );
133              
134 299         1373 my $costs_re = qr{ \( cost=(?\d+\.\d+)\.\.(?\d+\.\d+) \s+ rows=(?\d+) \s+ width=(?\d+) \) }xms;
135 299         924 my $analyze_re = qr{ \(
136             (?:
137             actual \s time=(?\d+\.\d+)\.\.(?\d+\.\d+) \s rows=(?\d+) \s loops=(?\d+)
138             |
139             actual \s rows=(?\d+) \s loops=(?\d+)
140             |
141             (? never \s+ executed )
142             )
143             \) }xms;
144              
145 299         804 my $guc_name = qr{ [a-zA-Z_.]+ }xms;
146 299         744 my $guc_value = qr{ ' (?:[^']+|'' )* ' }xms;
147 299         4151 my $single_guc = qr{ ( $guc_name ) \s* = \s* ( $guc_value ) }xms;
148 299         3777 my $multiple_gucs = qr{ $single_guc (?: , \s* $single_guc )* }xms;
149              
150 299         567 my $query = '';
151 299         451 my $plan_started = 0;
152             LINE:
153 299         560 for my $line ( @lines ) {
154              
155             # Remove trailing whitespace - it makes next line matches MUCH faster.
156 2343         10479 $line =~ s/\s+\z//;
157              
158             # There could be stray " at the end. No idea why, but some people paste such explains on explain.depesz.com
159 2343         4310 $line =~ s/\s*"\z//;
160              
161             # Replace tabs with 4 spaces
162 2343         3920 $line =~ s/\t/ /g;
163              
164 2343 100 100     53138 if (
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
165             ( $line =~ m{\(} )
166             && (
167             $line =~ m{
168             \A
169             (?\s* -> \s* | \s* )
170             (?\S.*?)
171             \s+
172             (?:
173             $costs_re \s+ $analyze_re
174             |
175             $costs_re
176             |
177             $analyze_re
178             )
179             \s*
180             \z
181             }xms
182             )
183             )
184             {
185 968         1811 $plan_started = 1;
186              
187 968         14601 my $new_node = Pg::Explain::Node->new( %+ );
188 968         4160 $new_node->explain( $self->explain );
189 968 100       4397 if ( defined $+{ 'never_executed' } ) {
190 22         63 $new_node->actual_loops( 0 );
191 22         41 $new_node->never_executed( 1 );
192             }
193 968         2913 my $element = { 'node' => $new_node, 'subelement-type' => 'subnode', };
194              
195 968         1406 $in_jit = undef;
196              
197 968         2997 my $prefix = $+{ 'prefix' };
198 968         4200 $prefix =~ s/->.*//;
199 968         2093 my $prefix_length = length $prefix;
200              
201 968 100       2278 if ( 0 == scalar keys %element_at_depth ) {
202 299         619 $element_at_depth{ '0' } = $element;
203 299         429 $top_node = $new_node;
204 299         1089 next LINE;
205             }
206 669         2211 my @existing_depths = sort { $a <=> $b } keys %element_at_depth;
  2799         4346  
207 669         1328 for my $key ( grep { $_ >= $prefix_length } @existing_depths ) {
  2208         3824  
208 320         723 delete $element_at_depth{ $key };
209             }
210              
211 669         1530 my $maximal_depth = ( sort { $b <=> $a } keys %element_at_depth )[ 0 ];
  2060         2620  
212 669 50       1430 if ( !defined $maximal_depth ) {
213 0         0 croak( "Didn't find current_element by depth - this shouldn't happen - please contact author.\n" );
214             }
215 669         976 my $previous_element = $element_at_depth{ $maximal_depth };
216              
217 669         1401 $element_at_depth{ $prefix_length } = $element;
218              
219 669 100       1668 if ( $previous_element->{ 'subelement-type' } eq 'subnode' ) {
    100          
    100          
    50          
220 583         1743 $previous_element->{ 'node' }->add_sub_node( $new_node );
221             }
222             elsif ( $previous_element->{ 'subelement-type' } eq 'initplan' ) {
223 31         108 $previous_element->{ 'node' }->add_initplan( $new_node, $previous_element->{ 'metainfo' } );
224             }
225             elsif ( $previous_element->{ 'subelement-type' } eq 'subplan' ) {
226 32         112 $previous_element->{ 'node' }->add_subplan( $new_node );
227             }
228             elsif ( $previous_element->{ 'subelement-type' } =~ /^cte:(.+)$/ ) {
229 23         99 $previous_element->{ 'node' }->add_cte( $1, $new_node );
230 23         111 delete $element_at_depth{ $maximal_depth };
231             }
232             else {
233 0         0 croak( "Bad subelement-type in previous_element - this shouldn't happen - please contact author.\n" );
234             }
235             }
236             elsif ( $line =~ m{ \A (\s*) InitPlan \s* ( \d+ )? \s* (?: \( returns \s+ (.*) \) \s* )? \z }xms ) {
237 29         116 my ( $prefix, $name, $returns ) = ( $1, $2, $3 );
238 29         50 $in_jit = undef;
239              
240 29         74 my @remove_elements = grep { $_ >= length $prefix } keys %element_at_depth;
  63         161  
241 29 100       86 delete @element_at_depth{ @remove_elements } unless 0 == scalar @remove_elements;
242              
243 29         89 my $maximal_depth = ( sort { $b <=> $a } keys %element_at_depth )[ 0 ];
  59         75  
244 29 50       66 if ( !defined $maximal_depth ) {
245 0         0 croak( "Didn't find current_element by depth - this shouldn't happen - please contact author (subplan).\n" );
246             }
247 29         49 my $previous_element = $element_at_depth{ $maximal_depth };
248              
249 29         47 my $metainfo = {};
250 29 100       83 $metainfo->{ 'name' } = $name if defined $name;
251 29 100       70 $metainfo->{ 'returns' } = $returns if defined $returns;
252 29 100       41 $metainfo = undef if 0 == scalar keys %{ $metainfo };
  29         87  
253              
254             $element_at_depth{ 1 + length $prefix } = {
255 29         155 'node' => $previous_element->{ 'node' },
256             'subelement-type' => 'initplan',
257             'metainfo' => $metainfo,
258             };
259 29         88 next LINE;
260             }
261             elsif ( $line =~ m{ \A (\s*) SubPlan \s* (?: \d+ \s* )? \s* (?: \( returns .* \) \s* )? \z }xms ) {
262 28         77 my $prefix = $1;
263              
264 28         48 $in_jit = undef;
265              
266 28         71 my @remove_elements = grep { $_ >= length $prefix } keys %element_at_depth;
  82         188  
267 28 100       119 delete @element_at_depth{ @remove_elements } unless 0 == scalar @remove_elements;
268              
269 28         79 my $maximal_depth = ( sort { $b <=> $a } keys %element_at_depth )[ 0 ];
  31         53  
270 28 50       74 if ( !defined $maximal_depth ) {
271 0         0 croak( "Didn't find current_element by depth - this shouldn't happen - please contact author (subplan).\n" );
272             }
273 28         59 my $previous_element = $element_at_depth{ $maximal_depth };
274              
275             $element_at_depth{ 1 + length $prefix } = {
276 28         125 'node' => $previous_element->{ 'node' },
277             'subelement-type' => 'subplan',
278             };
279 28         87 next LINE;
280             }
281             elsif ( $line =~ m{ \A (\s*) CTE \s+ (\S+) \s* \z }xms ) {
282 23         85 my ( $prefix, $cte_name ) = ( $1, $2 );
283              
284 23         39 $in_jit = undef;
285              
286 23         62 my @remove_elements = grep { $_ >= length $prefix } keys %element_at_depth;
  32         102  
287 23 100       85 delete @element_at_depth{ @remove_elements } unless 0 == scalar @remove_elements;
288              
289 23         93 my $maximal_depth = ( sort { $b <=> $a } keys %element_at_depth )[ 0 ];
  3         7  
290 23 50       61 if ( !defined $maximal_depth ) {
291 0         0 croak( "Didn't find current_element by depth - this shouldn't happen - please contact author (CTE).\n" );
292             }
293 23         45 my $previous_element = $element_at_depth{ $maximal_depth };
294              
295             $element_at_depth{ length $prefix } = {
296 23         112 'node' => $previous_element->{ 'node' },
297             'subelement-type' => 'cte:' . $cte_name,
298             };
299              
300 23         69 next LINE;
301             }
302             elsif ( $line =~ m{ \A \s* (Planning|Execution) \s+ time: \s+ (\d+\.\d+) \s+ ms \s* \z }xmsi ) {
303 174         504 my ( $type, $time ) = ( $1, $2 );
304              
305 174         252 $in_jit = undef;
306              
307 174 100       624 $self->explain->planning_time( $time ) if 'planning' eq lc( $type );
308 174 100       586 $self->explain->execution_time( $time ) if 'execution' eq lc( $type );
309             }
310             elsif ( $line =~ m{ \A \s* Total \s+ runtime: \s+ (\d+\.\d+) \s+ ms \s* \z }xmsi ) {
311 52         144 my ( $time ) = ( $1 );
312              
313 52         96 $in_jit = undef;
314              
315 52         125 $self->explain->total_runtime( $time );
316             }
317             elsif ( $line =~ m{ \A \s* Settings: \s* ( $multiple_gucs ) \s* \z }xmsi ) {
318 2         7 my $gucs = $1;
319 2         4 my $settings = {};
320 2         39 my @elements = $gucs =~ m{ $single_guc }xmsg;
321 2         9 for ( my $i = 0 ; $i < @elements ; $i += 2 ) {
322 4         6 my $val = $elements[ $i + 1 ];
323 4         43 $val =~ s/\A'|'\z//g;
324 4         10 $val =~ s/''/'/g;
325 4         17 $settings->{ $elements[ $i ] } = $val;
326             }
327 2 50       4 $self->explain->settings( $settings ) if 0 < scalar keys %{ $settings };
  2         9  
328             }
329             elsif ( $line =~ m{ \A \s* Trigger \s+ (.*) : \s+ time=(\d+\.\d+) \s+ calls=(\d+) \s* \z }xmsi ) {
330 20         121 my ( $name, $time, $calls ) = ( $1, $2, $3 );
331              
332 20         31 $in_jit = undef;
333              
334 20         37 $self->explain->add_trigger_time(
335             {
336             'name' => $name,
337             'time' => $time,
338             'calls' => $calls,
339             }
340             );
341             }
342             elsif ( $line =~ m{ \A (\s*) JIT: \s* \z }xmsi ) {
343 6         13 $in_jit = 1;
344 6         16 $jit = [ $line ];
345             }
346             elsif ( $line =~ m{ \A (\s*) Planning: \s* \z }xmsi ) {
347 11         27 $in_planning = 1;
348             }
349             elsif ( $line =~ m{ \A \s* Query \s+ Text: \s+ ( .* ) \z }xms ) {
350 4         16 $query = $1;
351 4         13 $plan_started = 0;
352             }
353             elsif ( $plan_started == 0 ) {
354 34         144 $query = "$query\n$line";
355             }
356             elsif ( $line =~ m{ \A (\s*) ( \S .* \S ) \s* \z }xms ) {
357 987         3333 my ( $infoprefix, $info ) = ( $1, $2 );
358 987 100       1735 if ( $in_jit ) {
359 18         23 push @{ $jit }, $line;
  18         36  
360 18         38 next LINE;
361             }
362 969         2050 my $maximal_depth = ( sort { $b <=> $a } grep { $_ < length $infoprefix } keys %element_at_depth )[ 0 ];
  3090         3859  
  2844         6730  
363 969 100       1994 next LINE unless defined $maximal_depth;
364 965         1450 my $previous_element = $element_at_depth{ $maximal_depth };
365 965 50       1622 next LINE unless $previous_element;
366 965         1306 my $node = $previous_element->{ 'node' };
367 965 100       4409 if ( $info =~ m{ \A Workers \s+ Launched: \s+ ( \d+ ) \z }xmsi ) {
    100          
    100          
368 16         67 $node->workers_launched( $1 );
369 16         39 $node->add_extra_info( $info );
370             }
371             elsif ( $info =~ m{ \A Buffers: \s }xms ) {
372 98         147 eval {
373 98         355 my $buffers = Pg::Explain::Buffers->new( $info );
374 98 100       232 if ( $in_planning ) {
375 11         19 $planning_buffers = $buffers;
376             }
377             else {
378 87         225 $node->buffers( $buffers );
379             }
380             };
381 98 50       271 $node->add_extra_info( $info ) if $EVAL_ERROR;
382             }
383             elsif ( $info =~ m{ \A I/O \s Timings: \s }xms ) {
384 26         38 eval {
385 26 100       40 if ( $in_planning ) {
386 8 50       22 $planning_buffers->add_timing( $info ) if $planning_buffers;
387             }
388             else {
389 18 50       39 $node->buffers->add_timing( $info ) if $node->buffers;
390             }
391             };
392 26 50       94 $node->add_extra_info( $info ) if $EVAL_ERROR;
393             }
394             else {
395 825         2150 $node->add_extra_info( $info );
396             }
397             }
398             }
399 299 100       762 $self->explain->jit( Pg::Explain::JIT->new( 'lines' => $jit ) ) if defined $jit;
400 299 100       701 $self->explain->query( $query ) if $query;
401 299 100       641 $self->explain->planning_buffers( $planning_buffers ) if $planning_buffers;
402 299         2304 return $top_node;
403             }
404              
405             =head1 AUTHOR
406              
407             hubert depesz lubaczewski, C<< >>
408              
409             =head1 BUGS
410              
411             Please report any bugs or feature requests to C.
412              
413             =head1 SUPPORT
414              
415             You can find documentation for this module with the perldoc command.
416              
417             perldoc Pg::Explain
418              
419             =head1 COPYRIGHT & LICENSE
420              
421             Copyright 2008-2021 hubert depesz lubaczewski, all rights reserved.
422              
423             This program is free software; you can redistribute it and/or modify it
424             under the same terms as Perl itself.
425              
426             =cut
427              
428             1; # End of Pg::Explain::FromText