Some examples:
SELECT foo FROM regexp_split_to_table('the quick brown fox jumps over the lazy dog', '\s+') AS foo;
- foo
+ foo
-------
- the
- quick
- brown
- fox
- jumps
- over
- the
- lazy
- dog
+ the
+ quick
+ brown
+ fox
+ jumps
+ over
+ the
+ lazy
+ dog
(9 rows)
SELECT regexp_split_to_array('the quick brown fox jumps over the lazy dog', '\s+');
- regexp_split_to_array
+ regexp_split_to_array
-----------------------------------------------
{the,quick,brown,fox,jumps,over,the,lazy,dog}
(1 row)
SELECT foo FROM regexp_split_to_table('the quick brown fox', '\s*') AS foo;
- foo
+ foo
-----
- t
- h
- e
- q
- u
- i
- c
- k
- b
- r
- o
- w
- n
- f
- o
- x
+ t
+ h
+ e
+ q
+ u
+ i
+ c
+ k
+ b
+ r
+ o
+ w
+ n
+ f
+ o
+ x
(16 rows)
SET xmloption TO DOCUMENT;
SELECT xml_is_well_formed('<>');
- xml_is_well_formed
+ xml_is_well_formed
--------------------
f
(1 row)
SELECT xml_is_well_formed('
');
- xml_is_well_formed
+ xml_is_well_formed
--------------------
t
(1 row)
SET xmloption TO CONTENT;
SELECT xml_is_well_formed('abc');
- xml_is_well_formed
+ xml_is_well_formed
--------------------
t
(1 row)
SELECT xml_is_well_formed_document('
bar');
- xml_is_well_formed_document
+ xml_is_well_formed_document
-----------------------------
t
(1 row)
SELECT xml_is_well_formed_document('
bar');
- xml_is_well_formed_document
+ xml_is_well_formed_document
-----------------------------
f
(1 row)
SELECT xpath('/my:a/text()', 'test',
ARRAY[ARRAY['my', 'http://example.com']]);
- xpath
+ xpath
--------
{test}
(1 row)
SELECT xpath_exists('/my:a/text()', 'test',
ARRAY[ARRAY['my', 'http://example.com']]);
- xpath_exists
+ xpath_exists
--------------
t
(1 row)
'concat(SIZE[@unit!="sq_km"], " ", SIZE[@unit!="sq_km"]/@unit)',
premier_name text PATH 'PREMIER_NAME' DEFAULT 'not specified');
- id | ordinality | COUNTRY_NAME | country_id | size_sq_km | size_other | premier_name
+ id | ordinality | COUNTRY_NAME | country_id | size_sq_km | size_other | premier_name
----+------------+--------------+------------+------------+--------------+---------------
1 | 1 | Australia | AU | | | not specified
5 | 2 | Japan | JP | | 145935 sq_mi | Shinzo Abe
SELECT xmltable.*
FROM xmlelements, XMLTABLE('/root' PASSING data COLUMNS element text);
- element
+ element
-------------------------
- Hello2a2 bbbxxxCC
+ Hello2a2 bbbxxxCC
]]>
SELECT JSON('{ "a" : 123, "b": [ true, "foo" ], "a" : "bar" }');
- json
+ json
--------------------------------------------------
{ "a" : 123, "b": [ true, "foo" ], "a" : "bar" }
(1 row)
SELECT JSON('{"a": 123, "b": [true, "foo"], "a": "bar"}' RETURNING jsonb);
- json
+ json
----------------------------------
{"a": "bar", "b": [true, "foo"]}
(1 row)
JSON_OBJECTAGG
-
+
JSON_OBJECTAGG (
{ key_expression { VALUE | ':' } value_expression }
(1 row)
SELECT JSON_VALUE('"03:04 2015-02-01"', '$.datetime("HH24:MI YYYY-MM-DD")' RETURNING date);
- json_value
+ json_value
------------
2015-02-01
(1 row)
-
+
VALUE | SCALAR | ARRAY | OBJECT
SELECT JSON_SERIALIZE(JSON_SCALAR('foo'));
- json_serialize
+ json_serialize
----------------
"foo"
(1 row)
-
+
SQL/JSON Common Clauses
to log the memory contexts of a backend process. For example:
postgres=# SELECT pg_log_backend_memory_contexts(pg_backend_pid());
- pg_log_backend_memory_contexts
+ pg_log_backend_memory_contexts
--------------------------------
t
(1 row)
node_a_slot |
postgres=# SELECT slot_name, slot_type, active FROM pg_replication_slots;
- slot_name | slot_type | active
+ slot_name | slot_type | active
-------------+-----------+--------
node_a_slot | physical | f
(1 row)
INSERT INTO test VALUES (123, 'foo', 'bar');
SELECT hstore(t) FROM test AS t;
- hstore
+ hstore
---------------------------------------------
"col1"=>"123", "col2"=>"foo", "col3"=>"bar"
(1 row)
SELECT * FROM populate_record(null::test,
'"col1"=>"456", "col2"=>"zzz"');
- col1 | col2 | col3
+ col1 | col2 | col3
------+------+------
- 456 | zzz |
+ 456 | zzz |
(1 row)
INSERT INTO test VALUES (123, 'foo', 'bar');
SELECT (r).* FROM (SELECT t #= '"col3"=>"baz"' AS r FROM test t) s;
- col1 | col2 | col3
+ col1 | col2 | col3
------+------+------
123 | foo | baz
(1 row)
details such as whitespace. For example, note the differences here:
SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::json;
- json
+ json
-------------------------------------------------
{"bar": "baz", "balance": 7.77, "active":false}
(1 row)
SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::jsonb;
- jsonb
+ jsonb
--------------------------------------------------
{"bar": "baz", "active": false, "balance": 7.77}
(1 row)
example:
SELECT '{"reading": 1.230e-5}'::json, '{"reading": 1.230e-5}'::jsonb;
- json | jsonb
+ json | jsonb
-----------------------+-------------------------
{"reading": 1.230e-5} | {"reading": 0.00001230}
(1 row)
any combination of INSERT, UPDATE,
DELETE, and TRUNCATE, similar to how triggers are fired by
particular event types. By default, all operation types are replicated.
- (Row filters have no effect for TRUNCATE. See
+ (Row filters have no effect for TRUNCATE. See
).
By default, all data from all published tables will be replicated to the
- appropriate subscribers. The replicated data can be reduced by using a
+ appropriate subscribers. The replicated data can be reduced by using a
row filter. A user might choose to use row filters
for behavioral, security or performance reasons. If a published table sets a
row filter, a row is replicated only if its data satisfies the row filter
test_pub=# \dRp+
Publication p1
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
----------+------------+---------+---------+---------+-----------+----------
postgres | f | t | t | t | t | f
Tables:
"public.t1" WHERE ((a > 5) AND (c = 'NSW'::text))
Publication p2
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
----------+------------+---------+---------+---------+-----------+----------
postgres | f | t | t | t | t | f
Tables:
"public.t2" WHERE (e = 99)
Publication p3
- Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
+ Owner | All tables | Inserts | Updates | Deletes | Truncates | Via root
----------+------------+---------+---------+---------+-----------+----------
postgres | f | t | t | t | t | f
Tables:
test_pub=# \d t1
Table "public.t1"
- Column | Type | Collation | Nullable | Default
+ Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
- a | integer | | not null |
- b | integer | | |
- c | text | | not null |
+ a | integer | | not null |
+ b | integer | | |
+ c | text | | not null |
Indexes:
"t1_pkey" PRIMARY KEY, btree (a, c)
Publications:
test_pub=# \d t2
Table "public.t2"
- Column | Type | Collation | Nullable | Default
+ Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
- d | integer | | not null |
- e | integer | | |
- f | integer | | |
+ d | integer | | not null |
+ e | integer | | |
+ f | integer | | |
Indexes:
"t2_pkey" PRIMARY KEY, btree (d)
Publications:
test_pub=# \d t3
Table "public.t3"
- Column | Type | Collation | Nullable | Default
+ Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
- g | integer | | not null |
- h | integer | | |
- i | integer | | |
+ g | integer | | not null |
+ h | integer | | |
+ i | integer | | |
Indexes:
"t3_pkey" PRIMARY KEY, btree (g)
Publications:
test_pub=# INSERT INTO t1 VALUES (9, 109, 'NSW');
INSERT 0 1
-test_pub=# SELECT * FROM t1;
+test_pub=# SELECT * FROM t1;
a | b | c
---+-----+-----
2 | 102 | NSW
test_sub=# SELECT * FROM t1;
- a | b | c
+ a | b | c
---+-----+-----
6 | 106 | NSW
9 | 109 | NSW
UPDATE 1
test_pub=# SELECT * FROM t1;
- a | b | c
+ a | b | c
---+-----+-----
2 | 102 | NSW
3 | 103 | QLD
test_sub=# SELECT * FROM t1;
- a | b | c
+ a | b | c
---+-----+-----
9 | 109 | NSW
6 | 999 | NSW
UPDATE 1
test_pub=# SELECT * FROM t1;
- a | b | c
+ a | b | c
-----+-----+-----
3 | 103 | QLD
4 | 104 | VIC
test_sub=# SELECT * FROM t1;
- a | b | c
+ a | b | c
-----+-----+-----
9 | 109 | NSW
6 | 999 | NSW
UPDATE 1
test_pub=# SELECT * FROM t1;
- a | b | c
+ a | b | c
-----+-----+-----
3 | 103 | QLD
4 | 104 | VIC
test_sub=# SELECT * FROM t1;
- a | b | c
+ a | b | c
-----+-----+-----
6 | 999 | NSW
555 | 102 | NSW
INSERT 0 3
test_pub=# SELECT * FROM parent ORDER BY a;
- a
+ a
---
2
3
test_sub=# SELECT * FROM parent ORDER BY a;
- a
+ a
---
2
3
INSERT 0 3
test_pub=# SELECT * FROM parent ORDER BY a;
- a
+ a
---
2
3
test_sub=# SELECT * FROM child ORDER BY a;
- a
+ a
---
5
6
postgres=# -- There are no changes to see yet
postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
- lsn | xid | data
+ lsn | xid | data
-----+-----+------
(0 rows)
postgres=# -- DDL isn't replicated, so all you'll see is the transaction
postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
- lsn | xid | data
+ lsn | xid | data
-----------+-------+--------------
0/BA2DA58 | 10297 | BEGIN 10297
0/BA5A5A0 | 10297 | COMMIT 10297
postgres=# -- Once changes are read, they're consumed and not emitted
postgres=# -- in a subsequent call:
postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
- lsn | xid | data
+ lsn | xid | data
-----+-----+------
(0 rows)
postgres=*# COMMIT;
postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
- lsn | xid | data
+ lsn | xid | data
-----------+-------+---------------------------------------------------------
0/BA5A688 | 10298 | BEGIN 10298
0/BA5A6F0 | 10298 | table public.data: INSERT: id[integer]:1 data[text]:'1'
postgres=# -- You can also peek ahead in the change stream without consuming changes
postgres=# SELECT * FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL);
- lsn | xid | data
+ lsn | xid | data
-----------+-------+---------------------------------------------------------
0/BA5A8E0 | 10299 | BEGIN 10299
0/BA5A8E0 | 10299 | table public.data: INSERT: id[integer]:3 data[text]:'3'
postgres=# -- The next call to pg_logical_slot_peek_changes() returns the same changes again
postgres=# SELECT * FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL);
- lsn | xid | data
+ lsn | xid | data
-----------+-------+---------------------------------------------------------
0/BA5A8E0 | 10299 | BEGIN 10299
0/BA5A8E0 | 10299 | table public.data: INSERT: id[integer]:3 data[text]:'3'
postgres=# -- options can be passed to output plugin, to influence the formatting
postgres=# SELECT * FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'include-timestamp', 'on');
- lsn | xid | data
+ lsn | xid | data
-----------+-------+---------------------------------------------------------
0/BA5A8E0 | 10299 | BEGIN 10299
0/BA5A8E0 | 10299 | table public.data: INSERT: id[integer]:3 data[text]:'3'
postgres=*# PREPARE TRANSACTION 'test_prepared1';
postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
- lsn | xid | data
+ lsn | xid | data
-----------+-----+---------------------------------------------------------
0/1689DC0 | 529 | BEGIN 529
0/1689DC0 | 529 | table public.data: INSERT: id[integer]:3 data[text]:'5'
postgres=# COMMIT PREPARED 'test_prepared1';
postgres=# select * from pg_logical_slot_get_changes('regression_slot', NULL, NULL);
- lsn | xid | data
+ lsn | xid | data
-----------+-----+--------------------------------------------
0/168A060 | 529 | COMMIT PREPARED 'test_prepared1', txid 529
(4 row)
postgres=*# INSERT INTO data(data) VALUES('6');
postgres=*# PREPARE TRANSACTION 'test_prepared2';
postgres=# select * from pg_logical_slot_get_changes('regression_slot', NULL, NULL);
- lsn | xid | data
+ lsn | xid | data
-----------+-----+---------------------------------------------------------
0/168A180 | 530 | BEGIN 530
0/168A1E8 | 530 | table public.data: INSERT: id[integer]:4 data[text]:'6'
postgres=# ROLLBACK PREPARED 'test_prepared2';
postgres=# select * from pg_logical_slot_get_changes('regression_slot', NULL, NULL);
- lsn | xid | data
+ lsn | xid | data
-----------+-----+----------------------------------------------
0/168A4B8 | 530 | ROLLBACK PREPARED 'test_prepared2', txid 530
(1 row)
gid field, which is part of the
txn parameter, can be used in this callback to
check if the plugin has already received this PREPARE
- in which case it can either error out or skip the remaining changes of
+ in which case it can either error out or skip the remaining changes of
the transaction.
typedef void (*LogicalDecodeBeginPrepareCB) (struct LogicalDecodingContext *ctx,
-
+
Stream Change Callback
SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event is NOT NULL;
- pid | wait_event_type | wait_event
+ pid | wait_event_type | wait_event
------+-----------------+------------
2540 | Lock | relation
6644 | LWLock | ProcArray
not a valid
BRIN page. For example:
test=# SELECT brin_page_type(get_raw_page('brinidx', 0));
- brin_page_type
+ brin_page_type
----------------
meta
about a
BRIN index metapage. For example:
test=# SELECT * FROM brin_metapage_info(get_raw_page('brinidx', 0));
- magic | version | pagesperrange | lastrevmappage
+ magic | version | pagesperrange | lastrevmappage
------------+---------+---------------+----------------
0xA8109CFA | 1 | 4 | 2
For example:
test=# SELECT * FROM brin_revmap_data(get_raw_page('brinidx', 2)) LIMIT 5;
- pages
+ pages
---------
(6,137)
(6,138)
test=# SELECT * FROM brin_page_items(get_raw_page('brinidx', 5),
'brinidx')
ORDER BY blknum, attnum LIMIT 6;
- itemoffset | blknum | attnum | allnulls | hasnulls | placeholder | value
+ itemoffset | blknum | attnum | allnulls | hasnulls | placeholder | value
------------+--------+--------+----------+----------+-------------+--------------
- 137 | 0 | 1 | t | f | f |
+ 137 | 0 | 1 | t | f | f |
137 | 0 | 2 | f | f | f | {1 .. 88}
- 138 | 4 | 1 | t | f | f |
+ 138 | 4 | 1 | t | f | f |
138 | 4 | 2 | f | f | f | {89 .. 176}
- 139 | 8 | 1 | t | f | f |
+ 139 | 8 | 1 | t | f | f |
139 | 8 | 2 | f | f | f | {177 .. 264}
The returned columns correspond to the fields in the
For example:
test=# SELECT * FROM gist_page_opaque_info(get_raw_page('test_gist_idx', 2));
- lsn | nsn | rightlink | flags
+ lsn | nsn | rightlink | flags
-----+-----+-----------+--------
0/1 | 0/0 | 1 | {leaf}
(1 row)
the data stored in a page of a
GiST index. For example:
test=# SELECT * FROM gist_page_items(get_raw_page('test_gist_idx', 0), 'test_gist_idx');
- itemoffset | ctid | itemlen | dead | keys
+ itemoffset | ctid | itemlen | dead | keys
------------+-----------+---------+------+-------------------
1 | (1,65535) | 40 | f | (p)=((166,166))
2 | (2,65535) | 40 | f | (p)=((332,332))
example:
test=# SELECT * FROM gist_page_items_bytea(get_raw_page('test_gist_idx', 0));
- itemoffset | ctid | itemlen | dead | key_data
+ itemoffset | ctid | itemlen | dead | key_data
------------+-----------+---------+------+-----------------------------------------&zwsp;-------------------------------------------
1 | (1,65535) | 40 | f | \x00000100ffff28000000000000c0644000000000&zwsp;00c06440000000000000f03f000000000000f03f
2 | (2,65535) | 40 | f | \x00000200ffff28000000000000c0744000000000&zwsp;00c074400000000000e064400000000000e06440
the given
HASH index page. For example:
test=# SELECT hash_page_type(get_raw_page('con_hash_index', 0));
- hash_page_type
+ hash_page_type
----------------
metapage
index page. For example:
test=# SELECT * FROM hash_page_items(get_raw_page('con_hash_index', 1)) LIMIT 5;
- itemoffset | ctid | data
+ itemoffset | ctid | data
------------+-----------+------------
1 | (899,77) | 1053474816
2 | (897,29) | 1053474816
index. For example:
test=# SELECT * FROM hash_bitmap_info('con_hash_index', 2052);
- bitmapblkno | bitmapbit | bitstatus
+ bitmapblkno | bitmapbit | bitstatus
-------------+-----------+-----------
65 | 3 | t
EXPLAIN SELECT * FROM pgbench_accounts WHERE filler LIKE '%x%';
- QUERY PLAN
+ QUERY PLAN
-------------------------------------------------------------------&zwsp;------------------
Gather (cost=1000.00..217018.43 rows=1 width=97)
Workers Planned: 2
SELECT stxname, stxkeys, stxddependencies
FROM pg_statistic_ext join pg_statistic_ext_data on (oid = stxoid)
WHERE stxname = 'stts';
- stxname | stxkeys | stxddependencies
+ stxname | stxkeys | stxddependencies
---------+---------+------------------------------------------
stts | 1 5 | {"1 => 5": 1.000000, "5 => 1": 0.423130}
(1 row)
SELECT m.* FROM pg_statistic_ext join pg_statistic_ext_data on (oid = stxoid),
pg_mcv_list_items(stxdmcv) m WHERE stxname = 'stts3';
- index | values | nulls | frequency | base_frequency
+ index | values | nulls | frequency | base_frequency
-------+------------------------+-------+-----------+----------------
0 | {Washington, DC} | {f,f} | 0.003467 | 2.7e-05
1 | {Apo, AE} | {f,f} | 0.003067 | 1.9e-05
postgres=# SELECT * FROM pg_freespace('foo');
- blkno | avail
+ blkno | avail
-------+-------
0 | 0
1 | 0
(20 rows)
postgres=# SELECT * FROM pg_freespace('foo', 7);
- pg_freespace
+ pg_freespace
--------------
1216
(1 row)
calls | 1
total_exec_time | 0.189497
rows | 1
-hit_percent |
+hit_percent |
-[ RECORD 2 ]---+--------------------------------------------------&zwsp;---------------------------
query | SELECT query, calls, total_exec_time, rows, $1 * shared_blks_hit / +
| nullif(shared_blks_hit + shared_blks_read, $2) AS hit_percent+
calls | 0
total_exec_time | 0
rows | 0
-hit_percent |
+hit_percent |
DETAIL: Could not open file "pg_xact/0EED": No such file or directory.
test=# select heap_force_kill('t1'::regclass, ARRAY['(0, 1)']::tid[]);
- heap_force_kill
+ heap_force_kill
-----------------
-
+
(1 row)
test=# select * from t1 where ctid = '(0, 1)';
CONTEXT: while scanning block 0 of relation "public.t1"
test=# select ctid from t1 where xmin = 507;
- ctid
+ ctid
-------
(0,3)
(1 row)
test=# select heap_force_freeze('t1'::regclass, ARRAY['(0, 3)']::tid[]);
- heap_force_freeze
+ heap_force_freeze
-------------------
-
+
(1 row)
test=# select ctid from t1 where xmin = 2;
follows:
postgres=# select start_lsn, end_lsn, prev_lsn, xid, resource_manager, record_type, record_length, main_data_length, fpi_length, description from pg_get_wal_records_info('0/14F9A30', '0/15011D7');
- start_lsn | end_lsn | prev_lsn | xid | resource_manager | record_type | record_length | main_data_length | fpi_length | description
+ start_lsn | end_lsn | prev_lsn | xid | resource_manager | record_type | record_length | main_data_length | fpi_length | description
-----------+-----------+-----------+-----+------------------+--------------+---------------+------------------+------------+---------------------
0/14FA118 | 0/14FB4B0 | 0/14F9958 | 725 | Btree | INSERT_LEAF | 5013 | 2 | 4960 | off 246
0/14FB4B0 | 0/14FD050 | 0/14FA118 | 725 | Btree | INSERT_LEAF | 7045 | 2 | 6992 | off 130
follows:
postgres=# select * from pg_get_wal_stats('0/12FBA30', '0/15011D7') where count > 0;
- resource_manager/record_type | count | count_percentage | record_size | record_size_percentage | fpi_size | fpi_size_percentage | combined_size | combined_size_percentage
+ resource_manager/record_type | count | count_percentage | record_size | record_size_percentage | fpi_size | fpi_size_percentage | combined_size | combined_size_percentage
------------------------------+-------+------------------+-------------+------------------------+----------+---------------------+---------------+--------------------------
XLOG | 10 | 0.10871929 | 796 | 0.052369177 | 352 | 0.061031006 | 1148 | 0.054751817
Transaction | 187 | 2.0330508 | 62773 | 4.1298623 | 0 | 0 | 62773 | 2.9938467
postgres=# select * from pg_get_wal_stats('0/14AFC30', '0/15011D7', true) where count > 0;
- resource_manager/record_type | count | count_percentage | record_size | record_size_percentage | fpi_size | fpi_size_percentage | combined_size | combined_size_percentage
+ resource_manager/record_type | count | count_percentage | record_size | record_size_percentage | fpi_size | fpi_size_percentage | combined_size | combined_size_percentage
------------------------------+-------+------------------+-------------+------------------------+----------+---------------------+---------------+--------------------------
XLOG/CHECKPOINT_SHUTDOWN | 1 | 0.32894737 | 114 | 0.22891566 | 0 | 0 | 114 | 0.03534489
XLOG/CHECKPOINT_ONLINE | 4 | 1.3157895 | 456 | 0.91566265 | 0 | 0 | 456 | 0.14137957
EXPLAIN (ANALYZE, TIMING OFF) SELECT * FROM t WHERE a = 1;
- QUERY PLAN
+ QUERY PLAN
-------------------------------------------------------------------&zwsp;------------
Seq Scan on t (cost=0.00..170.00 rows=100 width=8) (actual rows=100 loops=1)
Filter: (a = 1)
EXPLAIN (ANALYZE, TIMING OFF) SELECT * FROM t WHERE a = 1 AND b = 1;
- QUERY PLAN
+ QUERY PLAN
-------------------------------------------------------------------&zwsp;----------
Seq Scan on t (cost=0.00..195.00 rows=1 width=8) (actual rows=100 loops=1)
Filter: ((a = 1) AND (b = 1))
CREATE STATISTICS stts (dependencies) ON a, b FROM t;
ANALYZE t;
EXPLAIN (ANALYZE, TIMING OFF) SELECT * FROM t WHERE a = 1 AND b = 1;
- QUERY PLAN
+ QUERY PLAN
-------------------------------------------------------------------&zwsp;------------
Seq Scan on t (cost=0.00..195.00 rows=100 width=8) (actual rows=100 loops=1)
Filter: ((a = 1) AND (b = 1))
accurate:
EXPLAIN (ANALYZE, TIMING OFF) SELECT COUNT(*) FROM t GROUP BY a;
- QUERY PLAN
+ QUERY PLAN
-------------------------------------------------------------------&zwsp;----------------------
HashAggregate (cost=195.00..196.00 rows=100 width=12) (actual rows=100 loops=1)
Group Key: a
in the following example, is off by an order of magnitude:
EXPLAIN (ANALYZE, TIMING OFF) SELECT COUNT(*) FROM t GROUP BY a, b;
- QUERY PLAN
+ QUERY PLAN
-------------------------------------------------------------------&zwsp;-------------------------
HashAggregate (cost=220.00..230.00 rows=1000 width=16) (actual rows=100 loops=1)
Group Key: a, b
CREATE STATISTICS stts (dependencies, ndistinct) ON a, b FROM t;
ANALYZE t;
EXPLAIN (ANALYZE, TIMING OFF) SELECT COUNT(*) FROM t GROUP BY a, b;
- QUERY PLAN
+ QUERY PLAN
-------------------------------------------------------------------&zwsp;-------------------------
HashAggregate (cost=220.00..221.00 rows=100 width=16) (actual rows=100 loops=1)
Group Key: a, b
SELECT m.* FROM pg_statistic_ext join pg_statistic_ext_data on (oid = stxoid),
pg_mcv_list_items(stxdmcv) m WHERE stxname = 'stts2';
- index | values | nulls | frequency | base_frequency
+ index | values | nulls | frequency | base_frequency
-------+----------+-------+-----------+----------------
0 | {0, 0} | {f,f} | 0.01 | 0.0001
1 | {1, 1} | {f,f} | 0.01 | 0.0001
SELECT query_hosts('192.168.1.0/30');
SELECT release_hosts_query();
- query_hosts
+ query_hosts
-----------------
(1,192.168.1.1)
(2,192.168.1.2)
DETAIL: strict_multi_assignment check of extra_warnings is active.
HINT: Make sure the query returns the exact list of columns.
- foo
+ foo
-----
-
+
(1 row)
Example usage of the function:
postgres=# SELECT * FROM postgres_fdw_get_connections() ORDER BY 1;
- server_name | valid
+ server_name | valid
-------------+-------
loopback1 | t
loopback2 | f
Example usage of the function:
postgres=# SELECT postgres_fdw_disconnect('loopback1');
- postgres_fdw_disconnect
+ postgres_fdw_disconnect
-------------------------
t
Example usage of the function:
postgres=# SELECT postgres_fdw_disconnect_all();
- postgres_fdw_disconnect_all
+ postgres_fdw_disconnect_all
-----------------------------
t
postgres=# \d tab
Table "public.tab"
- Column | Type | Collation | Nullable | Default
+ Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
- col | integer | | |
+ col | integer | | |
Indexes:
"idx" btree (col) INVALID
Total Cost: 5.98 +
Plan Rows: 1 +
Plan Width: 4 +
- Index Cond: "(i = 4)"
+ Index Cond: "(i = 4)"
(1 row)
EXPLAIN ANALYZE EXECUTE query(100, 200);
- QUERY PLAN
+ QUERY PLAN
-------------------------------------------------------------------&zwsp;-----------------------------------------------------
HashAggregate (cost=9.54..9.54 rows=1 width=8) (actual time=0.156..0.161 rows=11 loops=1)
Group Key: foo
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
first | integer | | not null | 0
- second | text | | |
+ second | text | | |
Now we change the prompt to something more interesting:
with the \crosstabview command:
testdb=> SELECT first, second, first > 2 AS gt2 FROM my_table;
- first | second | gt2
+ first | second | gt2
-------+--------+-----
1 | one | f
2 | two | f
(4 rows)
testdb=> \crosstabview first second
- first | one | two | three | four
+ first | one | two | three | four
-------+-----+-----+-------+------
- 1 | f | | |
- 2 | | f | |
- 3 | | | t |
+ 1 | f | | |
+ 2 | | f | |
+ 3 | | | t |
4 | | | | t
(4 rows)
testdb(> row_number() over(order by t2.first) AS ord
testdb(> FROM my_table t1 CROSS JOIN my_table t2 ORDER BY 1 DESC
testdb(> \crosstabview "A" "B" "AxB" ord
- A | 101 | 102 | 103 | 104
+ A | 101 | 102 | 103 | 104
---+-----+-----+-----+-----
4 | 404 | 408 | 412 | 416
3 | 303 | 306 | 309 | 312
reindexed in a separate transaction. Those commands cannot be used inside
a transaction block when working on a partitioned table or index.
-
+
When using the TABLESPACE clause with
REINDEX on a partitioned index or table, only the
will be generated. Indexes on TOAST tables are rebuilt, but not moved
to the new tablespace.
-
+
Rebuilding Indexes Concurrently
SAVEPOINT foo;
FETCH 1 FROM foo;
- ?column?
+ ?column?
----------
1
ROLLBACK TO SAVEPOINT foo;
FETCH 1 FROM foo;
- ?column?
+ ?column?
----------
2
UNION ALL
SELECT * FROM t
- x
+ x
--------------------
0.534150459803641
0.520092216785997
SELECT SESSION_USER, CURRENT_USER;
- session_user | current_user
+ session_user | current_user
--------------+--------------
peter | peter
SELECT SESSION_USER, CURRENT_USER;
- session_user | current_user
+ session_user | current_user
--------------+--------------
peter | paul
SELECT SESSION_USER, CURRENT_USER;
- session_user | current_user
+ session_user | current_user
--------------+--------------
peter | peter
SELECT SESSION_USER, CURRENT_USER;
- session_user | current_user
+ session_user | current_user
--------------+--------------
paul | paul
Show all settings:
SHOW ALL;
- name | setting | description
+ name | setting | description
-------------------------+---------+-------------------------------------------------
allow_system_table_mods | off | Allows modifications of the structure of ...
.
Release date:
-
+
This is just a placeholder for now.
-
+
SELECT count(*) FROM words WHERE word = 'caterpiler';
- count
+ count
-------
0
(1 row)
SELECT word FROM words ORDER BY word <-> 'caterpiler' LIMIT 10;
- word
+ word
---------------
cater
caterpillar
SELECT * FROM shoelace_log;
- sl_name | sl_avail | log_who | log_when
+ sl_name | sl_avail | log_who | log_when
---------+----------+---------+----------------------------------
sl7 | 6 | Al | Tue Oct 20 16:14:45 1998 MET DST
(1 row)
SELECT * FROM shoelace_log;
- sl_name | sl_avail | log_who| log_when
+ sl_name | sl_avail | log_who| log_when
---------+----------+--------+----------------------------------
sl7 | 6 | Al | Tue Oct 20 19:14:45 1998 MET DST
sl3 | 10 | Al | Tue Oct 20 19:25:16 1998 MET DST
to functions in
PostgreSQL. An example is:
SELECT concat_lower_or_upper('Hello', 'World', true);
- concat_lower_or_upper
+ concat_lower_or_upper
-----------------------
HELLO WORLD
(1 row)
Another example is:
SELECT concat_lower_or_upper('Hello', 'World');
- concat_lower_or_upper
+ concat_lower_or_upper
-----------------------
hello world
(1 row)
For example:
SELECT concat_lower_or_upper(a => 'Hello', b => 'World');
- concat_lower_or_upper
+ concat_lower_or_upper
-----------------------
hello world
(1 row)
order, for example:
SELECT concat_lower_or_upper(a => 'Hello', b => 'World', uppercase => true);
- concat_lower_or_upper
+ concat_lower_or_upper
-----------------------
HELLO WORLD
(1 row)
SELECT concat_lower_or_upper(a => 'Hello', uppercase => true, b => 'World');
- concat_lower_or_upper
+ concat_lower_or_upper
-----------------------
HELLO WORLD
(1 row)
An older syntax based on ":=" is supported for backward compatibility:
SELECT concat_lower_or_upper(a := 'Hello', uppercase := true, b := 'World');
- concat_lower_or_upper
+ concat_lower_or_upper
-----------------------
HELLO WORLD
(1 row)
For example:
SELECT concat_lower_or_upper('Hello', 'World', uppercase => true);
- concat_lower_or_upper
+ concat_lower_or_upper
-----------------------
HELLO WORLD
(1 row)
postgres[33712]=#* SELECT * FROM pg_logical_slot_get_changes('test_slot', NULL, NULL, 'stream-changes', '1');
- lsn | xid | data
+ lsn | xid | data
-----------+-----+--------------------------------------------------
0/16B21F8 | 503 | opening a streamed block for transaction TXN 503
0/16B21F8 | 503 | streaming change for TXN 503
SELECT to_tsvector('fat cats ate fat rats') @@ to_tsquery('fat & rat');
- ?column?
+ ?column?
----------
t
SELECT 'fat cats ate fat rats'::tsvector @@ to_tsquery('fat & rat');
- ?column?
+ ?column?
----------
f
SELECT to_tsvector('fatal error') @@ to_tsquery('fatal <-> error');
- ?column?
+ ?column?
----------
t
SELECT to_tsvector('error is not fatal') @@ to_tsquery('fatal <-> error');
- ?column?
+ ?column?
----------
f
SELECT phraseto_tsquery('cats ate rats');
- phraseto_tsquery
+ phraseto_tsquery
-------------------------------
'cat' <-> 'ate' <-> 'rat'
SELECT phraseto_tsquery('the cats ate the rats');
- phraseto_tsquery
+ phraseto_tsquery
-------------------------------
'cat' <-> 'ate' <2> 'rat'
SELECT to_tsquery('english', 'The & Fat & Rats');
- to_tsquery
+ to_tsquery
---------------
'fat' & 'rat'
SELECT to_tsquery('english', 'Fat | Rats:AB');
- to_tsquery
+ to_tsquery
------------------
'fat' | 'rat':AB
SELECT to_tsquery('supern:*A & star:A*B');
- to_tsquery
+ to_tsquery
--------------------------
'supern':*A & 'star':*AB
SELECT plainto_tsquery('english', 'The Fat Rats');
- plainto_tsquery
+ plainto_tsquery
-----------------
'fat' & 'rat'
SELECT plainto_tsquery('english', 'The Fat & Rats:C');
- plainto_tsquery
+ plainto_tsquery
---------------------
'fat' & 'rat' & 'c'
INSERT INTO aliases VALUES(to_tsquery('supernovae'), to_tsquery('supernovae|sn'));
SELECT ts_rewrite(to_tsquery('supernovae & crab'), 'SELECT * FROM aliases');
- ts_rewrite
+ ts_rewrite
---------------------------------
'crab' & ( 'supernova' | 'sn' )
WHERE t = to_tsquery('supernovae');
SELECT ts_rewrite(to_tsquery('supernovae & crab'), 'SELECT * FROM aliases');
- ts_rewrite
+ ts_rewrite
---------------------------------------------
'crab' & ( 'supernova' | 'sn' & !'nebula' )
INSERT INTO messages VALUES('title here', 'the body text is here');
SELECT * FROM messages;
- title | body | tsv
+ title | body | tsv
------------+-----------------------+----------------------------
title here | the body text is here | 'bodi':4 'text':5 'titl':1
SELECT title, body FROM messages WHERE tsv @@ to_tsquery('title & body');
- title | body
+ title | body
------------+-----------------------
title here | the body text is here
SELECT alias, description, token FROM ts_debug('foo-bar-beta1');
- alias | description | token
+ alias | description | token
-----------------+------------------------------------------+---------------
numhword | Hyphenated word, letters and digits | foo-bar-beta1
hword_asciipart | Hyphenated word part, all ASCII | foo
SELECT alias, description, token FROM ts_debug('http://example.com/stuff/index.html');
- alias | description | token
+ alias | description | token
----------+---------------+------------------------------
protocol | Protocol head | http://
url | URL | example.com/stuff/index.html
SELECT * FROM ts_debug('english', 'Paris');
- alias | description | token | dictionaries | dictionary | lexemes
+ alias | description | token | dictionaries | dictionary | lexemes
-----------+-----------------+-------+----------------+--------------+---------
asciiword | Word, all ASCII | Paris | {english_stem} | english_stem | {pari}
WITH my_synonym, english_stem;
SELECT * FROM ts_debug('english', 'Paris');
- alias | description | token | dictionaries | dictionary | lexemes
+ alias | description | token | dictionaries | dictionary | lexemes
-----------+-----------------+-------+---------------------------+------------+---------
asciiword | Word, all ASCII | Paris | {my_synonym,english_stem} | my_synonym | {paris}
(1 row)
mydb=# SELECT 'indexes are very useful'::tsvector;
- tsvector
+ tsvector
---------------------------------
'are' 'indexes' 'useful' 'very'
(1 row)
SELECT * FROM ts_debug('english', 'a fat cat sat on a mat - it ate a fat rats');
- alias | description | token | dictionaries | dictionary | lexemes
+ alias | description | token | dictionaries | dictionary | lexemes
-----------+-----------------+-------+----------------+--------------+---------
asciiword | Word, all ASCII | a | {english_stem} | english_stem | {}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | fat | {english_stem} | english_stem | {fat}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | cat | {english_stem} | english_stem | {cat}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | sat | {english_stem} | english_stem | {sat}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | on | {english_stem} | english_stem | {}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | a | {english_stem} | english_stem | {}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | mat | {english_stem} | english_stem | {mat}
- blank | Space symbols | | {} | |
- blank | Space symbols | - | {} | |
+ blank | Space symbols | | {} | |
+ blank | Space symbols | - | {} | |
asciiword | Word, all ASCII | it | {english_stem} | english_stem | {}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | ate | {english_stem} | english_stem | {ate}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | a | {english_stem} | english_stem | {}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | fat | {english_stem} | english_stem | {fat}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | rats | {english_stem} | english_stem | {rat}
SELECT * FROM ts_debug('public.english', 'The Brightest supernovaes');
- alias | description | token | dictionaries | dictionary | lexemes
+ alias | description | token | dictionaries | dictionary | lexemes
-----------+-----------------+-------------+-------------------------------+----------------+-------------
asciiword | Word, all ASCII | The | {english_ispell,english_stem} | english_ispell | {}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | Brightest | {english_ispell,english_stem} | english_ispell | {bright}
- blank | Space symbols | | {} | |
+ blank | Space symbols | | {} | |
asciiword | Word, all ASCII | supernovaes | {english_ispell,english_stem} | english_stem | {supernova}
SELECT alias, token, dictionary, lexemes
FROM ts_debug('public.english', 'The Brightest supernovaes');
- alias | token | dictionary | lexemes
+ alias | token | dictionary | lexemes
-----------+-------------+----------------+-------------
asciiword | The | english_ispell | {}
- blank | | |
+ blank | | |
asciiword | Brightest | english_ispell | {bright}
- blank | | |
+ blank | | |
asciiword | supernovaes | english_stem | {supernova}
SELECT * FROM ts_token_type('default');
- tokid | alias | description
+ tokid | alias | description
-------+-----------------+------------------------------------------
1 | asciiword | Word, all ASCII
2 | word | Word, all letters
=> \dF russian
List of text search configurations
- Schema | Name | Description
+ Schema | Name | Description
------------+---------+------------------------------------
pg_catalog | russian | configuration for russian language
=> \dF+ russian
Text search configuration "pg_catalog.russian"
Parser: "pg_catalog.default"
- Token | Dictionaries
+ Token | Dictionaries
-----------------+--------------
asciihword | english_stem
asciiword | english_stem
=> \dFp
List of text search parsers
- Schema | Name | Description
+ Schema | Name | Description
------------+---------+---------------------
pg_catalog | default | default word parser
=> \dFp+
Text search parser "pg_catalog.default"
- Method | Function | Description
+ Method | Function | Description
-----------------+----------------+-------------
- Start parse | prsd_start |
- Get next token | prsd_nexttoken |
- End parse | prsd_end |
- Get headline | prsd_headline |
- Get token types | prsd_lextype |
+ Start parse | prsd_start |
+ Get next token | prsd_nexttoken |
+ End parse | prsd_end |
+ Get headline | prsd_headline |
+ Get token types | prsd_lextype |
Token types for parser "pg_catalog.default"
- Token name | Description
+ Token name | Description
-----------------+------------------------------------------
asciihword | Hyphenated word, all ASCII
asciiword | Word, all ASCII
=> \dFt
List of text search templates
- Schema | Name | Description
+ Schema | Name | Description
------------+-----------+-----------------------------------------------------------
pg_catalog | ispell | ispell dictionary
pg_catalog | simple | simple dictionary: just lower case and check for stopword
WHERE attnum > 0 AND attrelid = 'pg_tablespace'::regclass
GROUP BY attrelid;
- attrelid | array_accum
+ attrelid | array_accum
---------------+---------------------------------------
pg_tablespace | {spcname,spcowner,spcacl,spcoptions}
(1 row)
WHERE attnum > 0 AND attrelid = 'pg_tablespace'::regclass
GROUP BY attrelid;
- attrelid | array_accum
+ attrelid | array_accum
---------------+---------------------------
pg_tablespace | {name,oid,aclitem[],text[]}
(1 row)
$$ LANGUAGE SQL;
SELECT mleast(10, -1, 5, 4.4);
- mleast
+ mleast
--------
-1
(1 row)
$$;
SELECT foo(10, 20, 30);
- foo
+ foo
-----
60
(1 row)
SELECT foo(10, 20);
- foo
+ foo
-----
33
(1 row)
SELECT foo(10);
- foo
+ foo
-----
15
(1 row)
also works:
SELECT make_array2('a', 'b') AS textarray;
- textarray
+ textarray
-----------
{a,b}
(1 row)
$$ LANGUAGE SQL;
SELECT anyleast(10, -1, 5, 4);
- anyleast
+ anyleast
----------
-1
(1 row)
SELECT anyleast('abc'::text, 'def');
- anyleast
+ anyleast
----------
abc
(1 row)
$$ LANGUAGE SQL;
SELECT concat_values('|', 1, 4, 2);
- concat_values
+ concat_values
---------------
1|4|2
(1 row)