github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/scripts/plandiff (about)

     1  #!/bin/bash
     2  set -euo pipefail
     3  
     4  cd "$(dirname $0)/.."
     5  if [[ $# < 4 || $# > 5 ]]; then
     6      cat 1>&2 <<EOF
     7  $0 compares plans of newline-separated queries in queryfile by running EXPLAIN
     8  (VERBOSE) <query> (or a user-provided explain variant) against each cockroach
     9  server specified.
    10  Note that this tool assumes the server is up and running in insecure mode.
    11  Usage: $0 path/to/cockroach/binary queryfile oldpgurl newpgurl [explain variant]
    12  EOF
    13      exit 1
    14  fi
    15  
    16  COCKROACH_BINARY=$1
    17  QUERYFILE=$2
    18  OLDPGURL=$3
    19  NEWPGURL=$4
    20  if [[ $# < 5 ]]; then
    21      EXPLAIN_VARIANT="EXPLAIN (VERBOSE)"
    22  else
    23      EXPLAIN_VARIANT=$5
    24  fi
    25  
    26  dest=$(mktemp -d)
    27  echo "Writing query plans to ${dest}"
    28  
    29  pgurls=($OLDPGURL $NEWPGURL)
    30  
    31  querynum=0
    32  while read line
    33  do
    34      for pgurl in "${pgurls[@]}"
    35      do
    36          version="old"
    37          if [[ $pgurl != $OLDPGURL ]]; then
    38              version="new"
    39          fi
    40  
    41          # Run the sql client displaying the results in table format for easy
    42          # to read output. The sed command removes the line "(n rows)" from the
    43          # output.
    44          $COCKROACH_BINARY sql --insecure --url=$pgurl --format=table -e "$EXPLAIN_VARIANT $line" | sed '/^(/ d' > $dest/query$querynum.$version
    45      done
    46  
    47      # Diff the query plans.
    48      tmpfile=$(mktemp)
    49      if ! diff "$dest/query$querynum.old" "$dest/query$querynum.new" > $tmpfile; then
    50          echo "found difference in plans for query $querynum: $line"
    51          cat $tmpfile
    52          echo ""
    53      fi
    54      rm $tmpfile
    55      ((querynum++))
    56  done < $QUERYFILE