| 1 | package DBIO::PostgreSQL::Introspect::Indexes; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect PostgreSQL indexes | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 171 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 171 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 171 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 171 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 171 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 171 | ▶ | use DBIO::PostgreSQL::Introspect::Parse (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=16 SCAR=56.1
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 0 | my ($class, $dbh, $filter) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # One entry per position of indkey, in a JSON array: the arrays of a handle | ||||||||||||||||||||||||||||
| 19 | # come back as ArrayRefs or as strings by pg_expand_array, a JSON text does | ||||||||||||||||||||||||||||
| 20 | # not. The key positions are the first indnkeyatts of them; the rest are | ||||||||||||||||||||||||||||
| 21 | # INCLUDE columns. indclass and indoption have an entry for key positions | ||||||||||||||||||||||||||||
| 22 | # only. | ||||||||||||||||||||||||||||
| 23 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 24 | SELECT | ||||||||||||||||||||||||||||
| 25 | sn.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 26 | ct.relname AS table_name, | ||||||||||||||||||||||||||||
| 27 | ci.relname AS index_name, | ||||||||||||||||||||||||||||
| 28 | am.amname AS access_method, | ||||||||||||||||||||||||||||
| 29 | i.indisunique AS is_unique, | ||||||||||||||||||||||||||||
| 30 | i.indisprimary AS is_primary, | ||||||||||||||||||||||||||||
| 31 | i.indisvalid AS is_valid, | ||||||||||||||||||||||||||||
| 32 | i.indnkeyatts AS key_count, | ||||||||||||||||||||||||||||
| 33 | pg_catalog.pg_get_indexdef(i.indexrelid) AS definition, | ||||||||||||||||||||||||||||
| 34 | pg_catalog.pg_get_expr(i.indpred, i.indrelid) AS predicate, | ||||||||||||||||||||||||||||
| 35 | pg_catalog.pg_get_expr(i.indexprs, i.indrelid) AS expressions, | ||||||||||||||||||||||||||||
| 36 | ( | ||||||||||||||||||||||||||||
| 37 | SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_object( | ||||||||||||||||||||||||||||
| 38 | 'column', a.attname, | ||||||||||||||||||||||||||||
| 39 | 'opclass', CASE | ||||||||||||||||||||||||||||
| 40 | WHEN oc.oid IS NULL OR (oc.opcdefault AND ia.attoptions IS NULL) THEN NULL | ||||||||||||||||||||||||||||
| 41 | WHEN pg_catalog.pg_opclass_is_visible(oc.oid) | ||||||||||||||||||||||||||||
| 42 | THEN pg_catalog.quote_ident(oc.opcname) | ||||||||||||||||||||||||||||
| 43 | ELSE pg_catalog.quote_ident(ocn.nspname) || '.' || pg_catalog.quote_ident(oc.opcname) | ||||||||||||||||||||||||||||
| 44 | END, | ||||||||||||||||||||||||||||
| 45 | 'options', ( | ||||||||||||||||||||||||||||
| 46 | SELECT pg_catalog.string_agg( | ||||||||||||||||||||||||||||
| 47 | pg_catalog.quote_ident(pg_catalog.split_part(o.opt, '=', 1)) || '=' | ||||||||||||||||||||||||||||
| 48 | || pg_catalog.quote_literal(pg_catalog.substr(o.opt, pg_catalog.strpos(o.opt, '=') + 1)), | ||||||||||||||||||||||||||||
| 49 | ', ' ORDER BY o.n) | ||||||||||||||||||||||||||||
| 50 | FROM pg_catalog.unnest(ia.attoptions) WITH ORDINALITY AS o(opt, n) | ||||||||||||||||||||||||||||
| 51 | ), | ||||||||||||||||||||||||||||
| 52 | 'indoption', i.indoption[k.n - 1] | ||||||||||||||||||||||||||||
| 53 | ) ORDER BY k.n) | ||||||||||||||||||||||||||||
| 54 | FROM pg_catalog.unnest(i.indkey) WITH ORDINALITY AS k(attnum, n) | ||||||||||||||||||||||||||||
| 55 | LEFT JOIN pg_catalog.pg_attribute a | ||||||||||||||||||||||||||||
| 56 | ON a.attrelid = i.indrelid AND a.attnum = k.attnum | ||||||||||||||||||||||||||||
| 57 | LEFT JOIN pg_catalog.pg_attribute ia | ||||||||||||||||||||||||||||
| 58 | ON ia.attrelid = i.indexrelid AND ia.attnum = k.n | ||||||||||||||||||||||||||||
| 59 | LEFT JOIN pg_catalog.pg_opclass oc | ||||||||||||||||||||||||||||
| 60 | ON oc.oid = i.indclass[k.n - 1] AND k.n <= i.indnkeyatts | ||||||||||||||||||||||||||||
| 61 | LEFT JOIN pg_catalog.pg_namespace ocn ON ocn.oid = oc.opcnamespace | ||||||||||||||||||||||||||||
| 62 | ) AS positions, | ||||||||||||||||||||||||||||
| 63 | con.conname AS constraint_name, | ||||||||||||||||||||||||||||
| 64 | con.contype AS constraint_type, | ||||||||||||||||||||||||||||
| 65 | con.condeferrable AS is_deferrable, | ||||||||||||||||||||||||||||
| 66 | con.condeferred AS is_deferred, | ||||||||||||||||||||||||||||
| 67 | ( | ||||||||||||||||||||||||||||
| 68 | SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_object( | ||||||||||||||||||||||||||||
| 69 | 'schema_name', fn.nspname, | ||||||||||||||||||||||||||||
| 70 | 'table_name', fc.relname, | ||||||||||||||||||||||||||||
| 71 | 'constraint_name', fk.conname | ||||||||||||||||||||||||||||
| 72 | ) ORDER BY fn.nspname, fc.relname, fk.conname) | ||||||||||||||||||||||||||||
| 73 | FROM pg_catalog.pg_constraint fk | ||||||||||||||||||||||||||||
| 74 | JOIN pg_catalog.pg_class fc ON fc.oid = fk.conrelid | ||||||||||||||||||||||||||||
| 75 | JOIN pg_catalog.pg_namespace fn ON fn.oid = fc.relnamespace | ||||||||||||||||||||||||||||
| 76 | WHERE fk.contype = 'f' AND fk.conindid = i.indexrelid | ||||||||||||||||||||||||||||
| 77 | AND fk.conparentid = 0 | ||||||||||||||||||||||||||||
| 78 | ) AS referenced_by, | ||||||||||||||||||||||||||||
| 79 | ci.reloptions AS storage_params | ||||||||||||||||||||||||||||
| 80 | FROM pg_catalog.pg_index i | ||||||||||||||||||||||||||||
| 81 | JOIN pg_catalog.pg_class ci ON ci.oid = i.indexrelid | ||||||||||||||||||||||||||||
| 82 | JOIN pg_catalog.pg_class ct ON ct.oid = i.indrelid | ||||||||||||||||||||||||||||
| 83 | JOIN pg_catalog.pg_namespace sn ON sn.oid = ct.relnamespace | ||||||||||||||||||||||||||||
| 84 | JOIN pg_catalog.pg_am am ON am.oid = ci.relam | ||||||||||||||||||||||||||||
| 85 | LEFT JOIN pg_catalog.pg_constraint con | ||||||||||||||||||||||||||||
| 86 | ON con.conindid = i.indexrelid | ||||||||||||||||||||||||||||
| 87 | AND con.conrelid = i.indrelid | ||||||||||||||||||||||||||||
| 88 | AND con.contype IN ('p', 'u', 'x') | ||||||||||||||||||||||||||||
| 89 | WHERE ct.relkind IN ('r', 'm', 'p') | ||||||||||||||||||||||||||||
| 90 | AND NOT ci.relispartition | ||||||||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||||||||
| 92 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('sn'); | |||||||||||||||||||||||||||
| 93 | 0 | $sql .= DBIO::PostgreSQL::Introspect->extension_member_filter('pg_class', 'ct.oid'); | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | my @bind; | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ($filter && @$filter) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$filter and @$filter
MC/DC0%0/2
$filter and @$filter
$filter
@$filter
Truth table0%0/3
A and B
A= $filterB= @$filter
| |||||||||||||||||||||||||||||
| 97 | 0 | $sql .= ' AND sn.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 98 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | $sql .= q{ | |||||||||||||||||||||||||||
| 102 | ORDER BY sn.nspname, ct.relname, ci.relname | ||||||||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 106 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my $json = JSON::MaybeXS->new; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | my %indexes; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | my $table_key = DBIO::PostgreSQL::Introspect->qualified_key($row); | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | my @positions = @{ $json->decode($row->{positions} // '[]') }; | ||||||||||||||||||||||||||
|
Condition0%0/2
$row->{"positions"} // "[]"
MC/DC0%0/1
$row->{"positions"} // "[]"
$row->{"positions"}
Truth table0%0/2
A
A= $row->{"positions"}
| |||||||||||||||||||||||||||||
| 114 | 0 | my @key = splice @positions, 0, $row->{key_count}; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | my (%opclasses, %sort_options); | |||||||||||||||||||||||||||
| 117 | 0 | for my $position (@key) { | |||||||||||||||||||||||||||
| 118 | 0 | my $column = $position->{column}; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | next unless defined $column; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (defined $position->{opclass}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | $opclasses{$column} = $position->{opclass} | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | . (defined $position->{options} ? "($position->{options})" : ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | my $sort = _sort_options($position->{indoption}); | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | $sort_options{$column} = $sort if length $sort; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | $indexes{$table_key}{ $row->{index_name} } = { | ||||||||||||||||||||||||||||
| 131 | schema_name => $row->{schema_name}, | ||||||||||||||||||||||||||||
| 132 | table_name => $row->{table_name}, | ||||||||||||||||||||||||||||
| 133 | index_name => $row->{index_name}, | ||||||||||||||||||||||||||||
| 134 | access_method => $row->{access_method}, | ||||||||||||||||||||||||||||
| 135 | is_unique => $row->{is_unique} ? 1 : 0, | ||||||||||||||||||||||||||||
| 136 | is_primary => $row->{is_primary} ? 1 : 0, | ||||||||||||||||||||||||||||
| 137 | is_valid => $row->{is_valid} ? 1 : 0, | ||||||||||||||||||||||||||||
| 138 | definition => $row->{definition}, | ||||||||||||||||||||||||||||
| 139 | predicate => $row->{predicate}, | ||||||||||||||||||||||||||||
| 140 | expressions => $row->{expressions}, | ||||||||||||||||||||||||||||
| 141 | 0 | columns => [ map { $_->{column} } @key ], | |||||||||||||||||||||||||||
| 142 | 0 | ▶ | include_columns => [ map { $_->{column} } @positions ], | ||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$row->{"referenced_by"} // "[]"
MC/DC0%0/1
$row->{"referenced_by"} // "[]"
$row->{"referenced_by"}
Truth table0%0/2
A
A= $row->{"referenced_by"}
| |||||||||||||||||||||||||||||
| 143 | storage_params => DBIO::PostgreSQL::Introspect::Parse->storage_params($row->{storage_params}), | ||||||||||||||||||||||||||||
| 144 | opclasses => \%opclasses, | ||||||||||||||||||||||||||||
| 145 | sort_options => \%sort_options, | ||||||||||||||||||||||||||||
| 146 | constraint_name => $row->{constraint_name}, | ||||||||||||||||||||||||||||
| 147 | constraint_type => $row->{constraint_type}, | ||||||||||||||||||||||||||||
| 148 | deferrable => $row->{is_deferrable} ? 1 : 0, | ||||||||||||||||||||||||||||
| 149 | initially_deferred => $row->{is_deferred} ? 1 : 0, | ||||||||||||||||||||||||||||
| 150 | 0 | ▶ | referenced_by => $json->decode($row->{referenced_by} // '[]'), | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 0 | return \%indexes; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | # The sort of a key column as the definition writes it, from its indoption | ||||||||||||||||||||||||||||
| 158 | # bits (1 DESC, 2 NULLS FIRST): nothing where it is the default -- ascending | ||||||||||||||||||||||||||||
| 159 | # with the nulls last, descending with them first. | ||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub _sort_options { | ||||||||||||||||||||||||||
|
Subroutine
_sort_options: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 161 | 0 | my ($option) = @_; | |||||||||||||||||||||||||||
| 162 | 0 | ▶ | $option //= 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$option //= 0
MC/DC0%0/1
$option //= 0
$option
Truth table0%0/2
A
A= $option
| |||||||||||||||||||||||||||||
| 163 | 0 | my ($desc, $nulls_first) = ($option & 1, $option & 2); | |||||||||||||||||||||||||||
| 164 | 0 | ▶ | return $desc | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 165 | ? ($nulls_first ? 'DESC' : 'DESC NULLS LAST') | ||||||||||||||||||||||||||||
| 166 | : ($nulls_first ? 'NULLS FIRST' : ''); | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 171 | 1; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | __END__ | ||||||||||||||||||||||||||||