From 49fd96f8b999a9ac35c46931fa468a3619d06b8f Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Tue, 15 Apr 2025 08:57:06 +0200 Subject: [PATCH] PostgreSQL: Display partition info (bug #1031) --- adminer/drivers/pgsql.inc.php | 22 ++++++++++++++++------ adminer/lang/cs.inc.php | 1 - adminer/lang/pl.inc.php | 1 - adminer/lang/xx.inc.php | 1 - adminer/table.inc.php | 6 +++++- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/adminer/drivers/pgsql.inc.php b/adminer/drivers/pgsql.inc.php index eddd4b87..dcdce6df 100644 --- a/adminer/drivers/pgsql.inc.php +++ b/adminer/drivers/pgsql.inc.php @@ -338,6 +338,19 @@ if (isset($_GET["pgsql"])) { return get_vals("SELECT relname FROM pg_inherits JOIN pg_class ON inhrelid = oid WHERE inhparent = " . $this->tableOid($table) . " ORDER BY 1"); } + function partitionsInfo(string $table): array { + $row = connection()->query("SELECT * FROM pg_partitioned_table WHERE partrelid = " . driver()->tableOid($table))->fetch_assoc(); + if ($row) { + $attrs = get_vals("SELECT attname FROM pg_attribute WHERE attrelid = $row[partrelid] AND attnum IN (" . str_replace(" ", ", ", $row["partattrs"]) . ")"); //! ordering + $by = array('h' => 'HASH', 'l' => 'LIST', 'r' => 'RANGE'); + return array( + "partition_by" => $by[$row["partstrat"]], + "partition" => implode(", ", array_map('Adminer\idf_escape', $attrs)), + ); + } + return array(); + } + function tableOid(string $table): string { return "(SELECT oid FROM pg_class WHERE relnamespace = $this->nsOid AND relname = " . q($table) . " AND relkind IN ('r', 'm', 'v', 'f', 'p'))"; } @@ -943,12 +956,9 @@ AND typelem = 0" } $return .= implode(",\n ", $return_parts) . "\n)"; - $table_oid = driver()->tableOid($status['Name']); - $row = connection()->query("SELECT * FROM pg_partitioned_table WHERE partrelid = $table_oid")->fetch_assoc(); - if ($row) { - $attrs = get_vals("SELECT attname FROM pg_attribute WHERE attrelid = $row[partrelid] AND attnum IN (" . str_replace(" ", ", ", $row["partattrs"]) . ")"); //! ordering - $by = array('h' => 'HASH', 'l' => 'LIST', 'r' => 'RANGE'); - $return .= "\nPARTITION BY $by[partstrat](" . implode(", ", array_map('Adminer\idf_escape', $attrs)) . ")"; + $partition = driver()->partitionsInfo($status['Name']); + if ($partition) { + $return .= "\nPARTITION BY $partition[partition_by]($partition[partition])"; } //! parse pg_class.relpartbound to create PARTITION OF diff --git a/adminer/lang/cs.inc.php b/adminer/lang/cs.inc.php index a950a36e..1a96c67d 100644 --- a/adminer/lang/cs.inc.php +++ b/adminer/lang/cs.inc.php @@ -195,7 +195,6 @@ Lang::$translations = array( 'Partition name' => 'Název oddílu', 'Values' => 'Hodnoty', 'Inherits from' => 'Zděděná z', - 'Inherited tables' => 'Zděděné tabulky', 'View' => 'Pohled', 'Materialized view' => 'Materializovaný pohled', diff --git a/adminer/lang/pl.inc.php b/adminer/lang/pl.inc.php index 78da24c9..d00f1c02 100644 --- a/adminer/lang/pl.inc.php +++ b/adminer/lang/pl.inc.php @@ -196,7 +196,6 @@ Lang::$translations = array( 'Partition name' => 'Nazwa partycji', 'Values' => 'Wartości', 'Inherits from' => 'Dziedziczy po', - 'Inherited tables' => 'Tabele dziedziczone', 'View' => 'Perspektywa', 'Materialized view' => 'Zmaterializowana perspektywa', diff --git a/adminer/lang/xx.inc.php b/adminer/lang/xx.inc.php index 34dfdbb7..283be7e5 100644 --- a/adminer/lang/xx.inc.php +++ b/adminer/lang/xx.inc.php @@ -197,7 +197,6 @@ Lang::$translations = array( 'Partition name' => 'Xx', 'Values' => 'Xx', 'Inherits from' => 'Xx', - 'Inherited tables' => 'Xx', 'View' => 'Xx', 'Materialized view' => 'Xx', diff --git a/adminer/table.inc.php b/adminer/table.inc.php index 85067d45..a9846d37 100644 --- a/adminer/table.inc.php +++ b/adminer/table.inc.php @@ -110,6 +110,10 @@ if (support(is_view($table_status) ? "view_trigger" : "trigger")) { $inherited = driver()->inheritedTables($TABLE); if ($inherited) { - echo "

" . lang('Inherited tables') . "

\n"; + echo "

" . lang('Partitions') . "

\n"; + $partition = driver()->partitionsInfo($TABLE); + if ($partition) { + echo "

BY " . h("$partition[partition_by]($partition[partition])") . "\n"; + } tables_links($inherited); }