-
-
Notifications
You must be signed in to change notification settings - Fork 104
Partitioning: Initial suggestion #485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
m0rt3nlund
wants to merge
7
commits into
ash-project:main
Choose a base branch
from
m0rt3nlund:Partitioning-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0e37920
Initial suggestion
37dd643
Fix primary key generation for multitenancy
4e08d72
Partitioning of resource
8b9ea15
spelling
aea852e
Rename methods
66f1fbf
Merge branch 'main' of https://github.com/m0rt3nlund/ash_postgres int…
13af63c
Merge branch 'main' of https://github.com/m0rt3nlund/ash_postgres int…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
defmodule AshPostgres.Partitioning do | ||
@moduledoc false | ||
|
||
@doc """ | ||
Create a new partition for a resource | ||
""" | ||
def create_partition(resource, opts) do | ||
repo = AshPostgres.DataLayer.Info.repo(resource) | ||
|
||
resource | ||
|> AshPostgres.DataLayer.Info.partitioning_method() | ||
|> case do | ||
:range -> | ||
create_range_partition(repo, resource, opts) | ||
|
||
:list -> | ||
create_list_partition(repo, resource, opts) | ||
|
||
:hash -> | ||
create_hash_partition(repo, resource, opts) | ||
|
||
unsupported_method -> | ||
raise "Invalid partition method, got: #{unsupported_method}" | ||
end | ||
end | ||
|
||
@doc """ | ||
Check if partition exists | ||
""" | ||
def exists?(resource, opts) do | ||
repo = AshPostgres.DataLayer.Info.repo(resource) | ||
key = Keyword.fetch!(opts, :key) | ||
table = AshPostgres.DataLayer.Info.table(resource) | ||
partition_name = table <> "_" <> "#{key}" | ||
|
||
partition_exists?(repo, resource, partition_name) | ||
end | ||
|
||
# TBI | ||
defp create_range_partition(repo, resource, opts) do | ||
end | ||
|
||
defp create_list_partition(repo, resource, opts) do | ||
key = Keyword.fetch!(opts, :key) | ||
table = AshPostgres.DataLayer.Info.table(resource) | ||
partition_name = table <> "_" <> "#{key}" | ||
|
||
if partition_exists?(repo, resource, partition_name) do | ||
{:error, :allready_exists} | ||
else | ||
Ecto.Adapters.SQL.query( | ||
repo, | ||
"CREATE TABLE #{partition_name} PARTITION OF public.#{table} FOR VALUES IN (#{key})" | ||
) | ||
|
||
if partition_exists?(repo, resource, partition_name) do | ||
:ok | ||
else | ||
{:error, "Unable to create partition"} | ||
end | ||
end | ||
end | ||
|
||
# TBI | ||
defp create_hash_partition(repo, resource, opts) do | ||
end | ||
|
||
defp partition_exists?(repo, resource, parition_name) do | ||
%Postgrex.Result{} = | ||
result = | ||
repo | ||
|> Ecto.Adapters.SQL.query!( | ||
"select table_name from information_schema.tables t where t.table_schema = 'public' and t.table_name = $1", | ||
[parition_name] | ||
) | ||
|
||
result.num_rows > 0 | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
priv/resource_snapshots/test_repo/partitioned_posts/20250214114101.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"attributes": [ | ||
{ | ||
"allow_nil?": false, | ||
"default": "fragment(\"gen_random_uuid()\")", | ||
"generated?": false, | ||
"primary_key?": true, | ||
"references": null, | ||
"size": null, | ||
"source": "id", | ||
"type": "uuid" | ||
}, | ||
{ | ||
"allow_nil?": false, | ||
"default": "1", | ||
"generated?": false, | ||
"primary_key?": true, | ||
"references": null, | ||
"size": null, | ||
"source": "key", | ||
"type": "bigint" | ||
} | ||
], | ||
"base_filter": null, | ||
"check_constraints": [], | ||
"custom_indexes": [], | ||
"custom_statements": [], | ||
"has_create_action": false, | ||
"hash": "7FE5D9659135887A47FAE2729CEB0281FA8FF392EDB3B43426EAFD89A1518FEB", | ||
"identities": [], | ||
"multitenancy": { | ||
"attribute": null, | ||
"global": null, | ||
"strategy": null | ||
}, | ||
"partitioning": { | ||
"attribute": "key", | ||
"method": "list" | ||
}, | ||
"repo": "Elixir.AshPostgres.TestRepo", | ||
"schema": null, | ||
"table": "partitioned_posts" | ||
} |
20 changes: 20 additions & 0 deletions
20
priv/test_repo/migrations/20250214114101_partitioned_post.exs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule AshPostgres.TestRepo.Migrations.PartitionedPost do | ||
@moduledoc """ | ||
Updates resources based on their most recent snapshots. | ||
|
||
This file was autogenerated with `mix ash_postgres.generate_migrations` | ||
""" | ||
|
||
use Ecto.Migration | ||
|
||
def up do | ||
create table(:partitioned_posts, primary_key: false, options: "PARTITION BY LIST (key)") do | ||
add(:id, :uuid, null: false, default: fragment("gen_random_uuid()"), primary_key: true) | ||
add(:key, :bigint, null: false, default: 1, primary_key: true) | ||
end | ||
end | ||
|
||
def down do | ||
drop(table(:partitioned_posts)) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.