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 73     73   1004 use v5.18;
  73         256  
5 73     73   404 use strict;
  73         145  
  73         1583  
6 73     73   347 use warnings;
  73         179  
  73         2021  
7 73     73   369 use warnings qw( FATAL utf8 );
  73         136  
  73         2198  
8 73     73   393 use utf8;
  73         139  
  73         365  
9 73     73   1832 use open qw( :std :utf8 );
  73         145  
  73         327  
10 73     73   9221 use Unicode::Normalize qw( NFC );
  73         170  
  73         3297  
11 73     73   419 use Unicode::Collate;
  73         139  
  73         1659  
12 73     73   372 use Encode qw( decode );
  73         138  
  73         2896  
13 73     73   33906 use English qw( -no_match_vars );
  73         250457  
  73         404  
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 73     73   42803 use Carp;
  73         153  
  73         4176  
22 73     73   40740 use Pg::Explain::Node;
  73         207  
  73         3223  
23 73     73   33472 use Pg::Explain::Buffers;
  73         192  
  73         2502  
24 73     73   33855 use Pg::Explain::JIT;
  73         233  
  73         232506  
25              
26             =head1 NAME
27              
28             Pg::Explain::FromText - Parser for text based explains
29              
30             =head1 VERSION
31              
32             Version 2.3
33              
34             =cut
35              
36             our $VERSION = '2.3';
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 300     300 1 723 my $class = shift;
54 300         777 my $self = bless {}, $class;
55 300         783 return $self;
56             }
57              
58             =head2 explain
59              
60             Get/Set master explain object.
61              
62             =cut
63              
64 1572 100   1572 1 2631 sub explain { my $self = shift; $self->{ 'explain' } = $_[ 0 ] if 0 < scalar @_; return $self->{ 'explain' }; }
  1572         4104  
  1572         5369  
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 300     300 1 530 my $self = shift;
74 300         494 my $source = shift;
75              
76 300         5295 my @lines = split /\r?\n/, $source;
77              
78 300         844 my @out = ();
79 300         746 for my $l ( @lines ) {
80              
81             # Ignore certain lines
82 2381 100       6077 next if $l =~ m{\A \s* \( \d+ \s+ rows? \) \s* \z}xms;
83 2380 50       6511 next if $l =~ m{\A \s* query \s plan \s* \z}xmsi;
84 2380 100       8622 next if $l =~ m{\A \s* (?: -+ | ─+ ) \s* \z}xms;
85              
86 2378 100       8557 if ( $l =~ m{ \A Trigger \s+ }xms ) {
    100          
    100          
87 18         36 push @out, $l;
88             }
89             elsif ( $l =~ m{ \A (?: Total \s+ runtime | Planning \s+ time | Execution \s+ time | Time | Filter | Output | JIT | Planning | Settings | Query \s+ Identifier ): }xmsi ) {
90 167         435 push @out, $l;
91             }
92             elsif ( $l =~ m{\A\S} ) {
93 231 100       654 if ( 0 < scalar @out ) {
94 26         103 $out[ -1 ] .= $l;
95             }
96             else {
97 205         543 push @out, $l;
98             }
99             }
100             else {
101 1962         3767 push @out, $l;
102             }
103             }
104              
105 300         1424 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 300     300 1 543 my $self = shift;
119 300         571 my $source = shift;
120              
121             # Store jit text info, and flag whether we're in JIT parsing phase
122 300         532 my $jit = undef;
123 300         512 my $in_jit = undef;
124              
125             # Store information about planning buffers
126 300         471 my $planning_buffers = undef;
127 300         455 my $in_planning = undef;
128              
129 300         495 my $top_node = undef;
130 300         637 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 300         1014 my @lines = $self->split_into_lines( $source );
133              
134 300         1551 my $costs_re = qr{ \( cost=(?\d+\.\d+)\.\.(?\d+\.\d+) \s+ rows=(?\d+) \s+ width=(?\d+) \) }xms;
135 300         1125 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 300         895 my $guc_name = qr{ [a-zA-Z_.]+ }xms;
146 300         908 my $guc_value = qr{ ' (?:[^']+|'' )* ' }xms;
147 300         4997 my $single_guc = qr{ ( $guc_name ) \s* = \s* ( $guc_value ) }xms;
148 300         4561 my $multiple_gucs = qr{ $single_guc (?: , \s* $single_guc )* }xms;
149              
150 300         715 my $query = '';
151 300         528 my $plan_started = 0;
152             LINE:
153 300         672 for my $line ( @lines ) {
154              
155             # Remove trailing whitespace - it makes next line matches MUCH faster.
156 2352         13090 $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 2352         5190 $line =~ s/\s*"\z//;
160              
161             # Replace tabs with 4 spaces
162 2352         4848 $line =~ s/\t/ /g;
163              
164 2352 100 100     65267 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 970         2081 $plan_started = 1;
186              
187 970         16641 my $new_node = Pg::Explain::Node->new( %+ );
188 970         4927 $new_node->explain( $self->explain );
189 970 100       5186 if ( defined $+{ 'never_executed' } ) {
190 22         80 $new_node->actual_loops( 0 );
191 22         58 $new_node->never_executed( 1 );
192             }
193 970         3520 my $element = { 'node' => $new_node, 'subelement-type' => 'subnode', };
194              
195 970         1747 $in_jit = undef;
196              
197 970         3614 my $prefix = $+{ 'prefix' };
198 970         5079 $prefix =~ s/->.*//;
199 970         2360 my $prefix_length = length $prefix;
200              
201 970 100       2599 if ( 0 == scalar keys %element_at_depth ) {
202 300         679 $element_at_depth{ '0' } = $element;
203 300         502 $top_node = $new_node;
204 300         1308 next LINE;
205             }
206 670         2597 my @existing_depths = sort { $a <=> $b } keys %element_at_depth;
  2822         5182  
207 670         1595 for my $key ( grep { $_ >= $prefix_length } @existing_depths ) {
  2209         4448  
208 320         829 delete $element_at_depth{ $key };
209             }
210              
211 670         1783 my $maximal_depth = ( sort { $b <=> $a } keys %element_at_depth )[ 0 ];
  2145         3392  
212 670 50       1744 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 670         1189 my $previous_element = $element_at_depth{ $maximal_depth };
216              
217 670         1620 $element_at_depth{ $prefix_length } = $element;
218              
219 670 100       2001 if ( $previous_element->{ 'subelement-type' } eq 'subnode' ) {
    100          
    100          
    50          
220 584         1899 $previous_element->{ 'node' }->add_sub_node( $new_node );
221             }
222             elsif ( $previous_element->{ 'subelement-type' } eq 'initplan' ) {
223 31         152 $previous_element->{ 'node' }->add_initplan( $new_node, $previous_element->{ 'metainfo' } );
224             }
225             elsif ( $previous_element->{ 'subelement-type' } eq 'subplan' ) {
226 32         158 $previous_element->{ 'node' }->add_subplan( $new_node );
227             }
228             elsif ( $previous_element->{ 'subelement-type' } =~ /^cte:(.+)$/ ) {
229 23         119 $previous_element->{ 'node' }->add_cte( $1, $new_node );
230 23         108 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         175 my ( $prefix, $name, $returns ) = ( $1, $2, $3 );
238 29         61 $in_jit = undef;
239              
240 29         88 my @remove_elements = grep { $_ >= length $prefix } keys %element_at_depth;
  63         200  
241 29 100       116 delete @element_at_depth{ @remove_elements } unless 0 == scalar @remove_elements;
242              
243 29         105 my $maximal_depth = ( sort { $b <=> $a } keys %element_at_depth )[ 0 ];
  61         98  
244 29 50       86 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         63 my $previous_element = $element_at_depth{ $maximal_depth };
248              
249 29         57 my $metainfo = {};
250 29 100       97 $metainfo->{ 'name' } = $name if defined $name;
251 29 100       122 $metainfo->{ 'returns' } = $returns if defined $returns;
252 29 100       50 $metainfo = undef if 0 == scalar keys %{ $metainfo };
  29         109  
253              
254             $element_at_depth{ 1 + length $prefix } = {
255 29         214 'node' => $previous_element->{ 'node' },
256             'subelement-type' => 'initplan',
257             'metainfo' => $metainfo,
258             };
259 29         124 next LINE;
260             }
261             elsif ( $line =~ m{ \A (\s*) SubPlan \s* (?: \d+ \s* )? \s* (?: \( returns .* \) \s* )? \z }xms ) {
262 28         90 my $prefix = $1;
263              
264 28         54 $in_jit = undef;
265              
266 28         77 my @remove_elements = grep { $_ >= length $prefix } keys %element_at_depth;
  82         236  
267 28 100       155 delete @element_at_depth{ @remove_elements } unless 0 == scalar @remove_elements;
268              
269 28         96 my $maximal_depth = ( sort { $b <=> $a } keys %element_at_depth )[ 0 ];
  33         60  
270 28 50       88 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         65 my $previous_element = $element_at_depth{ $maximal_depth };
274              
275             $element_at_depth{ 1 + length $prefix } = {
276 28         140 'node' => $previous_element->{ 'node' },
277             'subelement-type' => 'subplan',
278             };
279 28         110 next LINE;
280             }
281             elsif ( $line =~ m{ \A (\s*) CTE \s+ (\S+) \s* \z }xms ) {
282 23         108 my ( $prefix, $cte_name ) = ( $1, $2 );
283              
284 23         46 $in_jit = undef;
285              
286 23         69 my @remove_elements = grep { $_ >= length $prefix } keys %element_at_depth;
  32         123  
287 23 100       100 delete @element_at_depth{ @remove_elements } unless 0 == scalar @remove_elements;
288              
289 23         79 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         55 my $previous_element = $element_at_depth{ $maximal_depth };
294              
295             $element_at_depth{ length $prefix } = {
296 23         128 'node' => $previous_element->{ 'node' },
297             'subelement-type' => 'cte:' . $cte_name,
298             };
299              
300 23         83 next LINE;
301             }
302             elsif ( $line =~ m{ \A \s* (Planning|Execution) \s+ time: \s+ (\d+\.\d+) \s+ ms \s* \z }xmsi ) {
303 176         621 my ( $type, $time ) = ( $1, $2 );
304              
305 176         300 $in_jit = undef;
306              
307 176 100       715 $self->explain->planning_time( $time ) if 'planning' eq lc( $type );
308 176 100       742 $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         217 my ( $time ) = ( $1 );
312              
313 52         103 $in_jit = undef;
314              
315 52         159 $self->explain->total_runtime( $time );
316             }
317             elsif ( $line =~ m{ \A \s* Settings: \s* ( $multiple_gucs ) \s* \z }xmsi ) {
318 3         11 my $gucs = $1;
319 3         8 my $settings = {};
320 3         86 my @elements = $gucs =~ m{ $single_guc }xmsg;
321 3         27 for ( my $i = 0 ; $i < @elements ; $i += 2 ) {
322 5         14 my $val = $elements[ $i + 1 ];
323 5         27 $val =~ s/\A'|'\z//g;
324 5         17 $val =~ s/''/'/g;
325 5         24 $settings->{ $elements[ $i ] } = $val;
326             }
327 3 50       9 $self->explain->settings( $settings ) if 0 < scalar keys %{ $settings };
  3         17  
328             }
329             elsif ( $line =~ m{ \A \s* Trigger \s+ (.*) : \s+ time=(\d+\.\d+) \s+ calls=(\d+) \s* \z }xmsi ) {
330 20         94 my ( $name, $time, $calls ) = ( $1, $2, $3 );
331              
332 20         29 $in_jit = undef;
333              
334 20         40 $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         23 $in_jit = 1;
344 6         22 $jit = [ $line ];
345             }
346             elsif ( $line =~ m{ \A (\s*) Planning: \s* \z }xmsi ) {
347 11         38 $in_planning = 1;
348             }
349             elsif ( $line =~ m{ \A \s* Query \s+ Text: \s+ ( .* ) \z }xms ) {
350 4         19 $query = $1;
351 4         15 $plan_started = 0;
352             }
353             elsif ( $plan_started == 0 ) {
354 34         185 $query = "$query\n$line";
355             }
356             elsif ( $line =~ m{ \A (\s*) ( \S .* \S ) \s* \z }xms ) {
357 991         4026 my ( $infoprefix, $info ) = ( $1, $2 );
358 991 100       2059 if ( $in_jit ) {
359 18         27 push @{ $jit }, $line;
  18         37  
360 18         46 next LINE;
361             }
362 973         2420 my $maximal_depth = ( sort { $b <=> $a } grep { $_ < length $infoprefix } keys %element_at_depth )[ 0 ];
  3232         4797  
  2851         8118  
363 973 100       2449 next LINE unless defined $maximal_depth;
364 968         1762 my $previous_element = $element_at_depth{ $maximal_depth };
365 968 50       1926 next LINE unless $previous_element;
366 968         1495 my $node = $previous_element->{ 'node' };
367 968 100       5191 if ( $info =~ m{ \A Workers \s+ Launched: \s+ ( \d+ ) \z }xmsi ) {
    100          
    100          
368 16         80 $node->workers_launched( $1 );
369 16         52 $node->add_extra_info( $info );
370             }
371             elsif ( $info =~ m{ \A Buffers: \s }xms ) {
372 98         178 eval {
373 98         458 my $buffers = Pg::Explain::Buffers->new( $info );
374 98 100       292 if ( $in_planning ) {
375 11         36 $planning_buffers = $buffers;
376             }
377             else {
378 87         259 $node->buffers( $buffers );
379             }
380             };
381 98 50       329 $node->add_extra_info( $info ) if $EVAL_ERROR;
382             }
383             elsif ( $info =~ m{ \A I/O \s Timings: \s }xms ) {
384 26         48 eval {
385 26 100       68 if ( $in_planning ) {
386 8 50       34 $planning_buffers->add_timing( $info ) if $planning_buffers;
387             }
388             else {
389 18 50       57 $node->buffers->add_timing( $info ) if $node->buffers;
390             }
391             };
392 26 50       119 $node->add_extra_info( $info ) if $EVAL_ERROR;
393             }
394             else {
395 828         2759 $node->add_extra_info( $info );
396             }
397             }
398             }
399 300 100       848 $self->explain->jit( Pg::Explain::JIT->new( 'lines' => $jit ) ) if defined $jit;
400 300 100       858 $self->explain->query( $query ) if $query;
401 300 100       754 $self->explain->planning_buffers( $planning_buffers ) if $planning_buffers;
402 300         2875 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