Bài viết này giới thiệu về chức năng partitioning bảng dữ liệu theo kiến trúc mới, chức năng Declarative Partitioning được đưa vào từ phiên bản PostgreSQL 10. Show
Scale out cho hệ thống PostgreSQLTrước khi đi vào giới thiệu cụ thể, xin được tóm tắt các kỹ thuật scale out hệ thống của PostgreSQL hiện tại. Đối với các hệ thống lớn hay các hệ thống dữ liệu có độ lớn tăng dần theo thời gian, việc thiết kế scale out (chia nhỏ dữ liệu thành nhiều phần để dễ quản lý) là một việc quan trọng nếu không muốn xử lý các vấn đề về dung lượng sau vận hành. PostgeSQL cung cấp các chức năng như bên dưới để hỗ trợ scale out hệ thống.
Chức năng Partitioning của PostgreSQLChức năng này cũng là một chức năng được được PostgreSQL đưa vào sớm từ phiên bản 8.1. Mặc dù có một số hạn chế overhead do phải thiết dựa trên trigger, nhưng chức năng này cũng được sử dụng rộng dãi cho tới nay. Từ phiên bản 10 PostgreSQL hỗ trợ phương thức partitioning mới, không dựa vào trigger nên có performance tốt và cách sử dụng đơn giản. Chức năng Partitioning sử dụng kế thừaỞ các phiên bản trước của PostgreSQL 10, để sử dụng chức năng partitioning. Ta cần các bước tổng quan như sau.
Tạo bảng masterTa tạo bảng master với các trường cần thiết. Ở đây ta sử dụng cột range cho chức năng partitioning. 10000 postgres@postgres=# CREATE TABLE parent(id integer, childname text, range bigint); CREATE TABLE Tạo các bảng conBạn có thể định nghĩa số lượng bảng con tuỳ ý theo thiết kế hệ thống của bạn. Nhưng community khuyến cáo không nên sử dụng quá 100 bảng con. Ví dụ bên dưới mình tạo 3 bảng con kế thừa bảng master. Ở đây mình sử dụng ràng buộc CHECK dữ liệu, để bảng con tương ứng chỉ chứa những giá trị cho phép. Khi tham chiếu dữ liệu bảng cha, PostgreSQL có thể dựa vào ràng buộc CHECK của bảng con để bỏ qua tham chiếu tới các bảng con không cần thiết. 10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# Tạo TRIGGER cho bảng masterMặc định khi tạo table con ta đã có thể sử dụng chức năng partitioning để truy suất dữ liệu các bảng con thông qua bảng master. Nhưng để insert dữ liệu tới các bảng con thông qua bảng master ta phải sử dụng chức năng TRIGGER của PostgreSQL. Ví dụ bên dưới thực hiện:
10000 postgres@postgres=# CREATE OR REPLACE FUNCTION insert_to_child () RETURNS TRIGGER AS $$ BEGIN END;
$$
LANGUAGE PLPGSQL;
CREATE FUNCTION
10000 postgres@postgres=# CREATE TRIGGER insert_to_child_tg BEFORE INSERT ON parent FOR EACH ROW EXECUTE PROCEDURE insert_to_child();
CREATE TRIGGERSau khi tạo được bảng con và TRIGGER cho bảng master. Bảng master sẽ có cấu trúc như bên dưới. 10000 postgres@postgres=# \d+ parent Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
---+-+---+--+-+--+--+-
id | integer | | | | plain | |
childname | text | | | | extended | |
range | bigint | | | | plain | |
Triggers: Child tables: child1,
Thực hiện truy vấnINSERT & SELECT dữ liệuVí dụ bên dưới INSERT 3 dòng dữ liệu thông qua bảng master và xác nhận dữ liệu đã được ghi vào các bảng con nhờ TRIGGER đã định nghĩa. 10000 postgres@postgres=# INSERT INTO parent VALUES (1,'a',1), (2,'b',199999), (3,'c',200000); INSERT 0 0 10000 postgres@postgres=# SELECT FROM parent ; id | childname | range +---+ 1 | a | 1 2 | b | 199999 3 | c | 200000 (3 rows) 10000 postgres@postgres=# SELECT FROM parent ; id | childname | range +---+ 1 | a | 1 2 | b | 199999 3 | c | 200000 (3 rows) 10000 postgres@postgres=# SELECT FROM child1; id | childname | range +---+--- 1 | a | 1 (1 row) 10000 postgres@postgres=# SELECT FROM child2; id | childname | range +---+ 2 | b | 199999 (1 row) 10000 postgres@postgres=# SELECT * FROM child3; id | childname | range +---+ 3 | c | 200000 (1 row) Như kết quả EXPLAIN của câu lệnh SELECT bảng master bên dưới. Dữ liệu được tìm kiếm ở các bảng con chứa dữ liệu tương ứng và bảng master. 10000 postgres@postgres=# EXPLAIN (ANALYZE,VERBOSE) SELECT * FROM parent WHERE range IN (1,199999);
Append (cost=0.00..48.37 rows=23 width=44) (actual time=0.028..0.039 rows=2 loops=1) -> Seq Scan on public.parent (cost=0.00..0.00 rows=1 width=44) (actual time=0.011..0.011 rows=0 loops=1) -> Seq Scan on public.child1 (cost=0.00..24.12 rows=11 width=44) (actual time=0.015..0.016 rows=1 loops=1) -> Seq Scan on public.child2 (cost=0.00..24.12 rows=11 width=44) (actual time=0.008..0.009 rows=1 loops=1) Planning Time: 1.433 ms
Execution Time: 0.082 ms
(12 rows)
10000 postgres@postgres=# show constraint_exclusion ;
constraint_exclusion partition (1 row) PostgreSQL bỏ qua tìm kiếm dữ liệu dựa vào ràng buộc CHECK thông qua tham số constraint_exclusion. Nếu tham số này thiết lập là 10000 postgres@postgres=# \d+ parent Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
---+-+---+--+-+--+--+-
id | integer | | | | plain | |
childname | text | | | | extended | |
range | bigint | | | | plain | |
Triggers: Child tables: child1,
2 hoặc 10000 postgres@postgres=# \d+ parent Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
---+-+---+--+-+--+--+-
id | integer | | | | plain | |
childname | text | | | | extended | |
range | bigint | | | | plain | |
Triggers: Child tables: child1,
3, PostgreSQL sẽ sử dụng ràng buộc CHECK để bỏ qua tìm kiếm ở bảng con không cần thiết.
Ví dụ bên dưới khi set constraint_exclusion sang off, PostgreSQL scan tất cả các bảng con.
10000 postgres@postgres=# set constraint_exclusion TO off;
SET
10000 postgres@postgres=# EXPLAIN (ANALYZE,VERBOSE) SELECT * FROM parent WHERE range IN (1,199999);
Append (cost=0.00..72.55 rows=34 width=44) (actual time=0.021..0.053 rows=2 loops=1) -> Seq Scan on public.parent (cost=0.00..0.00 rows=1 width=44) (actual time=0.006..0.006 rows=0 loops=1) -> Seq Scan on public.child1 (cost=0.00..24.12 rows=11 width=44) (actual time=0.013..0.014 rows=1 loops=1) -> Seq Scan on public.child2 (cost=0.00..24.12 rows=11 width=44) (actual time=0.007..0.007 rows=1 loops=1) -> Seq Scan on public.child3 (cost=0.00..24.12 rows=11 width=44) (actual time=0.022..0.022 rows=0 loops=1) Planning Time: 0.299 ms
Execution Time: 0.128 ms
(16 rows)UPDATE dữ liệuNhư ví dụ bên dưới vì ta chưa tạo TRIGGER khi UPDATE bảng master. Nên dữ liệu UPDATE không nằm trong khoảng của ràng buộc CHECK sẽ gây ra lỗi. Nếu muốn khắc phục lỗi này ta phải định nghĩa thêm TRIGGER thực hiện điều hướng dữ liệu UPDATE tới các bảng con tương ứng. 10000 postgres@postgres=# UPDATE parent SET range = 200000 WHERE range = 1; ERROR: new row for relation "child1" violates check constraint "child1_range_check" DETAIL: Failing row contains (1, a, 200000). 10000 postgres@postgres=# UPDATE parent SET range = 2 WHERE range = 1; UPDATE 1 10000 postgres@postgres=# Declarative Partitioning (PostgreSQL 10 ~)Nếu như ta phải thực hiện nhiều thao tác mới sử dụng được chức năng Partitioning sử dụng kế thừa như trên, thì từ phiên bản 10 ta chỉ đơn giản thực hiện các thao tác bên dưới là có thể sử dụng được.
Tạo bảng master Declarative PartitioningChức năng Declarative Partitioning hỗ trợ các phương thức partitioning như sau:
Ví dụ bên dưới mình sử dụng phương thức range (tương ứng với ví dụ ở chức năng partitioning sử dụng kế thừa). 10000 postgres@postgres=# CREATE TABLE parent_pg10(id integer, name text, range bigint) PARTITION BY RANGE (range); CREATE TABLE Tạo bảng con tương ứng cho bảng master10000 postgres@postgres=# CREATE TABLE child1_pg10 PARTITION OF parent_pg10 FOR VALUES FROM (0) TO (99999) ; CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2_pg10 PARTITION OF parent_pg10 FOR VALUES FROM (99999) TO (200000) ; CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3_pg10 PARTITION OF parent_pg10 FOR VALUES FROM (200000) TO (9223372036854775807) ; CREATE TABLE Sau khi tạo được bảng con. Bảng master sẽ có cấu trúc như bên dưới. 10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# 0 Thực hiện truy vấnINSERT & SELECT dữ liệuVí dụ bên dưới INSERT 3 dòng dữ liệu thông qua bảng master và xác nhận dữ liệu đã được ghi vào các bảng con nhờ chức năng Declarative Partitioning. 10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# 1 Như kết quả EXPLAIN của câu lệnh SELECT bảng master bên dưới. Mặc định dữ liệu được tìm kiếm qua các bảng con chứa dữ liệu tương ứng (bảng master không chứa dữ liệu). 10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# 2 Cũng giống với chức năng partitioning sử dụng kết thừa. Sau khi set tham số constraint_exclusion sang off, dữ liệu được tìm kiếm trên tất cả các bảng. Ở phiên bản PostgreSQL 11, PostgreSQL không sử dụng exclusion constraint cho việc loại trừ tìm kiếm dữ liệu bảng con không cần thiết (prunning), nên tham số constraint_exclusion không có hiệu lực, thay vào đó ta sử dụng tham số enable_partition_pruning. 10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# 3 UPDATE dữ liệu
10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# 4
10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# 5 Ở trường hợp dữ liệu đồng thời được update trên một cột key, có thể xảy ra lỗi, vui lòng xem mục hạn chế ở cuối bài viết này. PerformanceNhư ví dụ bên dưới. Do có overhead bởi TRIGGER, Performance của INSERT giảm rõ rệt (~10 lần) khi sử dụng phương thức partitioning sử dụng kế thừa.
10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# 6
10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# 7 Một số hạn chế và chú ý liên quan tới chức năng Declarative Partitioning
10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# 8
10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# 9
10000 postgres@postgres=# CREATE TABLE child1(check(range <= 99999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child2(check(range > 99999 and range <= 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# CREATE TABLE child3(check(range > 199999)) inherits(parent); CREATE TABLE 10000 postgres@postgres=# 4
10000 postgres@postgres=# CREATE OR REPLACE FUNCTION insert_to_child () RETURNS TRIGGER AS $$ BEGIN END;
$$
LANGUAGE PLPGSQL;
CREATE FUNCTION
10000 postgres@postgres=# CREATE TRIGGER insert_to_child_tg BEFORE INSERT ON parent FOR EACH ROW EXECUTE PROCEDURE insert_to_child();
CREATE TRIGGER1
10000 postgres@postgres=# CREATE OR REPLACE FUNCTION insert_to_child () RETURNS TRIGGER AS $$ BEGIN END;
$$
LANGUAGE PLPGSQL;
CREATE FUNCTION
10000 postgres@postgres=# CREATE TRIGGER insert_to_child_tg BEFORE INSERT ON parent FOR EACH ROW EXECUTE PROCEDURE insert_to_child();
CREATE TRIGGER2
10000 postgres@postgres=# CREATE OR REPLACE FUNCTION insert_to_child () RETURNS TRIGGER AS $$ BEGIN END;
$$
LANGUAGE PLPGSQL;
CREATE FUNCTION
10000 postgres@postgres=# CREATE TRIGGER insert_to_child_tg BEFORE INSERT ON parent FOR EACH ROW EXECUTE PROCEDURE insert_to_child();
CREATE TRIGGER3
10000 postgres@postgres=# CREATE OR REPLACE FUNCTION insert_to_child () RETURNS TRIGGER AS $$ BEGIN END;
$$
LANGUAGE PLPGSQL;
CREATE FUNCTION
10000 postgres@postgres=# CREATE TRIGGER insert_to_child_tg BEFORE INSERT ON parent FOR EACH ROW EXECUTE PROCEDURE insert_to_child();
CREATE TRIGGER4
Ta có thể sử dụng chức năng partitioning table kết hợp với postgres_fdw để scale out hệ thống như bên dưới ở phiên bản PostgreSQL 11. Ở phiên bản 10 có thể cấu hình được hệ thống bên dưới, nhưng hệ thống chưa hỗ trợ hoàn toàn các câu lệnh SQL cho postgres_fdw. Hệ thống ví dụ bên dưới, sử dụng chức năng partitioning với 2 bảng con, 2 bảng con này tiếp tục được đồng bộ sang các DB install khác thông qua chức năng postgres_fdw. 10000 postgres@postgres=# CREATE OR REPLACE FUNCTION insert_to_child () RETURNS TRIGGER AS $$ BEGIN END;
$$
LANGUAGE PLPGSQL;
CREATE FUNCTION
10000 postgres@postgres=# CREATE TRIGGER insert_to_child_tg BEFORE INSERT ON parent FOR EACH ROW EXECUTE PROCEDURE insert_to_child();
CREATE TRIGGER5 Những thay đổi ở phiên bản PostgreSQL 11Ở phiên bản PostgreSQL 11, có 3 cải thiện chính như bên dưới. Faster partition pruningỞ phiên bản 10 PostgreSQL sử dụng constraint_exclusion để bỏ qua partition không cần thiết. Việc loại bỏ này thực hiện bởi sử dụng giá trị ở WHERE clause rồi so sánh với các metadata của từng partition. Phiên bản 11 không sử dụng constraint_exclusion nữa mà thực hiện tìm kiếm trực tiếp tới partition cần thiết làm tăng performance. Partition Pruning at Execution TimePhiên bản 10 thực hiện bỏ qua partition không cần thiết (prunning) ở giai đoạn planning. Nếu chỉ thực hiện ở giai đoạn này, PostgreSQL sẽ không thể thực hiện prunning được những câu lệnh có truy vấn phụ như ví dụ bên dưới.
10000 postgres@postgres=# CREATE OR REPLACE FUNCTION insert_to_child () RETURNS TRIGGER AS $$ BEGIN END;
$$
LANGUAGE PLPGSQL;
CREATE FUNCTION
10000 postgres@postgres=# CREATE TRIGGER insert_to_child_tg BEFORE INSERT ON parent FOR EACH ROW EXECUTE PROCEDURE insert_to_child();
CREATE TRIGGER6
10000 postgres@postgres=# CREATE OR REPLACE FUNCTION insert_to_child () RETURNS TRIGGER AS $$ BEGIN END;
$$
LANGUAGE PLPGSQL;
CREATE FUNCTION
10000 postgres@postgres=# CREATE TRIGGER insert_to_child_tg BEFORE INSERT ON parent FOR EACH ROW EXECUTE PROCEDURE insert_to_child();
CREATE TRIGGER7 Như đã nói ở trên. Do PostgreSQL 10 không sử dụng exclusion constraint để bỏ qua partition không cần thiết, nên tham số constraint_exclusion không có hiệu lực đối với Declarative Partitioning trên phiên bản 11, thay vào đó là parameter enable_partition_pruning (mặc định là on). |