Merhaba,
Bugün range partition oluşturmak ve defult partition yaratacağız, bunun üzerine default partitiondan nasıl kurtulabilirizi yazmaya çalışacağım,
Aşağıdaki komutlar ile range partitionlu bir tablo oluşturup, Sonrasında onun partitionlarını oluşturacağız.
CREATE TABLE deneme (
ldate date not null,
sayi int,
sayi2 int
) PARTITION BY RANGE (ldate);
CREATE TABLE deneme_y2016 PARTITION OF deneme FOR VALUES FROM ('2016-01-01') TO ('2017-01-01');
CREATE TABLE deneme_y2017 PARTITION OF deneme FOR VALUES FROM ('2017-01-01') TO ('2018-01-01');
CREATE TABLE deneme_y2018 PARTITION OF deneme FOR VALUES FROM ('2018-01-01') TO ('2019-01-01');
CREATE TABLE deneme_y2019 PARTITION OF deneme FOR VALUES FROM ('2019-01-01') TO ('2020-01-01');
CREATE TABLE deneme_y2020 PARTITION OF deneme FOR VALUES FROM ('2020-01-01') TO ('2021-01-01');
Şimdi tabloya data atmaya başlayalım;
INSERT INTO deneme (ldate, sayi, sayi2) VALUES ('2016-07-10', 66, 100);
Aşağıdaki insert hata verecek çünkü ilgili tarihi kapsayacak bir partition olmadığını söylüyor.
INSERT INTO deneme (ldate, sayi, sayi2) VALUES ('2021-07-10', 66, 100);
ERROR: no partition of relation "deneme" found for row
DETAIL: Partition key of the failing row contains (ldate) = (2021-07-10).
Bu tabloya ilgili yıl ile alakalı değilde default partition tanımlayalım, Fakat default partition kesinlikle önerilmemektedir performans açısından çok sıkıntı yaşatır, çünkü tabloya gelen selectlerde ilgili partitiondan önce default partitionda varmı diyede bakar.
CREATE TABLE deneme_default PARTITION OF deneme DEFAULT;
Bu partition oluştuktan sonra tekrardan aynı insert'ümüzü çalıştırdığımızda insertler hata vermeyecektir.
INSERT INTO deneme (ldate, sayi, sayi2) VALUES ('2021-07-10', 66, 100);
INSERT INTO deneme (ldate, sayi, sayi2) VALUES ('2022-07-10', 66, 100);
Peki madem önerilmiyor bu default partition, varsayalımki tablomuzdada var, nasıl kurtuluruz?
Default partition yarattıktan sonra başka bir partition yaratmaya izin vermez postgresql deneyelim,
CREATE TABLE deneme_y2021 PARTITION OF deneme FOR VALUES FROM ('2021-01-01') TO ('2022-01-01');
ERROR: updated partition constraint for default partition "deneme_default" would be violated by some row
CREATE TABLE deneme_y2022 PARTITION OF deneme FOR VALUES FROM ('2022-01-01') TO ('2022-01-01');
ERROR: updated partition constraint for default partition "deneme_default" would be violated by some row
Ozaman default partitionu detach ederiz sonrasında yeni partitionlarımızı oluştururuz ve detach ettiğimiz default partitiondan da datayı aktarırız.
postgres=# alter table deneme detach partition deneme_default;
postgres=# CREATE TABLE deneme_y2021 PARTITION OF deneme FOR VALUES FROM ('2021-01-01') TO ('2022-01-01');
CREATE TABLE
postgres=# CREATE TABLE deneme_y2022 PARTITION OF deneme FOR VALUES FROM ('2022-01-01') TO ('2023-01-01');
CREATE TABLE
postgres=# insert into deneme select * from deneme_default ;
INSERT 0 4
postgres=# dt
List of relations
Schema | Name | Type | Owner
--------+-----------------+-------------------+----------
public | deneme | partitioned table | postgres
public | deneme_default | table | postgres
public | deneme_y2016 | table | postgres
public | deneme_y2017 | table | postgres
public | deneme_y2018 | table | postgres
public | deneme_y2019 | table | postgres
public | deneme_y2020 | table | postgres
public | deneme_y2021 | table | postgres
public | deneme_y2022 | table | postgres
(9 rows)
postgres=# select * from deneme_y2021
postgres-# ;
ldate | sayi | sayi2
------------+------+-------
2021-07-10 | 66 | 100
2021-07-10 | 66 | 100
(2 rows)
postgres=#
postgres=#
postgres=# select * from deneme_y2022;
ldate | sayi | sayi2
------------+------+-------
2022-07-10 | 66 | 100
2022-07-10 | 66 | 100
(2 rows)
Artık default partitionu drop edebilirriz.
postgres=# drop table deneme_default ;
DROP TABLE
Umarım Faydalı olmuştur..
Yorumlar
Henüz yorum yapılmadı.