]> git.joonet.de Git - adminer.git/commitdiff
PostgreSQL: Creating partitioned tables (fix #1031)
authorJakub Vrana <jakub@vrana.cz>
Mon, 14 Apr 2025 13:50:17 +0000 (15:50 +0200)
committerJakub Vrana <jakub@vrana.cz>
Mon, 14 Apr 2025 13:50:17 +0000 (15:50 +0200)
CHANGELOG.md
adminer/drivers/pgsql.inc.php

index da1841d3c662e0e05b1a8442399a4f9cdb64e7f6..aaf26b58b68c57c2549fe444c6d512e957a7246d 100644 (file)
@@ -1,4 +1,5 @@
 ## Adminer dev
+- PostgreSQL: Creating partitioned tables (bug #1031)
 - PostgreSQL: Move partitioned tables from table list to parent table
 - Designs: adminer.css with 'prefers-color-scheme: dark' don't disable dark mode
 - Plugins: Method bodyClass() to add &lt;body class>
index 26855a76a00e0568b6c483af09a743bac4783ee7..afaacea9e029b9003ddd877b493b2b5cf11622b3 100644 (file)
@@ -206,6 +206,7 @@ if (isset($_GET["pgsql"])) {
                public $operators = array("=", "<", ">", "<=", ">=", "!=", "~", "!~", "LIKE", "LIKE %%", "ILIKE", "ILIKE %%", "IN", "IS NULL", "NOT LIKE", "NOT IN", "IS NOT NULL"); // no "SQL" to avoid CSRF
                public $functions = array("char_length", "lower", "round", "to_hex", "to_timestamp", "upper");
                public $grouping = array("avg", "count", "count distinct", "max", "min", "sum");
+               public $partitionBy = array("RANGE", "LIST", "HASH");
 
                public string $nsOid = "(SELECT oid FROM pg_namespace WHERE nspname = current_schema())";
 
@@ -635,7 +636,29 @@ ORDER BY conkey, conname") as $row
                }
                $alter = array_merge($alter, $foreign);
                if ($table == "") {
-                       array_unshift($queries, "CREATE TABLE " . table($name) . " (\n" . implode(",\n", $alter) . "\n)");
+                       $status = "";
+                       if ($partitioning) {
+                               $status = " PARTITION BY $partitioning[partition_by]($partitioning[partition])";
+                               $partition_names = array_values($partitioning["partition_names"]);
+                               $partition_values = array_values($partitioning["partition_values"]);
+                               if ($partitioning["partition_by"] == 'HASH') {
+                                       $partitions = +$partitioning["partitions"];
+                                       for ($i=0; $i < $partitions; $i++) {
+                                               $queries[] = "CREATE TABLE " . idf_escape($name . "_$i") . " PARTITION OF " . idf_escape($name) . " FOR VALUES WITH (MODULUS $partitions, REMAINDER $i)";
+                                       }
+                               } else {
+                                       foreach ($partition_names as $i => $val) {
+                                               $value = $partition_values[$i];
+                                               $queries[] = "CREATE TABLE " . idf_escape($name . "_$val") . " PARTITION OF " . idf_escape($name) . " FOR VALUES "
+                                                       . ($partitioning["partition_by"] == 'LIST'
+                                                               ? "IN ($value)"
+                                                               : "FROM (" . ($i ? $partition_values[$i - 1] : "MINVALUE") . ") TO ($value)"
+                                                       )
+                                               ;
+                                       }
+                               }
+                       }
+                       array_unshift($queries, "CREATE TABLE " . table($name) . " (\n" . implode(",\n", $alter) . "\n)$status");
                } elseif ($alter) {
                        array_unshift($queries, "ALTER TABLE " . table($table) . "\n" . implode(",\n", $alter));
                }