| 1 | package DBIx::Fast::Profile::Pg; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use v5.38; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Object::Pad 0.807; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.21'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | class DBIx::Fast::Profile::Pg :isa(DBIx::Fast::Profile::Base) { | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 0 | ▶ | method init () { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | # Nothing to enable server-side: pg_stat_* views are always available. | ||||||||||||||||||||||||||||
| 12 | 1 | } | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # Same allowlist strategy as Profile::MariaDB, tuned for PostgreSQL: | ||||||||||||||||||||||||||||
| 15 | # only DML/CTE heads, no terminators or comments, and no file/DoS | ||||||||||||||||||||||||||||
| 16 | # functions. String literals are stripped under BOTH quoting conventions | ||||||||||||||||||||||||||||
| 17 | # (standard_conforming_strings on and off) before scanning, so a crafted | ||||||||||||||||||||||||||||
| 18 | # literal cannot hide a token in either server mode. | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | method _validate_explainable_sql ($sql_text) { | ||||||||||||||||||||||||||
|
Subroutine
_validate_explainable_sql: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | return unless defined $sql_text && length $sql_text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $sql_text and length $sql_text
MC/DC0%0/2
defined $sql_text and length $sql_text
defined $sql_text
length $sql_text
Truth table0%0/3
A and B
A= defined $sql_textB= length $sql_text
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | $sql_text =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | ▶ | return unless $sql_text =~ /^(?:SELECT|INSERT|UPDATE|DELETE|TABLE|WITH|VALUES)\b/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | return if $sql_text =~ m{;|--|/\*|\$\w*\$}; # terminators / comments / dollar-quoting | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | my @bare; | |||||||||||||||||||||||||||
| 29 | 0 | (my $b1 = $sql_text) =~ s/'(?:[^'\\]|\\.)*'//g; # backslash-escape parsing | |||||||||||||||||||||||||||
| 30 | 0 | $b1 =~ s/"(?:[^"\\]|\\.)*"//g; | |||||||||||||||||||||||||||
| 31 | 0 | (my $b2 = $sql_text) =~ s/'[^']*'//g; # standard_conforming_strings | |||||||||||||||||||||||||||
| 32 | 0 | $b2 =~ s/"[^"]*"//g; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | for my $bare ($b1, $b2) { | |||||||||||||||||||||||||||
| 35 | # File access / server-side reads and writes | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | return if $bare =~ /\bpg_(?:read_file|read_binary_file|ls_dir|stat_file)\b/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | return if $bare =~ /\blo_(?:import|export)\b/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | # EXPLAIN-time DoS | ||||||||||||||||||||||||||||
| 39 | 0 | ▶ | return if $bare =~ /\bpg_sleep(?:_for|_until)?\s*\(/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | return if $bare =~ /\bpg_advisory_lock\w*\s*\(/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | # Remote execution | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | return if $bare =~ /\bdblink\w*\s*\(/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | return $sql_text; | |||||||||||||||||||||||||||
| 46 | 1 | } | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Returns the plan as an arrayref of hashrefs with a single QUERY PLAN | ||||||||||||||||||||||||||||
| 49 | # column (PostgreSQL's native EXPLAIN output, one row per plan line). | ||||||||||||||||||||||||||||
| 50 | # Plain EXPLAIN never executes the statement. | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | method explain_query ($sql, @params) { | ||||||||||||||||||||||||||
|
Subroutine
explain_query: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 0 | my $safe = $self->_validate_explainable_sql($sql); | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | return unless $safe; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | return $self->dbix->all( "EXPLAIN $safe", @params ); | |||||||||||||||||||||||||||
| 55 | 1 | } | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Key names mirror Profile::MariaDB::get_table_stats where an equivalent | ||||||||||||||||||||||||||||
| 58 | # exists (table_name/table_rows/data_length/index_length); engine is the | ||||||||||||||||||||||||||||
| 59 | # table access method (heap unless a custom AM is in use). | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | method get_table_stats ($table) { | ||||||||||||||||||||||||||
|
Subroutine
get_table_stats: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 61 | 0 | return $self->dbix->all(q{ | |||||||||||||||||||||||||||
| 62 | SELECT c.relname AS table_name, | ||||||||||||||||||||||||||||
| 63 | am.amname AS engine, | ||||||||||||||||||||||||||||
| 64 | c.reltuples::bigint AS table_rows, | ||||||||||||||||||||||||||||
| 65 | pg_table_size(c.oid) AS data_length, | ||||||||||||||||||||||||||||
| 66 | pg_indexes_size(c.oid) AS index_length | ||||||||||||||||||||||||||||
| 67 | FROM pg_class c | ||||||||||||||||||||||||||||
| 68 | JOIN pg_namespace n ON n.oid = c.relnamespace | ||||||||||||||||||||||||||||
| 69 | LEFT JOIN pg_am am ON am.oid = c.relam | ||||||||||||||||||||||||||||
| 70 | WHERE c.relname = ? | ||||||||||||||||||||||||||||
| 71 | AND n.nspname = current_schema() | ||||||||||||||||||||||||||||
| 72 | }, $table); | ||||||||||||||||||||||||||||
| 73 | 1 | } | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | method get_index_stats ($table) { | ||||||||||||||||||||||||||
|
Subroutine
get_index_stats: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 0 | return $self->dbix->all(q{ | |||||||||||||||||||||||||||
| 77 | SELECT i.relname AS index_name, | ||||||||||||||||||||||||||||
| 78 | a.attname AS column_name, | ||||||||||||||||||||||||||||
| 79 | array_position(ix.indkey, a.attnum) + 1 AS seq_in_index, | ||||||||||||||||||||||||||||
| 80 | s.idx_scan AS scans | ||||||||||||||||||||||||||||
| 81 | FROM pg_index ix | ||||||||||||||||||||||||||||
| 82 | JOIN pg_class i ON i.oid = ix.indexrelid | ||||||||||||||||||||||||||||
| 83 | JOIN pg_class t ON t.oid = ix.indrelid | ||||||||||||||||||||||||||||
| 84 | JOIN pg_namespace n ON n.oid = t.relnamespace | ||||||||||||||||||||||||||||
| 85 | JOIN pg_attribute a ON a.attrelid = t.oid | ||||||||||||||||||||||||||||
| 86 | AND a.attnum = ANY (ix.indkey) | ||||||||||||||||||||||||||||
| 87 | LEFT JOIN pg_stat_user_indexes s ON s.indexrelid = ix.indexrelid | ||||||||||||||||||||||||||||
| 88 | WHERE t.relname = ? | ||||||||||||||||||||||||||||
| 89 | AND n.nspname = current_schema() | ||||||||||||||||||||||||||||
| 90 | ORDER BY i.relname, seq_in_index | ||||||||||||||||||||||||||||
| 91 | }, $table); | ||||||||||||||||||||||||||||
| 92 | 1 | } | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Global = pg_stat_database counters for the current database. | ||||||||||||||||||||||||||||
| 95 | # Session = the current backend's row from pg_stat_activity. | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | method get_stats () { | ||||||||||||||||||||||||||
|
Subroutine
get_stats: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 0 | my $global = $self->dbix->hash(q{ | |||||||||||||||||||||||||||
| 98 | SELECT xact_commit, xact_rollback, | ||||||||||||||||||||||||||||
| 99 | blks_read, blks_hit, | ||||||||||||||||||||||||||||
| 100 | tup_returned, tup_fetched, | ||||||||||||||||||||||||||||
| 101 | tup_inserted, tup_updated, tup_deleted, | ||||||||||||||||||||||||||||
| 102 | deadlocks, temp_files, temp_bytes | ||||||||||||||||||||||||||||
| 103 | FROM pg_stat_database | ||||||||||||||||||||||||||||
| 104 | WHERE datname = current_database() | ||||||||||||||||||||||||||||
| 105 | }); | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | my $session = $self->dbix->hash(q{ | |||||||||||||||||||||||||||
| 108 | SELECT state, backend_start, xact_start, query_start | ||||||||||||||||||||||||||||
| 109 | FROM pg_stat_activity | ||||||||||||||||||||||||||||
| 110 | WHERE pid = pg_backend_pid() | ||||||||||||||||||||||||||||
| 111 | }); | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | return { global => $global // {}, session => $session // {} }; | ||||||||||||||||||||||||||
|
Condition0%0/4
$global // {}
$session // {}
MC/DC0%0/1
$global // {}
$global
MC/DC0%0/1
$session // {}
$session
Truth table0%0/2
A
A= $global
Truth table0%0/2
A
A= $session
| |||||||||||||||||||||||||||||
| 114 | 1 | } | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | method print_stats () { | ||||||||||||||||||||||||||
|
Subroutine
print_stats: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 0 | my $stats = $self->get_stats; | |||||||||||||||||||||||||||
| 118 | # json/callback consumers also get the session block text mode omits. | ||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return if $self->emit( stats => $stats ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | $self->out("PostgreSQL Statistics (current database):\n"); | |||||||||||||||||||||||||||
| 122 | 0 | for my $k (sort keys %{ $stats->{global} }) { | |||||||||||||||||||||||||||
| 123 | 0 | ▶ | $self->outf(" %-14s: %s\n", $k, $stats->{global}{$k} // 0); | ||||||||||||||||||||||||||
|
Condition0%0/2
$stats->{"global"}{$k} // 0
MC/DC0%0/1
$stats->{"global"}{$k} // 0
$stats->{"global"}{$k}
Truth table0%0/2
A
A= $stats->{"global"}{$k}
| |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 1 | } | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Same keys as Profile::MariaDB::get_current_connections. | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | method get_current_connections () { | ||||||||||||||||||||||||||
|
Subroutine
get_current_connections: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | 0 | return $self->dbix->hash(q{ | |||||||||||||||||||||||||||
| 130 | SELECT COUNT(*) AS total_connections, | ||||||||||||||||||||||||||||
| 131 | COUNT(*) FILTER (WHERE state = 'active') AS active, | ||||||||||||||||||||||||||||
| 132 | COUNT(*) FILTER (WHERE state LIKE 'idle%') AS sleeping | ||||||||||||||||||||||||||||
| 133 | FROM pg_stat_activity | ||||||||||||||||||||||||||||
| 134 | WHERE datname = current_database() | ||||||||||||||||||||||||||||
| 135 | }); | ||||||||||||||||||||||||||||
| 136 | 1 | } | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | method get_processes () { | ||||||||||||||||||||||||||
|
Subroutine
get_processes: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 139 | 0 | return $self->dbix->all(q{ | |||||||||||||||||||||||||||
| 140 | SELECT pid AS id, | ||||||||||||||||||||||||||||
| 141 | usename AS "user", | ||||||||||||||||||||||||||||
| 142 | COALESCE(client_addr::text, 'local') AS host, | ||||||||||||||||||||||||||||
| 143 | datname AS db, | ||||||||||||||||||||||||||||
| 144 | state, | ||||||||||||||||||||||||||||
| 145 | EXTRACT(EPOCH FROM (now() - query_start))::int AS seconds_running, | ||||||||||||||||||||||||||||
| 146 | query | ||||||||||||||||||||||||||||
| 147 | FROM pg_stat_activity | ||||||||||||||||||||||||||||
| 148 | WHERE datname = current_database() | ||||||||||||||||||||||||||||
| 149 | ORDER BY query_start | ||||||||||||||||||||||||||||
| 150 | }); | ||||||||||||||||||||||||||||
| 151 | 1 | } | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Requires the pg_stat_statements extension; returns an empty arrayref | ||||||||||||||||||||||||||||
| 154 | # when it is not installed instead of dying. | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | method get_slow_queries () { | ||||||||||||||||||||||||||
|
Subroutine
get_slow_queries: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 0 | my $rows = eval { | |||||||||||||||||||||||||||
| 157 | 0 | $self->dbix->all(q{ | |||||||||||||||||||||||||||
| 158 | SELECT query AS sql_text, | ||||||||||||||||||||||||||||
| 159 | calls AS stages, | ||||||||||||||||||||||||||||
| 160 | total_exec_time / 1000 AS total_time | ||||||||||||||||||||||||||||
| 161 | FROM pg_stat_statements | ||||||||||||||||||||||||||||
| 162 | WHERE total_exec_time / 1000 > ? | ||||||||||||||||||||||||||||
| 163 | ORDER BY total_exec_time DESC | ||||||||||||||||||||||||||||
| 164 | LIMIT 20 | ||||||||||||||||||||||||||||
| 165 | }, $self->slow_query); | ||||||||||||||||||||||||||||
| 166 | }; | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | return $rows // []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$rows // []
MC/DC0%0/1
$rows // []
$rows
Truth table0%0/2
A
A= $rows
| |||||||||||||||||||||||||||||
| 168 | 1 | } | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 1 | 1; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | __END__ | ||||||||||||||||||||||||||||