| 1 | package DBIO::PostgreSQL::Introspect::Indexes; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect PostgreSQL indexes | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use DBIO::PostgreSQL::Introspect (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use DBIO::PostgreSQL::Introspect::Parse (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 0 | my ($class, $dbh, $filter) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 0 | my $sql = q{ | |||||||||||||||||||||||||||
| 16 | SELECT | ||||||||||||||||||||||||||||
| 17 | sn.nspname AS schema_name, | ||||||||||||||||||||||||||||
| 18 | ct.relname AS table_name, | ||||||||||||||||||||||||||||
| 19 | ci.relname AS index_name, | ||||||||||||||||||||||||||||
| 20 | am.amname AS access_method, | ||||||||||||||||||||||||||||
| 21 | i.indisunique AS is_unique, | ||||||||||||||||||||||||||||
| 22 | i.indisprimary AS is_primary, | ||||||||||||||||||||||||||||
| 23 | i.indisvalid AS is_valid, | ||||||||||||||||||||||||||||
| 24 | pg_catalog.pg_get_indexdef(i.indexrelid) AS definition, | ||||||||||||||||||||||||||||
| 25 | pg_catalog.pg_get_expr(i.indpred, i.indrelid) AS predicate, | ||||||||||||||||||||||||||||
| 26 | pg_catalog.pg_get_expr(i.indexprs, i.indrelid) AS expressions, | ||||||||||||||||||||||||||||
| 27 | array_agg(a.attname ORDER BY k.n) AS column_names, | ||||||||||||||||||||||||||||
| 28 | ci.reloptions AS storage_params | ||||||||||||||||||||||||||||
| 29 | FROM pg_catalog.pg_index i | ||||||||||||||||||||||||||||
| 30 | JOIN pg_catalog.pg_class ci ON ci.oid = i.indexrelid | ||||||||||||||||||||||||||||
| 31 | JOIN pg_catalog.pg_class ct ON ct.oid = i.indrelid | ||||||||||||||||||||||||||||
| 32 | JOIN pg_catalog.pg_namespace sn ON sn.oid = ct.relnamespace | ||||||||||||||||||||||||||||
| 33 | JOIN pg_catalog.pg_am am ON am.oid = ci.relam | ||||||||||||||||||||||||||||
| 34 | LEFT JOIN LATERAL unnest(i.indkey) WITH ORDINALITY AS k(attnum, n) ON true | ||||||||||||||||||||||||||||
| 35 | LEFT JOIN pg_catalog.pg_attribute a | ||||||||||||||||||||||||||||
| 36 | ON a.attrelid = i.indrelid AND a.attnum = k.attnum | ||||||||||||||||||||||||||||
| 37 | WHERE ct.relkind IN ('r', 'm', 'p') | ||||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||
| 39 | 0 | $sql .= DBIO::PostgreSQL::Introspect->system_schema_filter('sn'); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | my @bind; | |||||||||||||||||||||||||||
| 42 | 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
| |||||||||||||||||||||||||||||
| 43 | 0 | $sql .= ' AND sn.nspname = ANY($1)'; | |||||||||||||||||||||||||||
| 44 | 0 | push @bind, $filter; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | $sql .= q{ | |||||||||||||||||||||||||||
| 48 | GROUP BY sn.nspname, ct.relname, ci.relname, am.amname, | ||||||||||||||||||||||||||||
| 49 | i.indisunique, i.indisprimary, i.indisvalid, | ||||||||||||||||||||||||||||
| 50 | i.indexrelid, i.indrelid, i.indpred, i.indexprs, | ||||||||||||||||||||||||||||
| 51 | ci.reloptions | ||||||||||||||||||||||||||||
| 52 | ORDER BY sn.nspname, ct.relname, ci.relname | ||||||||||||||||||||||||||||
| 53 | }; | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | my $sth = $dbh->prepare($sql); | |||||||||||||||||||||||||||
| 56 | 0 | $sth->execute(@bind); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my %indexes; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | my $table_key = DBIO::PostgreSQL::Introspect->qualified_key($row); | |||||||||||||||||||||||||||
| 61 | 0 | my $columns = $row->{column_names}; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | if (!ref $columns) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | my @parts = grep { $_ ne 'NULL' } @{ DBIO::PostgreSQL::Introspect->normalize_array($columns) // [] }; | ||||||||||||||||||||||||||
|
Condition0%0/2
"DBIO::PostgreSQL::Introspect"->normalize_array($columns) // []
MC/DC0%0/1
"DBIO::PostgreSQL::Introspect"->normalize_array($columns) // []
"DBIO::PostgreSQL::Introspect"->normalize_array($columns)
Truth table0%0/2
A
A= "DBIO::PostgreSQL::Introspect"->normalize_array($columns)
| |||||||||||||||||||||||||||||
| 64 | 0 | $columns = \@parts; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | $indexes{$table_key}{ $row->{index_name} } = { | ||||||||||||||||||||||||||||
| 67 | index_name => $row->{index_name}, | ||||||||||||||||||||||||||||
| 68 | access_method => $row->{access_method}, | ||||||||||||||||||||||||||||
| 69 | is_unique => $row->{is_unique} ? 1 : 0, | ||||||||||||||||||||||||||||
| 70 | is_primary => $row->{is_primary} ? 1 : 0, | ||||||||||||||||||||||||||||
| 71 | is_valid => $row->{is_valid} ? 1 : 0, | ||||||||||||||||||||||||||||
| 72 | definition => $row->{definition}, | ||||||||||||||||||||||||||||
| 73 | predicate => $row->{predicate}, | ||||||||||||||||||||||||||||
| 74 | expressions => $row->{expressions}, | ||||||||||||||||||||||||||||
| 75 | columns => $columns, | ||||||||||||||||||||||||||||
| 76 | include_columns => DBIO::PostgreSQL::Introspect::Parse->include_columns($row->{definition}), | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | storage_params => DBIO::PostgreSQL::Introspect::Parse->storage_params($row->{storage_params}), | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 78 | }; | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | return \%indexes; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 15 | 1; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =pod | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head1 NAME | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | DBIO::PostgreSQL::Introspect::Indexes - Introspect PostgreSQL indexes | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 VERSION | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | version 0.900001 | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Fetches index metadata from C<pg_catalog.pg_index>, including access method, | ||||||||||||||||||||||||||||
| 103 | uniqueness, primary key flag, validity, the full index definition string from | ||||||||||||||||||||||||||||
| 104 | C<pg_get_indexdef>, partial index predicates, and expression index expressions. | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head1 METHODS | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 fetch | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | my $indexes = DBIO::PostgreSQL::Introspect::Indexes->fetch($dbh, $filter); | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Returns a hashref keyed by C<schema.table>. Each value is a hashref keyed by | ||||||||||||||||||||||||||||
| 113 | index name. Each index entry has: C<index_name>, C<access_method>, C<is_unique>, | ||||||||||||||||||||||||||||
| 114 | C<is_primary>, C<is_valid>, C<definition> (full C<pg_get_indexdef> output), | ||||||||||||||||||||||||||||
| 115 | C<predicate>, C<expressions>, C<columns> (ArrayRef of column names, empty for | ||||||||||||||||||||||||||||
| 116 | expression-only indexes). | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | DBIO & DBIx::Class Authors | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||
| 125 | Portions Copyright (C) 2005-2025 DBIx::Class Authors | ||||||||||||||||||||||||||||
| 126 | Based on DBIx::Class, heavily modified. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 129 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||