9 July 2019
Günce Kaya
guncekaya.blogspot.com
@gguncesi
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
New Introspection functions
guncekaya.blogspot.com / @gguncesi
New Introspection functions
test=# SELECT pg_partition_root('players_2019') ;
pg_partition_root
-------------------
players
(1 row)
guncekaya.blogspot.com / @gguncesi
New Introspection functions
test=# SELECT pg_partition_ancestors('players_2019') ;
pg_partition_ancestors
------------------------
players_2019
players
(2 rows)
guncekaya.blogspot.com / @gguncesi
New Introspection functions
test=# SELECT pg_partition_tree('players');
pg_partition_tree
----------------------------------------
(players,,f,0)
(players_2019,players,t,1)
(players_2018,players,t,1)
(players_2017,players,t,1)
(4 rows)
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
CTE can be inlined
[pg11] # EXPLAIN WITH test AS (SELECT generate_series(1,5)) SELECT * FROM test;
QUERY PLAN
---------------------------------------------------------
CTE Scan on test (cost=5.02..25.02 rows=1000 width=4)
CTE test
-> ProjectSet (cost=0.00..5.02 rows=1000 width=4)
-> Result (cost=0.00..0.01 rows=1 width=0)
(4 rows)
[pg12]=# EXPLAIN WITH test as (SELECT generate_series(1,5)) SELECT * FROM test;
QUERY PLAN
------------------------------------------------
ProjectSet (cost=0.00..0.04 rows=5 width=4)
-> Result (cost=0.00..0.01 rows=1 width=0)
(2 rows)
guncekaya.blogspot.com / @gguncesi
CTE with MATERIALIZED
[pg12]=# EXPLAIN WITH test as (SELECT generate_series(1,5))
SELECT * FROM test;
QUERY PLAN
------------------------------------------------
ProjectSet (cost=0.00..0.04 rows=5 width=4)
-> Result (cost=0.00..0.01 rows=1 width=0)
(2 rows)
[pg12]=# EXPLAIN WITH test as MATERIALIZED (SELECT generate_series(1,5))
SELECT * FROM test;
QUERY PLAN
--------------------------------------------------------
CTE Scan on test (cost=0.04..0.14 rows=5 width=4)
CTE test
-> ProjectSet (cost=0.00..0.04 rows=5 width=4)
-> Result (cost=0.00..0.01 rows=1 width=0)
(4 rows)
guncekaya.blogspot.com / @gguncesi
CTE with MATERIALIZED
[pg12]=# EXPLAIN WITH test AS NOT MATERIALIZED (SELECT generate_series(1,5))
SELECT * FROM test;
QUERY PLAN
------------------------------------------------
ProjectSet (cost=0.00..0.04 rows=5 width=4)
-> Result (cost=0.00..0.01 rows=1 width=0)
(2 rows)
guncekaya.blogspot.com / @gguncesi
EXPLAIN (SETTINGS ON)
[pg12]=# SET seq_page_cost=1.5;
SET
[pg12]=# EXPLAIN (settings on) SELECT * FROM test4;
QUERY PLAN
----------------------------------------------------------
Seq Scan on test4 (cost=0.00..35.40 rows=2040 width=12)
Settings: seq_page_cost = '1.5'
(2 rows)
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
-[ RECORD 1 ]------+-------------------------------
pid | 13836
datid | 16384
datname | test
relid | 24586
index_relid | 0
command | CREATE INDEX
phase | building index: scanning table
lockers_total | 0
lockers_done | 0
current_locker_pid | 0
blocks_total | 44248
blocks_done | 27186
tuples_total | 0
tuples_done | 0
partitions_total | 0
partitions_done | 0
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
SET effective_cache_size ='3.90';
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
Generated Columns
[pg12]=# CREATE TABLE test4(c1 int, c2 int, c3 int GENERATED ALWAYS AS (c1 * c2) STORED);
CREATE TABLE
[pg12]=# \d test4
Table "public.test4"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+--------------------------------------
c1 | integer | | |
c2 | integer | | |
c3 | integer | | | generated always as (c1 * c2) stored
guncekaya.blogspot.com / @gguncesi
Generated Columns
Can not be specified as a partitioning keys!
[pg12]=# SELECT * FROM information_schema.column_column_usage ;
table_catalog | table_schema | table_name | column_name | dependent_column
---------------+--------------+------------+-------------+------------------
test | public | test4 | c1 | c3
test | public | test4 | c2 | c3
(2 rows)
[pg12]=# SELECT attname, attgenerated FROM pg_attribute
WHERE attname IN ('c1', 'c2', 'c3') ;
attname | attgenerated
---------+--------------
c1 |
c2 |
c3 | s
(3 rows)
guncekaya.blogspot.com / @gguncesi
[pg12]=# COPY(SELECT generate_series(1,10) as a, generate_series(1,5) as b)
TO '/tmp/test.csv'
HEADER DELIMITER ';' CSV;
COPY 10
[pg12]=# COPY test3 FROM '/tmp/test.csv' HEADER DELIMITER ';' CSV
WHERE a>5;
COPY 5
-- OR
[pg12]=# \copy test3 from '/tmp/test.csv' HEADER DELIMITER ';' CSV
WHERE a>5;
COPY 5
guncekaya.blogspot.com / @gguncesi
[pg12]=# BEGIN;
BEGIN
[pg12]=# INSERT INTO test VALUES(1);
INSERT 0 1
[pg12]=# COMMIT AND CHAIN;
COMMIT
[pg12]=# INSERT INTO test VALUES(2);
INSERT 0 1
[pg12]=# ROLLBACK AND CHAIN;
ROLLBACK
[pg12]=# END;
COMMIT
[pg12]=# SELECT * FROM test;
generate_series
-----------------
1
(1 row)
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
postgres=# SELECT date_trunc('day',
TIMESTAMP WITH TIME ZONE '2019-05-24 20:38:40+00', 'Africa/Malabo') ;
date_trunc
------------------------
2019-05-23 19:00:00-04
(1 row)
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
[pg12]=# ALTER TABLE pg_attribute SET (autovacuum_vacuum_scale_factor=0);
ALTER TABLE
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com / @gguncesi
guncekaya.blogspot.com
@gguncesi
guncekaya.blogspot.com / @gguncesi