Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
1a34ebd9
Commit
1a34ebd9
authored
Jun 26, 2020
by
Andreas Brandl
Committed by
Adam Hegyi
Jun 26, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add helper for hash partitioning
This adds a basic migration helper to create hash partitions
parent
04c798f3
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
93 additions
and
3 deletions
+93
-3
changelogs/unreleased/ab-hash-partitioning.yml
changelogs/unreleased/ab-hash-partitioning.yml
+5
-0
db/migrate/20200623170000_create_static_partitions_schema.rb
db/migrate/20200623170000_create_static_partitions_schema.rb
+19
-0
db/structure.sql
db/structure.sql
+5
-0
lib/gitlab/database.rb
lib/gitlab/database.rb
+5
-2
lib/gitlab/database/partitioning_migration_helpers/table_management_helpers.rb
...artitioning_migration_helpers/table_management_helpers.rb
+17
-0
rubocop/cop/migration/with_lock_retries_disallowed_method.rb
rubocop/cop/migration/with_lock_retries_disallowed_method.rb
+1
-0
spec/lib/gitlab/database/partitioning_migration_helpers/table_management_helpers_spec.rb
...ioning_migration_helpers/table_management_helpers_spec.rb
+32
-0
spec/support/helpers/partitioning_helpers.rb
spec/support/helpers/partitioning_helpers.rb
+9
-1
No files found.
changelogs/unreleased/ab-hash-partitioning.yml
0 → 100644
View file @
1a34ebd9
---
title
:
Create schema for static partitions
merge_request
:
35268
author
:
type
:
other
db/migrate/20200623170000_create_static_partitions_schema.rb
0 → 100644
View file @
1a34ebd9
# frozen_string_literal: true
class
CreateStaticPartitionsSchema
<
ActiveRecord
::
Migration
[
6.0
]
include
Gitlab
::
Database
::
SchemaHelpers
DOWNTIME
=
false
def
up
execute
'CREATE SCHEMA gitlab_partitions_static'
create_comment
(
:schema
,
:gitlab_partitions_static
,
<<~
EOS
.
strip
)
Schema to hold static partitions, e.g. for hash partitioning
EOS
end
def
down
execute
'DROP SCHEMA gitlab_partitions_static'
end
end
db/structure.sql
View file @
1a34ebd9
...
...
@@ -4,6 +4,10 @@ CREATE SCHEMA gitlab_partitions_dynamic;
COMMENT
ON
SCHEMA
gitlab_partitions_dynamic
IS
'Schema to hold partitions managed dynamically from the application, e.g. for time space partitioning.'
;
CREATE
SCHEMA
gitlab_partitions_static
;
COMMENT
ON
SCHEMA
gitlab_partitions_static
IS
'Schema to hold static partitions, e.g. for hash partitioning'
;
CREATE
EXTENSION
IF
NOT
EXISTS
pg_trgm
WITH
SCHEMA
public
;
CREATE
TABLE
public
.
abuse_reports
(
...
...
@@ -14164,6 +14168,7 @@ COPY "schema_migrations" (version) FROM STDIN;
20200623000148
20200623000320
20200623121135
20200623170000
20200624075411
\
.
lib/gitlab/database.rb
View file @
1a34ebd9
...
...
@@ -22,12 +22,15 @@ module Gitlab
MIN_SCHEMA_VERSION
=
20190506135400
MIN_SCHEMA_GITLAB_VERSION
=
'11.11.0'
# Schema we store dynamically managed partitions in
# Schema we store dynamically managed partitions in
(e.g. for time partitioning)
DYNAMIC_PARTITIONS_SCHEMA
=
:gitlab_partitions_dynamic
# Schema we store static partitions in (e.g. for hash partitioning)
STATIC_PARTITIONS_SCHEMA
=
:gitlab_partitions_static
# This is an extensive list of postgres schemas owned by GitLab
# It does not include the default public schema
EXTRA_SCHEMAS
=
[
DYNAMIC_PARTITIONS_SCHEMA
].
freeze
EXTRA_SCHEMAS
=
[
DYNAMIC_PARTITIONS_SCHEMA
,
STATIC_PARTITIONS_SCHEMA
].
freeze
define_histogram
:gitlab_database_transaction_seconds
do
docstring
"Time spent in database transactions, in seconds"
...
...
lib/gitlab/database/partitioning_migration_helpers/table_management_helpers.rb
View file @
1a34ebd9
...
...
@@ -70,6 +70,23 @@ module Gitlab
drop_table
(
part_table_name
)
end
def
create_hash_partitions
(
table_name
,
number_of_partitions
)
transaction
do
(
0
..
number_of_partitions
-
1
).
each
do
|
partition
|
decimals
=
Math
.
log10
(
number_of_partitions
).
ceil
suffix
=
"%0
#{
decimals
}
d"
%
partition
partition_name
=
"
#{
table_name
}
_
#{
suffix
}
"
schema
=
Gitlab
::
Database
::
STATIC_PARTITIONS_SCHEMA
execute
(
<<~
SQL
)
CREATE TABLE
#{
schema
}
.
#{
partition_name
}
PARTITION OF
#{
table_name
}
FOR VALUES WITH (MODULUS
#{
number_of_partitions
}
, REMAINDER
#{
partition
}
);
SQL
end
end
end
private
def
assert_table_is_allowed
(
table_name
)
...
...
rubocop/cop/migration/with_lock_retries_disallowed_method.rb
View file @
1a34ebd9
...
...
@@ -10,6 +10,7 @@ module RuboCop
ALLOWED_MIGRATION_METHODS
=
%i[
create_table
create_hash_partitions
drop_table
add_foreign_key
remove_foreign_key
...
...
spec/lib/gitlab/database/partitioning_migration_helpers/table_management_helpers_spec.rb
View file @
1a34ebd9
...
...
@@ -320,6 +320,38 @@ RSpec.describe Gitlab::Database::PartitioningMigrationHelpers::TableManagementHe
end
end
describe
'#create_hash_partitions'
do
before
do
connection
.
execute
(
<<~
SQL
)
CREATE TABLE
#{
partitioned_table
}
(id serial not null, some_id integer not null, PRIMARY KEY (id, some_id))
PARTITION BY HASH (some_id);
SQL
end
it
'creates partitions for the full hash space (8 partitions)'
do
partitions
=
8
migration
.
create_hash_partitions
(
partitioned_table
,
partitions
)
(
0
..
partitions
-
1
).
each
do
|
partition
|
partition_name
=
"
#{
partitioned_table
}
_
#{
"%01d"
%
partition
}
"
expect_hash_partition_of
(
partition_name
,
partitioned_table
,
partitions
,
partition
)
end
end
it
'creates partitions for the full hash space (16 partitions)'
do
partitions
=
16
migration
.
create_hash_partitions
(
partitioned_table
,
partitions
)
(
0
..
partitions
-
1
).
each
do
|
partition
|
partition_name
=
"
#{
partitioned_table
}
_
#{
"%02d"
%
partition
}
"
expect_hash_partition_of
(
partition_name
,
partitioned_table
,
partitions
,
partition
)
end
end
end
def
filter_columns_by_name
(
columns
,
names
)
columns
.
reject
{
|
c
|
names
.
include?
(
c
.
name
)
}
end
...
...
spec/support/helpers/partitioning_helpers.rb
View file @
1a34ebd9
...
...
@@ -31,6 +31,14 @@ module PartitioningHelpers
expect_total_partitions
(
table_name
,
partitions
.
size
,
schema:
Gitlab
::
Database
::
DYNAMIC_PARTITIONS_SCHEMA
)
end
def
expect_hash_partition_of
(
partition_name
,
table_name
,
modulus
,
remainder
)
definition
=
find_partition_definition
(
partition_name
,
schema:
Gitlab
::
Database
::
STATIC_PARTITIONS_SCHEMA
)
expect
(
definition
).
not_to
be_nil
expect
(
definition
[
'base_table'
]).
to
eq
(
table_name
.
to_s
)
expect
(
definition
[
'condition'
]).
to
eq
(
"FOR VALUES WITH (modulus
#{
modulus
}
, remainder
#{
remainder
}
)"
)
end
private
def
find_partitioned_columns
(
table
)
...
...
@@ -55,7 +63,7 @@ module PartitioningHelpers
SQL
end
def
find_partition_definition
(
partition
,
schema:
Gitlab
::
Database
::
DYNAMIC_PARTITIONS_SCHEMA
)
def
find_partition_definition
(
partition
,
schema:
)
connection
.
select_one
(
<<~
SQL
)
select
parent_class.relname as base_table,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment