Günce Kaya
guncekaya.blogspot.com
22 Eylül 2018
6. Backup and Streaming Replication
7. Alter Table Add Column
8. SHA-2 Fonksiyonları
9. Stored Procedure
10. Group Access on Data Folder
11. Insert On Conflict Do Nothing
12. Pg_stat_statement İyileştirmesi
pic: www.fotolia.com
pic: https://www.fotolia.com
pic: http://www.bakayelektronik.com
CREATE TABLE <partitioned_table>
(col1 data_type,
...,
...)
PARTITION BY HASH(<hash_key>);
CREATE TABLE <partition_table>
PARTITION OF <partitioned_table>
FOR VALUES WITH (MODULUS X, REMAINDER Y);
hash_key = sütun adı
CREATE TABLE ->
CREATE PARTITION -> TABLES
INSERT 10000 ->
rows
[pg11] # create table part_t1
partition of part_t for values with (modulus 3, remainder 0);
CREATE TABLE
[pg11] # create table part_t2
partition of part_t for values with (modulus 3, remainder 1);
CREATE TABLE
[pg11] # create table part_t3
partition of part_t for values with (modulus 3, remainder 2);
CREATE TABLE
[pg11] # create table part_t(date_ date)
partition by hash (date_);
CREATE TABLE
[pg11] # insert into part_t
select i from
generate_Series('20180101',
'20190202',
'1 hour'::interval
) as i;
INSERT 0 9529
[pg11] # select count(*) from part_t1;
count
-------
2904
(1 row)
[pg11] # select count(*) from part_t2;
count
-------
3144
(1 row)
[pg11] # select count(*) from part_t3;
count
-------
3481
(1 row)
PG10'da Partition tablosu üzerinde UNIQUE KEY oluşturulamazdı.
PG11 ile Partitionlar arası UNIQUE KEY
Mevcut/oluşturulacak child tablolarda otomatik oluşturulur.
Foreign Key desteği geldi!
SET ENABLE_PARTITIONWISE_AGGREGATE=OFF
SET ENABLE_PARTITIONWISE_AGGREGATE=ON
Default off
Partitioned tablolar arasındaki JOIN, partition keyleri eşleşen partition tablolarının JOIN yapılmasıyla gerçekleşir!
JOIN koşulunda tüm partition key'lerinin olması gerekir!
Partition key'lerin veri tipi aynı olmalı.
Daha fazla IO ve Memory
SET ENABLE_PARTITIONWISE_JOIN_AGGREGATE=ON
CREATE UNIQUE INDEX id_idx ON products_2
USING btree (id) INCLUDE (p_status, date);
=# CREATE UNIQUE INDEX new_unique_idx_2
ON new_example(a, b) INCLUDE (round(c));
ERROR: 0A000: expressions are not supported in
included columns
[pg11] # EXPLAIN CREATE MATERIALIZED VIEW mv1
AS SELECT COUNT(*) FROM products;
QUERY PLAN
------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=255070.26..255070.27 rows=1 width=8)
-> Gather (cost=255070.05..255070.26 rows=2 width=8)
Workers Planned: 2
-> Partial Aggregate (cost=254070.05..254070.06 rows=1 width=8)
-> Parallel Append (cost=0.00..233226.32 rows=8337489 width=0)
-> Parallel Seq Scan on products_p2 (cost=0.00..63928.14 rows=2782714 width=0)
-> Parallel Seq Scan on products_p1 (cost=0.00..63809.85 rows=2777585 width=0)
-> Parallel Seq Scan on products_p3 (cost=0.00..63800.90 rows=2777190 width=0)
(8 rows)
CREATE MATERIALIZED VIEW
CREATE INDEX
max_parallel_maintenance_workers en az 2 olmalı
[pg11] # EXPLAIN
SELECT max(id) INTO val FROM products;
QUERY PLAN
------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=255070.26..255070.27 rows=1 width=4)
-> Gather (cost=255070.05..255070.26 rows=2 width=4)
Workers Planned: 2
-> Partial Aggregate (cost=254070.05..254070.06 rows=1 width=4)
-> Parallel Append (cost=0.00..233226.32 rows=8337489 width=4)
-> Parallel Seq Scan on products_p2 (cost=0.00..63928.14 rows=2782714 width=4)
-> Parallel Seq Scan on products_p1 (cost=0.00..63809.85 rows=2777585 width=4)
-> Parallel Seq Scan on products_p3 (cost=0.00..63800.90 rows=2777190 width=4)
(8 rows)
pg_read_server_files
pg_write_server_files
pg_execute_server_program
Superuser olmayan kullanıcıların sunucu üzerinde işlem yapmasını sağlar.
GRANT pg_execute_server_program TO admin_user;
[pg11] # \du gunce
List of roles
Role name | Attributes | Member of
-----------+------------+-------------------------
gunce | | {}
[pg11] # select current_user;
current_user
--------------
gunce
(1 row)
[pg11] # copy(select 1 ) to '/tmp/copytest.csv';
ERROR: 42501: must be superuser or a member of the
pg_write_server_files role to COPY to a file
[pg11] # GRANT pg_write_server_files to gunce ;
GRANT ROLE
[pg11] # copy(select 1 ) to '/home/guncek/copytest.csv';
ERROR: 42501: could not open file "/home/guncek/copytest.csv"
for writing: Permission denied
HINT: COPY TO instructs the PostgreSQL server process to write
a file. You may want a client-side facility such as psql's \copy.
LOCATION: BeginCopyTo, copy.c:1866
[pg11] # copy(select 1 ) to '/var/lib/pgsql/copytest.csv';
COPY 1
[pg11] # \du postgres
List of roles
-[ RECORD 1 ]----------------------------------------------------------
Role name | postgres
Attributes | Superuser, Create role, Create DB, Replication, Bypass RLS
Member of | {}
[pg11] # select current_user;
current_user
--------------
postgres
(1 row)
[pg11] # copy(select 1 ) to '/home/guncek/copytest.csv';
ERROR: 42501: could not open file
"/home/guncek/copytest.csv" for writing: Permission denied
HINT: COPY TO instructs the PostgreSQL server process to write
a file. You may want a client-side facility such as psql's \copy.
LOCATION: BeginCopyTo, copy.c:1866
initdb -D /pg_data --wal-segsize=32
SHOW wal_segment_size;
/usr/pgsql-11/bin/pg_controldata | grep "Bytes per WAL segment"
[postgres] # show wal_segment_size ;
wal_segment_size
------------------
64MB
(1 row)
[postgres@asus-guncek ~]$ /usr/pgsql-11/bin/pg_ctl
-D /var/lib/pgsql/11-3/data/ stop
[postgres@asus-guncek ~]$ /usr/pgsql-11/bin/pg_resetwal
-D /var/lib/pgsql/11-3/data/
--wal-segsize=32
Write-ahead log reset
[postgres@asus-guncek ~]$ /usr/pgsql-11/bin/pg_ctl
-D /var/lib/pgsql/11-3/data/ start
[postgres] # show wal_segment_size ;
wal_segment_size
------------------
32MB
(1 row)
archive_command -> cp, scp, rsync
16MB'lik 100 adet dosya
64MB'lik 25 adet dosya
16MB'lik dosyaların SCP ile aktarımı -> 24sn
64MB'lik dosyaların SCP ile aktarımı -> 8sn
16MB'lik dosyaların RSYNC ile aktarımı -> 3dk24sn
64MB'lik dosyaların RSYNC ile aktarımı -> 14sn
pg_attribute tablosunda iki yeni sütun
attmissingval
atthasmissing
PG10
PG11
PG10 daki tablonun boyutu
PG11 deki tablonun boyutu
add_col tablosunun "g" sütunun pg_attribute tablosundaki görüntüsü
sha224(), sha256(), sha384(), sha512()
[postgres] # SELECT LENGTH(sha224('test')),sha224('a');
length | sha224
--------+------------------------------------------------------------
28 | \xabd37534c7d9a2efb9465de931cd7055ffdb8879563ae98078d6d6d5
(1 row)
[postgres] # SELECT LENGTH(sha256('test')),sha256('a');
length | sha256
--------+--------------------------------------------------------------------
32 | \xca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
(1 row)
[postgres] # SELECT LENGTH(sha384('test')),sha384('a');
length | sha384
--------+----------------------------------------------------------------------------------------------------
48 | \x54a59b9f22b0b80880d8427e548b7c23abd873486e1f035dce9cd697e85175033caa88e6d57bc35efae0b5afd3145f31
(1 row)
[postgres] # SELECT LENGTH(sha512('test')),sha512('a');
-[ RECORD 1 ]------------------------------------------------------------------------------------------------------------------------------
length | 64
sha512 | \x1f40fc92da241694750979ee6cf582f2d5d7d28e18335de05abc54d0560e0f5302860c652bf08d560252aa5e74210546f369fbbbce8c12cfc7957b2652fe9a75
[postgres] #
CREATE PROCEDURE
CALL <PROCEDURENAME>
-g veya --allow-group-access
/usr/pgsql-11/bin/initdb -g -D /var/lib/pgsql/11-2/data
[postgres@asus-guncek ~]$ /usr/pgsql-11/bin/pg_basebackup
-D /var/lib/pgsql/11-3/data/ -p5411 -h 192.168.122.100
--create-slot --slot='test'
[postgres] # SELECT slot_name, slot_type, restart_lsn
FROM pg_replication_slots;
slot_name | slot_type | restart_lsn
-----------+-----------+-------------
test | physical | 3/6E000000
(1 row)
pg_basebackup: could not send replication command
"CREATE_REPLICATION_SLOT "test" PHYSICAL RESERVE_WAL":
ERROR: replication slot "test" already exists
PG10
PG11
Partition tablolarında TRIGGER desteği
Partition Foreign tablolarında postgres_fdw için push down aggregate özelliği geldi.
SP-GIST(Space partitioned) için yeni TEXT prefix operator -> ^@
LIKE 'kelime%' ile aynı
Daha etkili
Diğer fonksiyonlar;
websearch_to_tsquery()
json(b)_to_tsvector()
guncekaya.blogspot.com
twitter: @gguncesi