From 5492f6a2b814d01ad50a9d7e07f41db09ba77be1 Mon Sep 17 00:00:00 2001 From: Hubert depesz Lubaczewski Date: Mon, 7 Mar 2022 13:15:51 +0100 Subject: [PATCH] Dont use cursor for getting plans Cursors cause bug in Pg15 --- scripts/find_interesting_plans | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/scripts/find_interesting_plans b/scripts/find_interesting_plans index d0f22fd..ab5a3fb 100755 --- a/scripts/find_interesting_plans +++ b/scripts/find_interesting_plans @@ -68,11 +68,12 @@ for my $partno ( 0 .. $#{ $partitions } ) { my $errors = 0; my $interesting = 0; - my $query = sprintf "select id, plan FROM %s WHERE is_public and NOT is_deleted and NOT is_anonymized and plan ~ 'actual time=[0-9]'", $part; - $dbh->do( "DECLARE csr CURSOR FOR $query" ); + my $prev_id = ''; + my $query = sprintf "select id, plan FROM %s WHERE is_public and NOT is_deleted and NOT is_anonymized and plan ~ 'actual time=[0-9]' AND id > ? order by ID ASC LIMIT 100", $part; while ( 1 ) { - my $rows = $dbh->selectall_arrayref( 'FETCH 100 FROM csr', { 'Slice' => {} } ); + my $rows = $dbh->selectall_arrayref( $query, { 'Slice' => {} }, $prev_id ); last if 0 == scalar @{ $rows }; + $prev_id = $rows->[ -1 ]->{ 'id' }; for my $row ( @{ $rows } ) { $seen++; my $explain; @@ -88,8 +89,8 @@ for my $partno ( 0 .. $#{ $partitions } ) { my $is_interesting = 0; - for my $line ( @{ output_lines( $row->{'id'}, $explain ) } ) { - print $out join("\t", @{$line}) . "\n"; + for my $line ( @{ output_lines( $row->{ 'id' }, $explain ) } ) { + print $out join( "\t", @{ $line } ) . "\n"; $is_interesting = 1; } @@ -111,7 +112,8 @@ exit; sub output_lines { my ( $id, $plan ) = @_; - my $ret = []; + my $ret = []; + my $plan_len = length( $plan->source ); for my $node ( $plan->top_node, $plan->top_node->all_recursive_subnodes ) { next unless $node->estimated_row_width; @@ -120,16 +122,17 @@ sub output_lines { next unless $node->extra_info; # At least 3 pages worth of data is returned - next unless $node->total_rows * $node->estimated_row_width > 3 * 8192; + next unless ( $node->total_rows + $node->total_rows_removed ) * $node->estimated_row_width > 3 * 8192; # At least 90% of rows were removed - next unless $node->total_rows_removed > 9 * $node->total_rows; + next unless $node->total_rows_removed > 2 * $node->total_rows; my @filter_lines = grep { /^Filter:/ } @{ $node->extra_info }; + # There are filter expressions next if 0 == scalar @filter_lines; - push @{ $ret }, map { [ $id, $node->id, $node->type, $node->estimated_row_width, $node->total_rows, $node->total_rows_removed, $_ ] } @filter_lines; + push @{ $ret }, map { [ $id, $plan_len, $node->type, $_ ] } @filter_lines; } return $ret; }