Skip to content

Commit add3442

Browse files
Add support for ONLY in index creation (#451)
By default when creating an index on a partitioned table, it will also recurse and create indexes on all the underlying partitions. This commit adds the option to only create the index on the partioned table. Then the partitions can be indexed individually and attached to the parent index. I could not find support for this in MySQL or MSSQL. In Postgres it was added in version 11. See also https://www.postgresql.org/docs/11/sql-createindex.html and best practices https://www.postgresql.org/docs/14/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE-BEST-PRACTICES
1 parent 151780d commit add3442

3 files changed

Lines changed: 11 additions & 0 deletions

File tree

lib/ecto/adapters/postgres/connection.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,7 @@ if Code.ensure_loaded?(Postgrex) do
925925
if_do(command == :create_if_not_exists, "IF NOT EXISTS "),
926926
quote_name(index.name),
927927
" ON ",
928+
if_do(index.only, "ONLY "),
928929
quote_table(index.prefix, index.table),
929930
if_do(index.using, [" USING " , to_string(index.using)]),
930931
?\s, ?(, fields, ?),

lib/ecto/migration.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ defmodule Ecto.Migration do
366366
concurrently: false,
367367
using: nil,
368368
include: [],
369+
only: false,
369370
nulls_distinct: nil,
370371
where: nil,
371372
comment: nil,
@@ -379,6 +380,7 @@ defmodule Ecto.Migration do
379380
unique: boolean,
380381
concurrently: boolean,
381382
using: atom | String.t,
383+
only: boolean,
382384
include: [atom | String.t],
383385
nulls_distinct: boolean | nil,
384386
where: atom | String.t,
@@ -720,6 +722,8 @@ defmodule Ecto.Migration do
720722
This option is currently only supported by PostgreSQL 15+.
721723
For MySQL, it is always false. For MSSQL, it is always true.
722724
See the dedicated section on this option for more information.
725+
* `:only` - Indicates not to recurse creating indexes on partitions, if the table is partitioned.
726+
This option is currently only supported by PostgreSQL 11+. Defaults to `false`.
723727
* `:comment` - adds a comment to the index.
724728
725729
## Adding/dropping indexes concurrently

test/ecto/adapters/postgres_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,12 @@ defmodule Ecto.Adapters.PostgresTest do
18281828
[~s|CREATE INDEX "posts_permalink_index" ON "posts" USING hash ("permalink")|]
18291829
end
18301830

1831+
test "create an index without recursively creating indexes on partitions" do
1832+
create = {:create, index(:posts, [:permalink], only: true)}
1833+
assert execute_ddl(create) ==
1834+
[~s|CREATE INDEX "posts_permalink_index" ON ONLY "posts" ("permalink")|]
1835+
end
1836+
18311837
test "drop index" do
18321838
drop = {:drop, index(:posts, [:id], name: "posts$main"), :restrict}
18331839
assert execute_ddl(drop) == [~s|DROP INDEX "posts$main"|]

0 commit comments

Comments
 (0)