From e282d6eb89af6fee80c9508bed020bffa03653a5 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Fri, 6 Jun 2025 10:18:52 +0200 Subject: [PATCH] PostgreSQL: Display index expressions --- CHANGELOG.md | 1 + adminer/drivers/pgsql.inc.php | 19 +++++++++---------- adminer/indexes.inc.php | 9 +++++---- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc006010..002fba8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Warn about exceeded max_file_uploads in import - MySQL 5.0-: Do not load partitioning info in alter table (bug #1099) - PostgreSQL: Show structure of inherited tables +- PostgreSQL: Display index expressions - PostgreSQL: Add SQL operator to select - PostgreSQL: Hide only partitions, not all inherited tables from menu - PostgreSQL 11-: Avoid duplicate oid in table status (bug #1089) diff --git a/adminer/drivers/pgsql.inc.php b/adminer/drivers/pgsql.inc.php index d1b98bd4..7698706e 100644 --- a/adminer/drivers/pgsql.inc.php +++ b/adminer/drivers/pgsql.inc.php @@ -529,7 +529,7 @@ ORDER BY a.attnum") as $row $table_oid = driver()->tableOid($table); $columns = get_key_vals("SELECT attnum, attname FROM pg_attribute WHERE attrelid = $table_oid AND attnum > 0", $connection2); foreach ( - get_rows("SELECT relname, indisunique::int, indisprimary::int, indkey, indoption, (indpred IS NOT NULL)::int as indispartial, pg_am.amname as algorithm, pg_get_expr(pg_index.indpred, pg_index.indrelid, true) AS partial + get_rows("SELECT relname, indisunique::int, indisprimary::int, indkey, indoption, amname, pg_get_expr(indpred, indrelid, true) AS partial, pg_get_expr(indexprs, indrelid) AS indexpr FROM pg_index JOIN pg_class ON indexrelid = oid JOIN pg_am ON pg_am.oid = pg_class.relam @@ -537,18 +537,17 @@ WHERE indrelid = $table_oid ORDER BY indisprimary DESC, indisunique DESC", $connection2) as $row ) { $relname = $row["relname"]; - $return[$relname]["type"] = ($row["indispartial"] ? "INDEX" : ($row["indisprimary"] ? "PRIMARY" : ($row["indisunique"] ? "UNIQUE" : "INDEX"))); + $return[$relname]["type"] = ($row["partial"] ? "INDEX" : ($row["indisprimary"] ? "PRIMARY" : ($row["indisunique"] ? "UNIQUE" : "INDEX"))); $return[$relname]["columns"] = array(); $return[$relname]["descs"] = array(); - $return[$relname]["algorithm"] = $row["algorithm"]; + $return[$relname]["algorithm"] = $row["amname"]; $return[$relname]["partial"] = $row["partial"]; - if ($row["indkey"]) { - foreach (explode(" ", $row["indkey"]) as $indkey) { - $return[$relname]["columns"][] = $columns[$indkey]; - } - foreach (explode(" ", $row["indoption"]) as $indoption) { - $return[$relname]["descs"][] = (intval($indoption) & 1 ? '1' : null); // 1 - INDOPTION_DESC - } + $indexpr = preg_split('~(?<=\)), (?=\()~', $row["indexpr"]); //! '), (' used in expression + foreach (explode(" ", $row["indkey"]) as $indkey) { + $return[$relname]["columns"][] = ($indkey ? $columns[$indkey] : array_shift($indexpr)); + } + foreach (explode(" ", $row["indoption"]) as $indoption) { + $return[$relname]["descs"][] = (intval($indoption) & 1 ? '1' : null); // 1 - INDOPTION_DESC } $return[$relname]["lengths"] = array(); } diff --git a/adminer/indexes.inc.php b/adminer/indexes.inc.php index 99c85305..ac029390 100644 --- a/adminer/indexes.inc.php +++ b/adminer/indexes.inc.php @@ -12,6 +12,7 @@ if (preg_match('~MyISAM|M?aria' . (min_version(5.7, '10.2.2') ? '|InnoDB' : '') $index_types[] = "SPATIAL"; } $indexes = indexes($TABLE); +$fields = fields($TABLE); $primary = array(); if (JUSH == "mongo") { // doesn't support primary key $primary = $indexes["_id_"]; @@ -38,7 +39,7 @@ if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"]) { if ($column != "") { $length = idx($index["lengths"], $key); $desc = idx($index["descs"], $key); - $set[] = idf_escape($column) . ($length ? "(" . (+$length) . ")" : "") . ($desc ? " DESC" : ""); + $set[] = ($fields[$column] ? idf_escape($column) : $column) . ($length ? "(" . (+$length) . ")" : "") . ($desc ? " DESC" : ""); $columns[] = $column; $lengths[] = ($length ?: null); $descs[] = $desc; @@ -81,7 +82,7 @@ if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"]) { page_header(lang('Indexes'), $error, array("table" => $TABLE), h($TABLE)); -$fields = array_keys(fields($TABLE)); +$fields_keys = array_keys($fields); if ($_POST["add"]) { foreach ($row["indexes"] as $key => $index) { if ($index["columns"][count($index["columns"])] != "") { @@ -138,7 +139,7 @@ if (support("partial_indexes")) { if ($primary) { echo "PRIMARY"; foreach ($primary["columns"] as $key => $column) { - echo select_input(" disabled", $fields, $column); + echo select_input(" disabled", $fields_keys, $column); echo " "; } echo "\n"; @@ -158,7 +159,7 @@ foreach ($row["indexes"] as $index) { foreach ($index["columns"] as $key => $column) { echo "" . select_input( " name='indexes[$j][columns][$i]' title='" . lang('Column') . "'", - ($fields ? array_combine($fields, $fields) : $fields), + ($fields && ($column == "" || $fields[$column]) ? array_combine($fields_keys, $fields_keys) : array()), $column, "partial(" . ($i == count($index["columns"]) ? "indexesAddColumn" : "indexesChangeColumn") . ", '" . js_escape(JUSH == "sql" ? "" : $_GET["indexes"] . "_") . "')" );