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BDOOVFKHPD4XHU\LQJ2WKHU7DEXODU)RUPDWVZLWKRJUBIGZ
2))6(7FODXVH/,0,7DQG2))6(7
2'%& 2SHQ'DWDEDVH&RQQHFWLYLW\ 'DWDEDVH'ULYHUV
2
QXPUDQJHGDWDW\SH%XLOWLQ5DQJH7\SHV
service postgresql-9.5 reload
,I\RXKDYH3RVWJUH64/LQVWDOOHGDVDVHUYLFHLQ5HG+DW(QWHUSULVH/LQX[
&HQW26RU8EXQWXHQWHULQVWHDG
pg_ctl reload -D your_data_directory_here
$UHORDGFDQEHGRQHLQVHYHUDOZD\V2QHZD\LVWRRSHQDFRQVROHZLQGRZDQG
UXQWKLVFRPPDQG
5HORDGLQJ
6RPHFRQILJXUDWLRQFKDQJHVUHTXLUHD3RVWJUH64/VHUYLFHUHVWDUWZKLFKFORVHV
DQ\DFWLYHFRQQHFWLRQVIURPFOLHQWV2WKHUFKDQJHVUHTXLUHMXVWDUHORDG1HZ
XVHUVFRQQHFWLQJDIWHUDUHORDGZLOOUHFHLYHWKHQHZVHWWLQJ([WDQWXVHUVZLWK
DFWLYHFRQQHFWLRQVZLOOQRWEHDIIHFWHGGXULQJDUHORDG,I\RXߞUHQRWVXUH
ZKHWKHUDFRQILJXUDWLRQFKDQJHUHTXLUHVDUHORDGRUUHVWDUWORRNXQGHUWKH
FRQWH[WVHWWLQJDVVRFLDWHGZLWKDFRQILJXUDWLRQ,IWKHFRQWH[WLVpostmaster
\RXߞOOQHHGDUHVWDUW,IWKHFRQWH[WLVuserDUHORDGZLOOVXIILFH
0DNLQJ&RQILJXUDWLRQV7DNH(IIHFW
SELECT name, setting FROM pg_settings WHERE category = 'File Locations';
name
|
setting
-------------------+-----------------------------------------config_file
| /etc/postgresql/9.6/main/postgresql.conf
data_directory
| /var/lib/postgresql/9.6/main
external_pid_file | /var/run/postgresql/9.6-main.pid
hba_file
| /etc/postgresql/9.6/main/pg_hba.conf
ident_file
| /etc/postgresql/9.6/main/pg_ident.conf
(5 rows)
([DPS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LQJ/RJLQ5ROHV
/2*,13$66:25'FODXVH&UHDWLQJ/RJLQ5ROHV
ORJLFDOUHSOLFDWLRQ5HSOLFDWLRQ-DUJRQ5HSOLFDWLQJ2QO\6RPH7DEOHVRU
'DWDEDVHVZLWK/RJLFDO5HSOLFDWLRQ5HSOLFDWLQJ2QO\6RPH7DEOHVRU'DWDEDVHV
ZLWK/RJLFDO5HSOLFDWLRQ
6HWWLQJVZLWKXVHURUVXSHUXVHUFRQWH[WFDQEHVHWIRUDVSHFLILFGDWDEDVH
XVHUVHVVLRQDQGIXQFWLRQOHYHO)RUH[DPSOH\RXPLJKWZDQWWRVHW
ZRUNBPHPKLJKHUIRUDQ64/JXUXOHYHOXVHUZKRZULWHVPLQGERJJOLQJ
TXHULHV6LPLODUO\LI\RXKDYHRQHIXQFWLRQWKDWLVVRUWLQWHQVLYH\RXFRXOG
3RVWPDVWHUVHWWLQJVDIIHFWWKHHQWLUHVHUYHU SRVWPDVWHUUHSUHVHQWVWKH
3RVWJUH64/VHUYLFH DQGWDNHHIIHFWRQO\DIWHUDUHVWDUW
6XSHUXVHUVHWWLQJVFDQEHFKDQJHGRQO\E\DVXSHUXVHUDQGZLOODSSO\WRDOO
XVHUVZKRFRQQHFWDIWHUDUHORDG8VHUVFDQQRWLQGLYLGXDOO\RYHUULGHWKH
VHWWLQJ
8VHUVHWWLQJVFDQEHFKDQJHGE\HDFKXVHUWRDIIHFWMXVWWKDWXVHUߞVVHVVLRQV
,IVHWE\WKHVXSHUXVHUWKHVHWWLQJEHFRPHVDGHIDXOWIRUDOOXVHUVZKR
FRQQHFWDIWHUDUHORDG
7KHFRQWH[WLVWKHVFRSHRIWKHVHWWLQJ6RPHVHWWLQJVKDYHDZLGHUHIIHFW
WKDQRWKHUVGHSHQGLQJRQWKHLUFRQWH[W
SELECT
name,
context ,
unit ,
setting, boot_val, reset_val
FROM pg_settings
WHERE name IN ('listen_addresses','deadlock_timeout','shared_buffers',
'effective_cache_size','work_mem','maintenance_work_mem')
ORDER BY context, name;
name
| context
| unit | setting | boot_val | reset_val
---------------------+------------+------+-------- +-----------+---------listen_addresses
| postmaster |
| *
| localhost | *
shared_buffers
| postmaster | 8kB | 131584 | 1024
| 131584
deadlock_timeout
| superuser | ms | 1000
| 1000
| 1000
effective_cache_size | user
| 8kB | 16384 | 16384
| 16384
maintenance_work_mem | user
| kB | 16384 | 16384
| 16384
work_mem
| user
| kB | 5120
| 1024
| 5120
([DPSOH.H\VHWWLQJV
$QHDV\ZD\WRUHDGWKHFXUUHQWVHWWLQJVZLWKRXWRSHQLQJWKHFRQILJXUDWLRQILOHV
LVWRTXHU\WKHYLHZQDPHGSJBVHWWLQJV:HGHPRQVWUDWHLQ([DPSOH
&KHFNLQJSRVWJUHVTOFRQIVHWWLQJV
SRVWJUHVTOFRQIDQGSODFHDQ\FXVWRPVHWWLQJVLQSRVWJUHVTODXWRFRQI
ORFDOYDULDEOHV:ULWLQJ3/SJ64/)XQFWLRQV
SELECT name, sourcefile, sourceline, setting, applied
FROM pg_file_settings
WHERE name IN ('listen_addresses','deadlock_timeout','shared_buffers',
'effective_cache_size','work_mem','maintenance_work_mem')
([DPSOH4XHU\LQJSJBILOHBVHWWLQJV
ORJLFDOGHFRGLQJ5HSOLFDWLRQ-DUJRQ
ORFNBWLPHRXWVHWWLQJ0DQDJLQJ&RQQHFWLRQV
OLVWVRIREMHFWV5HWULHYLQJ'HWDLOVRI'DWDEDVH2EMHFWV
OLVWHQBDGGUHVVHVQHWZRUNVHWWLQJ&KHFNLQJSRVWJUHVTOFRQIVHWWLQJV
/LYH6FULSWODQJXDJH:ULWLQJ3/93/&RIIHH6FULSWDQG3//LYH6FULSW
)XQFWLRQV
UHWULHYLQJFRPPDQGKLVWRU\5HWULHYLQJ3ULRU&RPPDQGV
UHVWRUHBFRPPDQGGLUHFWLYHDQG&RQILJXULQJWKH6ODYHVIRU)XOO6HUYHU
&OXVWHU5HSOLFDWLRQ
SVTOWRRODQGSVTO&XVWRPL]DWLRQV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RQWKHORFDOV\VWHP RUORFDOKRVWPHDQLQJWKH,3Y
RU,3YORFDOKRVW,3DGGUHVV%XWPDQ\SHRSOHFKDQJHWKHVHWWLQJWR PHDQLQJDOODYDLODEOH,3DGGUHVVHV
OLVWHQBDGGUHVVHV
MVRQBDJJIXQFWLRQ2XWSXWWLQJ-621&RPSRVLWH7\SHVLQ4XHULHV
MVRQEBLQVHUWIXQFWLRQ-621
MVRQEBVHWIXQFWLRQ(GLWLQJ-621%GDWD
ORDER BY name;
name
| sourcefile
| sourceline | setting | applied
---------------------+-------------------------------+------------+---------+-------effective_cache_size | E:/data96/postgresql.auto.conf|
11 | 8GB
| t
listen_addresses
| E:/data96/postgresql.conf
|
59 | *
| t
maintenance_work_mem | E:/data96/postgresql.auto.conf|
3 | 16MB
| t
shared_buffers
| E:/data96/postgresql.conf
|
115 | 128MB | f
shared_buffers
| E:/data96/postgresql.auto.conf|
5 | 131584 | t
3D\VSHFLDODWWHQWLRQWRWKHIROORZLQJQHWZRUNVHWWLQJVLQSRVWJUHVTOFRQIRU
SRVWJUHVTODXWRFRQIEHFDXVHDQLQFRUUHFWHQWU\KHUHZLOOSUHYHQWFOLHQWVIURP
FRQQHFWLQJ&KDQJLQJWKHLUYDOXHVUHTXLUHVDVHUYLFHUHVWDUW
MVRQEBH[WUDFWBSDWKBWH[WIXQFWLRQ%LQDU\-621MVRQE
MVRQEBHDFKIXQFWLRQ%LQDU\-621MVRQE
MVRQEBDUUD\BHOHPHQWVIXQFWLRQ%LQDU\-621MVRQE
3/9DQG:ULWLQJ)XQFWLRQV
7KLVLVDQHZVHWWLQJLQWURGXFHGLQIRUSDUDOOHOLVP7KHVHWWLQJGHWHUPLQHV
WKHPD[LPXPSDUDOOHOZRUNHUWKUHDGVWKDWFDQEHVSDZQHGIRUHDFKJDWKHU
RSHUDWLRQ7KHGHIDXOWVHWWLQJLVZKLFKPHDQVSDUDOOHOLVPLVFRPSOHWHO\
WXUQHGRII,I\RXKDYHPRUHWKDQRQ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'filename'
,I\RXKDYHWRWUDFNPDQ\VHWWLQJVFRQVLGHURUJDQL]LQJWKHPLQWRPXOWLSOH
FRQILJXUDWLRQILOHVDQGWKHQOLQNLQJWKHPEDFNXVLQJWKHLQFOXGHRU
LQFOXGHBLIBH[LVWVGLUHFWLYHZLWKLQWKHSRVWJUHVTOFRQI7KHH[DFWV\QWD[LVDV
IROORZV
SELECT pg_reload_conf();
'HSHQGLQJRQWKHSDUWLFXODUVHWWLQJFKDQJHG\RXPD\QHHGWRUHVWDUWWKHVHUYLFH
,I\RXMXVWQHHGWRUHORDGLWKHUHߞVDFRQYHQLHQWFRPPDQG
7KLVFRPPDQGLVZLVHHQRXJKWRQRWGLUHFWO\HGLWSRVWJUHVFRQIEXWZLOOPDNH
WKHFKDQJHLQSRVWJUHVDXWRFRQI
ALTER SYSTEM SET work_mem = '500MB';
3RVWJUH64/LQWURGXFHGWKHDELOLW\WRFKDQJHVHWWLQJVXVLQJWKH$/7(5
6<67(064/FRPPDQG)RUH[DPSOHWRVHWWKHZRUNBPHPJOREDOO\HQWHUWKH
IROORZLQJ
&KDQJLQJWKHSRVWJUHVTOFRQIVHWWLQJV
,QYHUVLRQWKHUHLVDQDGGLWLRQDOVHWWLQJFDOOHGmax_parallel_workers
ZKLFKFRQWUROVWKHVXEVHWRIPD[BZRUNHUBSURFHVVHVDOORFDWHGIRU
SDUDOOHOL]DWLRQ
H[SHULPHQWZLWKWKLVVHWWLQJWRILQGZKDWZRUNVEHVWIRU\RXUVHUYHU$OVR
QRWHWKDWWKHQXPEHU\RXKDYHKHUHVKRXOGEHOHVVWKDQ
max_worker_processesZKLFKGHIDXOWVWREHFDXVHWKHSDUDOOHO
EDFNJURXQGZRUNHUSURFHVVHVDUHDVXEVHWRIWKHPD[LPXPDOORZHG
SURFHVVHV
DATABASE
all
all
all
all
USER
all
all
all
all
ADDRESS
127.0.0.1/32
::1/128
192.168.54.0/24
0.0.0.0/0
METHOD
ident
trust
md5
md5
66/FRQILJXUDWLRQVHWWLQJVFDQEHIRXQGLQSRVWJUHVFRQIRU
SRVWJUHVDXWRFRQIsslssl_cert_filessl_key_file2QFHWKHVHUYHU
FRQILUPVWKDWWKHFOLHQWLVDEOHWRVXSSRUW66/LWZLOOKRQRUWKHFRQQHFWLRQ
$XWKHQWLFDWLRQPHWKRG7KHXVXDOFKRLFHVDUHLGHQWWUXVWPGSHHUDQG
SDVVZRUG
,3YV\QWD[IRUGHILQLQJQHWZRUNUDQJH7KLVDSSOLHVRQO\WRVHUYHUVZLWK
,3YVXSSRUWDQGPD\SUHYHQWSJBKEDFRQIIURPORDGLQJLI\RXDGGWKLV
VHFWLRQZLWKRXWDFWXDOO\KDYLQJ,3YQHWZRUNLQJHQDEOHGRQWKHVHUYHU
,3YV\QWD[IRUGHILQLQJQHWZRUNUDQJH7KHILUVWSDUWLVWKHQHWZRUNDGGUHVV
IROORZHGE\WKHELWPDVNIRULQVWDQFH3RVWJUH64/ZLOO
DFFHSWFRQQHFWLRQUHTXHVWVIURPDQ\,3DGGUHVVZLWKLQWKHUDQJH
66/FRQQHFWLRQUXOH,QRXUH[DPSOHZHDOORZDQ\RQHWRFRQQHFWWRRXU
VHUYHURXWVLGHRIWKHDOORZHG,3UDQJHDVORQJDVWKH\FDQFRQQHFWXVLQJ66/
# TYPE DATABASE
USER
ADDRESS
METHOD
# Allow replication connections from localhost,
# by a user with replication privilege.
#host replication postgres 127.0.0.1/32 trust
#host replication postgres ::1/128
trust
# TYPE
host
host
host
hostssl
([DPSOH6DPSOHSJBKEDFRQI
,'(17,7<TXDOLILHU)HDWXUHV,QWURGXFHGLQ3RVWJUH64/%DVLF7DEOH
&UHDWLRQ%DVLF7DEOH&UHDWLRQ
7KHSJBKEDFRQIILOHFRQWUROVZKLFK,3DGGUHVVHVDQGXVHUVFDQFRQQHFWWRWKH
GDWDEDVH)XUWKHUPRUHLWGLFWDWHVWKHDXWKHQWLFDWLRQSURWRFROWKDWWKHFOLHQWPXVW
IROORZ&KDQJHVWRWK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H'%1RWDEOH3RVWJUH64/)RUNV:LQGRZVDQG'HVNWRS/LQX[PDF26
HQGRIOLIH (2/ VXSSRUW:K\8SJUDGH"
HQDEOHBVHTVFDQVHWWLQJ6WUDWHJ\6HWWLQJV
HQDEOHBQHVWORRSVHWWLQJ6WUDWHJ\6HWWLQJV
HIIHFWLYHBFDFKHBVL]HQHWZRUNVHWWLQJ&KHFNLQJSRVWJUHVTOFRQIVHWWLQJV
(
G\QDPLFBVKDUHGBPHPRU\BW\SHQHWZRUNVHWWLQJ3DUDOOHOL]HG4XHULHV
:KHQ\RXILUVWORJLQWKHYDOXHVRIWKHVHWZRYDULDEOHVDUHWKHVDPHSET ROLE
FKDQJHVWKHcurrent_userZKLOHSET SESSION AUTHORIZATIONFKDQJHVERWK
WKHcurrent_userDQGsession_userYDULDEOHV
SELECT session_user, current_user;
SET ROLELVDFRPPDQGDYDLODEOHWRDOOXVHUVEXWDPRUHSRZHUIXOFRPPDQG
FDOOHGSET SESSION AUTHORIZATIONLVDYDLODEOHWRSHRSOHZKRORJLQDV
VXSHUXVHUV,QRUGHUWRXQGHUVWDQGWKHGLIIHUHQFHVZHߞOOILUVWLQWURGXFHWZR
JOREDOYDULDEOHVWKDW3RVWJUH64/KDVFDOOHGcurrent_userDQGsession_user
<RXFDQVHHWKHVHYDOXHVZKHQ\RXORJLQE\UXQQLQJWKH64/VWDWHPHQW
7KLVIHDWXUHWKRXJKSHFXOLDULVXVHIXOLI\RXZDQWWRSUHYHQW\RXUVHOIIURP
XQLQWHQWLRQDOO\GRLQJVXSHUXVHUWKLQJVZKLOH\RXDUHORJJHGLQ
+LVVXSHUXVHUULJKWVZLOOODVWRQO\IRUKLVFXUUHQWVHVVLRQ
SET ROLE royalty;
$OWKRXJKleoLVDPHPEHURIWKHUR\DOW\JURXSDQGKHLQKHULWVPRVWULJKWVRI
UR\DOW\ZKHQKHORJVLQKHVWLOOZLOOQRWKDYHVXSHUXVHUULJKWV+HFDQJDLQ
VXSHUXVHUULJKWVE\GRLQJ
G\QDPLF64/H[HFXWLRQ'\QDPLF64/([HFXWLRQ'\QDPLF64/([HFXWLRQ
G\QDPLFEDFNJURXQGZRUNHUV)HDWXUHV,QWURGXFHGLQ3RVWJUH64/
'XQVWDQ$QGUHZ%DVLF)XQFWLRQV
'7' 'RFXPHQW7\SH'HILQLWLRQ ,QVHUWLQJ;0/'DWD
'67 GD\OLJKWVDYLQJWLPH 7HPSRUDOV
'5237$%/(FRPPDQG3DUWLWLRQHG7DEOHV
'52368%6&5,37,21FRPPDQG5HSOLFDWLQJ2QO\6RPH7DEOHVRU'DWDEDVHV
ZLWK/RJLFDO5HSOLFDWLRQ
'52367$7,67,&6FRPPDQG7DEOH6WDWLVWLFV
'52338%/,&$7,21FRPPDQG5HSOLFDWLQJ2QO\6RPH7DEOHVRU'DWDEDVHV
ZLWK/RJLFDO5HSOLFDWLRQ
'5230$7(5,$/,=('9,(:FRPPDQG0DWHULDOL]HG9LHZV
'523)25(,*17$%/(FRPPDQG4XHU\LQJ)ODW)LOHV
GROODUTXRWLQJ 'ROODU4XRWLQJ'2
'RFXPHQW7\SH'HILQLWLRQ '7' ,QVHUWLQJ;0/'DWD
'2FRPPDQG'2
'MDQJRZHEIUDPHZRUN'DWDEDVH'ULYHUV
',67,1&721FODXVH',67,1&721
GLVWDQFHRSHUDWRU!)HDWXUHV,QWURGXFHGLQ3RVWJUH64/
GLVFUHWHUDQJHW\SHV'LVFUHWH9HUVXV&RQWLQXRXV5DQJHV
?G)FRPPDQG)76&RQILJXUDWLRQV
GHSHQGHQFLHVVWDWLVWLF7DEOH6WDWLVWLFV
SELECT session_user, current_user;
session_user | current_user
--------------+-------------leo
| leo
(1 row)
SET SESSION AUTHORIZATION regina;
ERROR: permission denied to set session authorization
SET ROLE regina;
ERROR: permission denied to set role "regina"
ALTER ROLE leo SUPERUSER;
ERROR: must be superuser to alter superusers
SET ROLE royalty;
SELECT session_user, current_user;
session_user | current_user
--------------+-------------leo
| royalty
(1 row)
SET ROLE regina;
ERROR: permission denied to set role "regina"
ALTER ROLE leo SUPERUSER;
SET ROLE regina;
SELECT session_user, current_user;
session_user | current_user
--------------+-------------leo
| regina
(1 row)
SET SESSION AUTHORIZATION regina;
([DPSOH6(752/(DQG6(7$87+25,=$7,21
:HߞOOGRDVHWRIH[HUFLVHVWKDWLOOXVWUDWHWKHGLIIHUHQFHVEHWZHHQSET ROLEDQG
SET SESSION AUTHORIZATIONE\ILUVWORJJLQJLQDVOHRDQGWKHQUXQQLQJWKH
FRGHLQ([DPSOH
$VHVVLRQBXVHUWKDWKDVVXSHUXVHUULJKWVFDQ6(752/(WRDQ\RWKHUUROH
6(76(66,21$87+25,=$7,21FKDQJHVWKHYDOXHVRIWKHFXUUHQWBXVHU
DQGVHVVLRQBXVHUYDULDEOHVWRWKRVHRIWKHXVHUEHLQJLPSHUVRQDWHG
7KH6(76(66,21$87+25,=$7,21SULYLOHJHLVLQHIIHFWIRUWKHOLIHRI
WKHVHVVLRQPHDQLQJWKDWHYHQLI\RX6(76(66,21$87+25,=$7,21WRD
XVHUWKDWLVQRWDVXSHUXVHU\RXVWLOOKDYHWKH6(76(66,21
$87+25,=$7,21SULYLOHJHIRUWKHOLIHRI\RXUVHVVLRQ
2QO\DXVHUWKDWORJVLQDVDVXSHUXVHUKDVSHUPLVVLRQWRGR6(76(66,21
$87+25,=$7,21WRDQRWKHUUROH
$WHPSODWHGDWDEDVHLVDVWKHQDPHVXJJHVWVDGDWDEDVHWKDWVHUYHVDVDVNHOHWRQ
IRUQHZGDWDEDVHV:KHQ\RXFUHDWHDQHZGDWDEDVH3RVWJUH64/FRSLHVDOOWKH
GDWDEDVHVHWWLQJVDQGGDWDIURPWKHWHPSODWHGDWDEDVHWRWKHQHZGDWDEDVH
7HPSODWH'DWDEDVHV
7KLVFUHDWHVDFRS\RIWKHWHPSODWHGDWDEDVH$Q\UROHZLWK&5($7('%
SULYLOHJHFDQFUHDWHQHZGDWDEDVHV
CREATE DATABASE mydb;
7KHPLQLPXP64/FRPPDQGWRFUHDWHDGDWDEDVHLV
'DWDEDVH&UHDWLRQ
,Q([DPSOHOHRZDVXQDEOHWRXVH6(76(66,21$87+25,=$7,21
EHFDXVHKHߞVQRWDVXSHUXVHU+HZDVDOVRXQDEOHWR6(752/(WRUHJLQD
EHFDXVHKHLVQRWLQWKHUHJLQDJURXS+RZHYHUKHZDVDEOHWRSET ROLE
royaltyVLQFHKHLVDPHPEHURIWKHUR\DOW\JURXS KHߞVDNLQJFRQVRUW (YHQ
WKRXJKUR\DOW\KDVVXSHUXVHUULJKWVKHVWLOOZDVQߞWDEOHWRLPSHUVRQDWHWKH
TXHHQUHJLQDEHFDXVHKLV6(752/(DELOLWLHVDUHVWLOOEDVHGRQEHLQJWKH
SRZHUOHVVOHR6LQFHUR\DOW\LVDJURXSWKDWKDVVXSHUXVHUULJKWVKHZDVDEOHWR
SURPRWHKLVRZQDFFRXQWOHRWREHDVXSHUXVHU2QFHOHRLVSURPRWHGWRSRZHU
KHFDQWKHQLPSHUVRQDWHUHJLQD+HLVQRZDEOHWRFRPSOHWHO\WDNHRYHUKHU
VHVVLRQBXVHUDQGFXUUHQWBXVHUSHUVRQDZLWK6(76(66,21$87+25,=$7,21
ERROR: permission denied to set session authorization
-- After ending session and logging back in as leo
SELECT session_user, current_user;
SET SESSION AUTHORIZATION regina;
SELECT session_user, current_user;
session_user | current_user
--------------+-------------leo | leo
(1 row)
SET SESSION AUTHORIZATION
session_user | current_user
--------------+-------------regina | regina
(1 row)
GHOLPLWHUVSVTO([SRUW([SRUWLQJTXHULHVDVDVWUXFWXUHGILOHRUUHSRUWLQ
SJ$GPLQ
'(/(7(86,1*FRPPDQG'(/(7(86,1*
'(/(7(FRPPDQG5HVWULFWLQJ'(/(7(83'$7(DQG6(/(&7IURP
,QKHULWHG7DEOHV
GHIDXOWSULYLOHJHV'HIDXOW3ULYLOHJHV'HIDXOW3ULYLOHJHV
'(&/$5(FRPPDQG:ULWLQJ3/SJ64/)XQFWLRQV
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LOHJHVLQRWKHUGDWDEDVHSURGXFWVPLJKWEHFDOOHGULJKWVRUSHUPLVVLRQV
127(
6RPHSULYLOHJHVPDNHVHQVHZLWKRXWDFRQWH[W&5($7('%DQG&5($7(
52/(DUHWZRSULYLOHJHVZKHUHFRQWH[WLVLUUHOHYDQW
0RVWSULYLOHJHVPXVWKDYHDFRQWH[W)RUH[DPSOHDUROHKDYLQJDQ$/7(5
SULYLOHJHLVPHDQLQJOHVVXQOHVVTXDOLILHGZLWKDGDWDEDVHREMHFWVXFKDV$/7(5
SULYLOHJHRQWDEOHV6(/(&7SULYLOHJHRQWDEOH(;(&87(SULYLOHJHRQ
IXQFWLRQDQGVRRQ1RWDOOSULYLOHJHVDSSO\WRDOOREMHFWVDQ(;(&87(
SULYLOHJHIRUDWDEOHLVQRQVHQVH
6RPHSULYLOHJHVDUHE\GHIDXOWJUDQWHGWR38%/,&7KHVHDUH&211(&7DQG
7KH*5$17FRPPDQGLVFRYHUHGLQGHWDLOLQ*5$17:HVWURQJO\UHFRPPHQG
WKDW\RXWDNHWKHWLPHWRVWXG\WKLVGRFXPHQWEHIRUH\RXLQDGYHUWHQWO\NQRFND
ELJKROHLQ\RXUVHFXULW\ZDOO
GRANT USAGE ON SCHEMA my_schema TO PUBLIC;
7RJUDQWSULYLOHJHVWRDOOUROHV\RXFDQXVHWKH38%/,&DOLDVDVLQ
1RWHWKDWALL TABLESLQFOXGHVUHJXODUWDEOHVIRUHLJQWDEOHVDQGYLHZV
PUBLIC;
ALL TABLES IN SCHEMA my_schema TO
GRANT SELECT, REFERENCES, TRIGGER ON
7RJUDQWVSHFLILFSULYLOHJHVRQ$//REMHFWVRIDVSHFLILFW\SHXVH$//
LQVWHDGRIWKHVSHFLILFREMHFWQDPHDVLQ
GRANT ALL ON ALL TABLES IN SCHEMA public TO mydb_admin WITH GRANT OPTION;
:KHQJUDQWLQJSULYLOHJHV\RXFDQDGG:,7+*5$17237,217KLVPHDQV
WKDWWKHJUDQWHHFDQJUDQ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WXDOV
FHUWDXWKHQWLFDWLRQPHWKRG$XWKHQWLFDWLRQPHWKRGV
$OORZVDOOXVHUVWKDWFDQFRQQHFWWRWKHGDWDEDVHWRDOVREHDEOHWRXVHDQG
FUHDWHREMHFWVLQDVFKHPDLIWKH\KDYHULJKWVWRWKRVHREMHFWVLQWKHVFKHPD
*5$1786$*(RQDVFKHPDLVWKHILUVWVWHSWRJUDQWLQJDFFHVVWRREMHFWVLQ
ALTER DEFAULT PRIVILEGES IN SCHEMA my_schema
GRANT USAGE ON TYPES TO PUBLIC;
ALTER DEFAULT PRIVILEGES IN SCHEMA my_schema
GRANT ALL ON FUNCTIONS TO mydb_admin WITH GRANT OPTION;
ALTER DEFAULT PRIVILEGES IN SCHEMA my_schema
GRANT SELECT, UPDATE ON SEQUENCES TO public;
ALTER DEFAULT PRIVILEGES IN SCHEMA my_schema
GRANT ALL ON TABLES TO mydb_admin WITH GRANT OPTION;
ALTER DEFAULT PRIVILEGES IN SCHEMA my_schema
GRANT SELECT, REFERENCES ON TABLES TO PUBLIC;
GRANT USAGE ON SCHEMA my_schema TO PUBLIC;
([DPSOH'HILQLQJGHIDXOWSULYLOHJHVRQDVFKHPD
/HWߞVVXSSRVHZHZDQWDOOXVHUVRIRXUGDWDEDVHWRKDYH(;(&87(DQG
6(/(&7SULYLOHJHVDFFHVVWRDQ\IXWXUHWDEOHVDQGIXQFWLRQVLQDSDUWLFXODU
VFKHPD:HFDQGHILQHSULYLOHJHVDVVKRZQLQ([DPSOH$OOUROHVRID
3RVWJUH64/VHUYHUDUHPHPEHUVRIWKHJURXS38%/,&
$GGLQJRUFKDQJLQJGHIDXOWSULYLOHJHVZRQߞWDIIHFWSULYLOHJHVHWWLQJVRQH[LVWLQJREMHFWV
:$51,1*
'HIDXOWSULYLOHJHVHDVHSULYLOHJHPDQDJHPHQWE\OHWWLQJ\RXVHWSULYLOHJHVEHIRUH
WKHLUFUHDWLRQ
'HIDXOW3ULYLOHJHV
REVOKE EXECUTE ON ALL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RUHSJ6FULSW'DWDEDVH%DFNXS8VLQJSJBGXPS
SJBEDVHEDFNXSWRRO%DFNXSDQG5HVWRUH
SJ$GPLQWRRO%DFNXSDQG5HVWRUH6HOHFWLYHEDFNXSRIGDWDEDVHDVVHWV
DERXW%DFNXSDQG5HVWRUH
7RJHWPRUHGHWDLOVDERXWDSDUWLFXODUH[WHQVLRQDOUHDG\LQVWDOOHGLQ\RXU
GDWDEDVHHQWHUWKHIROORZLQJFRPPDQGIURPSVTO
,I\RXZDQWWRVHHDOOWKHH[WHQVLRQVLQVWDOOHGRQWKHVHUYHUUHJDUGOHVVRILIWKH\
DUHLQVWDOOHGLQ\RXUFXUUHQWGDWDEDVHOHDYHRXWWKHWHERE installed_version
IS NOT NULL
SELECT name, default_version, installed_version, left(comment,30) As comment
FROM pg_available_extensions
WHERE installed_version IS NOT NULL
ORDER BY name;
name
| default_version | installed_version |
comment
---------------+-----------------+-------------------+-------------------------------btree_gist
| 1.5
| 1.5
| support for indexing common da
fuzzystrmatch | 1.1
| 1.1
| determine similarities and dis
hstore
| 1.4
| 1.4
| data type for storing sets of
ogr_fdw
| 1.0
| 1.0
| foreign-data wrapper for GIS d
pgrouting
| 2.4.1
| 2.4.1
| pgRouting Extension
plpgsql
| 1.0
| 1.0
| PL/pgSQL procedural language
plv8
| 1.4.10
| 1.4.10
| PL/JavaScript (v8) trusted pro
postgis
| 2.4.0dev
| 2.4.0dev
| PostGIS geometry, geography, a
(8 rows)
([DPSOH([WHQVLRQVLQVWDOOHGLQDGDWDEDVH
7RVHHZKLFKH[WHQVLRQV\RXKDYHDOUHDG\LQVWDOOHGLQDGDWDEDVHFRQQHFWWRWKH
GDWDEDVHDQGUXQWKHTXHU\LQ([DPSOH<RXUOLVWFRXOGYDU\VLJQLILFDQWO\
IURPRXUV
2FFDVLRQDOO\SUXQHH[WHQVLRQVWKDW\RXQRORQJHUQHHGWRDYRLGEORDW/HDYLQJ
ROGH[WHQVLRQV\RXGRQߞWQHHGPD\FDXV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------------function dmetaphone_alt(text)
function dmetaphone(text)
function difference(text,text)
function text_soundex(text)
function soundex(text)
function metaphone(text,integer)
function levenshtein_less_equal(text,text,integer,integer,integer,integer)
function levenshtein_less_equal(text,text,integer)
function levenshtein(text,text,integer,integer,integer)
function levenshtein(text,text)
7KLVVKRZVZKDWߞVSDFNDJHGLQWKHH[WHQVLRQ
SELECT pg_describe_object(D.classid,D.objid,0) AS description
FROM pg_catalog.pg_depend AS D INNER JOIN pg_catalog.pg_extension AS E
ON D.refobjid = E.oid
WHERE
D.refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass AND
deptype = 'e' AND
E.extname = 'fuzzystrmatch';
$OWHUQDWLYHO\H[HFXWHWKHIROORZLQJTXHU\
\dx+ fuzzystrmatch
EDFNXSDQGUHVWRUH
EDFNVODVK ? 5HJXODU([SUHVVLRQVDQG3DWWHUQ0DWFKLQJ
EDFNJURXQGZRUNHUVG\QDPLF)HDWXUHV,QWURGXFHGLQ3RVWJUH64/
EDFNUHIHUHQFLQJ5HJXODU([SUHVVLRQVDQG3DWWHUQ0DWFKLQJ
%7UHH*L67LQGH[HV3RVWJUH64/6WRFN,QGH[HV
%7UHH*,1LQGH[HV3RVWJUH64/6WRFN,QGH[HV
%7UHHLQGH[HV3RVWJUH64/6WRFN,QGH[HV2SHUDWRU&ODVVHV
%
DXWRFRPPLWFRPPDQGV$XWRFRPPLW&RPPDQGV
DXWKHQWLFDWLRQPHWKRGV7KHSJBKEDFRQI)LOH$XWKHQWLFDWLRQPHWKRGV
DWVLJQVHOHFWLQJDWWULEXWHVRIHOHPHQWV4XHU\LQJ;0/'DWD
DV\QFKURQRXVUHSOLFDWLRQ5HSOLFDWLRQ-DUJRQ
DUUD\BXSSHUIXQFWLRQ5HIHUHQFLQJ(OHPHQWVLQDQ$UUD\
DUUD\BWRBMVRQIXQFWLRQ2XWSXWWLQJ-621&RPSRVLWH7\SHVLQ4XHULHV
DUUD\BDJJIXQFWLRQ)HDWXUHV,QWURGXFHG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QJPDWFKLQJ:HGLVFXVVLWVXVHLQ:KHUH
LV6RXQGH[DQG2WKHU)X]]\7KLQJV
IX]]\VWUPDWFK
LQFOXGHGZLWK3RVW*,6LWVHOI,QDGGLWLRQWKHUHLVSJSRLQWFORXGIRUPDQDJLQJ
SRLQWFORXGVDQGSJ5RXWLQJIRUQHWZRUNURXWLQJZKLFKDUHSDFNDJHG
VHSDUDWHO\
" NH\H[LVWVRSHUDWRU %LQDU\-621MVRQE
HTXDOLW\RSHUDWRU $UUD\&RQWDLQPHQW&KHFNV%LQDU\-621MVRQE
# FRQWDLQHGLQRSHUDWRU $UUD\&RQWDLQPHQW&KHFNV&RQWDLQVDQGFRQWDLQHG
LQRSHUDWRUV%LQDU\-621MVRQE
! GLVWDQFHRSHUDWRU )HDWXUHV,QWURGXFHGLQ3RVWJUH64/
FRORQ 6KRUWFXWV
!!RSHUDWRU4XHU\LQJ-621
!RSHUDWRU4XHU\LQJ-621
VXEWUDFWLRQRSHUDWRU 'DWHWLPH2SHUDWRUVDQG)XQFWLRQV(GLWLQJ-621%GDWD
(GLWLQJ-621%GDWD
DGGLWLRQRSHUDWRU 'DWHWLPH2SHUDWRUVDQG)XQFWLRQV
SDUHQWKHVHV %XLOGLQJ&XVWRP'DWD7\SHV
RYHUODSRSHUDWRU $UUD\&RQWDLQPHQW&KHFNV2YHUODSRSHUDWRU([FOXVLRQ
&RQVWUDLQWV
DQGRSHUDWRU 764XHULHV
DQGRSHUDWRU 764XHULHV
GROODUTXRWLQJ 'ROODU4XRWLQJ'2
!!RSHUDWRU4XHU\LQJ-621
!SRLQWHUV\PERO4XHU\LQJ-621
6\PEROV
,QGH[
show
list
list
output
this help, then exit
backslash commands, then exit
special variables, then exit
version information, then exit
database server host or socket directory
database server port (default: "5432")
database user name
never prompt for password
force password prompt (should happen automatically)
7KHVHLWHPVDUHQHZIHDWXUHVLQWURGXFHGLQ3RVWJUH64/
For more information, type "\?" (for internal commands) or "\help" (for SQL
commands) from within psql, or consult the psql section in the PostgreSQL
documentation.
Connection options:
-h, --host=HOSTNAME
-p, --port=PORT
-U, --username=USERNAME
-w, --no-password
-W, --password
Output format options:
-A, --no-align
unaligned table output mode
-F, --field-separator=STRING
set field separator (default: "|")
-H, --html
HTML table output mode
-P, --pset=VAR[=ARG]
set printing option VAR to ARG (see \pset command)
-R, --record-separator=STRING
set record separator (default: newline)
-t, --tuples-only
print rows only
-T, --table-attr=TEXT
set HTML table tag attributes (e.g., width, border)
-x, --expanded
turn on expanded table output
-z, --field-separator-zero
set field separator to zero byte
-0, --record-separator-zero
set record separator to zero byte
Input and output options:
-a, --echo-all
echo all input from script
-b, --echo-errors
echo failed commands
-e, --echo-queries
echo commands sent to server
-E, --echo-hidden
display queries that internal commands generate
-L, --log-file=FILENAME send session log to file
-n, --no-readline
disable enhanced command-line editing (readline)
-o, --output=FILENAME
send query results to file (or |pipe)
-q, --quiet
run quietly (no messages, only query output)
-s, --single-step
single-step mode (confirm each query)
-S, --single-line
single-line mode (end of line terminates SQL command)
-?, --help[=options]
--help=commands
--help=variables
--version
)RUWKHUHVWRIWKLVVHFWLRQZHߞOOIRFXVRXUGLVFXVVLRQRQXVLQJSJBGXPSDQG
8VHSJBGXPSWREDFNXSVSHFLILFGDWDEDVHV7REDFNXSDOOGDWDEDVHVLQSODLQ
WH[WDORQJZLWKVHUYHUJOREDOVXVHSJBGXPSDOOZKLFKQHHGVWRUXQXQGHUD
VXSHUXVHUDFFRXQWVRWKDWLWEDFNXSDOOGDWDEDVHV8VHSJBEDVHEDFNXSWRGR
V\VWHPOHYHOGLVNEDFNXSRIDOOGDWDEDVHV
3RVWJUH64/VKLSVZLWKWKUHHXWLOLWLHVIRUEDFNXSSJBGXPSSJBGXPSDOODQG
SJBEDVHEDFNXS<RXߞOOILQGDOORIWKHPLQWKH3RVWJUH64/ELQIROGHU
%DFNXSDQG5HVWRUH
$QH[WHQVLRQWKDWDGGHGDQ;0/GDWDW\SHUHODWHGIXQFWLRQVDQGRSHUDWRUV
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UH7ZRSRSXODURSHQVRXUFHRQHV\RXPLJKWZDQW
WRFRQVLGHUDUHSJ%DFN5HVWDQG%DUPDQ7KHVHRIIHUDGGLWLRQDOIHDWXUHVOLNH
EDFNXSVFKHGXOLQJPXOWLVHUYHUVXSSRUWDQGUHVWRUHVKRUWFXWV
0RVWRIWKHFRPPDQGOLQHRSWLRQVIRUWKHVHWRROVH[LVWERWKLQ*18VW\OH WZR
K\SKHQVSOXVDZRUG DQGWKHWUDGLWLRQDOVLQJOHOHWWHUVW\OH RQHK\SKHQSOXVDQ
DOSKDEHWLFFKDUDFWHU <RXFDQXVHERWKVW\OHVLQWHUFKDQJHDEO\HYHQLQWKHVDPH
FRPPDQG:HߞOOEHFRYHULQJMXVWWKHEDVLFVKHUHIRUDPRUHLQGHSWKGLVFXVVLRQ
VHHWKH3RVWJUH64/GRFXPHQWDWLRQ%DFNXSDQG5HVWRUH
SJBGXPSDOOSJBEDVHEDFNXSLVWKHPRVWHIILFLHQWZD\RIGRLQJDIXOOSRVWJUHVTO
VHUYHUFOXVWHUEDFNXS,I\RXKDYHDUHDVRQDEO\VL]HGGDWDEDVHDVLQ*%RU
PRUH\RXVKRXOGEHXVLQJSJBEDVHEDFNXSDVSDUWRI\RXUEDFNXSVWUDWHJ\
SJBEDVHEDFNXSKRZHYHUUHTXLUHVHQDEOLQJRIIHDWXUHVWKDWDUHRIWHQWXUQHGRII
EXWWKDWDUHDOVRQHHGHGIRUUHSOLFDWLRQVRZHߞOOVDYHGLVFXVVLRQRI
SJBEDVHEDFNXSIRUߡ6HWWLQJ8S)XOO6HUYHU5HSOLFDWLRQߢ
General options:
-c, --command=COMMAND
run only single command (SQL or internal) and exit
-d, --dbname=DBNAME
database name to connect to
-f, --file=FILENAME
execute commands from file, then exit
-l, --list
list available databases, then exit
-v, --set=, --variable=NAME=VALUE
set psql variable NAME to VALUE
(e.g., -v ON_ERROR_STOP=1)
-X, --no-psqlrc
do not read startup file (~/.psqlrc)
-1 ("one"), --single-transaction
execute command file as a single transaction
psql --help
psql is the PostgreSQL interactive terminal.
Usage:
psql [OPTION]... [DBNAME [USERNAME]]
([DPSOH%SVTOEDVLFKHOSVFUHHQ
([DPSOH%VKRZVWKHQRQLQWHUDFWLYHFRPPDQGVKHOSVFUHHQ([DPSOHVRIWKHLU
XVDJHDUHFRYHUHGLQߡ,QWHUDFWLYHYHUVXV1RQLQWHUDFWLYHSVTOߢ
SVTO1RQLQWHUDFWLYH&RPPDQGV
1HZIHDWXUHVLQWURGXFHGLQ3RVWJUH64/$OOFRQGLWLRQDORSWLRQVDUH
QHZ
1HZIHDWXUHVLQWURGXFHGLQ3RVWJUH64/
1HZIHDWXUHLQWURGXFHGLQ3RVWJUH64/
\t [on|off]
\T [STRING]
\x [on|off]
Connection
\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
connect to new database (currently "postgres")
\encoding [ENCODING] show or set client encoding
\password [USERNAME] securely change the password for a user
\conninfo
display information about current connection
Operating System
\cd [DIR]
change the current working directory
\setenv NAME [VALUE]
set or unset environment variable
\timing [on|off]
toggle timing of commands (currently off)
\! [COMMAND]
execute command in shell or start interactive shell
|unicode_border_linestyle|unicode_column_linestyle
|unicode_header_linestyle
})
show only rows (currently off)
set HTML <table> tag attributes, or unset if none
toggle expanded output (currently off)
\dA[+] [PATTERN]
list access methods
\db[+] [PATTERN]
list tablespaces
\dc[S] [PATTERN]
list conversions
\dC
[PATTERN]
list casts
\dd[S] [PATTERN]
show comments on objects
\ddp
[PATTERN]
list default privileges
\dD[S] [PATTERN]
list domains
\det[+] [PATTERN]
list foreign tables
\des[+] [PATTERN]
list foreign servers
\deu[+] [PATTERN]
list user mappings
\dew[+] [PATTERN]
list foreign-data wrappers
\df[antw][S+] [PATRN] list [only agg/normal/trigger/window] functions
\dF[+] [PATTERN]
list text search configurations
\dFd[+] [PATTERN]
list text search dictionaries
\dFp[+] [PATTERN]
list text search parsers
\dFt[+] [PATTERN]
list text search templates
\dg[S+] [PATTERN]
list roles
\di[S+] [PATTERN]
list indexes
\dl
list large objects, same as \lo_list
\dL[S+] [PATTERN]
list procedural languages
\dm[S+] [PATTERN]
list materialized views
\dn[S+] [PATTERN]
list schemas
\do[S] [PATTERN]
list operators
\dO[S+] [PATTERN]
list collations
\dp
[PATTERN]
list table, view, and sequence access privileges
\drds [PATRN1 [PATRN2]] list per-database role settings
\dRp[+] [PATTERN]
list replication publications
\dRs[+] [PATTERN]
list replication subscriptions
\ds[S+] [PATTERN]
list sequences
\dt[S+] [PATTERN]
list tables
\dT[S+] [PATTERN]
list data types
\du[S+] [PATTERN]
list roles
\dv[S+] [PATTERN]
list views
\dE[S+] [PATTERN]
list foreign tables
\dx[+] [PATTERN]
list extensions
\dy
[PATTERN]
list event triggers
\l[+]
list databases
\sf[+] FUNCNAME
show a function's definition
\sv[+] VIEWNAME
show a view's definition
\z
[PATTERN]
same as \dp
Formatting
\a
toggle between unaligned and aligned output mode
\C [STRING]
set table title, or unset if none
\f [STRING]
show or set field separator for unaligned query output
\H
toggle HTML output mode (currently off)
\pset NAME [VALUE]
set table output option
(NAME := {format|border|expanded|fieldsep|fieldsep_zero
| footer|null|
numericlocale|recordsep|tuples_only|title|tableattr|pager
pg_dump -h localhost -p 5432 -U someuser -F p --column-inserts \
-f select_tables.backup mydb
7RFUHDWHDSODLQWH[W64/EDFNXSRIVHOHFWWDEOHVXVHIXOIRUSRUWLQJVWUXFWXUH
DQGGDWDWRORZHUYHUVLRQVRI3RVWJUH64/RUQRQ3RVWJUH64/GDWDEDVHV SODLQ
WH[WJHQHUDWHVDQ64/VFULSWWKDW\RXFDQUXQRQDQ\V\VWHPWKDWVSHDNV64/ pg_dump -h localhost -p 5432 -U someuser -F c -b -v -N public \
-f all_sch_except_pub.backup mydb
7RFUHDWHDFRPSUHVVHGEDFNXSRIDOOREMHFWVLQDOOVFKHPDVH[FOXGLQJWKHSXEOLF
VFKHPD
pg_dump -h localhost -p 5432 -U someuser -F c -b -v \
-n hr -n payroll -f hr.backup mydb
7RFUHDWHDFRPSUHVVHGEDFNXSRIDOOREMHFWVLQWKHhrDQGpayrollVFKHPDV
pg_dump -h localhost -p 5432 -U someuser -F c -b -v -t *.pay* -f pay.backup mydb
7RFUHDWHDFRPSUHVVHGEDFNXSRIWDEOHVZKRVHQDPHVVWDUWZLWKSD\LQDQ\
VFKHPD
pg_dump -h localhost -p 5432 -U someuser -C -F p -b -v -f mydb.backup mydb
7RFUHDWHDSODLQWH[WVLQJOHGDWDEDVHEDFNXSLQFOXGLQJD&RSWLRQZKLFK
VWDQGVIRU&5($7('$7$%$6(
pg_dump -h localhost -p 5432 -U someuser -F c -b -v -f mydb.backup mydb
7RFUHDWHDFRPSUHVVHGVLQJOHGDWDEDVHEDFNXS
7KHQH[WH[DPSOHVGHPRQVWUDWHDIHZFRPPRQEDFNXSVFHQDULRVDQG
FRUUHVSRQGLQJSJBGXPSRSWLRQV7KH\VKRXOGZRUNIRUDQ\YHUVLRQRI
3RVWJUH64/
GDLO\UHJLPHQZHKDYHLQFOXGHGDIXOOGXPSRIWKHKHOSLQߡ'DWDEDVH%DFNXS
8VLQJSJBGXPSߢVR\RXFDQVHHWKHP\ULDGVZLWFKHVLQDVLQJOHJODQFH
,WߞVDJRRGLGHDWREDFNXSJOREDOVRQDGDLO\EDVLV$OWKRXJK\RXFDQXVH
SJBGXPSDOOWREDFNXSGDWDEDVHVDVZHOOZHSUHIHUEDFNLQJXSGDWDEDVHV
LQGLYLGXDOO\XVLQJSJBGXPSRUXVLQJSJBEDVHEDFNXSWRGRD3RVWJUH64/
VHUYLFHOHYHOEDFNXS5HVWRULQJIURPDKXJHSODLQWH[WEDFNXSWULHVRXUSDWLHQFH
8VLQJSJBEDVHEDFNXSLQFRQMXQFWLRQZLWKVWUHDPLQJUHSOLFDWLRQLVWKHIDVWHVW
8VHWKHSJBGXPSDOOXWLOLW\WREDFNXSDOOGDWDEDVHVRQDVHUYHULQWRDVLQJOH
SODLQWH[WILOH7KLVFRPSUHKHQVLYHEDFN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FWHUVWKDWFRXOGWKURZRIIWKHFRPPDQGOLQH
LQWHUSUHWHUZUDSWKHILOHSDWKLQGRXEOHTXRWHV"/path with spaces/mydb.backup"$VD
JHQHUDOUXOH\RXFDQDOZD\VXVHGRXEOHTXRWHVLI\RXDUHQߞWVXUH
7,3
\?
General
\copyright
show PostgreSQL usage and distribution terms
\errverbose
show most recent error message at maximum verbosity
\g [FILE] or ;
execute query (and send results to file or |pipe)
\gexec
execute query, then execute each value in its result
\gset [PREFIX]
execute query and store results in psql variables
\h [NAME]
help on syntax of SQL commands, * for all commands
\gx [FILE]
as \g, but forces expanded output mode
\q
quit psql
\crosstabview [COLUMNS] execute query and display results in crosstab
\watch [SEC]
execute query every SEC seconds
Help
\? [commands]
show help on backslash commands
\? options
show help on psql command-line options
\? variables
show help on special variables
\h [NAME]
help on syntax of SQL commands, * for all commands
Query Buffer
\e [FILE] [LINE]
edit the query buffer (or file) with external editor
\ef [FUNCNAME [LINE]] edit function definition with external editor
\ev [VIEWNAME [LINE]] edit view definition with external editor
\p
show the contents of the query buffer
\r
reset (clear) the query buffer
\w FILE
write query buffer to file
Input/Output
\copy ...
perform SQL COPY with data stream to the client host
\echo [STRING]
write string to standard output
\i FILE
execute commands from file
\ir FILE
as \i, but relative to location of current script
\o [FILE]
send all query results to file or |pipe
\qecho [STRING]
write string to query output stream (see \o)
Conditional
\if EXPR
begin conditional block
\elif EXPR
alternative within current conditional block
\else
final alternative within current conditional block
\endif
end conditional block
Informational
(options: S = show system objects, + = additional detail)
\d[S+]
list tables, views, and sequences
\d[S+] NAME
describe table, view, sequence, or index
\da[S] [PATTERN]
list aggregates
([DPSOH%*HWWLQJDOLVWRILQWHUDFWLYHSVTOFRPPDQGV
([DPSOH%OLVWVFRPPDQGVDYDLODEOHLQSVTOZKHQ\RXODXQFKDQLQWHUDFWLYH
VHVVLRQ)RUH[DPSOHVRIXVDJHVHHߡ(QYLURQPHQW9DULDEOHVߢDQGߡ,QWHUDFWLYH
YHUVXV1RQLQWHUDFWLYHSVTOߢ
SVTO,QWHUDFWLYH&RPPDQGV
database server host or socket directory
database server port number
connect as specified database user
never prompt for password
force password prompt (should happen automatically)
do SET ROLE before restore
1HZIHDWXUHVLQWURGXFHGLQ3RVWJUH64/
1HZIHDWXUHVLQWURGXFHGLQ3RVWJUH64/3ULRUWRWKH-tRSWLRQ
PDWFKHGRQO\WDEOHV,QLWZDVFKDQJHGWRDOVRPDWFKIRUHLJQWDEOHV
YLHZVPDWHULDOL]HGYLHZVDQGVHTXHQFHV
1HZIHDWXUHVLQWURGXFHGLQ3RVWJUH64/
Connection options:
-h, --host=HOSTNAME
-p, --port=PORT
-U, --username=NAME
-w, --no-password
-W, --password
--role=ROLENAME
--data-only
--clean
--create
--exit-on-error
--index=NAME
--jobs=NUM
--use-list=FILENAME
restore only the data, no schema
clean (drop) database objects before recreating
create the target database
exit on error, default is to continue
restore named index
use this many parallel jobs to restore
use table of contents from this file for
selecting/ordering output
-n, --schema=NAME
restore only objects in this schema
-N, --exclude-schema=NAME
do not restore objects in this schema
-O, --no-owner
skip restoration of object ownership
-P, --function=NAME(args)
restore named function
-s, --schema-only
restore only the schema, no data
-S, --superuser=NAME
superuser user name to use for disabling triggers
-t, --table=NAME
restore named relation (table, view, etc.)
-T, --trigger=NAME
restore named trigger
-x, --no-privileges
skip restoration of access privileges (grant/revoke)
-1, --single-transaction
restore as a single transaction
--enable-row-security
enable row security
--disable-triggers
disable triggers during data-only restore
--no-data-for-failed-tables do not restore data of tables that could not be
created
--no-publications
do not restore publications
--no-security-labels
do not restore security labels
--no-subscriptions
do not restore subscriptions
--no-tablespaces
do not restore tablespace assignments
--section=SECTION
restore named section (pre-data, data, or post-data)
--strict-names
require table and/or schema include patterns to
match at least one entity each
--use-set-session-authorization
use SET SESSION AUTHORIZATION commands instead of
ALTER OWNER commands to set ownership
-a,
-c,
-C,
-e,
-I,
-j,
-L,
psql -U postgres -d mydb -f select_objects.sql
7RUHVWRUHWRDVSHFLILFGDWDEDVH
psql -U postgres --set ON_ERROR_STOP=on -f myglobals.sql
7RUHVWRUHVWRSSLQJLIDQ\HUURULVIRXQG
psql -U postgres -f myglobals.sql
7RUHVWRUHDEDFNXSDQGLJQRUHHUURUV
$SODLQ64/EDFNXSLVQRWKLQJPRUHWKDQDWH[WILOHFRQWDLQLQJDKHIW\64/
VFULSW,WߞVWKHOHDVWFRQYHQLHQWEDFNXSWRKDYHEXWLWߞVWKHPRVWYHUVDWLOH:LWK
64/EDFNXS\RXPXVWH[HFXWHWKHHQWLUHVFULSW<RXFDQߞWFKHUU\SLFNREMHFWV
XQOHVV\RXߞUHZLOOLQJWRPDQXDOO\HGLWWKHILOH5XQDOORIWKHIROORZLQJH[DPSOHV
IURPWKH26FRQVROHRUSVTO
8VLQJSVTOWRUHVWRUHSODLQWH[W64/EDFNXSV
8VHSJBUHVWRUHWRUHVWRUHFRPSUHVVHG7$5DQGGLUHFWRU\EDFNXSVFUHDWHG
ZLWKSJBGXPS
8VHSVTOWRUHVWRUHSODLQWH[WEDFNXSVJHQHUDWHGZLWKSJBGXPSDOORU
SJBGXPS
7KHUHDUHWZRZD\VWRUHVWRUHGDWDLQ3RVWJUH64/IURPEDFNXSVFUHDWHGZLWK
SJBGXPSRUSJBGXPSDOO
5HVWRULQJ'DWD
pg_dumpall -h localhost -U postgres --port=5432 -f myroles.sql --roles-only
7REDFNXSVSHFLILFJOREDOVHWWLQJVXVHWKHIROORZLQJ
pg_dumpall -h localhost -U postgres --port=5432 -f myglobals.sql --globals-only
7REDFNXSDOOJOREDOVDQGWDEOHVSDFHGHILQLWLRQVRQO\XVHWKHIROORZLQJ
ZD\WRUHFRYHUIURPPDMRUVHUYHUIDLOXUH
:KHQ\RXXVHWKH--createRSWLRQWKHGDWDEDVHQDPHLVDOZD\VWKHQDPHRIWKH
RQH\RXEDFNHGXS<RXFDQߞWUHQDPHLW,I\RXߞUHDOVRXVLQJWKH--dbname
RSWLRQWKDWGDWDEDVHQDPHPXVWEHGLIIHUHQWI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U\\RXKDYHWRXVHWKHSJBUHVWRUHXWLOLW\WRUHVWRUHSJBUHVWRUHSURYLGHVD
GL]]\LQJDUUD\RIRSWLRQVIDUVXUSDVVLQJWKHUHVWRUHXWLOLW\IRXQGLQRWKHU
GDWDEDVHSURGXFWVZHߞYHXVHG6RPHRILWVRXWVWDQGLQJIHDWXUHVLQFOXGH
8VLQJSJBUHVWRUH
connect using connection string
database server host or socket directory
alternative default database
database server port number
connect as specified database user
never prompt for password
force password prompt (should happen automatically)
do SET ROLE before dump
connect to database name
output file name
backup file format (should be automatic)
print summarized TOC of the archive
verbose mode
output version information, then exit
show this help, then exit
Options controlling the restore:
General options:
-d, --dbname=NAME
-f, --file=FILENAME
-F, --format=c|d|t
-l, --list
-v, --verbose
-V, --version
-?, --help
pg_restore --help
pg_restore restores a PostgreSQL database from an archive created by pg_dump.
Usage:
pg_restore [OPTION]... [FILE]
([DPSOH%SJBUHVWRUHKHOS
8VHpg_restoreWRUHVWRUHEDFNXSILOHVLQWDUFXVWRPRUGLUHFWRU\IRUPDWV
FUHDWHGXVLQJpg_dump([DPSOH%VKRZVWKHpg_restoreKHOSRXWSXW6HH
ߡ5HVWRULQJ'DWDߢIRUPRUHH[DPSOHV
'DWDEDVH5HVWRUHSJBUHVWRUH
1HZLQ3RVWJUH64/
If -f/--file is not used, then the SQL script will be written to the standard
output.
Connection options:
-d, --dbname=CONNSTR
-h, --host=HOSTNAME
-l, --database=DBNAME
-p, --port=PORT
-U, --username=NAME
-w, --no-password
-W, --password
--role=ROLENAME
--no-sync
do not wait for changes to be written safely to disk
--no-tablespaces
do not dump tablespace assignments
--no-unlogged-table-data do not dump unlogged table data
--no-role-passwords
do not dump passwords for roles
--quote-all-identifiers
quote all identifiers, even if not keywords
--use-set-session-authorization
use SET SESSION AUTHORIZATION commands instead o
ALTER OWNER commands to set ownership
output file name
verbose mode
output version information, then exit
fail after waiting TIMEOUT for a table lock
show this help, then exit
Options controlling the output content:
-a, --data-only
dump only the data, not the schema
-c, --clean
clean (drop) databases before recreating
-g, --globals-only
dump only global objects, no databases
-o, --oids
include OIDs in dump
-O, --no-owner
skip restoration of object ownership
-r, --roles-only
dump only roles, no databases or tablespaces
-s, --schema-only
dump only the schema, no data
-S, --superuser=NAME
superuser user name to use in the dump
-t, --tablespaces-only
dump only tablespaces, no databases or roles
-x, --no-privileges
do not dump privileges (grant/revoke)
--binary-upgrade
for use by upgrade utilities only
--column-inserts
dump data as INSERT commands with column names
--disable-dollar-quoting disable dollar quoting, use SQL standard quoting
--disable-triggers
disable triggers during data-only restore
--inserts
dump data as INSERT commands, rather than COPY
--no-publications
do not dump publications
--no-security-labels
do not dump security label assignments
--no-subscriptions
do not dump subscriptions
General options:
-f, --file=FILENAME
-v, --verbose
-V, --version
--lock-wait-timeout=TIMEOUT
-?, --help
pg_dumpall --help
pg_dumpall extracts a PostgreSQL database cluster into an SQL script file.
Usage:
pg_dumpall [OPTION]...
([DPSOH%SJBGXPSDOOKHOS
8VHpg_dump_allWREDFNXSDOOGDWDEDVHVRQ\RXUVHUYHURQWRDVLQJOHSODLQ
WH[WRUSODLQWH[W64/ILOH7KHEDFNXSURXWLQHZLOODXWRPDWLFDOO\LQFOXGHVHUYHU
OHYHOREMHFWVVXFKDVUROHVDQGWDEOHVSDFHV([DPSOH%VKRZVWKHpg_dumpall
KHOSRXWSXW6HHߡ6\VWHPZLGH%DFNXS8VLQJSJBGXPSDOOߢIRUWKHIXOO
GLVFXVVLRQ
6HUYHU%DFNXSSJBGXPSDOO
1HZIHDWXUHVLQWURGXFHGLQ3RVWJUH64/
1HZIHDWXUHVLQWURGXFHGLQ3RVWJUH64/
1HZIHDWXUHVLQWURGXFHGLQ3RVWJUH64/
&UHDWLQJ7DEOHVSDFHV
3RVWJUH64/XVHVWDEOHVSDFHVWRDVFULEHORJLFDOQDPHVWRSK\VLFDOORFDWLRQVRQ
GLVN,QLWLDOL]LQJD3RVWJUH64/FOXVWHUDXWRPDWLFDOO\EHJHWVWZRWDEOHVSD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WRUHFDQUH
FUHDWHWKHP
EHLQJUHVWRUHG:HXVXDOO\MXVWVSHFLI\WKHSRVWJUHVGDWDEDVH
:HKDYHDFWHGDVILUVWUHVSRQGHUVWRPDQ\3RVWJUH64/DFFLGHQWVVRZHWKRXJKW
LWEHVWWRHQGWKLVFKDSWHUE\LWHPL]LQJWKHPRVWFRPPRQPLVWDNHV
9HUERWHQ3UDFWLFHV
'XULQJWKHPRYH\RXUGDWDEDVHRUWDEOHZLOOEHORFNHG
ALTER TABLESPACE pg_default MOVE ALL TO secondary;
7RPRYHDOOREMHFWVIURPGHIDXOWWDEOHVSDFHWRVHFRQGDU\XVH
1HZLQ3RVWJUH64/LVWKHDELOLW\PRYHDJURXSRIREMHFWVIURPRQH
WDEOHVSDFHWRDQRWKHU,IWKHUROHUXQQLQJWKHFRPPDQGLVDVXSHUXVHUDOOREMHFWV
ZLOOEHPRYHG,IQRWRQO\WKHRZQHGREMHFWVZLOOEHPRYHG
ALTER TABLE mytable SET TABLESPACE secondary;
7RPRYHMXVWRQHWDEOH
ALTER DATABASE mydb SET TABLESPACE secondary;
<RXFDQVKXIIOHGDWDEDVHREMHFWVDPRQJGLIIHUHQWWDEOHVSDFHV7RPRYHDOO
REMHFWVLQWKHGDWDEDVHWR\RXUVHFRQGDU\WDEOHVSDFHLVVXHWKHIROORZLQJ64/
FRPPDQG
0RYLQJ2EMHFWV$PRQJ7DEOHVSDFHV
CREATE TABLESPACE secondary LOCATION '/usr/data/pgdata94_secondary';
)RU8QL[EDVHGV\VWHPV\RXILUVWPXVWFUHDWHWKHIROGHURUGHILQHDQIVWDE
ORFDWLRQWKHQXVHWKLVFRPPDQG
CREATE TABLESPACE secondary LOCATION 'C:/pgdata94_secondary';
7RFUHDWHDQHZWDEOHVSDFHVSHFLI\DORJLFDOQDPHDQGDSK\VLFDOIROGHUDQG
PDNHVXUHWKDWWKHSRVWJUHVVHUYLFHDFFRXQWKDVIXOODFFHVVWRWKHSK\VLFDOIROGHU
,I\RXDUHRQD:LQGRZVVHUYHUXVHWKHIROORZLQJFRPPDQG QRWHWKHXVHRI
8QL[VW\OHIRUZDUGVODVKHV include large objects in dump
exclude large objects in dump
clean (drop) database objects before recreating
include commands to create database in dump
dump the data in encoding ENCODING
dump the named schema(s) only
do NOT dump the named schema(s)
include OIDs in dump
skip restoration of object ownership in
1HZIHDWXUHVLQWURGXFHGLQ3RVWJUH64/
dump only the schema, no data
superuser user name to use in plain-text format
dump the named table(s) only
do NOT dump the named table(s)
do not dump privileges (grant/revoke)
for use by upgrade utilities only
dump data as INSERT commands with column names
disable dollar quoting, use SQL standard quoting
disable triggers during data-only restore
enable row security (dump only content user has
access to)
--exclude-table-data=TABLE do NOT dump data for the named table(s)
--if-exists
use IF EXISTS when dropping objects
--inserts
dump data as INSERT commands, rather than COPY
--no-publications
do not dump publications
--no-security-labels
do not dump security label assignments
--no-subscriptions
do not dump subscriptions
--no-synchronized-snapshots do not use synchronized snapshots in parallel jobs
--no-tablespaces
do not dump tablespace assignments
--no-unlogged-table-data
do not dump unlogged table data
--quote-all-identifiers
quote all identifiers, even if not key words
--section=SECTION
dump named section (pre-data, data, or post-data)
--serializable-deferrable wait until the dump can run without anomalies
--snapshot=SNAPSHOT
use given snapshot for the dump
--strict-names
require table and/or schema include patterns to
match at least one entity each
--use-set-session-authorization
use SET SESSION AUTHORIZATION commands instead of
ALTER OWNER commands to set ownership
Connection options:
-d, --dbname=DBNAME
database to dump
-h, --host=HOSTNAME
database server host or socket directory
-p, --port=PORT
database server port number
-U, --username=NAME
connect as specified database user
-w, --no-password
never prompt for password
-W, --password
force password prompt (should happen automatically)
--role=ROLENAME
do SET ROLE before dump
-b, --blobs
-B, --no-blobs
-c, --clean
-C, --create
-E, --encoding=ENCODING
-n, --schema=SCHEMA
-N, --exclude-schema=SCHEMA
-o, --oids
-O, --no-owner
plain-text format
-s, --schema-only
-S, --superuser=NAME
-t, --table=TABLE
-T, --exclude-table=TABLE
-x, --no-privileges
--binary-upgrade
--column-inserts
--disable-dollar-quoting
--disable-triggers
--enable-row-security
General options:
-f, --file=FILENAME
output file or directory name
-F, --format=c|d|t|p
output file format (custom, directory, tar, plain
text)
-j, --jobs=NUM
use this many parallel jobs to dump
-v, --verbose
verbose mode
-Z, --compress=0-9
compression level for compressed formats
--lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock
--no-sync
do not wait for changes to be written safely to disk
--help
show this help, then exit
--version
output version information, then exit
Options controlling the output content:
-a, --data-only
dump only the data, not the schema
pg_dump --help
pg_dump dumps a database as a text file or to other formats.
Usage:
pg_dump [OPTION]... [DBNAME]
([DPSOH%SJBGXPSKHOS
8VHpg_dumpWREDFNXSDOORUSDUWRIDGDWDEDVH%DFNXSILOHIRUPDWVDYDLODEOH
DUH7$5FRPSUHVVHG 3RVWJUH64/FXVWRPIRUPDW SODLQWH[WDQGSODLQWH[W
64/3ODLQWH[WEDFNXSFDQFRS\SVTOVSHFLILFFRPPDQGVWKHUHIRUHUHVWRUHE\
UXQQLQJWKHILOHZLWKLQSVTO$SODLQWH[W64/EDFNXSLVPHUHO\DILOHZLWK
VWDQGDUG64/CREATEDQGINSERTFRPPDQGV7RUHVWRUH\RXFDQUXQWKHILOH
XVLQJSVTORUSJ$GPLQ([DPSOH%VKRZVWKHpg_dumpKHOSRXWSXW)RUIXOO
FRYHUDJHRIpg_dumpXVDJHVHHߡ6HOHFWLYH%DFNXS8VLQJSJBGXPSߢ
'DWDEDVH%DFNXS8VLQJSJBGXPS
7KLVDSSHQGL[VXPPDUL]HVLQGLVSHQVDEOHFRPPDQGOLQHWRROVSDFNDJHGZLWK
3RVWJUH64/VHUYHU:HGLVFXVVHGWKHPDWOHQJWKLQWKHERRN+HUHZHOLVWWKHLU
KHOSPHVVDJHV:HKRSHWRVDYH\RXDELWRIWLPHZLWKWKHLULQFOXVLRQDQG
SHUKDSVPDNHWKLVERRNDQRWVRVWUDQJHEHGIHOORZ
$SSHQGL[%3RVWJUH64/
3DFNDJHG&RPPDQG/LQH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U\RXLQVWDOOLW\RXZRXOGGR
SJDGPLQDQGWKHDELOLW\WRLQVWDOODQGPRQLWRUELJVTOSDFNDJHV
ReleaseDt | Status
| Cur?
2017-05-11
1
2017-05-11
1
2017-05-11
1
2017-05-11
1
2017-05-11 Installed 1
2016-11-08
1
2016-09-01
1
2016-09-01
1
2016-09-23
1
2017-05-18
1
2017-04-15
1
2016-12-28
1
2017-04-15
1
2017-05-18 Installed 1
2017-02-23
1
2016-11-23
1
2017-05-18 Installed 1
2017-05-18
1
2017-03-23
1
2016-10-20 Installed 1
2017-02-23
1
2017-02-09
1
2017-02-09
1
2016-03-14
1
2016-10-20 Installed 0
2016-03-11
1
7KHSJGHYRSVSDFNDJHLVDZHEEDVHGDGPLQLVWUDWLRQWRROWKDWLQFOXGHV
pgc install pgdevops
7RLQVWDOOWKHELQDULHVIRUDSDFNDJH
Category
| Component
| Version |
PostgreSQL
pg92
9.2.21-1
PostgreSQL
pg93
9.3.17-1
PostgreSQL
pg94
9.4.12-1
PostgreSQL
pg95
9.5.7-1
PostgreSQL
pg96
9.6.3-1
Extensions
cassandra_fdw3-pg96 3.0.1-1
Extensions
hadoop_fdw2-pg96
2.5.0-1
Extensions
oracle_fdw1-pg96
1.5.0-1
Extensions
orafce3-pg96
3.3.1-1
Extensions
pgaudit11-pg96
1.1.0-2
Extensions
pgpartman2-pg96
2.6.4-1
Extensions
pldebugger96-pg96
9.6.0-1
Extensions
plprofiler3-pg96
3.2-1
Extensions
postgis23-pg96
2.3.2-3
Extensions
setuser1-pg96
1.2.0-1
Extensions
tds_fdw1-pg96
1.0.8-1
Servers
pgdevops
1.4-1
Applications
backrest
1.18
Applications
ora2pg
18.1
Applications
pgadmin3
1.23.0a
Applications
pgagent
3.4.1-1
Applications
pgbadger
9.1
Frameworks
java8
8u121
Frameworks
perl5
5.20.3.3
Frameworks
python2
2.7.12-1
Frameworks
tcl86
8.6.4-1
7KHRXWSXWZLOOVKRZVRPHWKLQJOLNH
pgc update
pgc list
7RX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SOHVRQO\ DQGXVHWKH\aRSWLRQWRJHWULGRIWKHH[WUDEUHDNLQJ
HOHPHQWVWKDWSVTOQRUPDOO\SXWVLQ:HWKHQXVHWKH\gRSWLRQWRIRUFHRXU
TXHU\RXWSXWWREHUHGLUHFWHGWRDILOH
:HFDOOWKH\oZLWKRXWILOHDUJXPHQWVWRVWRSUHGLUHFWLRQRITXHU\UHVXOWVWR
ILOH
7RH[HFXWHRXUJHQHUDWHGVFULSWZHXVHWKH\iIROORZHGE\WKHJHQHUDWHG
VFULSWQDPHFUHDWHBVFULSWVTO7KH\iLVWKHLQWHUDFWLYHYHUVLRQRIWKH
QRQLQWHUDFWLYH-fRSWLRQ
\a
\t
\g create_script.sql
SELECT
'CREATE TABLE staging.factfinder_import (
geo_id varchar(255), geo_id2 varchar(255), geo_display varchar(255),' ||
array_to_string(array_agg('s' ||
lpad(i::text,2,'0') || ' varchar(255),s' ||
lpad(i::text,2,'0') || '_perc varchar(255)'),',') ||
');'
FROM generate_series(1,51) As i;
\o
\i create_script.sql
([DPSOH6FULSWWKDWLQFOXGHVSVTOLQWHUDFWLYHFRPPDQGV
<RXFDQHPEHGLQWHUDFWLYHFRPPDQGVLQVLGHVFULSWILOHV([DPSOHLVWKH
FRQWHQWVRIDVFULSWQDPHGEXLOGBVWDJHSVTOZKLFKZHZLOOXVHWRFUHDWHD
VWDJLQJWDEOHFDOOHGVWDJLQJIDFWILQGHUBLPSRUWWKDWLVORDGHGLQ([DPSOH
7KHVFULSWILUVWJHQHUDWHVDCREATE TABLEVWDWHPHQWZKLFKLWZULWHVWRDQHZ
ILOHFDOOHGFUHDWHBVFULSWVTO,WWKHQH[HFXWHVWKHJHQHUDWHGFUHDWHBVFULSWVTO
psql -d postgresql_book -c "DROP TABLE IF EXISTS dross; CREATE SCHEMA staging;"
/LNH(QWHUSULVH'%%LJ64/KDVLWVRZQLQVWDOOHUV\VWHP7KHLQVWDOOHUFDQEH
WULJJHUHGYLDDZHELQWHUIDFHRUYLDWK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HLQVWDOOHUVDUHHDV\WRXVH7KH\FRPHSDFNDJHGZLWK3J$GPLQ 3RVWJUH64/
FRPHZLWKSJ$GPLQZKLOHROGHUYHUVLRQVFRPHZLWKSJ$GPLQ DQGD
VWDFNEXLOGHUIURPZKLFK\RXFDQLQVWDOODGGRQVOLNH-'%&1(7GULYHUV
5XE\3RVW*,6SKS3J$GPLQDQGSJ$JHQW
(QWHUSULVH'%EXLOGVLQVWDOOHUVIRU:LQGRZVDQGGHVNWRSYHUVLRQVRI/LQX[7KH\
RIIHUERWKELWDQGELWYHUVLRQVIRUHDFK26
:LQGRZVDQG'HVNWRS/LQX[
$SSHQGL[$,QVWDOOLQJ
3RVWJUH64/
title
--------------------PostgreSQL 10 Roadmap
PostgreSQL: Roadmap
(2 rows)
9RLO :HKDYHRXUUHVSRQVH
SELECT regexp_replace(title,E'(?x)(< [^>]*? >)','','g') As title
FROM www_fdw_google_search
WHERE q = 'New in PostgreSQL 10'
LIMIT 2;
1RZFRPHVWKHIXQSDUW:HVHDUFKZLWKWKHWHUPNew in PostgreSQL 9.4DQG
PL[LQDELWRIUHJXODUH[SUHVVLRQJRRGQHVVWRVWULSRII+70/WDJV
GRANT SELECT ON TABLE www_fdw_google_search TO public;
7KHXVHUPDSSLQJGRHVQߞWDVVLJQDQ\ULJKWV<RXVWLOOQHHGWRJUDQWULJKWVEHIRUH
EHLQJDEOHWRTXHU\WKHIRUHLJQWDEOH
CREATE FOREIGN TABLE www_fdw_google_search (
q text,
GsearchResultClass text,
unescapedUrl text,
url text,
visibleUrl text,
cacheUrl text,
title text,
content text
) SERVER www_fdw_server_google_search;
([DPSOH0DNHDIRUHLJQWDEOHIURP*RRJOH
1RZFUHDWH\RXUIRUHLJQWDEOHDVVKRZQLQ([DPSOH(DFKILHOGLQWKHWDEOH
FRUUHVSRQGVWRD*(7SDUDPHWHULQWKH85/WKDW*RRJOHFUHDWHVIRUDVHDUFK
CREATE USER MAPPING FOR public SERVER www_fdw_server_google_search;
1H[WHVWDEOLVKDWOHDVWRQHXVHUIRU\RXU)':$OOXVHUVWKDWFRQQHFWWR\RXU
VHUYHUVKRXOGEHDEOHWRDFFHVVWKH*RRJOHVHDUFKVHUYHUVRKHUHZHFUHDWHRQH
IRUWKHHQWLUHpublicJURXS
(DFK)':LVGLIIHUHQWDQGFRPHVZLWKLWVRZQ$3,VHWWLQJV
Null display is "NULL".
:KHQ\RXODXQFKSVTOQRZWKHUHVXOWRIH[HFXWLQJWKHFRQILJXUDWLRQILOHHFKRHV
WRWKHVFUHHQ
(DFKFRPPDQGPXVWEHRQDVLQJOHOLQHZLWKRXWEUHDNV2XUH[DPSOHVPD\DGGOLQHEUHDNVWR
DFFRPPRGDWHSULQWLQJ
:$51,1*
\pset null 'NULL'
\encoding latin1
\set PROMPT1 '%n@%M:%>%x %/# '
\pset pager always
\timing on
\set qstats92 '
SELECT usename, datname, left(query,100) || ''...'' As query
FROM pg_stat_activity WHERE state != ''idle'' ;
'
([DPSOH([DPSOHSVTOUFILOH
([DPSOHLVDJOLPSVHLQWRWKHFRQWHQWVRIDSVTOUFILOH<RXFDQLQFOXGHDQ\
SVTOFRPPDQG
2Q/LQX[8QL[WKHILOHLVFXVWRPDULO\QDPHGSVTOUFDQGVKRXOGEHSODFHGLQ
\RXUKRPHGLUHFWRU\2Q:LQGRZVWKHILOHLVFDOOHGSVTOUFFRQIDQGVKRXOGEH
SODFHGLQWKH$33'$7$?SRVWJUHVTOIROGHUZKLFKXVXDOO\UHVROYHVWR
&?8VHUV?username?$SS'DWD?5RDPLQJ?SRVWJUHVTO'RQߞWZRUU\LI\RXFDQߞWILQG
WKHILOHULJKWDIWHULQVWDOODWLRQ\RXXVXDOO\QHHGWRFUHDWHLW$Q\VHWWLQJVLQWKH
ILOHZLOORYHUULGHSVTOGHIDXOWV
,I\RXVSHQGPRVWRI\RXUGD\LQSVTOFRQVLGHUWDLORULQJWKHSVTOHQYLURQPHQWWR
PDNH\RXPRUHSURGXFWLYHSVTOUHDGVVHWW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QFKDQJHVHWWLQJVRQWKHIO\ZKLOHLQSVTOWKRXJKWKHFKDQJHZLOORQO\EH
LQHIIHFWGXULQJ\RXUSVTOVHVVLRQ7RUHPRYHDFRQILJXUDWLRQYDULDEOHRUVHWLW
EDFNWRWKHGHIDXOWLVVXHWKH\unsetFRPPDQGIROORZHGE\WKHVHWWLQJDVLQ
\unset qstat92
6RPHFRPPDQGVZRUNRQO\RQ/LQX[8QL[V\VWHPVZKLOHRWKHUVZRUNRQO\RQ
:LQGRZV,QHLWKHU26\RXVKRXOGXVHWKH/LQX[8QL[࠺VW\OHVODVK IRUZDUG
VODVK IRUSDWK,I\RXZDQWWRE\SDVVWKHFRQILJXUDWLRQILOHDQGVWDUWSVTOZLWKDOO
LWVGHIDXOWVVWDUWLWZLWKWKH-XRSWLRQ
Timing is on.
Pager is always used.
psql (9.6beta3)
Type "help" for help.
postgres@localhost:5442 postgresql_book#
7KHGHIDXOWIRUPDWVXSSRUWHGE\www_fdwLV-621VRZHGLGQߞWQHHGWRLQFOXGH
LWLQWKHOPTIONSPRGLILHU7KHRWKHUVXSSRUWHGIRUPDWLV;0/)RUGHWDLOVRQ
DGGLWLRQDOSDUDPHWHUVWKDW\RXFDQVHWUHIHUWRWKHwww_fdw GRFXPHQWDWLRQ
CREATE SERVER www_fdw_server_google_search
FOREIGN DATA WRAPPER www_fdw
OPTIONS (uri 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0');
7KHQFUHDWH\RXU*RRJOHIRUHLJQGDWDVHUYHU
CREATE EXTENSION www_fdw;
1RZFUHDWHDQH[WHQVLRQWRKROGWKH)':
7KHwww_fdw)':LVQRWJHQHUDOO\SDFNDJHGZLWK3RVWJUH64/,I\RXDUHRQ
/LQX[8QL[LWߞVDQHDV\FRPSLOHLI\RXKDYHWKHpostgresql-devSDFNDJH
LQVWDOOHGDQGFDQGRZQORDGWKHODWHVWVRXUFH:HGLGWKHZRUNRIFRPSLOLQJIRU
VRPH:LQGRZVSODWIRUPV\RXFD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RXVLQJBFVYZLOOEHOLQNHGLQDVIRUHLJQWDEOHVLQVFKHPDIIZLWKQDPHV
KRXVLQJBDQGKRXVLQJB
CREATE SERVER ogr_fdw_ff
FOREIGN DATA WRAPPER ogr_fdw
OPTIONS (datasource '/fdw_data/factfinder', format 'CSV');
CREATE SCHEMA ff;
IMPORT FOREIGN SCHEMA "Housing"
FROM SERVER ogr_fdw_ff INTO ff;
7KLVQH[WH[DPSOHZLOOFUHDWHDVHUYHUSRLQWLQJWRDIROGHURI&69ILOHV&UHDWHD
VFKHPDIIWRKRXVHIRUHLJQWDEOHVIRUWKH&69VHUYHU7KH)':ZLOOWKHQFUHDWH
IRUHLJQWDEOHVOLQNHGWR&69ILOHVZKHUHWKH&69ILOHQDPHEHJLQVZLWK+RXVLQJ
LQVFKHPDII
7KHVFKHPDLVFDVHVHQVLWLYHVRLIWKHQDPHRIDZRUNVKHHWFRQWDLQVXSSHUFDVH
FKDUDFWHUVRUQRQVWDQGDUGFKDUDFWHUVLWQHHGVWREHTXRWHG
CREATE SCHEMA wb_data;
IMPORT FOREIGN SCHEMA "Finance"
FROM SERVER ogr_fdw_wb INTO wb_data;
7KHogr_allVFKHPDLVDFDWFKDOOWKDWLPSRUWVDOOWDEOHVLQWKHIRUHLJQVHUYHU
UHJDUGOHVVRIVFKHPD6RPHGDWDVRXUFHVVFKHPDVDQGVRPHGRQߞW7R
DFFRPPRGDWHDOOLQSXWVRJUBIGZ LQSODFHRIRJUBDOO DFFHSWVWKHLQLWLDO
FKDUDFWHUVRIDWDEOHQDPHDVWKHVFKHPD6RIRUH[DPSOHLI\RXZDQWHGWR
LPSRUWMXVWDVXEVHWRIZRUNVKHHWVZKHUHWKHZRUNVKHHWQDPHEHJLQVZLWK
ߡ)LQDQFHߢ\RXZRXOGUHSODFHRJUBDOOZLWKߡ)LQDQFHߢ
CREATE SCHEMA wb_data;
IMPORT FOREIGN SCHEMA ogr_all
FROM SERVER ogr_fdw_wb INTO wb_data;
COMMIT;
7RPDNHWKHXSGDWHSHUPDQHQWUXQ
ROLLBACK;
7RXQGRWKHXSGDWHUXQ
UPDATE census.facts SET short_name = 'This is a mistake.';
%\GHIDXOWDXWRFRPPLWLVRQPHDQLQJDQ\64/FRPPDQG\RXLVVXHWKDW
FKDQJHVGDWDZLOOLPPHGLDWHO\FRPPLW(DFKFRPPDQGLVLWVRZQWUDQVDFWLRQ
DQGLVLUUHYHUVLEOH,I\RXDUHUXQQLQJDODUJHEDWFKRISUHFDULRXVXSGDWHV\RX
PD\ZDQWDVDIHW\QHW6WDUWE\WXUQLQJRIIDXWRFRPPLW\set AUTOCOMMIT off
1RZ\RXKDYHWKHRSWLRQWRUROOEDFN\RXUVWDWHPHQWV
$XWRFRPPLW&RPPDQGV
count
-------73
(1 row)
Time: 18.650 ms
:KHQHQDEOHGHDFKTXHU\\RXUXQZLOOUHSRUWWKHGXUDWLRQDWWKHHQG)RU
H[DPSOHZLWK\timing onH[HFXWLQJSELECT COUNT(*) FROM pg_tables;
RXWSXWV
<RXPD\ILQGLWLQVWUXFWLYHWRKDYHSVTORXWSXWWKHWLPHLWWRRNIRUHDFKTXHU\WR
H[HFXWH8VHWKH\timingFRPPDQGWRWRJJOHLWRQDQGRII
7LPLQJ([HFXWLRQV
postgres@localhost:5442 postgis_book#
6KRXOGZHVZLWFKWRDQRWKHUGDWDEDVHXVLQJ\connect postgis_bookRXU
SURPSWFKDQJHVWR
:LQGRZVGRHVQRWVWRUHWKHFRPPDQGKLVWRU\XQOHVV\RXߞUHUXQQLQJD/LQX[8QL[YLUWXDO
HQYLURQPHQWVXFKDV&\JZ\Q0LQJ:RU06<6
:$51,1*
\set HISTFILE ~/.psql_history - :DBNAME
,I\RXVSHQWWLPHEXLOGLQJDQGWHVWLQJDGLIILFXOWTXHU\RUSHUIRUPLQJDVHULHVRI
LPSRUWDQWXSGDWHV\RXPD\ZDQWWRKDYHWKHKLVWRU\RIFRPPDQGVSLSHGLQWR
VHSDUDWHILOHVIRUSHUXVDOODWHU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4XHU\LQJ2WKHU7DEXODU)RUPDWVZLWKRJUBIGZ
7KHH[WHQVLRQVRSWLRQLVDFRPPDVHSDUDWHGOLVWRIH[WHQVLRQVLQVWDOOHGRQWKH
IRUHLJQVHUYHU:KHQ3RVWJUH64/UXQVDTXHU\LQYROYLQJDQ\RIWKHW\SHVRU
IXQFWLRQVGHILQHGLQWKHH[WHQVLRQLQD:+(5(FODXVHLWZLOOWU\WRSXVKWKH
IXQFWLRQFDOOVWRWKHUHPRWHVHUYHUIRULPSURYHGSHUIRUPDQFH,IWKHH[WHQVLRQV
RSWLRQLVQRWVSHFLILHGDOOH[WHQVLRQIXQFWLRQVZLOOEHUXQORFDOO\ZKLFKPD\
UHTXLUHWUDQVIHUULQJPRUHGDWD
ALTER SERVER census(OPTION ADD extensions 'btree_gist, pg_trgm');
,I\RXWDNHDGYDQWDJHRI3RVWJUH64/H[WHQVLRQV\RXߞOOZDQWWRXVHWKH
SHUIRUPDQFHHQKDQFHPHQWIRUHLJQVHUYHURSWLRQLQWURGXFHGLQYHUVLRQFDOOHG
H[WHQVLRQV7RXWLOL]HLWDGGWKHRSWLRQWRDQH[LVWLQJSRVWJUHVBIGZVHUYHUDVZH
GRLQWKHIROORZLQJH[DPSOH
$FRPSDQLRQFODXVHWRLIMIT TOLVWKHEXCEPTFODXVH,QVWHDGRIEULQJLQJLQ
WDEOHVOLVWHGLWEULQJVLQWDEOHVQRWOLVWHG
IRUHLJQWDEOHV\RXH[SHFWHG
&UHDWHWDEOHDQGGRILUVWLQVHUW
,QVHUWHYHU\ILYHVHFRQGV
SELECT * INTO log_activity
FROM pg_stat_activity;
INSERT INTO log_activity
SELECT * FROM pg_stat_activity; \watch 5
([DPSOH/RJWUDIILFHYHU\ILYHVHFRQGV
$OWKRXJK\watchLVSULPDULO\IRUPRQLWRULQJTXHU\RXWSXW\RXFDQXVHLWWR
H[HFXWHVWDWHPHQWVDWIL[HGLQWHUYDOV,Q([DPSOHZHILUVWFUHDWHDWDEOH
XVLQJEXONLQVHUWV\QWD[ DQGWKHQORJDFWLYLW\HYHU\ILYHVHFRQGVDIWHU2QO\WKH
ODVWVWDWHPHQWWKDWGRHVWKHLQVHUWLVUHSHDWHGHYHU\ILYHVHFRQGV
SELECT datname, query
FROM pg_stat_activity
WHERE state = 'active' AND pid != pg_backend_pid();
\watch 10
([DPSOH:DWFKLQJFRQQHFWLRQWUDIILFHYHU\VHFRQGV
7KH\watchFRPPDQGKDVEHHQLQSVTOVLQFH3RVWJUH64/8VHLWWR
UHSHDWHGO\UXQDQ64/VWDWHPHQWDWIL[HGLQWHUYDOVVR\RXFDQPRQLWRUWKH
RXWSXW)RUH[DPSOHVXSSRVH\RXZDQWWRNHHSWDEVRQTXHULHVWKDWKDYH\HWWR
FRPSOHWH7DJWKHZDWFKFRPPDQGWRWKHHQGRIWKHTXHU\DVVKRZQLQ
([DPSOH
:DWFKLQJ6WDWHPHQWV
,QSVTO\RXFDQFDOORXWWRWKH26VKHOOZLWKWKH\!FRPPDQG/HWߞVVD\\RXߞUH
RQ:LQGRZVDQGQHHGDGLUHFWRU\OLVWLQJ,QVWHDGRIH[LWLQJSVTORURSHQLQJ
DQRWKHUZLQGRZ\RXFDQMXVWW\SH\! dirDWWKHSVTOSURPSW
([HFXWLQJ6KHOO&RPPDQGV
,QWKLVVHFWLRQZHFRYHUKHOSIXOIHDWXUHWWHVEXULHGLQVLGHWKHSVTO
GRFXPHQWDWLRQ
SVTO*HPV
1HZLQ3RVWJUH64/SVTOLVWKH\crosstabviewFRPPDQGZKLFKJUHDWO\
VLPSOLILHVFURVVWDETXHULHV7KLVODERUVDYLQJFRPPDQGLVDYDLODEOHRQO\LQWKH
SVTOHQYLURPHQW:HߞOOLOOXVWUDWHZLWKDQH[DPSOHLQ([DPSOHIROORZLQJLW
&URVVWDEV
\d+ pg_ts_dict
Table "pg_catalog.pg_ts_dict"
Column
| Type | Modifiers | Storage | Stats target | Description
---------------+------+-----------+----------+--------------+-----------dictname
| name | not null | plain
|
|
dictnamespace | oid | not null | plain
|
|
dictowner
| oid | not null | plain
|
|
dicttemplate
| oid | not null | plain
|
|
dictinitoption | text |
| extended |
|
Indexes:
"pg_ts_dict_dictname_index" UNIQUE, btree (dictname, dictnamespace)
"pg_ts_dict_oid_index" UNIQUE, btree (oid)
Has OIDs: yes
([DPSOH'HVFULEHREMHFWZLWK?G
,I\RXQHHGIXUWKHUGHWDLORQDSDUWLFXODUREMHFWXVHWKH\d+FRPPDQGDVVKRZQ
LQ([DPSOH
\dt+ pg_catalog.pg_t*
Schema
| Name
| Type | Owner
| Size | Description
-----------+------------------+-------+----------+--------+-----------pg_catalog | pg_tablespace
| table | postgres | 40 kB |
pg_catalog | pg_trigger
| table | postgres | 16 kB |
pg_catalog | pg_ts_config
| table | postgres | 40 kB |
pg_catalog | pg_ts_config_map | table | postgres | 48 kB |
pg_catalog | pg_ts_dict
| table | postgres | 40 kB |
pg_catalog | pg_ts_parser
| table | postgres | 40 kB |
pg_catalog | pg_ts_template | table | postgres | 40 kB |
pg_catalog | pg_type
| table | postgres | 112 kB |
([DPSOH/LVWWDEOHVZLWK?GW
9DULRXVSVTOGHVFULEHFRPPDQGVOLVWGDWDEDVHREMHFWVDORQJZLWKGHWDLOV
([DPSOHGHPRQVWUDWHVKRZWROLVWDOOWDEOHVDQGWKHLUVL]HVRQGLVNLQWKH
SJBFDWDORJVFKHPDWKDWEHJLQVZLWKWKHOHWWHUVpg_t
5HWULHYLQJ'HWDLOVRI'DWDEDVH2EMHFWV
7RNLOODZDWFKXVHCTRL-X CTRL-C
,IDWDEOHVSHFLILHGLQWKHLIMIT TOGRHVQRWH[LVWRQWKHUHPRWHVHUYHUQRHUURU
ZLOOEHWKURZQ<RXPLJKWZDQWWRYHULI\DIWHUWKHLPSRUWWKDW\RXKDYHDOOWKH
IMPORT FOREIGN SCHEMA census
LIMIT TO (facts, lu_fact_types)
FROM SERVER book_server INTO remote_census;
7REULQJLQRQO\DVXEVHWRIWDEOHVXVHLIMIT TORUEXCEPTPRGLILHUV)RU
H[DPSOHWREULQJLQMXVWWKHfactsDQGlu_fact_typesWDEOHVZHFRXOGKDYH
ZULWWHQ
7KHIMPORT FOREIGN SCHEMADVVKRZQLQ([DPSOHZLOOFUHDWHIRUHLJQ
WDEOHVZLWKWKHVDPHQDPHVDVWKRVHLQWKHIRUHLJQVFKHPDDQGFUHDWHWKHPLQWKH
GHVLJQDWHGVFKHPDremote_census
CREATE SCHEMA remote_census;
IMPORT FOREIGN SCHEMA public
FROM SERVER book_server
INTO remote_census
OPTIONS (import_default 'true');
([DPSOH8VH,03257)25(,*16&+(0$WROLQNDOOWDEOHVLQDVFKHPD
,Q([DPSOHZHLPSRUWDOOWDEOHVLQRXUERRNVSXEOLFVFKHPD
7KLVFRQWUROVZKHWKHU12718//FRQVWUDLQWVDUHLPSRUWHG7KHGHIDXOWLV
true
import_not_null
7KLVFRQWUROVZKHWKHUGHIDXOWYDOXHVIRUFROXPQVVKRXOGEHLQFOXGHG7KH
GHIDXOWIRUWKHRSWLRQLVfalseVRFROXPQVRQWKHORFDOVHUYHUKDYHQR
GHIDXOWV%XWGHIDXOWYDOXHVDUHXVHIXOGXULQJLQVHUWVLI\RXQHJOHFWWR
VSHFLI\WKHYDOXHRIDFROXPQ3RVWJUH64/DXWRPDWLFDOO\LQVHUWVWKHGHIDXOW
%HFDUHIXOWKRXJKߚWKHEHKDYLRURIGHIDXOWFRXOGEHXQH[SHFWHGLI\RXߞUH
UHO\LQJRQDVHTXHQFHIRUDXWRQXPEHULQJ7KHQH[WDVVLJQHGYDOXHIURPWKH
VHTXHQFHFRXOGEHGLIIHUHQWEHWZHHQWKHIRUHLJQVHUYHUDQGWKHORFDOVHUYHU
import_default
7KLVFRSLHVWKHFROODWLRQVHWWLQJVIURPWKHIRUHLJQVHUYHUIRUWKHIRUHLJQ
WDEOHV7KHGHIDXOWIRUWKLVVHWWLQJLVtrue
import_collate
3RVWJUH64/LQWURGXFHGWKHIMPORT FOREIGN SCHEMAFRPPDQGZKLFK
VDYHVDJUHDWGHDORIWLPHE\DXWRPDWLFDOO\FUHDWLQJWKHIRUHLJQWDEOHVIRU\RX
1RWDOO)':VVXSSRUW,03257)25(,*16&+(0$(DFK)':FDQDOVR
VXSSRUWDFXVWRPVHWRIVHUYHURSWLRQVZKHQLPSRUWLQJpostgres_fdwVXSSRUWV
WKHIROORZLQJFXVWRPRSWLRQV
,QDGGLWLRQWRFKDQJLQJOPTIONS\RXFDQDOVRDGGDQGGURSFROXPQVZLWKWKH
ALTER FOREIGN TABLE .. DROP COLUMNVWDWHPHQW
7KHupdatableSURSHUW\DWWKHWDEOHOHYHORYHUULGHVWKHIRUHLJQVHUYHUVHWWLQJ
ALTER FOREIGN TABLE ft_facts OPTIONS (SET updatable 'true');
<RXFDQVHWWKHWDEOHEDFNWRupdatableE\UXQQLQJ
ALTER FOREIGN TABLE ft_facts OPTIONS (ADD updatable 'false');
7KLVH[DPSOHLQFOXGHVRQO\WKHPRVWEDVLFRSWLRQVIRUWKHIRUHLJQWDEOH%\
GHIDXOWDOO3RVWJUH64/IRUHLJQWDEOHVDUHXSGDWDEOHXQOHVVWKHUHPRWHDFFRXQW
\RXXVHGRHVQߞWKDYHXSGDWHDFFHVV7KHupdatableVHWWLQJLVD%RROHDQVHWWLQJ
WKDWFDQEHFKDQJHGDWWKHIRUHLJQWDEOHRUWKHIRUHLJQVHUYHUGHILQLWLRQ)RU
H[DPSOHWRPDNH\RXUWDEOHUHDGRQO\H[HFXWH
CREATE FOREIGN TABLE ft_facts (
fact_type_id int NOT NULL,
tract_id varchar(11),
yr int, val numeric(12,3),
perc numeric(6,2)
)
SERVER book_server OPTIONS (schema_name 'census', table_name 'facts');
([DPSOH'HILQLQJD3RVWJUH64/IRUHLJQWDEOH
1RZ\RXDUHUHDG\WRFUHDWHDIRUHLJQWDEOH7KLVWDEOHFDQKDYHDVXEVHWRI
FROXPQVRIWKHWDEOHLWFRQQHFWVWR,Q([DPSOHZHFUHDWHDIRUHLJQWDEOH
W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FRQWUROVRUWLQJ7KHFURVVWDEXODWLRQRXWSXWVDWDEOH
ZKHUHWKHILUVWFROXPQVHUYHVDVDURZKHDGHUWKHVHFRQGFROXPQDVDFROXPQ
KHDGHUDQGWKHODVWDVWKHYDOXHWKDWJRHVLQHDFKFHOO<RXFDQDOVRRPLWWKH
FROXPQQDPHVIURPWKH\crosstabviewFRPPDQGLQZKLFKFDVHWKHSELECT
VWDWHPHQWPXVWUHTXHVWH[DFWO\WKUHHFROXPQVXVHGLQRUGHUIRUWKHFURVV
WDEXODWLRQ
SELECT student, subject, AVG(score)::numeric(5,2) As avg_score
FROM test_scores
GROUP BY student, subject
ORDER BY student, subject
\crosstabview student subject avg_score
student | algebra | calculus | chemistry | physics | scheme
---------+---------+----------+-----------+---------+-------alex
| 74.00 |
73.50 |
82.00 | 81.00 |
leo
| 82.00 |
65.50 |
75.50 | 72.00 |
regina | 72.50 |
64.50 |
73.50 | 84.00 | 90.00
sonia | 76.50 |
67.50 |
84.00 | 72.00 |
(4 rows)
([DPSOH&URVVWDEYLHZ
ZLWKDQH[SODQDWLRQ
SVTOKDVD\copyFRPPDQGWKDWOHWV\RXLPSRUWGDWDIURPDQGH[SRUWGDWDWRD
WH[WILOH7KHWDELVWKHGHIDXOWGHOLPLWHUEXW\RXFDQVSHFLI\RWKHUV1HZOLQH
EUHDNVPXVWVHSDUDWHWKHURZV)RURXUILUVWH[DPSOHZHGRZQORDGHGGDWDIURP
86&HQVXV)DFW)LQGHUFRYHULQJUDFLDOGHPRJUDSKLFVRIKRXVLQJLQ
0DVVDFKXVHWWV<RXFDQGRZQORDGWKHILOHZHXVHLQWKLVH[DPSOH
'(&BB6)B47+BZLWKBDQQFVYIURPWKH3RVWJUH64/%RRN'DWD
,PSRUWLQJDQG([SRUWLQJ'DWD
table_name | count
-----------+-----regina
| 1
(1 row)
SELECT
'SELECT ' || quote_literal(table_name) || ' AS table_name,
COUNT(*) As count FROM ' || quote_ident(table_name) AS cnt_q
FROM information_schema.tables
WHERE table_name IN ('leo','regina') \gexec
table_name | count
-----------+-----leo
| 1
(1 row)
([DPSOH8VLQJJH[HFWRUHWULHYHFRXQWVRIUHFRUGVLQHDFKWDEOH
,QWKHQH[WH[DPSOHZHXVHgexecWRREWDLQPHWDGDWDE\TXHU\LQJ
information_schema
SELECT
'CREATE TABLE ' || person.name || '( a integer, b integer)' As create,
'INSERT INTO ' || person.name || ' VALUES(1,2) ' AS insert
FROM (VALUES ('leo'),('regina')) AS person (name) \gexec
CREATE TABLE
INSERT 0 1
CREATE TABLE
INSERT 0 1
([DPSOH8VLQJJH[HFWRFUHDWHWDEOHVDQGLQVHUWGDWD
GLVFHUQZKHWKHUHDFKFHOOFRQWDLQVDOHJLWLPDWH64/JH[HFLVDOVRREOLYLRXVWR
WKHUHVXOWRIWKH64/H[HFXWLRQ6KRXOGWKH64/ZLWKLQDSDUWLFXODUFHOOWKURZDQ
HUURUJH[HFPHUULO\WUHDGVDORQJ+RZHYHULWVNLSVRYHUQXOOV([DPSOH
FUHDWHVWZRWDEOHVDQGLQVHUWVRQHURZLQHDFKWDEOHXVLQJWKH?JH[HFFRPPDQG
CREATE USER MAPPING FOR public SERVER book_server
OPTIONS (user 'role_on_foreign', password 'your_password');
1H[WFUHDWHDXVHUPDSSLQJLWVSXEOLFUROHWRDUROHRQWKHIRUHLJQVHUYHU
&KDQJHVWRFRQQHFWLRQVHWWLQJVVXFKDVWKHKRVWSRUWDQGGDWDEDVHGRQRWWDNHHIIHFWXQWLOD
QHZVHVVLRQLVFUHDWHG7KLVLVEHFDXVHWKHFRQQHFWLRQLVRSHQHGRQILUVWXVHDQGLVNHSWRSHQ
:$51,1*
ALTER SERVER book_server OPTIONS (SET host 'prod');
,I\RXQHHGWRFKDQJHRUDGGFRQQHFWLRQRSWLRQVWRWKHIRUHLJQVHUYHUDIWHU
FUHDWLRQ\RXFDQXVHWKHALTER SERVERFRPPDQG)RUH[DPSOHLI\RXQHHGHG
WRFKDQJHWKHVHUYHU\RXDUHSRLQWLQJWR\RXFRXOGHQWHU
CREATE SERVER book_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'localhost', port '5432', dbname 'postgresql_book');
1H[WFUHDWHDIRUHLJQVHUYHU
CREATE EXTENSION postgres_fdw;
6WDUWE\LQVWDOOLQJWKH)':IRUWKH3RVWJUH64/VHUYHULQDQHZGDWDEDVH
7KH3RVWJUH64/)':postgres_fdwLVSDFNDJHGZLWKPRVWGLVWULEXWLRQVRI
3RVWJUH64/VLQFH3RVWJUH64/7KLV)':DOORZV\RXWRUHDGDVZHOODVSXVK
XSGDWHVWRRWKHU3RVWJUH64/VHUYHUVHYHQGLIIHUHQWYHUVLRQV
4XHU\LQJ2WKHU3RVWJUH64/6HUYHUV
SELECT x[1] As geo_id, x[2] As tract_id
FROM staging.factfinder_array WHERE x[1] ~ '[0-9]+';
7KLVQH[WTXHU\ZLOOJLYHXVWKHILUVWWZRFROXPQVRIRXUGDWD
SELECT unnest(x) FROM staging.factfinder_array WHERE x[1] = 'GEO.id'
2XUH[DPSOH&69EHJLQVZLWKHLJKWKHDGHUURZVDQGKDVPRUHFROXPQVWKDQZH
FDUHWRFRXQW:KHQWKHVHWXSLVILQLVKHG\RXFDQILQDOO\TXHU\RXUGHOLPLWHGILOH
GLUHFWO\7KHIROORZLQJTXHU\ZLOOJLYHXVWKHQDPHVRIWKHKHDGHUURZVZKHUH
WKHILUVWFROXPQRIWKHKHDGHULVGEO.id
CREATE FOREIGN TABLE staging.factfinder_array (x text[])
SERVER file_taserver
OPTIONS (
format 'csv',
filename '/postgresql_book/ch10/DEC_10_SF1_QTH1_with_ann.csv',
header 'false',
delimiter ',',
quote '"',
encoding 'latin1',
null ''
);
([DPSOH0DNHDILOHWH[WDUUD\IRUHLJQWDEOHIURPDGHOLPLWHGILOH
1H[WUHJLVWHUWKHWDEOHV<RXFDQSODFHIRUHLJQWDEOHVLQDQ\VFKHPD\RXZDQW
,Q([DPSOHZHXVHRXUVWDJLQJVFKHPDDJDLQ
CREATE SERVER file_taserver FOREIGN DATA WRAPPER file_textarray_fdw;
7KHQFUHDWHDIRUHLJQVHUYHUDV\RXZRXOGZLWKDQ\)':
CREATE EXTENSION file_textarray_fdw;
7KHILUVWVWHSWRSHUIRUPDIWHU\RXKDYHLQVWDOOHGDQ)':LVWRFUHDWHDQ
H[WHQVLRQLQ\RXUGDWDEDVH
,I\RXDUHRQ/LQX[8QL[LWߞVDQHDV\FRPSLOHLI\RXKDYHWKHpostgresql-dev
SDFNDJHLQVWDOOHG:HGLGWKHZRUNRIFRPSLOLQJIRU:LQGRZV\RXFDQ
GRZQORDGRXUELQDULHVIURPRQHRIWKHIROORZLQJOLQNVRQHIRU:LQGRZV
)':VDQGDQRWKHUIRU:LQGRZVDQG)':V
8QIRUWXQDWHO\file_textarray_fdwLVQRWSDUWRIWKHFRUH3RVWJUH64/VR
\RXߞOOQHHGWRFRPSLOHLW\RXUVHOI)LUVWLQVWDOO3RVWJUH64/ZLWK3RVWJUH64/
GHYHORSPHQWKHDGHUV7KHQGRZQORDGWKHfile_textarray_fdwVRXUFHFRGH
IURPWKH$GXQVWDQ*LW+XEVLWH7KHUHLVDGLIIHUHQWEUDQFKIRUHDFKYHUVLRQRI
3RVWJUH64/VRPDNHVXUHWRSLFNWKHULJKWRQH2QFH\RXߞYHFRPSLOHGWKHFRGH
LQVWDOOLWDVDQH[WHQVLRQDV\RXZRXOGDQ\RWKHU)':
'XULQJLPSRUW\RXFDQUHSODFHQXOOYDOXHVZLWKVRPHWKLQJRI\RXURZQ
FKRRVLQJE\DGGLQJDNULL ASDVLQWKHIROORZLQJ
\copy sometable FROM somefile.txt DELIMITER '|';
,I\RXUILOHKDVQRQVWDQGDUGGHOLPLWHUVVXFKDVSLSHVLQGLFDWHWKHGHOLPLWHUDV
IROORZV
,Q([DPSOHZHODXQFKLQWHUDFWLYHSVTOFRQQHFWWRRXUGDWDEDVHXVH\cdWR
FKDQJHWKHFXUUHQWGLUHFWRU\WRWKHIROGHUFRQWD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WRDFFHSWWKHLQFRPLQJGDWD:HWKHQZULWHH[SORUDWLYHTXHULHVWR
JHWDVHQVHRIZKDWZHKDYHRQRXUKDQGV)LQDOO\ZHGLVWULEXWHWKHGDWDLQWR
YDULRXVQRUPDOL]HGSURGXFWLRQWDEOHVDQGGHOHWHWKHVWDJLQJVFKHPD
SVTO,PSRUW
WHERE s01 ~ E'^[0-9]+' )
6LQFH3RVWJUH64/SVTOFDQIHWFKGDWDIURPWKHRXWSXWRIFRPPDQGOLQH
SURJUDPVVXFKDVFXUOOVDQGZJHWDQGGXPSWKHGDWDLQWRDWDEOH([DPSOH
LPSRUWVDGLUHFWRU\OLVWLQJXVLQJDGLUFRPPDQG
&RS\LQJIURPRUWR3URJUDP
FORCE QUOTE *GRXEOHTXRWHVDOOFROXPQV)RUFODULW\ZHVSHFLILHGWKHTXRWLQJ
FKDUDFWHUHYHQWKRXJKSVTOGHIDXOWVWRGRXEOHTXRWHV
\connect postgresql_book
\copy staging.factfinder_import TO '/test.csv'
WITH CSV HEADER QUOTE '"' FORCE QUOTE *
([DPSOH([SRUWLQJGDWDZLWKSVTO
7KHGHIDXOWEHKDYLRURIH[SRUWLQJGDWDZLWKRXWTXDOLILFDWLRQVLVWRH[SRUWWRD
WDEGHOLPLWHGILOH+RZHYHUWKHWDEGHOLPLWHGIRUPDWGRHVQRWH[SRUWKHDGHU
FROXPQV<RXFDQXVHWKHHEADERRSWLRQRQO\ZLWKWKHFRPPDGHOLPLWHGIRUPDW
VHH([DPSOH \connect postgresql_book
\copy (SELECT * FROM staging.factfinder_import
TO '/test.tab'
WITH DELIMITER E'\t' CSV HEADER
([DPSOH([SRUWLQJGDWDZLWKSVTO
([SRUWLQJGDWDLVHYHQHDVLHUWKDQLPSRUWLQJ<RXFDQHYHQH[SRUWVHOHFWHGURZV
IURPDWDEOH8VHWKHSVTO\copyFRPPDQGWRH[SRUW([DPSOH
GHPRQVWUDWHVKRZWRH[SRUWWKHGDWDZHMXVWORDGHGEDFNWRDWDEGHOLPLWHGILOH
SVTO([SRUW
'RQߞWFRQIXVHWKH\copyFRPPDQGLQSVTOZLWKWKHCOPYVWDWHPHQWSURYLGHGE\WKH64/
ODQJXDJH%HFDXVHSVTOLVDFOLHQWXWLOLW\DOOSDWKVDUHLQWHUSUHWHGUHODWLYHWRWKHFRQQHFWHG
FOLHQW7KH64/FRS\LVVHUYHUEDVHGDQGUXQVXQGHUWKHFRQWH[WRIWKHSRVWJUHVVHUYLFH26
DFFRXQW7KHLQSXWILOHIRUDQ64/FRS\PXVWUHVLGHLQDSDWKDFFHVVLEOHE\WKHSRVWJUHV
VHUYLFHDFFRXQW
:$51,1*
\copy sometable FROM somefile.txt NULL As '';
2IWHQIODWILOHVKDYHDGLIIHUHQWQXPEHURIFROXPQVRQHDFKOLQHDQGFRXOG
LQFOXGHPXOWLSOHKHDGHUDQGIRRWHUURZV2XUIDYRULWH)':IRUKDQGOLQJWKHVH
ILOHVLVfile_textarray_fdw7KLVZUDSSHUFDQKDQGOHDQ\NLQGRIGHOLPLWHG
IODWILOHHYHQLIWKHQXPEHURIHOHPHQWVYDU\IURPURZWRURZE\WUHDWLQJHDFK
URZDVDWH[WDUUD\ text[] 4XHU\LQJ)ODW)LOHVDV-DJJHG$UUD\V
DROP FOREIGN TABLE staging.devs;
2QFH\RXQRORQJHUQHHGWKHIRUHLJQWDEOHGURSLWXVLQJ
SELECT * FROM staging.devs WHERE developer LIKE 'T%';
:KHQWKHVHWXSLVILQLVKHG\RXFDQILQDOO\TXHU\\RXUSLSHGHOLPLWHGILOH
GLUHFWO\
,QRXUH[DPSOHHYHQWKRXJKZHߞUHUHJLVWHULQJDSLSHGHOLPLWHGILOHZHVWLOOXVH
WKHcsvRSWLRQ$&69ILOHDVIDUDV)':LVFRQFHUQHGUHSUHVHQWVDILOH
GHOLPLWHGE\DQ\VSHFLILHGFKDUDFWHU
CREATE FOREIGN TABLE staging.devs (developer VARCHAR(150), company VARCHAR(150))
SERVER my_server
OPTIONS (
format 'csv',
header 'true',
filename '/postgresql_book/ch10/devs.psv',
delimiter '|',
null ''
);
([DPSOH0DNHDIRUHLJQWDEOHIURPDGHOLPLWHGILOH
Dev|Company
Tom Lane|Crunchy Data
Bruce Momjian|EnterpriseDB
+HUHDUHDIHZLQLWLDOOLQHVRIWKHSLSHGHOLPLWHGILOHZHDUHOLQNLQJWRWRVKRZ
WKHIRUPDWRIWKHGDWDZHDUHWDNLQJLQ
\RXZDQW:HXVXDOO\FUHDWHDVHSDUDWHVFKHPDWRKRXVHIRUHLJQGDWD)RUWKLV
H[DPSOHZHߞOOXVHRXUVWDJLQJVFKHPDDVVKRZQLQ([DPSOH
1H[W\RXPXVWUHJLVWHUWKHWDEOHV<RXFDQSODFHIRUHLJQWDEOHVLQDQ\VFKHPD
CREATE SERVER my_server FOREIGN DATA WRAPPER file_fdw;
$OWKRXJKfile_fdwFDQUHDGRQO\IURPILOHSDWKVDFFHVVLEOHE\\RXUORFDO
VHUYHU\RXVWLOOQHHGWRGHILQHDVHUYHUIRULWIRUWKHVDNHRIFRQVLVWHQF\,VVXH
WKHIROORZLQJFRPPDQGWRFUHDWHDߡIDX[ߢIRUHLJQVHUYHULQ\RXUGDWDEDVH
CREATE EXTENSION file_fdw;
7KHfile_fdwZUDSSHULVSDFNDJHGDVDQH[WHQVLRQ7RLQVWDOOLWXVHWKH
IROORZLQJ64/
4XHU\LQJ)ODW)LOHV
,QWKLVVHFWLRQZHߞOOGHPRQVWUDWHKRZWRUHJLVWHUIRUHLJQVHUYHUVIRUHLJQXVHUV
DQGIRUHLJQWDEOHVDQGILQDOO\KRZWRTXHU\IRUHLJQWDEOHV$OWKRXJKZHXVH
64/WRFUHDWHDQGGHOHWHREMHFWVLQRXUH[DPSOHV\RXFDQSHUIRUPWKHH[DFW
VDPHFRPPDQGVXVLQJSJ$GPLQ
8SWKURXJK3RVWJUH64/\RXFRXOGXVH)':VRQO\WRUHDGIURPIRUHLJQ
VRXUFHV9HUVLRQLQWURGXFHGDQ$3,IHDWXUHWRXSGDWHIRUHLJQWDEOHVDVZHOO
postgres_fdwVXSSRUWVXSGDWHV
0RVW3RVWJUH64/LQVWDOOVSURYLGHWZR)':V\RXFDQLQVWDOOfile_fdwDQG
postgres_fdwXVLQJWKH&5($7((;7(16,21FRPPDQG
UHVLGHVRQDQRWKHUGDWDEDVHSHUKDSVHYHQRQDQRWKHUSK\VLFDOVHUYHU2QFH\RX
SXWLQWKHHIIRUWWRHVWDEOLVKIRUHLJQWDEOHVWKH\SHUVLVWLQ\RXUGDWDEDVHDQG
\RXߞUHIRUHYHUIUHHIURPKDYLQJWRZRUU\DERXWWKHLQWULFDWHSURWRFROVRI
FRPPXQLFDWLQJZLWKDOLHQGDWDVRXUFHV<RXFDQDOVRILQGWKHVWDWXVRISRSXODU
)':VDQGH[DPSOHVRIXVDJHDW3RVWJUH64/:LNL)':<RXFDQILQGDFDWDORJ
RIVRPH)':VIRU3RVWJUH64/DW3*;1)':DQG3*;1)RUHLJQ'DWD
:UDSSHU<RXߞOOILQGWKHVRXUFHFRGHIRUPDQ\RIWKHVHDQGIRUDGGLWLRQDORQHV
RQ*LW+XEE\VHDUFKLQJIRU3RVWJUH64/)RUHLJQ'DWD:UDSSHUV,I\RXQHHGWR
ZUDSIRUHLJQGDWDVRXUFHVVWDUWE\YLVLWLQJWKHVHOLQNVWRVHHZKHWKHUVRPHRQH
KDVDOUHDG\GRQHWKHZRUNRIFUHDWLQJZUDSSHUV,IQRWWU\FUHDWLQJRQH\RXUVHOI
,I\RXVXFFHHGEHVXUHWRVKDUHLWZLWKRWKHUV
\o settings_report.html
\T 'cellspacing=0 cellpadding=0'
\qecho '<html><head><style>H2{color:maroon}</style>'
\qecho '<title>PostgreSQL Settings</title></head><body>'
([DPSOH6FULSWWRJHQHUDWHUHSRUW
1RWWRRVKDEE\%XWWKHFRPPDQGRXWSXWVRQO\DQ+70/WDEOHQRWDIXOO\
TXDOLILHG+70/GRFXPHQW7RFUHDWHDPHDWLHUUHSRUWFRPSRVHDVFULSWDV
VKRZQLQ([DPSOH
)LJXUH0LQLPDOLVW+70/UHSRUW
psql -d postgresql_book -H -c "
SELECT category, COUNT(*) As num_per_cat
FROM pg_settings
WHERE category LIKE '%Query%'
GROUP BY category
ORDER BY category;
" -o test.html
%HOLHYHLWRUQRWSVTOLVFDSDEOHRISURGXFLQJEDVLF+70/UHSRUWV7U\WKH
IROORZLQJDQGFKHFNRXWWKHJHQHUDWHGRXWSXWVKRZQLQ)LJXUH
%DVLF5HSRUWLQJ
+XEHUW/XEDF]HZVNLKDVPRUHH[DPSOHVRIXVLQJ\copy9LVLW'HSHV]3LSLQJ
FRS\WRIURPDQH[WHUQDOSURJUDP
\connect postgresql_book
CREATE TABLE dir_list (filename text);
\copy dir_list FROM PROGRAM 'dir C:\projects /b'
([DPSOH,PSRUWGLUHFWRU\OLVWLQJZLWKSVTO
([DPSOHGHPRQVWUDWHVWKDWE\LQWHUVSHUVLQJ64/DQGSVTOFRPPDQGV\RX
FDQFUHDWHDFRPSUHKHQVLYHWDEXODUUHSRUWUHSOHWHZLWKVXEUHSRUWV5XQ
([DPSOHE\FRQQHFWLQJLQWHUDFWLYHO\ZLWKSVTODQGH[HFXWLQJ\i
settings_report.psql$OWHUQDWLYHO\UXQSVTOQRQLQWHUDFWLYHO\E\H[HFXWLQJ
psql -f settings_report.psqlIURP\RXU26FRPPDQGOLQH7KHRXWSXW
JHQHUDWHGE\VHWWLQJVBUHSRUWKWPOLVVKRZQLQ)LJXUH
5HGLUHFWVTXHU\RXWSXWWRDILOH
&66WDEOHVHWWLQJVIRUTXHU\RXWSXW
$SSHQGVDGGLWLRQDO+70/
([SDQGPRGH5HSHDWVWKHFROXPQKHDGHUVIRUHDFKURZDQGRXWSXWVHDFK
FROXPQRIHDFKURZDVDVHSDUDWHURZ
)RUFHVWKHTXHULHVWRRXWSXWDVDQ+70/WDEOH
string_agg()LQWURGXFHGLQ3RVWJUH64/FRQFDWHQDWHVDOOSURSHUWLHVLQ
WKHVDPHFDWHJRU\LQWRDVLQJOHFROXPQ
7XUQVRIIH[SDQGPRGH7KHVHFRQGDQGWKLUGTXHULHVVKRXOGRXWSXWRQHURZ
SHUWDEOHURZ
7RJJOHVWXSOHVPRGH:KHQRQFROXPQKHDGHUVDQGURZFRXQWVDUHRPLWWHG
\qecho '<table><tr valign=''top''><td><h2>Planner Settings</h2>'
\x on
\t on
\pset format html
SELECT category,
string_agg(name || '=' || setting, E'\n' ORDER BY name) As settings
FROM pg_settings
WHERE category LIKE '%Planner%'
GROUP BY category
ORDER BY category;
\H
\qecho '</td><td><h2>File Locations</h2>'
\x off
\t on
\pset format html
SELECT name, setting FROM pg_settings WHERE category = 'File Locations'
ORDER BY name;
\qecho '<h2>Memory Settings</h2>'
SELECT name, setting, unit FROM pg_settings WHERE category ILIKE '%memory%'
ORDER BY name;
\qecho '</td></tr></table>'
\qecho '</body></html>'
\o
)':VDUHDQH[WHQVLEOHVWDQGDUGFRPSOLDQWPHWKRGIRU\RXU3RVWJUH64/VHUYHU
WRTXHU\RWKHUGDWDVRXUFHVERWKRWKHU3RVWJUH64/VHUYHUVDQGPDQ\W\SHVRI
QRQ3RVWJUH64/GDWDVRXUFHV$WWKHFHQWHURIWKHDUFKLWHFWXUHLVDIRUHLJQWDEOH
DWDEOHWKDW\RXFDQTXHU\OLNHRWKHUWDEOHVLQ\RXU3RVWJUH64/GDWDEDVHEXWWKDW
)RUHLJQ'DWD:UDSSHUV
,I\RXQRORQJHUQHHGDVXEVFULSWLRQRUSXEOLFDWLRQ\RXFDQGURSWKHPIURPWKH
SXEOLVKHUZLWK'52368%6&5,37,21DQG'52338%/,&$7,21
:KHQ\RXLQVSHFWWKHWDEOHVRQWKHERRNBVXEGDWDEDVH\RXVKRXOGILQGWKDWDOO
WKHWDEOHVDUHIXOORIGDWDFROOHFWHGGXULQJWKHLQLWLDOV\QFKURQL]DWLRQ,I\RXDGG
GDWDWRWKHSRVWJUHVTOBERRNGDWDEDVH\RXVKRXOGVHHWKHQHZUHFRUGVDSSHDURQ
WKHERRNBVXEGDWDEDVH
PUBLICATION full_db_pub;
user=postgres'
CONNECTION 'host=localhost port=5447 dbname=postgresql_book \
CREATE SUBSCRIPTION book_sub
\connect book_sub;
,QRUGHUWRXVHWKHSXEOLFDWLRQZHQHHGWRVXEVFULEHWRLW:HGRWKLVE\
H[HFXWLQJWKLVFRPPDQGZKHQFRQQHFWHGWRWKHVXEVFULEHUGDWDEDVH
ERRNBVXE
FOR ALL TABLES;
CREATE PUBLICATION full_db_pub
:HWKHQFUHDWHDSXEOLFDWLRQRQWKHSXEOLVKHUGDWDEDVHRILWHPVZHZDQWWR
UHSOLFDWH)RUWKLVH[HUFLVHZHߞOOUHSOLFDWHDOOWKHWDEOHVLQWKHGDWDEDVHXVLQJ
&5($7(38%/,&$7,211RWHWKDWWKLVFRPPDQGZLOODOVRUHSOLFDWHIXWXUH
WDEOHVFUHDWHGWKRXJKZHߞYHKDGWRFUHDWHWKHVWUXFWXUHRQWKHVXEVFULSWLRQ
GDWDEDVHV
\i pub_struct.sql
\connect book_sub;
CREATE DATABASE book_sub;
$QGWKHQXVHSVTORQWKHVXEVFULEHUVHUYHUWRFUHDWHRXUVXEVFULSWLRQGDWDEDVH
ZLWKVWUXFWXUHVDVIROORZV
-f pub_struct.sql postgresql_book
pg_dump -U postgres -p5447 -Fp --section pre-data --section post-data \
2QWKHGDWDEDVHZKHUH\RXZLOOEHUHSOLFDWLQJGDWDFUHDWHWKHWDEOHVWUXFWXUHV
IRUWDEOHV\RXZLOOEHUHSOLFDWLQJ,I\RXKDYHDORWRIWDEOHVRUZDQWWR
UHSOLFDWHDZKROHGDWDEDVHDVZHZLOOEHGRLQJXVHSJBGXPSRQWKH
SXEOLVKLQJGDWDEDVHWRFUHDWHEDFNXSVWUXFWXUHRIWDEOHV)RUH[DPSOHIRUWKH
SRVWJUHVTOBERRNGDWDEDVHZHZRXOGGXPSRXWWKHVWUXFWXUH
7KLVFDQEHVHWRQWKHVXEVFULSWLRQVHU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VGHSHQGLQJRQWKH26
trigger_file = 'failover.now'
application_name=replica1'
primary_conninfo = 'host=192.168.0.1 port=5432 user=pgrepuser password=woohoo
standby_mode = 'on'
7KHDSSOLFDWLRQBQDPHLVRSWLRQDOEXWXVHIXOLI\RXZDQWWRWUDFNWKHUHSOLFDLQ
SRVWJUHVTOV\VWHPYLHZV
&UHDWHDQHZILOHLQWKHGDWDIROGHUFDOOHGUHFRYHU\FRQIZLWKWKHIROORZLQJ
FRQWHQWVEXWVXEVWLWXWHWKHDFWXDOKRVWQDPH,3DGGUHVVDQGSRUWRI\RXU
PDVWHURQWKHVHFRQGOLQH7KLVILOHLVDXWRPDWLFDOO\FUHDWHGLI\RXXVHG
SJBEDVHEDFNXS<RXZLOOKDYHWRDGGWKHWULJJHUBILOHOLQHWKRXJK
<RXGRQߞWQHHGWRUXQWKHVODYHVRQWKHVDPHSRUWDVWKHPDVWHUVR\RXFDQ
RSWLRQDOO\FKDQJHWKHSRUWHLWKHUYLDSRVWJUHVTODXWRFRQISRVWJUHVTOFRQIRU
YLDVRPHRWKHU26VSHFLILFVWDUWXSVFULSWWKDWVHWVWKHPGPORTHQYLURQPHQW
YDULDEOHEHIRUHVWDUWXS
max_connections = 20 #set to higher or equal to master
hot_standby = on
$GGWKHIROORZLQJFRQILJXUDWLRQVHWWLQJWRWKHSRVWJUHVTODXWRFRQIILOH
2YHUZULWHWKHGDWDIROGHUILOHVZLWKWKRVH\RXJHQHUDWHGZLWKSJBEDVHEDFNXS
6KXWGRZQ3RVWJUH64/RQWKHQHZVODYH
LGHQWLFDOIRUPLFURYHUVLRQVLVQRWDUHTXLUHPHQWDQG\RXߞUHZHOFRPHWR
H[SHULPHQWDQGVHHKRZIDU\RXFDQGHYLDWH
SJ$GPLQXOWLPDWHO\LQWHUDFWVZLWK3RVWJUH64/YLD64/DQGLWߞVQRWVK\
DERXWOHWWLQJ\RXVHHWKHJHQHUDWHG64/:KHQ\RXXVHWKHJUDSKLFDO
LQWHUIDFHWRPD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WKH:$/ORJVDQGWKH-RVZLWFKWRDXWRPDWLFDOO\FUHDWHDFRQILJ
ILOH7KHFRPPDQG--xlog-method-streamZLOOVSDZQDQRWKHUGDWDEDVH
FRQQHFWLRQIRUFRS\LQJWKH:$/V
8VHWKHpg_basebackupXWLOLW\IRXQGLQWKHELQIROGHURI\RXU3RVWJUH64/
LQVWDOODWLRQWRFUHDWHDFOXVWHUEDFNXS7KLVZLOOFUHDWHDFRS\RIWKHGDWD
FOXVWHUILOHVLQWKHVSHFLILHGGLUHFWRU\
5HVWDUWWKH3RVWJUH64/VHUYLFHIRUWKHVHWWLQJVWRWDNHHIIHFW
host replication pgrepuser 192.168.0.0/24 md5
$GGDUXOHWRSJBKEDFRQIDOORZLQJWKHVODYHVWRUHSOLFDWH$VDQH[DPSOH
WKHIROORZLQJUXOHZLOODOORZD3RVWJUH64/DFFRXQWQDPHGpgrepuserRQD
VHUYHURQ\RXUSULYDWHQHWZRUNZLWKDQ,3DGGUHVVLQWKHUDQJHWR
WRUHSOLFDWHXVLQJDQPGSDVVZRUG
archive_command = 'copy %p ..\\archive\\%f'
2Q:LQGRZV
archive_command = 'rsync -av %p postgres@192.168.0.10:archive/%f'
<RXFDQDOVRXVHrsyncLQVWHDGRIcpLI\RXZDQWWRVWRUHWKH:$/VRQD
GLIIHUHQWVHUYHU
archive_command = 'cp %p ../archive/%f'
2Q/LQX[8QL[\RXUarchive_commandOLQHVKRXOGORRNVRPHWKLQJOLNH
$GGWKHarchive_commandFRQILJXUDWLRQGLUHFWLYHWRSRVWJUHVTODXWRFRQIRU
XVHALTER SYSTEMWRLQGLFDWHZKHUHWKH:$/VZLOOEHVDYHG:LWK
VWUHDPLQJ\RXߞUHIUHHWRFKRRVHDQ\GLUHFWRU\0RUHGHWDLOVRQWKLVVHWWLQJ
FDQEHIRXQGLQWKH3RVWJUH64/3*6WDQGE\GRFXPHQWDWLRQ
7KHWUHHOD\RXWRISJ$GPLQLVLQWXLWLYHWRIROORZEXWGRHVHQJ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H64/V\VWHPFDWDORJV
information_schemaFDWDORJDQGWKHpg_cataloginformation_schemaLVDQ
$16,64/VWDQGDUGFDWDORJIRXQGLQRWKHUGDWDEDVHVVXFKDV0\64/DQG64/
6HUYHU<RXPD\UHFRJQL]HVRPHRIWKHWDEOHVDQGFROXPQVIURPZRUNLQJZLWK
RWKHUGDWDEDVHSURGXFWV
)LJXUH+LGHRUXQKLGHGDWDEDVHREMHFWVLQWKHSJ$GPLQEURZVHWUHH
7KHVHVHWWLQJVDUHGHVFULEHGLQ6HUYHU&RQILJXUDWLRQ5HSOLFDWLRQ<RXPD\
ZDQWWRVHWZDOBNHHSBVHJPHQWVKLJKHULI\RXUVHUYHUVDUHIDUDSDUWDQG\RXU
SURGXFWLRQVHUYHUKDVDORWRIWUDQVDFWLRQV,I\RXDUHUXQQLQJYHUVLRQRU
DERYH\RXVKRXOGXVHreplicaLQVWHDGRIhot_standbyIRUWKHZDOBOHYHO
hot_standbyLVVWLOODFFHSWHGLQIRUEDFNZDUGFRPSDWLELOLW\EXWZLOOEH
UHDGDVreplica
,I\RXZDQWWRXVHORJLFDOUHSOLFDWLRQWRGRSDUWLDOUHSOLFDWLRQRIRQO\VRPH
WDEOHV\RXߞOOQHHGWRVHWZDOBOHYHO ORJLFDO/RJLFDOGRHVPRUHORJJLQJWKDQ
KRWBVWDQGE\VRZLOODOVRZRUNIRUGRLQJIXOOVHUYHUUHSOLFDWLRQ
wal_keep_segments = 10
max_wal_senders = 5
archive_mode = on
wal_level = hot_standby
listen_addresses = *
$OWHUWKHIROORZLQJFRQILJXUDWLRQVHWWLQJVLQSRVWJUHVTODXWRFRQI7KHVHFDQ
EHGRQHXVLQJALTER SYSTEM set variable=valueIROORZHGE\SELECT
pg_reload_conf();ZLWKRXWWKHQHHGWRWRXFKWKHSK\VLFDOFRQILJILOH
CREATE ROLE pgrepuser REPLICATION LOGIN PASSWORD 'woohoo';
&UHDWHDUHSOLFDWLRQDFFRXQW
7KHVWHSVIRUVHWWLQJXS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--------------+---------------+-----------facts
|
0 |
736
lu_fact_types |
0 |
4
7KHVHFRQGWLPH\RXUXQWKHTXHU\\RXVKRXOGQRWLFHDWOHDVWD
SHUIRUPDQFHVSHHGLQFUHDVHDQGVKRXOGVHHWKHIROORZLQJFDFKHGLQWKHEXIIHU
SELECT T.fact_subcats[2], COUNT(*) As num_fact
FROM
census.facts As F
INNER JOIN
census.lu_fact_types AS T ON F.fact_type_id = T.fact_type_id
GROUP BY T.fact_subcats[2];
([DPSOHUHWXUQVWKHQXPEHURIEXIIHUHGSDJHVRIWKHfactsDQG
lu_fact_typesWDEOHV2IFRXUVHWRDFWXDOO\VHHEXIIHUHGURZV\RXQHHGWRUXQ
DTXHU\7U\WKLVRQH
SELECT
C.relname,
COUNT(CASE WHEN B.isdirty THEN 1 ELSE NULL END) As dirty_buffers,
COUNT(*) As num_buffers
FROM
pg_class AS C INNER JOIN
pg_buffercache B ON C.relfilenode = B.relfilenode INNER JOIN
pg_database D ON B.reldatabase = D.oid AND D.datname = current_database()
WHERE C.relname IN ('facts','lu_fact_types')
GROUP BY C.relname;
([DPSOH$UHP\WDEOHURZVLQWKHEXIIHUFDFKH"
<RXFDQWKHQUXQDTXHU\DJDLQVWWKHpg_buffercacheYLHZDVVKRZQLQ
([DPSOH
CREATE EXTENSION pg_buffercache;
+RZGR\RXFKHFNZKDWߞVLQWKHFXUUHQWFDFKH"<RXFDQLQVWDOOWKH
pg_buffercacheH[WHQVLRQ
,I\RXH[HFXWHDFRPSOH[TXHU\WKDWWDNHVDZKLOHWRUXQVXEVHTXHQWUXQVDUH
RIWHQPXFKIDVWHU7KDQNFDFKLQJ,IWKHVDPHTXHU\H[HFXWHVLQVHTXHQFHE\WKH
VDPHXVHURUGLIIHUHQWXVHUVDQGQRFKDQJHVKDYHEHHQPDGHWRWKHXQGHUO\LQJ
GDWD\RXVKRXOGJHWEDFNWKHVDPHUHVXOW$VORQJDVWKHUHߞVVSDFHLQPHPRU\WR
FDFKHWKHGDWDWKHSOD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QJ*5283%<FODXVHV6SHFLILFDOO\WKH\DUHXVHIXO
RQO\RQTXHULHVWKDWJURXSE\DOOWKHFROXPQVLQ\RXUVWDWLVWLF
7KHndistinctVWDWLVWLFFDWDORJVKRZRIWHQFROXPQYDOXHVDUHVHHQWRJHWKHU
7KHdependenciesVWDWLVWLFFDWDORJVGHSHQGHQFLHVEHWZHHQFROXPQV)RU
H[DPSOH]LSFRGHLVVHHQRQO\ZLWK%RVWRQLQWKHcityFROXPQ
dependenciesVWDWLVWLFVDUHXVHGRQO\WRRSWLPL]HTXHULHVZLWKHTXDOLWLHV
VXFKDVDTXHU\VSHFLI\LQJcity = 'Boston' and zip = '02109'
<RXFDQFROOHFWWZRNLQGVRIVWDWLVWLFVDQGPXVWVSHFLI\RQHRUERWKLQ\RXU
VWDWHPHQW
$&5($7(67$7,67,&6VWDWHPHQWPXVWVSHFLI\WZRRUPRUHFROXPQVLQD
VLQJOHWDEOH([DPSOHFUHDWHVVWDWVRQWKHFROXPQVfact_type_idDQGyrLQ
WKHcensus.factsWDEOH7KHVWDWLVWLFVVKRXOGDOVREHQDPHGDOWKRXJKWKDWLV
RSWLRQDO,I\RXVSHFLI\DVFKHPDDVSDUWRIWKHQDPHWKHVWDWLVWLFVZLOOEH
FUHDWHGLQWKDWVFKHPDRWKHUZLVHWKH\JHWFUHDWHGLQWKHGHIDXOWVFKHPD
CREATE STATISTICS census.stats_facts_type_yr_dep_dist (dependencies, ndistinct)
ON fact_type_id, yr FROM census.facts;
ANALYZE census.facts;
([DPSOH0XOWLFROXPQVWDWV
9HUVLRQLQWURGXFHGVXSSRUWIRUPXOWLFROXPQVWDWVYLDWKHQHZ&5($7(
67$7,67,&6''/FRQVWUXFW7KLVIHDWXUHDOORZV\RXWRFUHDWHVWDWVDJDLQVWD
FRPELQDWLRQRIFROXPQV$PXOWLFROXPQVWDWLVXVHIXOLI\RXKDYHFROXPQVWKDW
DUHFRUUHODWHGLQYDOXH6D\IRUH[DPSOHWKDW\RXKDYH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WRSWLRQV
,Qߡ6HOHFWLYH%DFNXS8VLQJSJBGXPSߢZHGHPRQVWUDWHGKRZWREDFNXSD
GDWDEDVH7RUHSHDWWKHVDPHVWHSVXVLQJWKHSJ$GPLQLQWHUIDFHULJKWFOLFNWKH
GDWDEDVH\RXZDQWWREDFNXSDQGFKRRVH&XVWRPIRU)RUPDWDVVKRZQLQ
)LJXUH
%DFNLQJXSDQHQWLUHGDWDEDVH
,I\RXUVHUYHULVUHPRWHRU\RXUGDWDEDVHVDUHKXJHZHUHFRPPHQGXVLQJWKHFRPPDQGOLQH
WRROVIRUEDFNXSDQGUHVWRUHLQVWHDGRISJ$GPLQWRDYRLGDGGLQJDQRWKHUOD\HURIFRPSOH[LW\
WRZKDWFRXOGDOUHDG\EHDSUHWW\OHQJWK\SURFHVV$OVRNHHSLQPLQGWKDWLI\RXGRD
FRPSUHVVHG7$5GLUHFWRU\EDFNXSZLWKDQHZHUYHUVLRQRIpg_dump\RXQHHGWRXVHWKHVDPH
RUODWHUYHUVLRQRIpg_restore
:$51,1*
)LJXUHSJ$GPLQ)LOH࠱3UHIHUHQFHV
SELECT
attname As colname,
n_distinct,
most_common_vals AS common_vals,
most_common_freqs As dist_freq
FROM pg_stats
WHERE tablename = 'facts'
ORDER BY schemaname, tablename, attname;
colname
| n_distinct | common_vals
| dist_freq
-------------+------------+------------------+-----------------------------fact_type_id |
68 | {135,113...
| {0.0157,0.0156333,...
perc
|
985 | {0.00,...
| {0.1845,0.0579333,0.056...
tract_id
|
1478 | {25025090300... | {0.00116667,0.00106667,0.0...
val
|
3391 | {0.000,1.000,2...| {0.2116,0.0681333,0...
yr
|
2 | {2011,2010}
| {0.748933,0.251067}
([DPSOH'DWDGLVWULEXWLRQKLVWRJUDP
7RJHWDVHQVHRIWKHLQIRUPDWLRQFXOOHGDQGXVHGE\WKHSODQQHUTXHU\WKH
pg_statsWDEOHDVLOOXVWUDWHGLQ([DPSOH
7KHUHIRUHKDYLQJDFFXUDWHDQGFXUUHQ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SUHYLRXVH[DPSOHVXSSRVHZHZHUHWRZULWHDTXHU\RIWKH
IRUP
)URPWKLVSODQZHOHDUQWKDWRXULQGH[FDQEHXVHGEXWHQGVXSPDNLQJWKH
TXHU\WDNHORQJHUEHFDXVHWKHFRVWLVPRUHWKDQGRLQJDVHTXHQWLDOVFDQ
7KHUHIRUHXQGHUQRUPDOFLUFXPVWDQFHVWKHSODQQHUZLOORSWIRUWKHVHTXHQWLDO
VFDQ$VZHDGGPRUHGDWDWRRXUWDEOHZHߞOOSUREDEO\ILQGWKDWWKHSODQQHU
FKDQJHVVWUDWHJLHVWRDQLQGH[VFDQ
set enable_seqscan = false;
EXPLAIN (ANALYZE)
SELECT *
FROM census.lu_fact_types
WHERE fact_subcats && '{White alone, Black alone}'::varchar[];
Bitmap Heap Scan on lu_fact_types
(cost=12.02..14.04 rows=2 width=200)
(actual time=0.058..0.058 rows=2 loops=1)
Recheck Cond: (fact_subcats
&& '{"White alone","Black alone"}'::character varying[])
Heap Blocks: exact=1
-> Bitmap Index Scan on idx_lu_fact_types
(cost=0.00..12.02 rows=2 width=0)
(actual time=0.048..0.048 rows=2 loops=1)
Index Cond: (fact_subcats
&& '{"White alone","Black alone"}'::character varying[])
Planning time: 0.230 ms
Execution time: 0.119 ms
([DPSOH'LVDEOHVHTXHQWLDOVFDQFRHUFHLQGH[XVH
2EVHUYHWKDWZKHQenable_seqscanLVHQDEOHGRXULQGH[LVQRWEHLQJXVHGDQG
WKHSODQQHUKDVFKRVHQWRGRDVHTXHQWLDOVFDQ7KLVFRXOGEHEHFDXVHRXUWDEOHLV
VRVPDOORUEHFDXVHWKHLQGH[ZHKDYHLVQRJRRGIRUWKLVTXHU\,IZHUHSHDWWKH
TXHU\EXWWXUQRIIVHTXHQWLDOVFDQEHIRUHKDQGDVVKRZQLQ([DPSOHZH
FDQVHHWKDWZHKDYHVXFFHHGHGLQIRUFLQJWKHSODQQHUWRXVHWKHLQGH[
Filter: (fact_subcats
&& '{"White alone","Black alone"}'::character varying[])
Rows Removed by Filter: 66
Planning time: 0.182 ms
Execution time: 0.108 ms
,I\RXHYHUZDQWWREDFNXSWKHHQWLUHVHUYHULQYRNHpg_dumpallE\JRLQJWRWKH
SJ$GPLQGRHVQߞWJLYH\RXFRQWURORYHUZKLFKJOREDOREMHFWVWREDFNXSDVWKH
FRPPDQGOLQHLQWHUIDFHGRHVSJ$GPLQEDFNVXSDOOWDEOHVSDFHVDQGUROHV
SJ$GPLQSURYLGHVDJUDSKLFDOLQWHUIDFHWRpg_dumpallIRUEDFNLQJXSV\VWHP
REMHFWV7RXVHWKHLQWHUIDFHILUVWFRQQHFWWRWKHVHUYHU\RXZDQWWREDFNXS
7KHQIURPWKHWRSPHQXFKRRVH7RROV࠱%DFNXS*OREDOV
%DFNLQJXSV\VWHPZLGHREMHFWV
)LJXUH%DFNXSGDWDEDVH
7REDFNXSWKHVHOHFWHGDVVHW\RXFDQIRUJRWKHRWKHUWDEV VHH)LJXUH ,Q
SJ$GPLQ\RXFDQVHOHFWLYHO\GULOOGRZQWRPRUHLWHPVE\FOLFNLQJWKH2EMHFWV
WDEDVVKRZQLQ)LJXUH7KLVIHDWXUHLVQRW\HWSUHVHQWLQSJ$GPLQ
)LJXUHSJ$GPLQVFKHPDEDFNXS
SJ$GPLQSURYLGHVDJUDSKLFDOLQWHUIDFHWRpg_dumpIRUVHOHFWLYHEDFNXS5LJKW
FOLFNWKHDVVHW\RXZDQWWREDFNXSDQGVHOHFW%DFNXS VHH)LJXUH <RXFDQ
EDFNXSDQHQWLUHGDWDEDVHDSDUWLFXODUVFKHPDDWDEOHRUDQ\WKLQJHOVH
6HOHFWLYHEDFNXSRIGDWDEDVHDVVHWV
WRSPHQXDQGFKRRVLQJ7RROV࠱%DFNXS6HUYHU
set enable_seqscan = true;
EXPLAIN (ANALYZE)
SELECT *
FROM census.lu_fact_types
WHERE fact_subcats && '{White alone, Black alone}'::varchar[];
Seq Scan on lu_fact_types
(cost=0.00..2.85 rows=2 width=200)
actual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WLQVFULSWLQJWRROLQSJ$GPLQEXWLVQRWSUHVHQWLQSJ$GPLQ
,WߞVPRVWXVHIXOIRUUXQQLQJUHSHWLWLYH64/WDVNVSJ6FULSWFDQPDNHEHWWHUXVHRI
PHPRU\DQGWKXVEHPRUHHIILFLHQWWKDQHTXLYDOHQW3RVWJUH64/IXQFWLRQV7KLV
LVEHFDXVHVWRUHGIXQFWLRQVPDLQWDLQDOOWKHLUZRUNLQPHPRU\DQGFRPPLWDOOWKH
UHVXOWVRIDIXQFWLRQLQDVLQJOHEDWFK,QFRQWUDVWSJ6FULSWFRPPLWVHDFK64/
LQVHUWRUXSGDWHVWDWHPHQWDVLWUXQVWKURXJKWKHVFULSW7KLVPDNHVSJ6FULSW
SDUWLFXODUO\KDQG\IRUPHPRU\KXQJU\SURFHVVHVWKDW\RXGRQߞWQHHGFRPSOHWHG
DVVLQJOHWUDQVDFWLRQV$IWHUHDFKWUDQVDFWLRQFRPPLWVPHPRU\EHFRPHV
SJ6FULSW
%HKLQGWKHVFHQHVSJ$GPLQVLPSO\UXQVpg_dumpWRSHUIRUPEDFNXSV,I\RXHYHUZDQWWR
NQRZWKHDFWXDOFRPPDQGVSJ$GPLQLVXVLQJVD\IRUVFULSWLQJORRNDWWKH0HVVDJHVWDEDIWHU
\RXFOLFNWKH%DFNXSEXWWRQ<RXߞOOVHHWKHH[DFWFDOOZLWKDUJXPHQWVWRpg_dump
7,3
)LJXUHSJ$GPLQVHOHFWLYHEDFNXS2EMHFWVWDE
Loop through records using LINES function.
WHILE @I < LINES(@labels)
BEGIN
SET @tdef = @tdef + ', ' + @labels[@I][0] + ' numeric(12,3) ';
SET @tdef = 'census.hisp_pop(tract_id varchar(11) PRIMARY KEY ';
Labels will hold records.
SET @labels =
SELECT
quote_ident(
replace(
replace(lower(COALESCE(fact_subcats[4], fact_subcats[3])), ' ', '_')
,':',''
)
) As col_name,
fact_type_id
FROM census.lu_fact_types
WHERE category = 'Population' AND fact_subcats[3] ILIKE 'Hispan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JUH64/RQO\DVHTXHQWLDOVFDQLVSDUDOOHOL]DEOH
3RVWJUH64/LVDOVRDEOHWRSDUDOOHOL]HELWPDSKHDSVFDQVLQGH[VFDQVDQG
LQGH[RQO\VFDQV+RZHYHUIRULQGH[DQGLQGH[RQO\VFDQVRQO\%7UHHLQGH[HV
ZLOOSDUDOOHOL]H1RVXFKOLPLWDWLRQH[LVWVIRUELWPDSKHDSVFDQVIRUWKHPDQ\
LQGH[W\SHZLOOTXDOLI\%XWLQWKHELWPDSKHDSVFDQWKHEXLOGLQJRIWKHELWPDS
LQGH[LVQRWSDUDOOHOL]DEOHVRZRUNHUVPXVWZDLWIRUWKHELWPDSLQGH[WREHIXOO\
EXLOW
3DUDOOHO6FDQV
,QWKHSDUDOOHOSODQIRXUZRUNHUVHDFKWDNHDERXWPVWRDFFRPSOLVKWKHLU
SRUWLRQRIWKHWDVN
count | area_type_code
--------+--------------3718937 | M
2105205 | N
638571 | S
(3 rows)
,QERWKFDVHVWKHRXWSXWLVWKHIROORZLQJ
ORDER BY area_type_code;
Sort
(cost=176300.24..176300.25 rows=3 width=10)
(actual time=1647.060..1647.060 rows=3 loops=1)
Output: (COUNT(*)), area_type_code
Sort Key: labor.area_type_code
Sort Method: quicksort Memory: 25kB
-> HashAggregate
(cost=176300.19..176300.22 rows=3 width=10)
(actual time=1647.025..1647.025 rows=3 loops=1)
Output: count(*), area_type_code
Group Key: labor.area_type_code
-> Seq Scan on public.labor
(cost=0.00..143986.79 rows=6462679 width=2)
(actual time=0.076..620.563 rows=6462713 loops=1)
Output: series_id, year, period, value, footnote_codes, area_type_code
Planning time: 0.054 ms
Execution time: 1647.115 ms
loops=1
loops=1
loops=1
loops=1
set max_parallel_workers_per_gather=0;
EXPLAIN ANALYZE VERBOSE
SELECT COUNT(*), area_type_code
FROM labor
GROUP BY area_type_code
([DPSOH*URXSE\ZLWKRXWSDUDOOHOL]DWLRQ
7RVHHWKHFRVWDQGWLPLQJZLWKRXWSDUDOOHOL]DWLRQVHW
max_parallel_workers_per_gather=0DQGFRPSDUHWKHSODQDVVKRZQLQ
([DPSOH
ORDER BY area_type_code;
Finalize GroupAggregate
(cost=104596.49..104596.61 rows=3 width=10)
(actual time=500.440..500.444 rows=3 loops=1)
Output: COUNT(*), area_type_code
Group Key: labor.area_type_code
-> Sort
(cost=104596.49..104596.52 rows=12 width=10)
(actual time=500.433..500.435 rows=15 loops=1)
Output: area_type_code, (PARTIAL COUNT(*))
Sort Key: labor.area_type_code
Sort Method: quicksort Memory: 25kB
-> Gather
(cost=104595.05..104596.28 rows=12 width=10)
(actual time=500.159..500.382 rows=15 loops=1)
Output: area_type_code, (PARTIAL COUNT(*))
Workers Planned: 4
Workers Launched: 4
-> Partial HashAggregate
(cost=103595.05..103595.08 rows=3 width=10)
(actual time=483.081..483.082 rows=3 loops=5)
Output: area_type_code, PARTIAL count(*)
Group Key: labor.area_type_code
Worker 0: actual time=476.705..476.706 rows=3 loops=1
Worker 1: actual time=480.704..480.705 rows=3 loops=1
Worker 2: actual time=480.598..480.599 rows=3 loops=1
Worker 3: actual time=478.000..478.000 rows=3 loops=1
-> Parallel Seq Scan on public.labor
(cost=0.00..95516.70 rows=1615670 width=2)
(actual time=1.550..282.833 rows=1292543 loops=5)
Output: area_type_code
Worker 0: actual time=0.078..282.698 rows=1278313
Worker 1: actual time=3.497..282.068 rows=1338095
Worker 2: actual time=3.378..281.273 rows=1232359
Worker 3: actual time=0.761..278.013 rows=1318569
Planning time: 0.060 ms
Execution time: 512.667 ms
SET @I = @I + 1;
WHILE @I < LINES(@labels)
BEGIN
SET @tcols = @tcols + ', ' + @labels[@I][0] ;
SET @tload = @tload +
', MAX(CASE WHEN fact_type_id = ' +
CAST(@labels[@I][1] AS STRING) +
' THEN val ELSE NULL END)';
SET @fact_types = @fact_types + ', ' + CAST(@labels[@I][1] As STRING);
SET @I = @I + 1;
END
SET @tload = 'tract_id';
SET @tcols = 'tract_id';
SET @fact_types = '-1';
DECLARE @I, @labels, @tload, @tcols, @fact_types;
SET @I = 0;
SET @labels =
SELECT
quote_ident(
replace(
replace(
lower(COALESCE(fact_subcats[4], fact_subcats[3])), ' ', '_'),':'
,''
)
) As col_name,
fact_type_id
FROM census.lu_fact_types
WHERE category = 'Population' AND fact_subcats[3] ILIKE 'Hispanic or Latino%'
ORDER BY short_name;
([DPSOH3RSXODWLQJWDEOHVZLWKSJ6FULSWORRS
$OWKRXJKSJ6FULSWGRHVQRWKDYHDQH[HFXWHFRPPDQGWKDWDOORZV\RXWRUXQ
G\QDPLFDOO\JHQHUDWHG64/ZHDFFRPSOLVKHGWKHVDPHWKLQJLQ([DPSOHE\
DVVLJQLQJDQ64/VWULQJWRDYDULDEOH([DPSOHSXVKHVWKHHQYHORSHDELW
IXUWKHUE\SRSXODWLQJWKHcensus.hisp_popWDEOHZHMXVWFUHDWHG
create the table.
CREATE TABLE @tdef;
Print out table def.
PRINT @tdef;
SET @tdef = @tdef + ')';
END
:HZLOOJHWWKHJUDSKLFDOH[SODLQVKRZQLQ)LJXUH+HUHߞVDTXLFNWLSIRU
LQWHUSUHWLQJWKHJUDSKLFDOH[SODLQWULPWKHIDW7KHIDWWHUWKHDUURZWKHORQJHUD
VWHSWDNHVWRFRPSOHWH
SELECT left(tract_id, 5) As county_code, SUM(hispanic_or_latino) As tot,
SUM(white_alone) As tot_white,
SUM(COALESCE(hispanic_or_latino,0) - COALESCE(white_alone,0)) AS non_white
FROM census.hisp_pop
GROUP BY county_code
ORDER BY county_code;
6XSSRVHZHUXQWKHTXHU\
2QHRIWKHJUHDWJHPVLQSJ$GPLQLVLWVDWDJODQFHJUDSKLFDOH[SODLQRIWKH
TXHU\SODQ<RXFDQDFFHVVWKHJUDSKLFDOH[SODLQSODQE\RSHQLQJXSDQ64/
TXHU\ZLQGRZZULWLQJDTXHU\DQGFOLFNLQJWKHH[SODLQLFRQ
*UDSKLFDO([SODLQ
7KHOHVVRQWRWDNHDZD\IURP([DPSOHLVWKDW\RXFDQG\QDPLFDOO\DSSHQG
64/IUDJPHQWVLQWRDYDULDEOH
INSERT INTO census.hisp_pop(@tcols)
SELECT @tload FROM census.facts
WHERE fact_type_id IN(@fact_types) AND yr=2010
GROUP BY tract_id;
set max_parallel_workers_per_gather=4;
EXPLAIN ANALYZE VERBOSE
SELECT COUNT(*), area_type_code
FROM labor
GROUP BY area_type_code
([DPSOH*URXSE\ZLWKSDUDOOHOL]DWLRQ
7RLOOXVWUDWHWKHEHQHILWRISDUDOOHOL]DWLRQZHGRZQORDGHGDWDEOHIURPWKH86
%XUHDXRI/DERU6WDWLVWLFVZLWKPLOOLRQURZVRIGDWDDQGUDQWKHTXHU\LQ
([DPSOH
*HQHUDOO\SDUDOOHOL]DWLRQLVUDUHO\ZRUWKZKLOHIRUTXHULHVWKDWILQLVKLQDIHZ
PLOOLVHFRQGV%XWIRUTXHULHVRYHUDJLQRUPRXVGDWDVHWWKDWQRUPDOO\WDNH
VHFRQGVRUPLQXWHVWRFRPSOHWHSDUDOOHOL]DWLRQLVZRUWKWKHLQLWLDOVHWXSFRVW
7KHFRVWRIRUJDQL]LQJDGGLWLRQDOZRUNHUV HYHQRQH VLJQLILFDQWO\LQFUHDVHVWKH
WRWDOWLPHRIWKHTXHU\
Gather
(cost=1029.57..1051.65 rows=192 width=64)
(actual time=12.881..13.947 rows=1 loops=1)
Workers Planned: 1
Workers Launched: 1
Single Copy: true
-> HashAggregate
(cost=29.57..32.45 rows=192 width=64)
(actual time=0.230..0.231 rows=1 loops=1)
Group Key: "left"((tract_id)::text, 5)
-> Bitmap Heap Scan on hisp_pop
(cost=10.25..28.61 rows=192 width=36)
(actual time=0.127..0.184 rows=204 loops=1)
Recheck Cond:
(((tract_id)::text >= '25025000000'::text) AND
((tract_id)::text <= '25025999999'::text))
-> Bitmap Index Scan on hisp_pop_pkey
(cost=0.00..10.20 rows=192 width=0)
(actual time=0.106..0.106 rows=204 loops=1)
Index Cond:
(((tract_id)::text >= '25025000000'::text) AND
((tract_id)::text <= '25025999999'::text))
Planning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WHGE\
DZRUNHUDFWLQJDVWKHOHDGHU7KHOHDGHUGRHVWKHVDPHZRUNDVRWKHUZRUNHUV
EXWKDVWKHDGGHGUHVSRQVLELOLW\RIFROOHFWLQJDOOWKHDQVZHUVIURPIHOORZ
ZRUNHUV,IWKHJDWKHUQRGHLVWKHURRWQRGHRIDSODQWKHZKROHTXHU\ZLOOEH
UXQLQSDUDOOHO,ILWߞVORZHUGRZQRQO\WKHVXESODQLWHQFRPSDVVHVZLOOEH
SDUDOOHOL]HG
:KDW'RHVD3DUDOOHO4XHU\3ODQ/RRN/LNH"
max_parallel_workers_per_gatherQHHGVWREHJUHDWHUWKDQ]HURDQGOHVV
WKDQRUHTXDOWRmax_worker_processes)RU3RVWJUH64/WKLVVHWWLQJ
PXVWDOVREHOHVVWKDQRUHTXDOWRmax_parallel_workers<RXFDQDSSO\
WKLVSDUWLFXODUVHWWLQJDWWKHVHVVLRQRUIXQFWLRQOHYHO
max_parallel_workersDQHZVHWWLQJLQ3RVWJUH64/QHHGVWREHJUHDWHU
WKDQ]HURDQGOHVVWKDQRUHTXDOWRmax_worker_processes
max_worker_processesQHHGVWREHJUHDWHUWKDQ]HUR
dynamic_shared_memory_typeFDQQRWEHVHWWRnone
GroupAggregate (cost=111.29..151.93 rows=1478 width=20)
Output: ("left"((tract_id)::text, 5)), sum(hispanic_or_latino),
sum(white_alone), ...
-> Sort (cost=111.29..114.98 rows=1478 width=20)
Output: tract_id, hispanic_or_latino, white_alone,
("left"((tract_id)::text, 5))
Sort Key: ("left"((tract_id)::text, 5))
-> Seq Scan on census.hisp_pop (cost=0.00..33.48 rows=1478 width=20)
Output: tract_id, hispanic_or_latino
, white_alone, "left"((tract_id)::text, 5)
*UDSKLFDOH[SODLQLVGLVDEOHGLI4XHU\࠱([SODLQ࠱%XIIHUVLVHQDEOHG6RPDNH
VXUHWRXQFKHFNEXIIHUVEHIRUHWU\LQJDJUDSKLFDOH[SODLQ,QDGGLWLRQWRWKH
JUDSKLFDOH[SODL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V%XWZLWKHDFKQHZUHOHDVHZHH[SHFWWKHUDQJHRI
SDUDOOHOL]DEOHTXHULHVWRH[SDQG
$SDUDOOHOL]HGTXHU\LVRQHZKRVHH[HFXWLRQLVGLVWULEXWHGE\WKHSODQQHUDPRQJ
PXOWLSOHEDFNHQGSURFHVVHV%\VRGRLQJ3RVWJUH64/LVDEOHWRXWLOL]HPXOWLSOH
SURFHVVRUFRUHVVRWKDWZRUNFRPSOHWHVLQOHVVWLPH'HSHQGLQJRQWKHQXPEHURI
SURFHVVRUFRUHVLQ\RXUKDUGZDUHWKHWLPHVDYLQJVFRXOGEHVLJQLILFDQW+DYLQJ
WZRFRUHVFRXOGKDOYH\RXUWLPHIRXUFRXOGTXDUWHU\RXUWLPHHWF
3DUDOOHOL]HG4XHULHV
)RUWKLVSDUWLFXODUH[DPSOHWKHFILTERSHUIRUPDQFHLVRQO\DERXWDPLOOLVHFRQG
IDVWHUWKDQRXUCASEYHUVLRQDQGWKHSODQVDUHPRUHRUOHVVWKHVDPH
SELECT T.tract_id, COUNT(*) As tot,
COUNT(*) FILTER (WHERE F.fact_type_id = 131) AS type_1
FROM census.lu_tracts AS T LEFT JOIN census.facts AS F
ON T.tract_id = F.tract_id
GROUP BY T.tract_id;
([DPSOH8VLQJ),/7(5LQVWHDGRIVXETXHULHV
3RVWJUH64/LQWURGXFHGWKHFILTERFRQVWUXFWZKLFKZHLQWURGXFHGLQ
ߡ),/7(5&ODXVHIRU$JJUHJDWHVߢFILTERFDQRIWHQUHSODFHCASELQDJJUHJDWH
H[SUHVVLRQV1RWRQO\LVWKLVV\QWD[PRUHSOHDVDQWWRORRNDWEXWLQPDQ\
VLWXDWLRQVLWSHUIRUPVEHWWHU:HUHSHDW([DPSOHZLWKWKHHTXLYDOHQWILOWHU
YHUVLRQLQ([DPSOH
8VLQJ),/7(5,QVWHDGRI&$6(
(YHQWKRXJKRXUUHZULWWHQTXHU\VWLOOGRHVQߞWXVHWKHfact_typeLQGH[LWߞVIDVWHU
WKDQXVLQJVXETXHULHVEHFDXVHWKHSODQQHUVFDQVWKHfactsWDEOHRQO\RQFH$
VKRUWHUSODQLVJHQHUDOO\QRWRQO\HDVLHUWRFRPSUHKHQGEXWDOVRRIWHQSHUIRUPV
EHWWHUWKDQDORQJHURQHDOWKRXJKQRWDOZD\V
)LJXUH*UDSKLFDOH[SODLQZKHQXVLQJ&$6(
)LJXUHVKRZVWKHJUDSKLFDOSODQRI([DPSOH
SELECT T.tract_id, COUNT(*) As tot,
COUNT(CASE WHEN F.fact_type_id = 131 THEN 1 ELSE NULL END) AS type_1
FROM census.lu_tracts AS T LEFT JOIN census.facts AS F
ON T.tract_id = F.tract_id
GROUP BY T.tract_id;
([DPSOH8VLQJ&$6(LQVWHDGRIVXETXHULHV
:HQRZUHZULWHWKHTXHU\XVLQJCASE<RXߞOOILQGWKDWWKHHFRQRPL]HGTXHU\
VKRZQLQ([DPSOHLVJHQHUDOO\IDVWHUDQGPXFKHDVLHUWRUHDG
)LJXUH*UDSKLFDOSODQZKHQXVLQJVXETXHULHVLQVWHDGRI&$6(
(DFKVFKHGXOHGMREKDVWZRSDUWVWKHH[HFXWLRQVWHSVDQGWKHVFKHGXOH:KHQ
FUHDWLQJDQHZMREVWDUWE\DGGLQJRQHRUPRUHMREVWHSV)LJXUHVKRZV
ZKDWWKHVWHSDGGHGLWVFUHHQORRNVOLNH
6FKHGXOLQJ-REV
%DWFKMREVRIWHQIDLOLQSJ$JHQWHYHQZKHQWKH\PLJKWUXQILQHIURPWKHFRPPDQGOLQH7KLV
LVRIWHQGXHWRSHUPLVVLRQLVVXHVSJ$JHQWDOZD\VUXQVXQGHUWKHVDPHDFFRXQWDVWKHSJ$JHQW
VHUYLFHGDHPRQ,IWKLVDFFRXQWGRHVQߞWKDYHVXIILFLHQWSULYLOHJHVRUWKHQHFHVVDU\QHWZRUN
SDWKPDSSLQJVMREVIDLO
:$51,1*
,I\RXZDQWSJ$JHQWWRUXQEDWFKMREVRQDGGLWLRQDOVH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KHMREV7KHVFUHHQRIIHUV\RXDGURSGRZQOLVWRIGDWDEDVHVWRFKRRVHIURP,I
\RXFKRRVHDUHPRWHFRQQHFWLRQW\SHWKHWH[WER[IRUHQWHULQJDFRQQHFWLRQ
VWULQJEHFRPHVHQDEOHG7\SHLQWKHIXOOFRQQHFWLRQVWULQJLQFOXGLQJFUHGHQWLDOV
DQGWKHGDWDEDVH:KHQ\RXFRQQHFWWRDUHPRWH3RVWJUH64/VHUYHUZLWKDQ
HDUOLHUYHUVLRQRI3RVWJUH64/PDNHVXUHWKDWDOOWKH64/FRQVWUXFWV\RXXVHDUH
VXSSRUWHGRQWKDWYHUVLRQ
)RUHDFKVWHS\RXFDQHQWHUDQ64/VWDWHPHQWWRUXQSRLQWWRDVKHOOVFULSWRQ
WKH26RUHYHQFXWDQGSDVWHLQDIXOOVKHOOVFULSWDVZHFRPPRQO\GR
)LJXUHSJ$GPLQVWHSHGLWVFUHHQ
)LJXUHVKRZVWKHJUDSKLFDOSODQRI([DPSOH
SELECT T.tract_id, COUNT(*) As tot, type_1.tot AS type_1
FROM
census.lu_tracts AS T LEFT JOIN
(SELECT tract_id, COUNT(*) As tot
FROM census.facts
WHERE fact_type_id = 131
GROUP BY tract_id
) As type_1 ON T.tract_id = type_1.tract_id LEFT JOIN
census.facts AS F ON T.tract_id = F.tract_id
GROUP BY T.tract_id, type_1.tot;
([DPSOH8VLQJVXETXHULHVLQVWHDGRI&$6(
:HߞUHDOZD\VVXUSULVHGKRZIUHTXHQWO\SHRSOHIRUJHWDERXWXVLQJWKH$16,64/
CASEH[SUHVVLRQ,QPDQ\DJJUHJDWHVLWXDWLRQVDCASEFDQREYLDWHWKHQHHGIRU
LQHIILFLHQWVXETXHULHV:HߞOOGHPRQVWUDWHWKHSRLQWZLWKWZRHTXLYDOHQWTXHULHV
DQGWKHLUFRUUHVSRQGLQJSODQV([DPSOHXVHVVXETXHULHV
0DNH*RRG8VHRI&$6(
2XUH[HFXWLRQWLPHVN\URFNHWVWRPVDQGWKHSODQLVMXVWDVZHKDGLQ
)LJXUH$OWKRXJKRXUUHVXOWVLQWKLVH[DPSOHVXIIHUWKHORVVRIMXVW
PLOOLVHFRQGVLPDJLQHWDEOHVZLWKWHQVRIPLOOLRQVRIURZVDQGKXQGUHGVRI
FROXPQV7KRVHPLOOLVHFRQGVFRXOGWUDQVODWHLQWRRYHUWLPHDWWKHRIILFHZDLWLQJ
IRUDTXHU\WRILQLVK
SELECT * FROM vw_stats;
([HFXWLRQWLPHLVDERXWPVRQRXUVHUYHUEHFDXVHLWGRHVQߞWUXQDQ\
FRPSXWDWLRQIRUFHUWDLQILHOGVVXFKDVnum_factsDQGnum_fact_typesILHOGV
ZHGLGQRWDVNIRU,I\RXORRNHGDWWKHSODQ\RXPD\EHVWDUWOHGWRILQGWKDWLW
QHYHUHYHQWRXFKHVWKHIDFWVWDEOHEHFDXVHLWߞVVPDUWHQRXJKWRNQRZLWGRHVQߞW
QHHGWR%XWVXSSRVHZHHQWHU
SELECT tract_id FROM vw_stats;
1RZZHTXHU\RXUYLHZZLWKWKLVTXHU\
CREATE OR REPLACE VIEW vw_stats AS
SELECT tract_id,
(SELECT COUNT(*)
FROM census.facts As F
WHERE F.tract_id = T.tract_id) As num_facts,
(SELECT COUNT(*)
FROM census.lu_fact_types As Y
WHERE Y.fact_type_id IN (
SELECT fact_type_id
FROM census.facts F
WHERE F.tract_id = T.tract_id
)
) As num_fact_types
FROM census.lu_tracts As T;
7RGULYHKRPHRXUSRLQWOHWߞVZUDSRXUFHQVXVLQDYLHZDQGXVHWKHVORZ
VXETXHU\H[DPSOHIURP([DPSOH
6HFRQGZKHQ\RXGHILQHYLHZV\RXRIWHQZLOOLQFOXGHPRUHFROXPQVWKDQ
\RXߞOOQHHG<RXPLJKWHYHQJRVRIDUDVWRXVHSELECT *LQVLGHDYLHZ7KLVLV
XQGHUVWDQGDEOHDQGSHUIHFWO\ILQH3RVWJUH64/LVVPDUWHQRXJKWROHW\RXUHTXHVW
DOOW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OOLQWKHhost agent
ILHOGZKHQFUHDWLQJWKHMRE$JHQWVUXQQLQJRQRWKHUVHUYHUVZLOOVNLSWKHMRELILW
GRHVQߞWPDWFKWKHLUKRVWQDPH
,I\RXLQVWDOOHGSJ$JHQWRQPXOWLSOHVHUYHUVDQGKDYHWKHPDOOSRLQWLQJWRWKH
VDPHSJ$JHQWGDWDEDVHDOOWKHVHDJHQWVE\GHIDXOWZLOOH[HFXWHDOOMREV
2QFH\RXKDYHWKHVWHSVUHDG\JRDKHDGDQGVHWXSDVFKHGXOHWRUXQWKHP<RX
FDQVHWXSLQWULFDWHVFKHGXOHVZLWKWKHVFKHGXOLQJVFUHHQ<RXFDQHYHQVHWXS
PXOWLSOHVFKHGXOHV
$YRLG6(/(&7
SELECT *LVZDVWHIXO,WߞVDNLQWRSULQWLQJRXWDSDJHGRFXPHQWZKHQ\RX
QHHGRQO\SDJHV%HVLGHVWKHREYLRXVGRZQVLGHRIDGGLQJWRQHWZRUNWUDIILF
WKHUHDUHWZRRWKHUGUDZEDFNVWKDW\RXPLJKWQRWWKLQNRI
GLVDEOLQJVWHSVWKDWVKRXOGUHPDLQGRUPDQWEXWWKDW\RXGRQߞWZDQWWRGHOHWH
EHFDXVH\RXPLJKWUHDFWLYDWHWKHPODWHU
VXETXHU\VKRXOGZRUNZLWKWKHPDLQTXHU\QRWLQGHSHQGHQWO\RILW
:HILQGWKLVTXHU\HVVHQWLDOIRUPRQLWRULQJEDWFKMREVEHFDXVHVRPHWLPHVDMRE
ZLOOUHSRUWVXFFHVVHYHQWKRXJKLWIDLOHGSJ$JHQWFDQߞWDOZD\VGLVFHUQWKH
VXFFHVVRUIDLOXUHRIDVKHOOVFULSWRQWKH267KHjsloutputILHOGLQWKHORJV
SELECT j.jobname, s.jstname, l.jslstart,l.jslduration, l.jsloutput
FROM
pgagent.pga_jobsteplog As l INNER JOIN
pgagent.pga_jobstep As s ON s.jstid = l.jsljstid INNER JOIN
pgagent.pga_job As j ON j.jobid = s.jstjobid
WHERE jslstart > CURRENT_DATE
ORDER BY j.jobname, s.jstname, l.jslstart DESC;
([DPSOH/LVWORJVWHSUHVXOWVIURPWRGD\
$OWKRXJKSJ$GPLQDOUHDG\SURYLGHVDQLQWXLWLYHLQWHUIDFHWRSJ$JHQW
VFKHGXOLQJDQGORJJLQJ\RXPD\ILQGWKHQHHGWRJHQHUDWH\RXURZQMREUHSRUWV
7KLVLVHVSHFLDOO\WUXHLI\RXKDYHPDQ\MREVRU\RXZDQWWRFRPSLOHVWDWVIURP
\RXUMREUHVXOWV([DPSOHGHPRQVWUDWHVWKHRQHTXHU\ZHXVHRIWHQ
SELECT c.relname As table_name, d.description
FROM
pg_class As c INNER JOIN
pg_namespace n ON n.oid = c.relnamespace INNER JOIN
pg_description As d ON d.objoid = c.oid AND d.objsubid = 0
WHERE n.nspname = 'pgagent'
ORDER BY c.relname;
table_name
|
description
---------------+------------------------pga_job
| Job main entry
pga_jobagent | Active job agents
pga_jobclass | Job classification
pga_joblog
| Job run logs.
pga_jobstep
| Job step to be executed
pga_jobsteplog | Job step run logs.
pga_schedule | Job schedule exceptions
([DPSOH'HVFULSWLRQRISJ$JHQWWDEOHV
:LWK\RXUILQHO\KRQHG64/VNLOOV\RXFDQHDVLO\UHSOLFDWHMREVGHOHWHMREVDQG
HGLWMREVGLUHFWO\E\PHVVLQJZLWKSJ$JHQWPHWDWDEOHV-XVWEHFDUHIXO)RU
H[DPSOHWRJHWDJOLPSVHLQVLGHWKHWDEOHVFRQWUROOLQJDOORI\RXUDJHQWVDQG
MREVFRQQHFWWRWKHpostgresGDWDEDVHDQGH[HFXWHWKHTXHU\LQ([DPSOH
+HOSIXOSJ$JHQW4XHULHV
)LJXUHSJ$JHQWMREVLQSJ$GPLQ
.HHSLQPLQGWKDWZHߞUHQRWDVNLQJ\RXWRDYRLGVXETXHULHVHQWLUHO\:HߞUHRQO\
DVNLQJ\RXWRXVHWKHPMXGLFLRXVO\:KHQ\RXGRXVHWKHPSD\H[WUDDWWHQWLRQ
WRKRZ\RXLQFRUSRUDWHWKHPLQWRWKHPDLQTXHU\)LQDOO\UHPHPEHUWKDWD
)LJXUH*UDSKLFDOSODQDIWHUUHPRYLQJVXETXHULHV
)LJXUHVKRZVWKHJUDSKLFDOSODQRI([DPSOHGHPRQVWUDWLQJKRZPXFK
OHVVZRUNJRHVRQLQLW
)LJXUH7DEXODUSODQZKHQRYHUXVLQJVXETXHULHV
)LJXUH*UDSKLFDOSODQZKHQRYHUXVLQJVXETXHULHV
)LJXUHVKRZVWKHJUDSKLFDOSODQIRU([DPSOH ZHߞOOVDYH\RXWKHH\HVRUH
RIVHHLQJWKHJQDUOHGRXWSXWRIWKHWH[WEXPLAIN ZKLOH)LJXUHVKRZVWKH
WDEXODURXWSXWIURPKWWSH[SODLQGHSHV]FRPUHYHDOLQJDJUHDWGHDORI
LQHIILFLHQF\
SELECT T.tract_id,
COUNT(f.fact_type_id) As num_facts,
COUNT(DISTINCT fact_type_id) As num_fact_types
FROM census.lu_tracts As T LEFT JOIN census.facts As F ON T.tract_id = F.tract_id
GROUP BY T.tract_id;
([DPSOH2YHUXVHGVXETXHULHVVLPSOLILHG
IDVWHU,I\RXKDYHDODUJHUGDWDVHWRUZHDNHUKDUGZDUHWKHGLIIHUHQFHFRXOGEH
HYHQPRUHSURQRXQFHG
,QVRPHYHUVLRQVRISJ$JHQWUXQQLQJRQ:LQGRZVVKHOOVFULSWVRIWHQGHIDXOWWRIDLOHGHYHQ
ZKHQWKH\VXFFHHGHG,IWKLVKDSSHQV\RXVKRXOGVHWWKHVWHSVWDWXVWRignore7KLVLVD
NQRZQEXJWKDWZHKRSHZLOOEHIL[HGLQDIXWXUHUHOHDVH
:$51,1*
SURYLGHVWKHVKHOORXWSXWZKLFKXVXDOO\GHWDLOVZKDWZHQWZURQJ
6HULDODQGLWVELJJHUVLEOLQJELJVHULDODUHDXWRLQFUHPHQWLQJLQWHJHUVRIWHQXVHG
DVSULPDU\NH\VRIWDEOHVLQZKLFKDQDW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DQLQYHQWFXVWRPW\SHV,QWKLV
FKDSWHUZHGRQߞWLQWHQGWRFRYHUHYHU\GDWDW\SH)RUWKDWWKHUHߞVDOZD\VWKH
PDQXDO:HVKRZFDVHGDWDW\SHVWKDWDUHXQLTXHWR3RVWJUH64/DQGQXDQFHVLQ
KRZ3RVWJUH64/KDQGOHVFRPPRQGDWDW\SHV
&KDSWHU'DWD7\SHV
([DPSOHFDQEHPRUHHIILFLHQWO\ZULWWHQDV([DPSOH7KLVTXHU\
FRQVROLGDWLQJVHOHFWVDQGXVLQJDMRLQLVQRWRQO\VKRUWHUWKDQWKHSULRURQHEXW
SELECT tract_id,
(SELECT COUNT(*) FROM census.facts As F
WHERE F.tract_id = T.tract_id) As num_facts,
(SELECT COUNT(*)
FROM census.lu_fact_types As Y
WHERE Y.fact_type_id IN (
SELECT fact_type_id
FROM census.facts F
WHERE F.tract_id = T.tract_id
)
) As num_fact_types
FROM census.lu_tracts As T;
([DPSOH2YHUXVLQJVXETXHULHV
7KHXQQHFHVVDU\XVHRIVXETXHULHVDVVKRZQLQ([DPSOHLVDFRPPRQ
V\PSWRPRISLHFHPHDOWKLQNLQJ
$FODVVLFQHZELHPLVWDNHLVWRWKLQNRIVXETXHULHVDVLQGHSHQGHQWHQWLWLHV
8QOLNHFRQYHQWLRQDOSURJUDPPLQJODQJXDJHV64/GRHVQߞWWDNHNLQGO\WREODFN
ER[LQJߚZULWLQJDEXQFKRIVXETXHULHVLQGHSHQGHQWO\DQGWKHQDVVHPEOLQJWKHP
PLQGOHVVO\WRJHWWKHILQDOUHVXOW<RXKDYHWRWUHDWHDFKTXHU\KROLVWLFDOO\+RZ
\RXSLHFHWRJHWKHUGDWDIURPGLIIHUHQWYLHZVDQGWDEOHVLVHYHU\ELWDVLPSRUWDQW
DVKRZ\RXJRDERXWUHWULHYLQJWKHGDWDLQWKHILUVWSODFH
2YHUXVLQJ6XETXHULHVLQ6(/(&7
:ULWLQJHIILFLHQW64/WDNHVSUDFWLFH7KHUHߞVQRVXFKWKLQJDVDZURQJTXHU\DV
ORQJDV\RXJHWWKHH[SHFWHGUHVXOWEXWWKHUHLVVXFKDWKLQJDVDVORZTXHU\,Q
WKLVVHFWLRQZHSRLQWRXWVRPHRIWKHFRPPRQPLVWDNHVZHVHHSHRSOHPDNH
$OWKRXJKWKLVERRNLVDERXW3RVWJUH64/RXUUHFRPPHQGDWLRQVDUHDSSOLFDEOHWR
RWKHUUHODWLRQDOGDWDEDVHVDVZHOO
6HFRQGSHRSOHGRQߞWWHQGWRNHHSXSZLWKWKHODWHVWGHYHORSPHQWVLQWKHLU
GLDOHFWRI64/'RQߞWEHREOLYLRXVWRDOOWKHV\QWD[VDYLQJ DQGVDQLW\VDYLQJ
DGGHQGDWKDWKDYHFRPHDORQJLQQHZYHUVLRQVRI3RVWJUH64/
LQFRUSRUDWLQJPRUHWDEOHVLQVWHDGRIFRQVLGHULQJWKHVRPHWLPHVPRUHDSSURSULDWH
LQQHUMRLQ8QOLNHRWKHUSURJUDPPLQJODQJXDJHVWKH64/ODQJXDJHGRHVQRW
OHQGLWVHOIZHOOWREOLQGUHXVH
7KHUHDSSHDUWREHWZRSULPDU\FDXVHVIRUDOOWKLVEDGTXHU\LQJ)LUVWZHVHH
SHRSOHUHXVH64/SDWWHUQVZLWKRXWWKLQNLQJ)RUH[DPSOHLIWKH\VXFFHVVIXOO\
ZULWHDTXHU\XVLQJDOHIWMRLQWKH\ZLOOFRQWLQXHWRXVHOHIWMRLQZKHQ
7KHEHVWDQGHDVLHVWZD\WRLPSURYHTXHU\SHUIRUPDQFHLVWRVWDUWZLWKZHOO
ZULWWHQTXHULHV)RXURXWRIILYHTXHULHVZHHQFRXQWHUDUHQRWZULWWHQDV
HIILFLHQWO\DVWKH\FRXOGEH
:ULWLQJ%HWWHU4XHULHV
SELECT
query, calls, total_time, rows,
100.0*shared_blks_hit/NULLIF(shared_blks_hit+shared_blks_read,0) AS hit_percent
FROM pg_stat_statements As s INNER JOIN pg_database As d On d.oid = s.dbid
WHERE d.datname = 'postgresql_book'
ORDER BY total_time DESC LIMIT 5;
([DPSOH([SHQVLYHTXHULHVLQGDWDEDVH
7KHTXHU\LQ([DPSOHOLVWVWKHILYHPRVWFRVWO\TXHULHVLQWKH
postgresql_bookGDWDEDVH
$IXQFWLRQFDOOHGpg_stat_statements_resetZKLFKIOXVKHVWKHTXHU\ORJ
7KLVIXQFWLRQFDQEHUXQRQO\E\VXSHUXVHUV
$YLHZFDOOHGpg_stat_statementsZKLFKVKRZVDOOWKHGDWDEDVHVWRZKLFK
WKHFXUUHQWO\FRQQHFWHGXVHUKDVDFFHVV
7KHH[WHQVLRQSURYLGHVWZRNH\IHDWXUHV
,QDQ\GDWDEDVH\RXZDQWWRXVHIRUPRQLWRULQJHQWHU CREATE EXTENSION
pg_stat_statements;
5HVWDUW\RXUpostgresqlVHUYLFH
pg_stat_statements.track = all
pg_stat_statements.max = 10000
,QWKHFXVWRPL]HGRSWLRQVVHFWLRQRISRVWJUHVTOFRQIDGGWKHOLQHV
shared_preload_libraries = 'pg_stat_statements'
3RVWJUH64/KDVDQLIW\IXQFWLRQFDOOHGgenerate_seriesQRWIRXQGLQRWKHU
GDWDEDVHSURGXFWV7KHIXQFWLRQFRPHVLQWZRIRUPV2QHLVDQXPHULFYHUVLRQ
WKDWFUHDWHVDVHTXHQFHRILQWHJHUVLQFUHPHQWHGE\VRPHYDOXHDQGRQHWKDW
FUHDWHVDVHTXHQFHRIGDWHVRUWLPHVWDPSVLQFUHPHQWHGE\VRPHWLPHLQWHUYDO
*HQHUDWH6HULHV)XQFWLRQ
,I\RXUHQDPHDWDEOHWKDWKDVDVHULDOEDVHGRQDVHTXHQFH3RVWJUH64/ZLOOQRWDXWRPDWLFDOO\
UHQDPHWKHVHTXHQFHREMHFW7RDYRLGFRQIXVLRQ\RXVKRXOGUHQDPHWKHVHTXHQFHREMHFW
:$51,1*
CREATE SEQUENCE s START 1;
CREATE TABLE stuff(id bigint DEFAULT nextval('s') PRIMARY KE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GLQIRUPDWLRQDERXWHDFKSDUWE\PRXVLQJRYHUWKHQRGH
LQWKHGLVSOD\
)LJXUH*UDSKLFDOH[SODLQRXWSXW
SELECT
a As a_before, trim(a) As a_trim, rtrim(a) As a_rt,
i As i_before, ltrim(i, '0') As i_lt_0,
rtrim(i, '0') As i_rt_0, trim(i, '0') As i_t_0
FROM (
SELECT repeat(' ', 4) || i || repeat(' ', 4) As a, '0' || i As i
([DPSOH7ULPPLQJVSDFHVDQGFKDUDFWHUV
%\GHIDXOWWULPIXQFWLRQVUHPRYHVSDFHVEXW\RXFDQSDVVLQDQRSWLRQDO
DUJXPHQWLQGLFDWLQJRWKHUFKDUDFWHUVWRWULP
lpadWUXQFDWHVLQVWHDGRISDGGLQJLIWKHVWULQJLVWRRORQJ
SELECT
lpad('ab', 4, '0') As ab_lpad,
rpad('ab', 4, '0') As ab_rpad,
lpad('abcde', 4, '0') As ab_lpad_trunc;
ab_lpad | ab_rpad | ab_lpad_trunc
--------+---------+--------------00ab
| ab00
| abcd
([DPSOH8VLQJOSDGDQGUSDG
&RPPRQVWULQJPDQLSXODWLRQVDUHSDGGLQJ lpadrpad WULPPLQJZKLWHVSDFH
rtrimltrimtrimbtrim H[WUDFWLQJVXEVWULQJV substring DQG
FRQFDWHQDWLQJ || ([DPSOHGHPRQVWUDWHVSDGGLQJDQG([DPSOH
GHPRQVWUDWHVWULPPLQJ
6WULQJ)XQFWLRQV
2IWHQIRUFURVVV\VWHPFRPSDWLELOLW\\RXZDQWWRUHPRYHFDVHVHQVLWLYLW\IURP
\RXUFKDUDFWHUW\SHV7RGRWKLV\RXQHHGWRRYHUULGHFRPSDULVRQRSHUDWRUVWKDW
WDNHFDVHLQWRFRQVLGHUDWLRQ2YHUULGLQJRSHUDWRUVLVHDVLHUIRUYDUFKDUWKDQLWLV
IRUWH[W:HGHPRQVWUDWHDQH[DPSOHLQ8VLQJ06$FFHVVZLWK3RVWJUH64/
ZKHUHZHVKRZKRZWRPDNHYDUFKDUEHK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LWWLQJ6WULQJVLQWR$UUD\V7DEOHVRU6XEVWULQJV
$KHOSIXOIXQFWLRQIRUDJJUHJDWLQJVWULQJVLVWKHVWULQJBDJJIXQFWLRQZKLFKZH
GHPRQVWUDWHLQ([DPSOHVDQG
FROM generate_series(0, 200, 50) As i
) As x;
a_before | a_trim | a_rt | i_before | i_lt_0 | i_rt_0 | i_t_0
---------+--------+------+----------+--------+--------+-----0
| 0
| 0
| 00
|
|
|
50
| 50
| 50 | 050
| 50
| 05
| 5
100
| 100
| 100 | 0100
| 100
| 01
| 1
150
| 150
| 150 | 0150
| 150
| 015
| 15
200
| 200
| 200 | 0200
| 200
| 02
| 2
,IUHDGLQJWKHRXWSXWLVJLYLQJ\RXDKHDGDFKHVHH)LJXUHIRUWKHJUDSKLFDO
EXPLAIN (ANALYZE)RI([DPSOH
*UDSKLFDO2XWSXWV
Planning time: 0.200 ms
Execution time: 0.635 ms
7KHSDUHQWRI([DPSOHLVWKH+DVK$JJUHJDWH,WFRQWDLQVDVXESODQRI
%LWPDS+HDS6FDQZKLFKLQWXUQFRQWDLQVDVXESODQRI%LWPDS,QGH[6FDQ,Q
WKLVH[DPSOHEHFDXVHWKLVLVWKHILUVWWLPHZHߞUHUXQQLQJWKLVTXHU\RXUSODQQLQJ
WLPHJUHDWO\RYHUVKDGRZVWKHH[HFXWLRQWLPH+RZHYHU3RVWJUH64/FDFKHV
SODQVDQGGDWDVRLIZHZHUHWRUXQWKLVTXHU\RUDVLPLODURQHZLWKLQDVKRUW
SHULRGRIWLPHZHVKRXOGEHUHZDUGHGZLWKDPXFKUHGXFHGSODQQLQJWLPHDQG
DOVRSRVVLEO\UHGXFHGH[HFXWLRQWLPHLIPXFKRIWKHGDWDLWQHHGVLVDOUHDG\LQ
PHPRU\%HFDXVHRIFDFKLQJRXUVHFRQGUXQKDVWKHVHVWDWV
Group Key: "left"((tract_id)::text, 5)
-> Bitmap Heap Scan on hisp_pop
(cost=10.25..28.61 rows=192 width=16)
(actual time=0.441..0.550 rows=204 loops=1)
Recheck Cond:
(((tract_id)::text >= '25025000000'::text) AND
((tract_id)::text <= '25025999999'::text))
Heap Blocks: exact=15
-> Bitmap Index Scan on hisp_pop_pkey
(cost=0.00..10.20 rows=192 width=0)
(actual time=0.421..0.421 rows=204 loops=1)
Index Cond:
(((tract_id)::text >= '25025000000'::text) AND
((tract_id)::text <= '25025999999'::text))
Planning time: 4.835 ms
Execution time: 0.732 ms
HashAggregate
(cost=29.57..32.45 rows=192 width=16)
(actual time=0.664..0.664 rows=1 loops=1)
([DPSOH(;3/$,1 $1$/<=( RXWSXWRI+DVK$JJUHJDWHVWUDWHJ\SODQ
7KHRXWSXWRI([DPSOHLVVKRZQLQ([DPSOHFRQVLVWLQJRIDJURXSLQJ
DQGVXP
EXPLAIN (ANALYZE)
SELECT left(tract_id,5) AS county_code, SUM(white_alone) As w
FROM census.hisp_pop
WHERE tract_id BETWEEN '25025000000' AND '25025999999'
GROUP BY county_code;
([DPSOH(;3/$,1 $1$/<=( ZLWK*5283%<DQG680
0RUHFRPSOH[TXHULHVVXFKDVLQ([DPSOHLQFOXGHDGGLWLRQDOVWHSVUHIHUUHG
WRDVVXESODQVZLWKHDFKVXESODQKDYLQJLWVRZQFRVWDQGDOODGGLQJXSWRWKH
WRWDOFRVWRIWKHSODQ7KHSDUHQWSODQLVDOZD\VOLVWHGILUVWDQGLWVFRVWDQGWLPH
LVHTXDOWRWKHVXPRIDOOLWVVXESODQV7KHRXWSXWLQGHQWVWKHVXESODQV
7KHSODQQHUFRQFOXGHVWKDWXVLQJWKHLQGH[LVFKHDSHUWKDQDVHTXHQWLDOVFDQDQG
VZLWFKHVWRDQLQGH[VFDQ7KHHVWLPDWHGRYHUDOOFRVWGURSVIURPWR
7KHVWDUWXSFRVWLVQRORQJHU]HUREHFDXVHWKHSODQQHUILUVWVFDQVWKHLQGH[WKHQ
SXOOVWKHPDWFKLQJUHFRUGVIURPGDWDSDJHV RUIURPPHPRU\LILQVKDUHGEXIIHUV
DOUHDG\ <RXߞOODOVRQRWLFHWKDWWKHSODQQHUQRORQJHUQHHGHGWRVFDQ
UHFRUGV7KLVJUHDWO\UHGXFHGWKHFRVW
Index Scan using idx_hisp_pop_tract_id_pat on hisp_pop
(cost=0.28..8.29 rows=1 width=16)
(actual time=0.018..0.019 rows=1 loops=1)
Index Cond: ((tract_id)::text = '25025010103'::text)
Planning time: 0.110 ms
Execution time: 0.046 ms
([DPSOH(;3/$,1 $1$/<=( RXWSXWRILQGH[VWUDWHJ\SODQ
1RZZHߞOOUHSHDW([DPSOHZLWKWKHSODQRXWSXWLQ([DPSOH
ALTER TABLE census.hisp_pop ADD CONSTRAINT hisp_pop_pkey PRIMARY KEY(tract_id);
/HWߞVQRZDGGEDFNRXUSULPDU\NH\
LWWDNHVIRUWKHSODQQHUWRFRPHXSZLWKWKHH[HFXWLRQSODQZKHUHDVWKH
H[HFXWLRQWLPHLVHYHU\WKLQJWKDWIROORZV
[\\s]+IROORZHGE\]HURRUPRUHVSDFHV
[)-.]{0,1}IROORZHGE\]HURRURQHFORVHGSDUHQWKHVLVK\SKHQRUSHULRG
[0-9]{3}IROORZHGE\WKUHHGLJLWV
[(]{0,1}VWDUWVZLWK]HURRURQHRSHQSDUHQWKHVLV
7KHPDWFKLQJUXOHVIRU([DPSOHDUH
SELECT unnest(regexp_matches(
'Cell (619) 852-5083. Work (619)123-4567 , Casa 619-730-6254. Bésame mucho.',
E'[(]{0,1}[0-9]{3}[)-.]{0,1}[\\s]{0,1}[0-9]{3}[-.]{0,1}[0-9]{4}', 'g')
) As x;
x
-------------(619) 852-5083
(619)123-4567
619-730-6254
(3 rows)
([DPSOH5HWXUQSKRQHQXPEHUVLQSLHFHRIWH[WDVVHSDUDWHURZV
6XSSRVHVRPHILHOGFRQWDLQVWH[WZLWKHPEHGGHGSKRQHQXPEHUV([DPSOH
VKRZVKRZWRH[WUDFWWKHSKRQHQXPEHUVDQGWXUQWKHPLQWRURZVDOOLQRQHVWHS
7KH\\1\\2HWFUHIHUWRHOHPHQWVLQRXUSDWWHUQH[SUHVVLRQ:HXVHD
EDFNVODVK \ WRHVFDSHWKHSDUHQWKHVHV7KHE'FRQVWUXFWLV3RVWJUH64/V\QWD[
IRUGHQRWLQJWKDWWKHVWULQJWRIROORZVKRXOGEHWDNHQOLWHUDOO\
SELECT regexp_replace(
'6197306254',
'([0-9]{3})([0-9]{3})([0-9]{4})',
E'\(\\1\) \\2-\\3'
) As x;
x
-------------(619) 730-6254
([DPSOH5HIRUPDWDSKRQHQXPEHUXVLQJEDFNUHIHUHQFLQJ
([DPSOHVKRZV\RXKRZWRIRUPDWSKRQHQXPEHUVVWRUHGVLPSO\DV
FRQWLJXRXVGLJLWV
UHIHUHQFLQJDQGRWKHUIDLUO\DGYDQFHGVHDUFKSDWWHUQVDUHDOVRVXSSRUWHG,QWKLV
VHFWLRQZHߞOOSURYLGHDVPDOOVDPSOLQJ)RUPRUHLQIRUPDWLRQVHH3DWWHUQ
0DWFKLQJDQG6WULQJ)XQFWLRQV
SELECT description
FROM mytable
WHERE description ~
,QDGGLWLRQWRWKHZHDOWKRIUHJXODUH[SUHVVLRQIXQFWLRQV\RXFDQXVHUHJXODU
H[SUHVVLRQVZLWKWKHSIMILAR TO ~ RSHUDWRUV7KHIROORZLQJH[DPSOHUHWXUQV
DOOGHVFULSWLRQILHOGVZLWKHPEHGGHGSKRQHQXPEHUV
SELECT substring(
'Cell (619) 852-5083. Work (619)123-4567 , Casa 619-730-6254. Bésame mucho.'
from E'[(]{0,1}[0-9]{3}[)-.]{0,1}[\\s]{0,1}[0-9]{3}[-.]{0,1}[0-9]{4}')
As x;
x
---------------(619) 852-5083
(1 row)
([DPSOH5HWXUQILUVWSKRQHQXPEHULQSLHFHRIWH[W
,I\RXRQO\FDUHDERXWWKHILUVWPDWFK\RXFDQXWLOL]HWKHsubstringIXQFWLRQ
ZKLFKZLOOUHWXUQWKHILUVWPDWFKLQJYDOXHDVVKRZQLQ([DPSOH
7KHUHDUHPDQ\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RXWSXW
%HFDXVHZHRSWHGWRLQFOXGHWKHANALYZEDUJXPHQWLQ([DPSOHWKHSODQQHU
ZLOOUXQWKHTXHU\DQGZHߞUHEOHVVHGZLWKWKHDFWXDOWLPLQJVDVZHOO
.HHSLQPLQGWKDWWKHFRVWPHDVXUHLVUHSRUWHGLQDUELWUDU\XQLWVZKLFKYDULHV
EDVHGRQKDUGZDUHDQGFRQILJXUDWLRQFRVWVHWWLQJV$VVXFKLWߞVXVHIXORQO\DVDQ
HVWLPDWHZKHQFRPSDULQJGLIIHUHQWSODQVRQWKHVDPHVHUYHU7KHSODQQHUߞVMRELV
WRSLFNWKHSODQZLWKWKHORZHVWHVWLPDWHGRYHUDOOFRVWV
,QEXPLAINSODQV\RXߞOOVHHDEUHDNGRZQE\VWHSV(DFKVWHSKDVDUHSRUWHGFRVW
WKDWORRNVVRPHWKLQJOLNHcost=0.00..33.48DVVKRZQLQ([DPSOH,QWKLV
FDVHZHKDYH0.00ZKLFKLVWKHHVWLPDWHGVWDUWXSFRVWDQGWKHVHFRQGQXPEHU
33.48ZKLFKLVWKHWRWDOHVWLPDWHGFRVWRIWKHVWHS7KHVWDUWXSLVWKHWLPHEHIRUH
UHWULHYDORIGDWDDQGFRXOGLQFOXGHVFDQQLQJRILQGH[HVMRLQVRIWDEOHVHWF)RU
VHTXHQWLDOVFDQVWHSVWKHVWDUWXSFRVWLV]HUREHFDXVHWKHSODQQHUPLQGOHVVO\
SXOOVDOOGDWDUHWULHYDOEHJLQVULJKWDZD\
Seq Scan on hisp_pop
(cost=0.00..33.48 rows=1 width=16)
(actual time=0.213..0.346 rows=1 loops=1)
Filter: ((tract_id)::text = '25025010103'::text)
Rows Removed by Filter: 1477
Planning time: 0.095 ms
Execution time: 0.381 ms
([DPSOH(;3/$,1 $1$/<=( RXWSXW
8VLQJ(;3/$,1DORQHJLYHVXVHVWLPDWHGSODQFRVWV8VLQJ(;3/$,1LQ
FRQMXQFWLRQZLWK$1$/<=(JLYHVXVERWKHVWLPDWHGDQGDFWXDOFRVWVWRH[HFXWH
WKHSODQ([DPSOHVKRZVWKHRXWSXWRI([DPSOH
EXPLAIN (ANALYZE)
SELECT tract_id, hispanic_or_latino
FROM census.hisp_pop
WHERE tract_id = '25025010103';
([DPSOH(;3/$,1 $1$/<=( RIDVHTXHQWLDOVFDQ
'URSSLQJDOOLQGH[HVOHWVXVVHHWKHPRVWEDVLFRISODQVLQDFWLRQWKHVHTXHQWLDO
VFDQVWUDWHJ\6HH([DPSOH
ALTER TABLE census.hisp_pop DROP CONSTRAINT IF EXISTS hisp_pop_pkey;
,QRUGHUWRHQVXUHWKDWWKHSODQQHUGRHVQߞWXVHDQLQGH[ZHILUVWGURSWKHSULPDU\
NH\IURPRXUWDEOH
/HWߞVWU\DQH[DPSOH)LUVWZHߞOOXVHWKHEXPLAIN (ANALYZE)FRPPDQGZLWKD
WDEOHZHFUHDWHGLQ([DPSOHVDQG
6DPSOH5XQVDQG2XWSXW
<RXFDQXVHJUDSKLFDOH[SODLQZLWKD*8,VXFKDVSJ$GPLQ$IWHUODXQFKLQJ
SJ$GPLQFRPSRVH\RXUTXHU\DVXVXDOEXWLQVWHDGRIH[HFXWLQJLWFKRRVH
EXPLAINRUEXPLAIN (ANALYZE)IURPWKHGURSGRZQPHQX
7RVHHWKHUHVXOWVRIEXPLAIN (ANALYZE)RQDGDWDFKDQJLQJVWDWHPHQWVXFKDV
UPDATERUINSERTZLWKRXWPDNLQJWKHDFWXDOGDWDFKDQJHZUDSWKHVWDWHPHQWLQ
DWUDQVDFWLRQWKDW\RXDERUWSODFHBEGINEHIRUHWKHVWDWHPHQWDQGROLLBACK
DIWHULW
$QEXPLAINWKDWSURYLGHVDOOGHWDLOVLQFOXGLQJWLPLQJRXWSXWRIFROXPQVDQG
EXIIHUVZRXOGORRNOLNHEXPLAIN (ANALYZE, VERBOSE, BUFFERS)
your_query_here;
$GGLQJWKHBUFFERSDUJXPHQWZKLFKPXVWEHXVHGLQFRQMXQFWLRQZLWK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ZLQGRZIRUYDOXHVIURPWKHFXUUHQWSRVLWLRQLQWKHZLQGRZ
WKURXJKofsYDOXHV,IWKHVHYDOXHVDUHDOOWKHVDPHLWZLOOUHWXUQWUXHRWKHUZLVH
IDOVH7KHIXQFWLRQPHWKRGWKDW3/9SURYLGHVIRUVFDQQLQJYDOXHVRIDZLQGRZ
LVget_func_arg_in_partition:HXVHWKDWWRORRNIRUZDUGDQGH[LWZLWK
IDOVHDVVRRQDVWKHSDWWHUQRIHTXDOLW\IDLOVRUZHߞYHUHDFKHGWKHODVWYDOXH
7RGHFODUHDIXQFWLRQDVDZLQGRZIXQFWLRQLWPXVWKDYHDWINDOWGHVLJQDWRULQ
WKHIXQFWLRQHQYHORSHDVLQWKHODVWOLQHRI([DPSOH
}
return result;
$$ LANGUAGE plv8 WINDOW;
/** Get current value **/
var cval = winobj.get_func_arg_in_partition(0,
0,
winobj.SEEK_CURRENT,
false);
for (i = 1; i < ofs; i++){
/** get next value **/
nval = winobj.get_func_arg_in_partition(0,
i,
winobj.SEEK_CURRENT,
false);
result = (cval == nval) ? true : false;
if (!result){
break;
}
/** next current value is our last value **/
cval = nval;
CREATE FUNCTION run_begin(arg anyelement, ofs int) RETURNS boolean AS $$
var winobj = plv8.get_window_object();
var result = true;
([DPSOH3/9ZLQGRZIXQFWLRQWRIODJUHSHDWLQJGDWDYDOXHV
,Q([DPSOHZHߞOOFUHDWHDZLQGRZIXQFWLRQWKDWIRUHDFKURZUHWXUQVWUXH
LILWߞVWKHEHJLQQLQJRIDUXQDQGIDOVHRWKHUZLVH5XQVRUVWUHDNVDUHVHTXHQFHV
RILGHQWLFDORXWFRPH7KHIXQFWLRQOHWVWKHFDOOHUGHFLGHKRZPDQ\URZV
FRQVWLWXWHDߡUXQߢWKURXJKWKHofsDUJXPHQW
:KDWPDNHVZULWLQJZLQGRZIXQFWLRQVLQ3/9SRVVLEOHLVWKDW3/9FRPHV
SDFNDJHGZLWKDplv8.window_object()KHOSHUIXQFWLRQWKDWU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DUHJHWWLQJEDFNDWLPHVWDPSZLWKRXWWLPH]RQH6RWKH
DQVZHU\RXJHWZKHQ\RXUXQWKLVVDPHTXHU\ZLOOEHWKHVDPHDVPLQH
UHJDUGOHVVRIZKHUHLQWKHZRUOG\RXDUH
SELECT '2012-02-28 10:00 PM America/Los_Angeles'::timestamptz;
2012-02-29 01:00:00-05
([DPSOH,QSXWWLQJWLPHLQRQHWLPH]RQHDQGRXWSXWLQDQRWKHU
/HWߞVGULYHWKHSRLQWKRPHZLWKPRUHH[DPSOHVXVLQJD%RVWRQVHUYHU)RU
([DPSOH,LQSXWP\WLPHLQ/RV$QJHOHVORFDOWLPHEXWEHFDXVHP\VHUYHU
LVLQ%RVWRQ,JHWDWLPHUHWXUQHGLQ%RVWRQORFDOWLPH1RWHWKDWLWGRHVJLYHPH
WKHRIIVHWEXWWKDWLVPHUHO\GLVSOD\LQIRUPDWLRQ7KHWLPHVWDPSLVLQWHUQDOO\
VWRUHGLQ87&
('2012-10-25 10:00 AM'::timestamp, '2012-10-25 2:00 PM'::timestamp)
OVERLAPS
('2012-10-25 11:00 AM'::timestamp,'2012-10-26 2:00 PM'::timestamp) AS x,
('2012-10-25'::date,'2012-10-26'::date)
OVERLAPS
('2012-10-26'::date,'2012-10-27'::date) As y;
$V\RXFDQVHHLQ([DPSOHZHFDQH[SUHVVGDWHVLQRXUORFDOGDWHWLPH
2QFHDJDLQZHVWDUWZLWKWKHYHUVDWLOHgenerate_seriesIXQFWLRQ<RXFDQXVH
WKLVIXQFWLRQZLWKWHPSRUDOW\SHVDQGLQWHUYDOVWHSV
,QDGGLWLRQWRRSHUDWRUVDQGSUHGLFDWHV3RVWJUH64/FRPHVZLWKIXQFWLRQV
VXSSRUWLQJWHPSRUDOW\SHV$IXOOOLVWLQJFDQEHIRXQGDW'DWHWLPH)XQFWLRQVDQG
2SHUDWRUV:HߞOOGHPRQVWUDWHDVDPSOLQJKHUH
x |y
---+--t |f
SELECT
([DPSOH29(5/$36IRUWLPHVWDPSDQGGDWH
OVERLAPSGHPRQVWUDWHGLQ([DPSOHUHWXUQVtrueLIWZRWHPSRUDOUDQJHV
RYHUODS7KLVLVDQ$16,64/SUHGLFDWHHTXLYDOHQWWRWKHoverlapsIXQFWLRQ
OVERLAPSWDNHVIRXUSDUDPHWHUVWKHILUVWSDLUFRQVWLWXWLQJRQHUDQJHDQGWKHODVW
SDLUFRQVWLWXWLQJWKHRWKHUUDQJH$QRYHUODSFRQVLGHUVWKHWLPHSHULRGVWREHKDOI
RSHQPHDQLQJWKDWWKHVWDUWWLPHLVLQFOXGHGEXWWKHHQGWLPHLVRXWVLGHWKH
UDQJH7KLVLVVOLJKWO\GLIIHUHQWEHKDYLRUIURPWKHFRPPRQBETWEENSUHGLFDWH
ZKLFKFRQVLGHUVERWKVWDUWDQGHQGWREHLQFOXGHG7KLVTXLUNZRQߞWPDNHD
GLIIHUHQFHXQOHVVRQHRI\RXUUDQJHVLVDIL[HGSRLQWLQWLPH DSHULRGIRUZKLFK
VWDUWDQGHQGDUHLGHQWLFDO :DWFKRXWIRUWKLVLI\RXߞUHDQDYLGXVHURIWKH
OVERLAPSIXQFWLRQ
2012-02-10 22:00:00-05
SELECT '2012-02-10 11:00 PM'::timestamptz - interval '1 hour';
7KHVXEWUDFWLRQRSHUDWRU - VXEWUDFWVDQLQWHUYDOIURPDWHPSRUDOW\SH
24:20:00
SELECT '23 hours 20 minutes'::interval + '1 hour'::interval;
CREATE AGGREGATE geom_mean(numeric) (
SFUNC=geom_mean_state,
STYPE=numeric[],
FINALFUNC=geom_mean_final,
([DPSOH3/9JHRPHWULFPHDQDJJUHJDWHSXWWLQJDOOWKHSLHFHVWRJHWKHU
7KHILQDOCREATE AGGREGATESXWVDOOWKHSLHFHVWRJHWKHUDQGORRNVPRUHRUOHVV
WKHVDPHLQDOOODQJXDJHV2XU3/9YDULDQWLVVKRZQLQ([DPSOH
CREATE OR REPLACE FUNCTION geom_mean_final(in_num numeric[2])
RETURNS numeric AS
$$
return in_num[1] > 0 ? Math.exp(in_num[0]/in_num[1]) : 0;
$$
LANGUAGE plv8 IMMUTABLE PARALLEL SAFE;
([DPSOH3/9JHRPHWULFPHDQDJJUHJDWHILQDOIXQFWLRQ
CREATE OR REPLACE FUNCTION geom_mean_state(prev numeric[2], next numeric)
RETURNS numeric[2] AS
$$
return (next == null || next == 0) ? prev :
[(prev[0] == null)? 0: prev[0] + Math.log(next), prev[1] + 1];
$$
LANGUAGE plv8 IMMUTABLE PARALLEL SAFE;
([DPSOH3/9JHRPHWULFPHDQDJJUHJDWHVWDWHWUDQVLWLRQIXQFWLRQ
,Q([DPSOHVDQGZHUHIRUPXODWHWKHVWDWHWUDQVLWLRQDQGILQDOIXQFWLRQ
RIWKHJHRPHWULFPHDQDJJUHJDWHIXQFWLRQ VHHߡ:ULWLQJ64/$JJUHJDWH
)XQFWLRQVߢ XVLQJ3/9
:ULWLQJ$JJUHJDWH)XQFWLRQVZLWK3/9
&RIIHH6FULSWGRHVQߞWORRNDOOWKDWGLIIHUHQWIURP-DYD6FULSWH[FHSWIRUWKHODFN
RISDUHQWKHVHVFXUO\EUDFHVDQGVHPLFRORQV7KH/LYH6FULSWYHUVLRQORRNV
H[DFWO\OLNHWKH&RIIHH6FULSWH[FHSWZLWKDLANGUAGE pllsVSHFLILHU
CREATE OR REPLACE FUNCTION
validate_email(email text) returns boolean as
$$
re = /\S+@\S+\.\S+/
return re.test email
$$
LANGUAGE plcoffee IMMUTABLE STRICT PARALLEL SAFE;
([DPSOH3/&RIIHHYDOLGDWLRQRIHPDLOIXQFWLRQ
:HIHG([DPSOHWKURXJKDQRQOLQHFRQYHUWHUDQGDGGHGDreturnVWDWHPHQW
WRJHQHUDWHLWV&RIIHH6FULSWFRXQWHUSDUWLQ([DPSOH
<RXFDQVWRUHDZKROHVHWRIWKHVHYDOLGDWLRQIXQFWLRQVLQDPRGXOHVWDEOH<RX
FDQWKHQLQMHFWUHVXOWVRQWRWKHSDJHEXWDOVRXVHWKHYDOLGDWLRQIXQFWLRQVGLUHFWO\
LQWKHGDWDEDVHDVGHVFULEHGLQ$QGUHZ'XQVWDQߞVߡ/RDGLQJ8VHIXO0RGXOHVLQ
3/9ߢ7KLVLVSRVVLEOHEHFDXVHWKHevalIXQFWLRQLVSDUWRIWKH3/9
-DYD6FULSWODQJXDJH7KHEXLOWLQIXQFWLRQDOORZV\RXWRFRPSLOHIXQFWLRQVDW
VWDUWXSIRUODWHUXVH
$OWKRXJK\RXFDQFRGHWKHVDPHIXQFWLRQXVLQJ3/SJ64/DQG3RVWJUH64/ߞV
RZQUHJXODUH[SUHVVLRQVXSSRUWZHJXLOWOHVVO\SRDFKHGVRPHRQHHOVHߞVWLPH
WHVWHGFRGHDQGZDVWHGQRWLPHRIRXURZQ,I\RXߞUHDZHEGHYHORSHUDQGILQG
\RXUVHOIKDYLQJWRYDOLGDWHGDWDRQERWKWKHFOLHQWVLGHDQGWKHGDWDEDVHVLGH
XVLQJ3/9FRXOGKDOYH\RXUGHYHORSPHQWHIIRUWVSUHWW\PXFKE\FXWWLQJDQG
SDVWLQJ
email
| is_valid
----------------------+---------alexgomezq@gmail.com | t
alexgomezqgmail.com | f
alexgomezq@gmailcom | f
ZKLFKRXWSXWV
SELECT email, validate_email(email) AS is_valid
FROM (VALUES ('alexgomezq@gmail.com')
,('alexgomezqgmail.com'),('alexgomezq@gmailcom')) AS x (email);
([DPSOH&DOOLQJWKH3/9HPDLOYDOLGDWRU
2XUFRGHXVHVD-DYD6FULSWUHJH[REMHFWWRFKHFNWKHHPDLODGGUHVV7RXVHWKH
IXQFWLRQVHH([DPSOH
CREATE OR REPLACE FUNCTION
validate_email(email text) returns boolean as
$$
var re = /\S+@\S+\.\S+/;
return re.test(email);
$$ LANGUAGE plv8 IMMUTABLE STRICT PARALLEL SAFE;
([DPSOH8VLQJ3/9WRYDOLGDWHDQHPDLODGGUHVV
-DYD6FULSWH[DPSOHVRQWKHZHEWRYDOLGDWHHPDLODGGUHVVHV:HDUELWUDULO\
SLFNHGRQHDQGPDGHD3/9RXWRILWLQ([DPSOH
%\GHIDXOWgenerate_seriesDVVXPHVtimestamptzLI\RXGRQߞWH[SOLFLWO\FDVW
YDOXHVWRtimestamp
SELECT dt, date_part('hour',dt) As hr, to_char(dt,'HH12:MI AM') As mn
FROM
generate_series(
'2012-03-11 12:30 AM',
'2012-03-11 3:00 AM',
interval '15 minutes'
) As dt;
dt
| hr | mn
-----------------------+----+---------2012-03-11 00:30:00-05 | 0 | 12:30 AM
2012-03-11 00:45:00-05 | 0 | 12:45 AM
2012-03-11 01:00:00-05 | 1 | 01:00 AM
2012-03-11 01:15:00-05 | 1 | 01:15 AM
2012-03-11 01:30:00-05 | 1 | 01:30 AM
2012-03-11 01:45:00-05 | 1 | 01:45 AM
2012-03-11 03:00:00-04 | 3 | 03:00 AM
([DPSOH([WUDFWLQJHOHPHQWVRIDGDWHWLPHYDOXH
$QRWKHUSRSXODUDFWLYLW\LVWRH[WUDFWRUIRUPDWSDUWVRIDGDWHWLPHYDOXH+HUH
WKHIXQFWLRQVdate_partDQGto_charILWWKHELOO([DPSOHDOVRGULYHV
KRPHWKHEHKDYLRURI'67IRUDWLPH]RQH࠺DZDUHGDWDW\SH:HLQWHQWLRQDOO\
FKRVHDSHULRGWKDWFURVVHVDGD\OLJKWVDYLQJVZLWFKRYHULQUS/East%HFDXVHWKH
FORFNVSULQJVIRUZDUGDWDPWKHILQDOURZRIWKHWDEOHUHIOHFWVWKHQHZWLPH
SELECT (dt - interval '1 day')::date As eom
FROM generate_series('2/1/2012', '6/30/2012', interval '1 month') As dt;
eom
-----------2012-01-31
2012-02-29
2012-03-31
2012-04-30
2012-05-31
([DPSOH*HQHUDWHWLPHVHULHVXVLQJJHQHUDWHBVHULHV
IRUPDWRUWKHPRUHJOREDO,62\\\\PPGGIRUPDW3RVWJUH64/DXWRPDWLFDOO\
LQWHUSUHWVGLIIHULQJLQSXWIRUPDWV7REHVDIHZHWHQGWRVWLFNZLWKHQWHULQJGDWHV
LQ,62EHFDXVHGDWHIRUPDWVYDU\IURPFXOWXUHWRFXOWXUHVHUYHUWRVHUYHURU
HYHQGDWDEDVHWRGDWDEDVH
SELECT '{Alex,Sonia}'::text[] As name, '{46,43}'::smallint[] As age;
<RXFDQFDVWDVWULQJUHSUHVHQWDWLRQRIDQDUUD\WRDQDUUD\ZLWKV\QWD[RIWKH
IRUP
$OWKRXJKWKHarrayIXQFWLRQKDVWREHXVHGZLWKDTXHU\UHWXUQLQJDVLQJOH
FROXPQ\RXFDQVSHFLI\DFRPSRVLWHW\SHDVWKHRXWSXWWKHUHE\DFKLHYLQJ
PXOWLFROXPQUHVXOWV:HGHPRQVWUDWHWKLVLQߡ&XVWRPDQG&RPSRVLWH'DWD
7\SHVߢ
SELECT array(
SELECT DISTINCT date_part('year', log_ts)
FROM logs
ORDER BY date_part('year', log_ts)
);
,IWKHHOHPHQWVRI\RXUDUUD\FDQEHH[WUDFWHGIURPDTXHU\\RXFDQXVHWKHPRUH
VRSKLVWLFDWHGFRQVWUXFWRUIXQFWLRQarray()
SELECT ARRAY[2001, 2002, 2003] As yrs;
7KHPRVWUXGLPHQWDU\ZD\WRFUHDWHDQDUUD\LVWRW\SHWKHHOHPHQWV
$UUD\&RQVWUXFWRUV
$UUD\VSOD\DQLPSRUWDQWUROHLQ3RVWJUH64/7KH\DUHSDUWLFXODUO\XVHIXOLQ
EXLOGLQJDJJUHJDWHIXQFWLRQVIRUPLQJINDQGANYFODXVHVDQGKROGLQJ
LQWHUPHGLDU\YDOXHVIRUPRUSKLQJWRRWKHUGDWDW\SHV,Q3RVWJUH64/HYHU\GDWD
W\SHKDVDFRPSDQLRQDUUD\W\SH,I\RXGHILQH\RXURZQGDWDW\SH3RVWJUH64/
FUHDWHVDFRUUHVSRQGLQJDUUD\W\SHLQWKHEDFNJURXQGIRU\RX)RUH[DPSOH
integerKDVDQLQWHJHUDUUD\W\SHinteger[]characterKDVDFKDUDFWHUDUUD\
W\SHcharacter[]DQGVRIRUWK:HߞOO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/ ERWK
ELWDQGELW ,QRUGHUWRDJJUHJDWHDUUD\VWKH\PXVWEHRIWKHVDPHGDWDW\SHDQGWKHVDPH
GLPHQVLRQ7RIRUFHWKDWLQ([DPSOHZHFDVWWKHDJHVWRWH[W:HDOVRKDYH
WKHVDPHQXPEHURILWHPVLQWKHDUUD\VEHLQJDJJUHJDWHGWZRSHRSOHDQGWZR
DJHV$UUD\VZLWKWKHVDPHQXPEHURIHOHPHQWVDUHFDOOHGEDODQFHGDUUD\V
SELECT array_agg(f.t)
FROM ( VALUES ('{Alex,Sonia}'::text[]),
('{46,43}'::text[] ) ) As f(t);
array_agg
---------------------{{Alex,Sonia},{46,43}}
(1 row)
([DPSOH&UHDWLQJPXOWLGLPHQVLRQDODUUD\VIURPRQHGLPHQVLRQDODUUD\V
3RVWJUH64/LQWURGXFHGarray_aggIXQFWLRQVXSSRUWIRUDUUD\V,QSULRU
YHUVLRQVLI\RXZDQWHGWRDJJUHJDWHURZVRIDUUD\VZLWKDUUD\BDJJ\RXߞGJHWDQ
HUURUDUUD\BDJJVXSSRUWIRUDUUD\VPDNHVLWPXFKHDVLHUWREXLOG
PXOWLGLPHQVLRQDODUUD\VIURPRQHGLPHQVLRQDODUUD\VDVVKRZQLQ([DPSOH
SELECT array_agg(log_ts ORDER BY log_ts) As x
FROM logs
WHERE log_ts BETWEEN '2011-01-01'::timestamptz AND '2011-01-15'::timestamptz;
x
-----------------------------------------{'2011-01-01', '2011-01-13', '2011-01-14'}
([DPSOH8VLQJDUUD\BDJJ
array_aggLVDQDJJUHJDWHIXQFWLRQWKDWFDQWDNHDVHWRIDQ\GDWDW\SHDQG
FRQYHUWLWWRDQDUUD\DVGHPRQVWUDWHGLQ([DPSOH
SELECT string_to_array('CA.MA.TX', '.') As estados;
estados
---------{CA,MA,TX}
(1 row)
([DPSOH&RQYHUWLQJDGHOLPLWHGVWULQJWRDQDUUD\
<RXFDQFRQYHUWGHOLPLWHGVWULQJVWRDQDUUD\ZLWKWKHstring_to_array
IXQFWLRQDVGHPRQVWUDWHGLQ([DPSOH
name
| age
-------------+-------{Alex,Sonia} | {46,43}
SELECT
unnest( '{blind,mouse}'::varchar[]) AS v,
unnest('{1,2,3}'::smallint[]) AS i;
v
|i
------+blind |1
mouse |2
blind |3
mouse |1
blind |2
mouse |3
([DPSOH8QQHVWLQJXQEDODQFHGDUUD\V
,I\RXUHPRYHDQHOHPHQWRIRQHDUUD\VRWKDW\RXGRQߞWKDYHDQHTXDOQXPEHURI
HOHPHQWVLQERWK\RXJHWWKHUHVXOWVKRZQLQ([DPSOH
SELECT
unnest('{three,blind,mice}'::text[]) As t,
unnest('{1,2,3}'::smallint[]) As i;
t
|i
------+three |1
blind |2
mice |3
([DPSOH8QQHVWLQJEDODQFHGDUUD\V
$EDODQFHGXQQHVWDVVKRZQLQ([DPSOH\LHOGVWKUHHURZV
$OWKRXJK\RXFDQDGGPXOWLSOHXQQHVWVWRDVLQJOHSELECTLIWKHQXPEHURI
UHVXOWDQWURZVIURPHDFKDUUD\LVQRWEDODQFHG\RXPD\JHWVRPHKHDG
VFUDWFKLQJUHVXOWV
SELECT unnest('{XOX,OXO,XOX}'::char(3)[]) As tic_tac_toe;
tic_tac_toe
--XOX
OXO
XOX
([DPSOH([SDQGLQJDUUD\VZLWKXQQHVW
$FRPPRQIXQFWLRQXVHGZLWKDUUD\VLVunnestZKLFKDOORZV\RXWRH[SDQGWKH
HOHPHQWVRIDQDUUD\LQWRDVHWRIURZVDVGHPRQVWUDWHGLQ([DPSOH
8QQHVWLQJ$UUD\VWR5RZV
3/9 DND3/-DYD6FULSW LVDWUXVWHGODQJXDJHEXLOWDWRSWKH*RRJOH9
HQJLQH,WDOORZV\RXWRZULWHIXQFWLRQVLQ-DYD6FULSWDQGLQWHUIDFHZLWKWKH
-621GDWDW\SH,WLVQRWSDUWRIWKHFRUH3RVWJUH64/RIIHULQJVR\RXZRQߞWILQG
LWLQDOOSRSXODU3RVWJUH64/GLVWULEXWLRQV<RXFDQDOZD\VFRPSLOHLWIURP
VRXUFH)RU:LQGRZVZHߞYHEXLOW3/9H[WHQVLRQZLQGRZVELQDULHV<RXFDQ
:ULWLQJ3/93/&RIIHH6FULSWDQG
3//LYH6FULSW)XQFWLRQV
SELECT filename
FROM list_incoming_files() As filename
WHERE filename ILIKE '%.csv'
5XQWKHIXQFWLRQLQ([DPSOHZLWKWKHIROORZLQJTXHU\
CREATE OR REPLACE FUNCTION list_incoming_files()
RETURNS SETOF text AS
$$
import os
return os.listdir('/incoming')
$$
LANGUAGE 'plpython2u' VOLATILE SECURITY DEFINER;
([DPSOH/LVWLQJILOHVLQGLUHFWRULHV
5HFDOOWKDW3/3\WKRQLVDQXQWUXVWHGODQJXDJHZLWKRXWDWUXVWHGFRXQWHUSDUW
7KLVPHDQVRQO\VXSHUXVHUVFDQZULWHIXQFWLRQVXVLQJ3/3\WKRQDQGWKH
IXQFWLRQFDQLQWHUDFWZLWKWKHILOHV\VWHPRIWKH26([DPSOHWDNHV
DGYDQWDJHRIWKHXQWUXVWHGQDWXUHRI3/3\WKRQWRUHWULHYHILOHOLVWLQJVIURPD
GLUHFWRU\.HHSLQPLQGWKDWIURPWKHSHUVSHFWLYHRIWKH26D3/3\WKRQ
IXQFWLRQUXQVXQGHUWKHFRQWH[WRIWKHpostgresXVHUDFFRXQWFUHDWHGGXULQJ
LQVWDOODWLRQVR\RXQHHGWREHVXUHWKDWWKLVDFFRXQWKDVDGHTXDWHDFFHVVWRWKH
UHOHYDQWGLUHFWRULHV
SELECT search_term, left(postgresql_help_search(search_term),125) AS result
FROM (VALUES ('regexp_match'),('pg_trgm'),('tsvector')) As x(search_term);
([DPSOH8VLQJ3\WKRQIXQFWLRQVLQDTXHU\
&DOOLQJ3\WKRQIXQFWLRQVLVQRGLIIHUHQWIURPFDOOLQJIXQFWLRQVZULWWHQLQRWKHU
ODQJXDJHV,Q([DPSOHZHXVHWKHIXQFWLRQZHFUHDWHGLQ([DPSOHWR
RXWSXWWKHUHVXOWZLWKWKUHHVHDUFKWHUPV
,PSRUWVWKHOLEUDULHVZHߞOOEHXVLQJ
3HUIRUPVDVHDUFKDIWHUFRQFDWHQDWLQJWKHVHDUFKWHUP
5HDGVWKHUHVSRQVHDQGVDYHVWKHUHWULHYHG+70/WRDYDULDEOHFDOOHG
raw_html
6DYHVWKHSDUWRIWKHraw_htmlWKDWVWDUWVZLWK<!-- docbot goes here ->DQGHQGVMXVWEHIRUHWKHEHJLQQLQJRI<!-- pgContentWrap -->LQWRD
QHZYDULDEOHFDOOHGresult
5HPRYHVOHDGLQJDQGWUDLOLQJ+70/V\PEROVDQGZKLWHVSDFH
5HWXUQVresult
CREATE OR REPLACE FUNCTION postgresql_help_search(param_search text)
RETURNS text AS
$$
import urllib, re
response = urllib.urlopen(
'http://www.postgresql.org/search/?u=%2Fdocs%2Fcurrent%2F&q=' + param_search
)
raw_html = response.read()
result =
raw_html[raw_html.find("<!-- docbot goes here -->") :
raw_html.find("<!-- pgContentWrap -->") - 1]
result = re.sub('<[^<]+?>', '', result).strip()
return result
$$
LANGUAGE plpython2u SECURITY DEFINER STABLE;
([DPSOH6HDUFKLQJ3RVWJUH64/GRFXPHQWVXVLQJ3/3\WKRQ
3\WKRQDOORZV\RXWRSHUIRUPIHDWVWKDWDUHQߞWSRVVLEOHLQ3/SJ64/,Q
([DPSOHZHGHPRQVWUDWHKRZWRZULWHD3/3\WKRQIXQFWLRQWRGRDWH[W
VHDUFKRIWKHRQOLQH3RVWJUH64/GRFXPHQWVLWH
3RVWJUH64/DXWRPDWLFDOO\FRQYHUWV3RVWJUH64/GDWDW\SHVWR3\WKRQGDWDW\SHV
DQGEDFN3/3\WKRQLVFDSDEOHRIUHWXUQLQJDUUD\VDQGFRPSRVLWHW\SHV<RXFDQ
XVH3/3\WKRQWRZULWHWULJJHUVDQGFUHDWHDJJUHJDWHIXQFWLRQV:HߞYH
GHPRQVWUDWHGVRPHRIWKHVHLQWKH3RVWJUHV2Q/LQH-RXUQDOLQ3/3\WKRQ
([DPSOHV
%DVLF3\WKRQ)XQFWLRQ
H[WHQVLRQVZHUHFRPSLOHGDJDLQVW)RUH[DPSOHLI\RXUplpython2uZDV
FRPSLOHGDJDLQVW3\WKRQ\RXVKRXOGLQVWDOO3\WKRQ
SELECT
(OHPHQWVLQDUUD\VDUHPRVWFRPPRQO\UHIHUHQFHGXVLQJWKHLQGH[RIWKH
HOHPHQW3RVWJUH64/DUUD\LQGH[HVVWDUWDW,I\RXWU\WRDFFHVVDQHOHPHQW
DERYHWKHXSSHUERXQG\RXZRQߞWJHWDQHUURUߚRQO\NULLZLOOEHUHWXUQHG7KH
QH[WH[DPSOHJUDEVWKHILUVWDQGODVWHOHPHQWRIRXUDUUD\FROXPQ
5HIHUHQFLQJ(OHPHQWVLQDQ$UUD\
7KHUHVXOWLV{1,2,3,4,5}
SELECT '{1,2,3}'::integer[] || 4 || 5;
<RXFDQDOVRDGGDGGLWLRQDOHOHPHQWVWRDQH[LVWLQJDUUD\DVIROORZV
SELECT fact_subcats[1:2] || fact_subcats[3:4] FROM census.lu_fact_types;
7RJOXHWZRDUUD\VWRJHWKHUHQGWRHQGXVHWKHFRQFDWHQDWLRQRSHUDWRU||
SELECT fact_subcats[2:4] FROM census.lu_fact_types;
3RVWJUH64/DOVRVXSSRUWVDUUD\VOLFLQJXVLQJWKHstart:endV\QWD[,WUHWXUQV
DQRWKHUDUUD\WKDWLVDVXEDUUD\RIWKHRULJLQDO)RUH[DPSOHWRUHWXUQQHZDUUD\V
WKDWMXVWFRQWDLQHOHPHQWVWKURXJKRIHDFKRULJLQDODUUD\W\SH
$UUD\6OLFLQJDQG6SOLFLQJ
SELECT * FROM unnest('{blind,mouse}'::text[], '{1,2,3}'::int[]) AS f(t,i);
t
| i
-------+--blind | 1
mouse | 2
<NULL> | 3
([DPSOH8QQHVWLQJXQEDODQFHGDUUD\VZLWKPXOWLDUJXPHQWXQQHVW
9HUVLRQLQWURGXFHGDPXOWLDUJXPHQWunnestIXQFWLRQWKDWSXWVLQQXOO
SODFHKROGHUVZKHUHWKHDUUD\VDUHQRWEDODQFHG7KHPDLQGUDZEDFNZLWKWKH
QHZunnestLVWKDWLWFDQDSSHDURQO\LQWKHFROMFODXVH([DPSOHUHYLVLWV
RXUXQEDODQFHGDUUD\VXVLQJWKHYHUVLRQFRQVWUXFW
7KHHTXDOLW\RSHUDWRU = UHWXUQVtrueRQO\LIHOHPHQWVLQDOOWKHDUUD\VDUHHTXDO
DQGLQWKHVDPHRUGHU,I\RXGRQߞWFDUHDERXWRUGHURIHOHPHQWVDQGMXVWQHHGWR
NQRZZKHWKHUDOOWKHHOHPHQWVLQRQHDUUD\DSSHDUDVDVXEVHWRIWKHRWKHUDUUD\
XVHWKHFRQWDLQPHQWRSHUDWRUV @><@ ([DPSOHGHPRQVWUDWHVWKH
GLIIHUHQFHEHWZHHQWKHFRQWDLQV @> DQGFRQWDLQHGE\ @< RSHUDWRUV
SELECT fact_subcats
FROM census.lu_fact_types
WHERE fact_subcats && '{OCCUPANCY STATUS,For rent}'::varchar[];
fact_subcats
----------------------------------------------------------{S01,"OCCUPANCY STATUS","Total housing units"...}
{S02,"OCCUPANCY STATUS","Total housing units"...}
{S03,"OCCUPANCY STATUS","Total housing units"...}
{S10,"VACANCY STATUS","Vacant housing units","For rent"...}
(4 rows)
([DPSOH$UUD\RYHUODSVRSHUDWRU
7KHRYHUODSRSHUDWRU && UHWXUQVtrueLIWZRDUUD\VKDYHDQ\HOHPHQWVLQ
FRPPRQ([DPSOHZLOOOLVWDOOUHFRUGVLQRXUWDEOHZKHUHWKHIDFWBVXEFDWV
FRQWDLQVHOHPHQWVOCCUPANCY STATUSRUFor rent
3RVWJUH64/KDVVHYHUDORSHUDWRUVIRUZRUNLQJZLWKDUUD\GDWD:HDOUHDG\VDZ
WKHFRQFDWHQDWLRQRSHUDWRU || IRUFRPELQLQJPXOWLSOHDUUD\VLQWRRQHRUDGGLQJ
DQHOHPHQWWRDQDUUD\LQߡ$UUD\6OLFLQJDQG6SOLFLQJߢ$UUD\VDOVRVXSSRUWWKH
IROORZLQJFRPSDULVRQRSHUDWRUV=<><>@><@DQG&&7KHVHRSHUDWRUV
UHTXLUHERWKVLGHVRIWKHRSHUDWRUWREHDUUD\VRIWKHVDPHDUUD\GDWDW\SH,I\RX
KDYHD*L67RU*,1LQGH[RQ\RXUDUUD\FROXPQWKHFRPSDULVRQRSHUDWRUVFDQ
XWLOL]HWKHP
$UUD\&RQWDLQPHQW&KHFNV
:HXVHGWKHarray_upperIXQFWLRQWRJHWWKHXSSHUERXQGRIWKHDUUD\7KH
VHFRQGUHTXLUHGSDUDPHWHURIWKHIXQFWLRQLQGLFDWHVWKHGLPHQVLRQ,QRXUFDVH
RXUDUUD\LVRQHGLPHQVLRQDOEXW3RVWJUH64/GRHVVXSSRUWPXOWLGLPHQVLRQDO
DUUD\V
fact_subcats[1] AS primero,
fact_subcats[array_upper(fact_subcats, 1)] As segundo
FROM census.lu_fact_types;
7KHH[WHQVLRQVDUHFRPSLOHGDJDLQVWDVSHFLILFPLQRUYHUVLRQRI3\WKRQ<RX
VKRXOGLQVWDOOWKHPLQRUYHUVLRQRI3\WKRQWKDWPDWFKHVZKDW\RXUplpythonu
0DNHDEVROXWHO\VXUHWKDW\RXKDYH3\WKRQSURSHUO\UXQQLQJRQ\RXUVHUYHU
EHIRUHDWWHPSWLQJWRLQVWDOOWKHH[WHQVLRQRUHOVH\RXZLOOUXQLQWRHUURUVWKDW
FRXOGEHGLIILFXOWWRWURXEOHVKRRW
CREATE EXTENSION plpython2u;
CREATE EXTENSION plpython3u;
,QRUGHUWRXVH3/3\WKRQ\RXILUVWQHHGWRLQVWDOO3\WKRQRQ\RXUVHUYHU)RU
:LQGRZVDQG0DF3\WKRQLQVWDOOHUVDUHDYDLODEOH)RU/LQX[8QL[V\VWHPV
3\WKRQELQDULHVDUHXVXDOO\DYDLODEOHYLDWKHYDULRXVGLVWULEXWLRQV)RUGHWDLOV
VHH3/3\WKRQ$IWHULQVWDOOLQJ3\WKRQLQVWDOOWKH3RVWJUH64/3\WKRQH[WHQVLRQ
$OWKRXJK\RXFDQLQVWDOOERWKplpython2uDQGplpython3uLQWKHVDPHGDWDEDVH\RXFDQߞW
XVHERWKGXULQJWKHVDPHVHVVLRQ7KLVPHDQVWKDW\RXFDQߞWZULWHDTXHU\WKDWFDOOVERWK
plpython2uDQGplpython3uIXQFWLRQV<RXPD\HQFRXQWHUDWKLUGH[WHQVLRQFDOOHG
plpythonuWKLVLVDQDOLDVIRUplpython2uDQGLVOHIWDURXQGIRUEDFNZDUGFRPSDWLELOLW\
&$87,21
3\WKRQLVDVOLFNODQJXDJHZLWKDYDVWQXPEHURIDYDLODEOHOLEUDULHV3RVWJUH64/
LVWKHRQO\GDWDEDVHZHNQRZRIWKDWOHWV\RXFRPSRVHIXQFWLRQVXVLQJ3\WKRQ
3RVWJUH64/VXSSRUWVERWK3\WKRQDQG3\WKRQ
:ULWLQJ3/3\WKRQ)XQFWLRQV
Dupd_tsFROXPQ,WXSGDWHVWKHupd_tsILHOGWRWKHFXUUHQWWLPHEHIRUH
UHWXUQLQJWKHFKDQJHGUHFRUG
7KLVLVDQHZIHDWXUHLQWURGXFHGLQYHUVLRQWKDWDOORZVXVWROLPLWWKH
ILULQJRIWKHWULJJHUVRLWKDSSHQVRQO\LIVSHFLILHGFROXPQVKDYHFKDQJHG
3ULRUWRYHUVLRQWKHWULJJHUZRXOGILUHRQDQ\XSGDWHDQG\RXZRXOGQHHG
WRSHUIRUPDFROXPQZLVHFRPSDULVRQXVLQJOLD.some_columnDQG
NEW.some_columnWRGHWHUPLQHZKDWFKDQJHG 7KLVIHDWXUHLVQRWVXSSRUWHG
IRUINSTEAD OFWULJJHUV
'HILQHVWKHWULJJHUIXQFWLRQ7KLVIXQFWLRQFDQEHXVHGRQDQ\WDEOHWKDWKDV
CREATE TRIGGER trig_1
BEFORE INSERT OR UPDATE OF session_state, session_id
ON web_sessions
FOR EACH ROW EXECUTE PROCEDURE trig_time_stamper();
CREATE OR RE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URZQIXQFWLRQV
7KHVHRIWHQZHUHFOXPV\DQGVORZDQGGLGQߞWDOZD\VSURGXFHWKHH[SHFWHG
UHVXOWV:HߞYHEHHQVRKDSS\ZLWKUDQJHVWKDWZHߞYHFRQYHUWHGDOORIRXU
WHPSRUDOWDEOHVWRXVHWKHPZKHUHSRVVLEOH:HKRSH\RXVKDUHRXUMR\
5DQJH7\SHV
SELECT '{1,2,3}'::int[] @> '{3,2}'::int[] AS contains;
contains
-------t
(1 row)
SELECT '{1,2,3}'::int[] <@ '{3,2}'::int[] AS contained_by;
contained_by
-----------f
(1 row)
([DPSOH$UUD\FRQWDLQPHQWRSHUDWRUV
)RUQXPEHUOLNHUDQJHVLIHLWKHUWKHVWDUWSRLQWRUWKHHQGSRLQWLVOHIWEODQN
3RVWJUH64/UHSODFHVLWZLWKDQXOO)RUSUDFWLFDOLW\\RXFDQLQWHUSUHWWKHQXOOWR
UHSUHVHQWHLWKHU-infinityRQWKHOHIWRUinfinityRQWKHULJKW,QDFWXDOLW\
\RXߞUHERXQGE\WKHVPDOOHVWDQGODUJHVWYDOXHVIRUWKHSDUWLFXODUGDWDW\SH6RD
int4rangeRI(,)ZRXOGEH[-2147483648,2147483647)
$FRQWLQXRXVGDWHDQGWLPH WLPHVWDPS UDQJHDOORZLQJIRUIUDFWLRQDO
VHFRQGVtstrangeLVQRWWLPH]RQH࠺DZDUHtstzrangeLVWLPH]RQH࠺DZDUH
tsrangetstzrange
$GLVFUHWHGDWHUDQJHRIFDOHQGDUGDWHVZLWKRXWWLPH]RQHDZDUHQHVV
daterange
$FRQWLQXRXVUDQJHRIGHFLPDOVIORDWLQJSRLQWQXPEHUVRUGRXEOHSUHFLVLRQ
QXPEHUV
numrange
$UDQJHRILQWHJHUV,QWHJHUUDQJHVDUHGLVFUHWHDQGVXEMHFWWR
FDQRQLFDOL]DWLRQ
int4rangeint8range
3RVWJUH64/FRPHVZLWKVL[EXLOWLQUDQJHW\SHVIRUQXPEHUVDQGGDWHWLPHV
%XLOWLQ5DQJH7\SHV
$GLVFUHWHUDQJHKDVPXOWLSOHUHSUHVHQWDWLRQV2XUHDUOLHUH[DPSOHRI[-2,2)FDQ
EHUHSUHVHQWHGLQWKHIROORZLQJZD\VDQGVWLOOLQFOXGHWKHVDPHQXPEHURIYDOXHV
LQWKHUDQJH[-2,1], (-3,1], (-3,2), [-2,2)2IWKHVHIRXU
UHSUHVHQWDWLRQVWKHRQHZLWK[)LVFRQVLGHUHGWKHFDQRQLFDOIRUP7KHUHߞV
QRWKLQJPDJLFDODERXWFORVHGRSHQUDQJHVH[FHSWWKDWLIHYHU\RQHDJUHHVWR
XVLQJWKDWUHSUHVHQWDWLRQIRUGLVFUHWHUDQJHVZHFDQHDVLO\FRPSDUHDPRQJPDQ\
UDQJHVZLWKRXWKDYLQJWRZRUU\ILUVWDERXWFRQYHUWLQJRSHQWRFORVHRUYLFH
YHUVD3RVWJUH64/FDQRQLFDOL]HVDOOGLVFUHWHUDQJHVIRUERWKVWRUDJHDQGGLVSOD\
6RLI\RXHQWHUDGDWHUDQJHDV(2014-1-5,2014-2-1]3RVWJUH64/UHZULWHVLW
DV[2014-01-06,2014-02-02)
HQGSRLQWV
| 74.7697097102419689
| 73.8824162064128504
| 73.5955049035237656
CREATE FUNCTION select_logs_rt(param_user_name varchar)
RETURNS TABLE (log_id int, user_name varchar(50),
description text, log_ts timestamptz) AS
$$
([DPSOH)XQFWLRQWRUHWXUQDWDEOHXVLQJ3/SJ64/
7RGHPRQVWUDWHV\QWD[GLIIHUHQFHVIURP64/LQ([DPSOHZHUHZULWH
([DPSOHDVD3/SJ64/IXQFWLRQ
%DVLF3/SJ64/)XQFWLRQ
:KHQ\RXUIXQFWLRQDOQHHGVRXWJURZ64/WXUQLQJWR3/SJ64/LVDFRPPRQ
SUDFWLFH3/SJ64/VXUSDVVHV64/LQWKDW\RXFDQGHFODUHORFDOYDULDEOHVXVLQJ
DECLAREDQG\RXFDQLQFRUSRUDWHFRQWUROIORZ
:ULWLQJ3/SJ64/)XQFWLRQV
WITH X AS (SELECT
tract_id,
left(tract_id,5) As county,
geom_mean(val) OVER (PARTITION BY tract_id) As div_tract,
ROW_NUMBER() OVER (PARTITION BY tract_id) As rn,
geom_mean(val) OVER(PARTITION BY left(tract_id,5)) As div_county
FROM census.vw_facts WHERE category = 'Population' AND short_name != 'white_alone'
)
SELECT tract_id, county, div_tract, div_county
FROM X
WHERE rn = 1
ORDER BY div_tract DESC, div_county DESC LIMIT 5;
tract_id
| county |
div_tract
|
div_county
------------+--------+----------------------+--------------------25025160101 | 25025 | 302.6815688785928786 | 85.1549046212833364
25027731900 | 25027 | 265.6136902148147729 | 73.5955049035237656
25021416200 | 25021 | 261.9351057509603296 | 73.8824162064128504
25025130406 | 25025 | 260.3241378371627137 | 85.1549046212833364
25017342500 | 25017 | 257.4671462282508267 | 74.7697097102419689
([DPSOH7RSILYHPRVWUDFLDOO\GLYHUVHFHQVXVWUDFWVZLWKDYHUDJHV
/HWߞVJRLQWRRYHUGULYHDQGHQJDJHRXUQHZIXQFWLRQDVDZLQGRZDJJUHJDWHDV
VKRZQLQ([DPSOH
25017
25021
25027
SELECT left(tract_id,5) As county, geom_mean(val) As div_county
FROM census.vw_facts
WHERE category = 'Population' AND short_name != 'white_alone'
GROUP BY county
ORDER BY div_county DESC LIMIT 5;
county |
div_county
-------+--------------------25025 | 85.1549046212833364
25013 | 79.5972921427888918
([DPSOH7RSILYHPRVWUDFLDOO\GLYHUVHFRXQWLHVXVLQJJHRPHWULFPHDQ
/HWߞVWDNHRXUQHZIXQFWLRQIRUDWHVWGULYH,Q([DPSOHZHFRPSXWHD
KHXULVWLFUDWLQJIRUUDFLDOGLYHUVLW\DQGOLVWWKHWRSILYHPRVWUDFLDOO\GLYHUVH
FRXQWLHVLQ0DVVDFKXVHWWV
CREATE AGGREGATE geom_mean(numeric) (
SFUNC=geom_mean_state,
STYPE=numeric[],
FINALFUNC=geom_mean_final,
PARALLEL = safe,
INITCOND='{0,0}'
);
([DP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DOZD\VXVH>IRUWKHIRUPHUDQG IRUWKH
ODWWHUDVLQ[-infinity, infinity)
7,3
$GDWHUDQJHEHWZHHQDQGLQFOXVLYH1RWHWKH
FDQRQLFDOL]DWLRQRQWKHXSSHUERXQG
$GDWHUDQJHJUHDWHUWKDQDQGOHVVWKDQRUHTXDOWR
1RWLFHWKHFDQRQLFDOL]DWLRQ
$OOLQWHJHUVJUHDWHUWKDQ1RWHWKHFDQRQLFDOL]DWLRQ
$WLPHVWDPSJUHDWHUWKDQ$0DQGOHVVWKDQRUHTXDOWR
30
SELECT '[2013-01-05,2013-08-13]'::daterange;
SELECT '(2013-01-05,2013-08-13]'::daterange;
SELECT '(0,)'::int8range;
SELECT '(2013-01-05 10:00,2013-08-13 14:00]'::tsrange;
[2013-01-05,2013-08-14)
[2013-01-06,2013-08-14)
[1,)
("2013-01-05 10:00:00","2013-08-13 14:00:00"]
([DPSOH'HILQLQJUDQJHVZLWKFDVWV
$UDQJHUHJDUGOHVVRIW\SHLVDOZD\VFRPSULVHGRIWZRHOHPHQWVRIWKHVDPH
W\SHZLWKWKHERXQGLQJFRQGLWLRQGHQRWHGE\EUDFNHWVRUSDUHQWKHVHVDVVKRZQ
LQ([DPSOH
'HILQLQJ5DQJHV
,QDGGLWLRQWRWKHEXLOWLQUDQJHW\SHV\RXFDQFUHDWH\RXURZQUDQJHW\SHV
:KHQ\RXGR\RXFDQVHWWKHUDQJHWREHHLWKHUGLVFUHWHRUFRQWLQXRXV
)RUWHPSRUDOUDQJHV-infinityDQGinfinityDUHYDOLGXSSHUDQGORZHU
ERXQGV
('Alex','[2012-04-24, infinity)'::daterange),
('Sonia','[2011-04-24, 2012-06-01)'::daterange),
('Leo','[2012-06-20, 2013-04-20)'::daterange),
('Regina','[2012-06-20, 2013-04-20)'::daterange);
TABLE employment (id serial PRIMARY KEY, employee varchar(20),
daterange);
INDEX ix_employment_period ON employment USING gist (period);
INTO employment (employee,period)
SELECT
e1.employee,
string_agg(DISTINCT e2.employee, ', ' ORDER BY e2.employee) As colleagues
([DPSOH:KRZRUNHGZLWKZKRP"
$VWKHQDPHVXJJHVWVWKHRYHUODSRSHUDWRU&&UHWXUQVtrueLIWZRUDQJHVKDYH
DQ\YDOXHVLQFRPPRQ([DPSOHGHPRQVWUDWHVWKLVRSHUDWRUDQGSXWVWRXVH
WKHstring_aggIXQFWLRQIRUDJJUHJDWLQJWKHOLVWRIHPSOR\HHVLQWRDVLQJOHWH[W
ILHOG
2YHUODSRSHUDWRU
7ZRUDQJHRSHUDWRUVWHQGWREHXVHGPRVWRIWHQRYHUODS && DQGFRQWDLQV @> 7KRVHDUHWKHRQHVZHߞOOFRYHU7RVHHWKHIXOOFDWDORJRIUDQJHRSHUDWRUVJRWR
5DQJH2SHUDWRUV
5DQJH2SHUDWRUV
$GGD*L67LQGH[RQWKHUDQJHILHOG
CREATE
period
CREATE
INSERT
VALUES
([DPSOH7DEOHZLWKGDWHUDQJH
7HPSRUDOUDQJHVDUHSRSXODU6XSSRVH\RXKDYHDQemploymentWDEOHWKDWVWRUHV
HPSOR\PHQWKLVWRU\,QVWHDGRIFUHDWLQJVHSDUDWHFROXPQVIRUVWDUWDQGHQGGDWHV
\RXFDQGHVLJQDWDEOHDVVKRZQLQ([DPSOH,QWKHH[DPSOHZHDGGHGDQ
LQGH[WRWKHperiodFROXPQWRVSHHGXSTXHULHVXVLQJRXUUDQJHFROXPQ
'HILQLQJ7DEOHVZLWK5DQJHV
7KHWKLUGDUJXPHQWGHQRWHVWKHERXQG,IRPLWWHGWKHRSHQFORVH[)FRQYHQWLRQ
LVXVHGE\GHIDXOW:HVXJJHVWWKDW\RXDOZD\VLQFOXGHWKHWKLUGHOHPHQWIRU
FODULW\
CREATE OR REPLACE FUNCTION geom_mean_state(prev numeric[2], next numeric)
RETURNS numeric[2] AS
$$
SELECT
CASE
WHEN $2 IS NULL OR $2 = 0 THEN $1
ELSE ARRAY[COALESCE($1[1],0) + ln($2), $1[2] + 1]
END;
$$
LANGUAGE sql IMMUTABLE PARALLEL SAFE;
([DPSOH*HRPHWULFPHDQDJJUHJDWHVWDWHIXQFWLRQ
7REXLOGRXUJHRPHWULFPHDQDJJUHJDWHZHQHHGWZRVXEIXQFWLRQVDVWDWH
WUDQVLWLRQIXQFWLRQWRVXPWKHORJV VHH([DPSOH DQGDILQDOIXQFWLRQWR
H[SRQHQWLDWHWKHORJV:HߞOODOVRVSHFLI\DQLQLWLDOFRQGLWLRQRI]HURZKHQZH
DVVHPEOHHYHU\WKLQJWRJHWKHU
<HV,Q3RVWJUH64/\RXDUHDEOHWRDXWKRU\RXURZQDJJUHJDWHIXQFWLRQVWR
H[SDQGEH\RQGWKHXVXDODJJUHJDWHV0,10$&2817$9*HWF:H
GHPRQVWUDWHE\FUHDWLQJDQDJJUHJDWHIXQFWLRQWRFRPSXWHWKHJHRPHWULFPHDQ
$JHRPHWULFPHDQLVWKHQWKURRWRIDSURGXFWRIQSRVLWLYHQXPEHUV
(x1*x2*x3...xn)(1/n) ,WKDVYDULRXVXVHVLQILQDQFHHFRQRPLFVDQG
VWDWLVWLFV$JHRPHWULFPHDQVXEVWLWXWHVIRUWKHPRUHFRPPRQDULWKPHWLFPHDQ
ZKHQWKHQXPEHUVUDQJHDFURVVYDVWO\GLIIHUHQWVFDOHV$PRUHVXLWDEOH
FRPSXWDWLRQDOIRUPXODXVHVORJDULWKPVWRWUDQVIRUPDPXOWLSOLFDWLYHSURFHVVWR
DQDGGLWLYHRQH EXP(SUM(LN(x))/n) :HߞOOEHXVLQJWKLVPHWKRGLQRXU
H[DPSOH
:ULWLQJ64/$JJUHJDWH)XQFWLRQV
SELECT * FROM select_logs_xxx('alex');
&DOODOOWKHVHIXQFWLRQVXVLQJ
CREATE OR REPLACE FUNCTION select_logs_so(param_user_name varchar)
RETURNS SETOF logs AS
$$
SELECT * FROM logs WHERE user_name = $1;
$$
LANGUAGE 'sql' STABLE PARALLEL SAFE;
8VLQJDFRPSRVLWHW\SH
LANGUAGE 'sql' STABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION select_logs_out(param_user_name varchar, OUT log_id int
, OUT user_name varchar, OUT description text, OUT log_ts timestamptz)
RETURNS SETOF record AS
$$
SELECT * FROM logs WHERE user_name = $1;
$$
8VLQJOUTSDUDPHWHUV
CREATE OR REPLACE FUNCTION select_logs_rt(param_user_name varchar)
RETURNS TABLE (log_id int, user_name varchar(50),
description text, log_ts timestamptz) AS
$$
SELECT log_id, user_name, description, log_ts FROM logs WHERE user_name = $1;
$$
LANGUAGE 'sql' STABLE PARALLEL SAFE;
8VLQJRETURNS TABLE
([DPSOH([DPSOHVRIIXQFWLRQUHWXUQLQJVHWV
)XQFWLRQVLQDOPRVWDOOODQJXDJHVFDQUHWXUQVHWV64/IXQFWLRQVDUHQR
H[FHSWLRQ7KHUHDUHWKUHHFRPPRQDSSURDFKHVWRGRLQJWKLVWKH$16,64/
VWDQGDUGRETURNS TABLEV\QWD[OUTSDUDPHWHUVDQGFRPSRVLWHGDWDW\SHV7KH
RETURNS TABLEDSSURDFKLVFORVHVWWRZKDW\RXߞOOILQGLQRWKHUGDWDEDVH
SURGXFWV,Q([DPSOHZHGHPRQVWUDWHKRZWRZULWHWKHVDPHIXQFWLRQWKUHH
ZD\V
SELECT update_logs(12, 'alex', 'Fell back asleep.');
7RH[HFXWH
CREATE OR REPLACE FUNCTION
update_logs(log_id int, param_user_name varchar, param_description text)
RETURNS void AS
$$
UPDATE logs SET user_name = $2, description = $3
, log_ts = CURRENT_TIMESTAMP WHERE log_id = $1;
$$
LANGUAGE 'sql' VOLATILE;
([DPSOH64/IXQFWLRQWRXSGDWHDUHFRUG
6LPLODUO\\RXFDQXSGDWHGDWDZLWKDQ64/IXQFWLRQDQGUHWXUQDVFDODURUYRLG
DVVKRZQLQ([DPSOH
SELECT write_to_log('alex', 'Logged in at 11:59 AM.') As new_id;
7RFDOOWKHIXQFWLRQH[HFXWHVRPHWKLQJOLNH
7RFUHDWHDWDEOHWRVWRUH-621GHILQHDFROXPQDVDjsonW\SH
,QVHUWLQJ-621'DWD
3RVWJUH64/SURYLGHV-621 -DYD6FULSW2EMHFW1RWDWLRQ DQGPDQ\VXSSRUW
IXQFWLRQV-621KDVEHFRPHWKHPRVWSRSXODUGDWDLQWHUFKDQJHIRUPDWIRUZHE
DSSOLFDWLRQV9HUVLRQVLJQLILFDQWO\EHHIHGXS-621VXSSRUWZLWKQHZ
IXQFWLRQVIRUH[WUDFWLQJHGLWLQJDQGFDVWLQJWRRWKHUGDWDW\SHV9HUVLRQ
LQWURGXFHGWKH-621%GDWDW\SHDELQDU\IRUPRI-621WKDWFDQDOVRWDNH
DGYDQWDJHRILQGH[HV9HUVLRQLQWURGXFHGPRUHIXQFWLRQVIRUMVRQELQFOXGLQJ
IXQFWLRQVIRUVHWWLQJHOHPHQWVLQDMVRQEREMHFW9HUVLRQLQWURGXFHGWKH
MVRQEBLQVHUWIXQFWLRQIRULQVHUWLQJHOHPHQWVLQWRDQH[LVWLQJMVRQEDUUD\RU
DGGLQJDQHZNH\YDOXH
-621
7KHUHYHUVHRIWKHFRQWDLQVRSHUDWRULVWKHFRQWDLQHGRSHUDWRU <@ ZKRVHILUVW
DUJXPHQWLVWKHYDOXHDQGWKHVHFRQGWKHUDQJH
SELECT employee FROM employment WHERE period @> CURRENT_DATE GROUP BY employee;
employee
-------Alex
([DPSOH:KRLVFXUUHQWO\ZRUNLQJ"
,QWKHFRQWDLQVRSHUDWRU @> WKHILUVWDUJXPHQWLVDUDQJHDQGWKHVHFRQGLVD
YDOXH,IWKHVHFRQGLVZLWKLQWKHILUVWWKHFRQWDLQVRSHUDWRUUHWXUQVWUXH
([DPSOHGHPRQVWUDWHVLWVXVH
&RQWDLQVDQGFRQWDLQHGLQRSHUDWRUV
FROM employment As e1 INNER JOIN employment As e2
ON e1.period && e2.period
WHERE e1.employee <> e2.employee
GROUP BY e1.employee;
employee | colleagues
---------+------------------Alex
| Leo, Regina, Sonia
Leo
| Alex, Regina
Regina | Alex, Leo
Sonia
| Alex
INSERT INTO persons (person)
VALUES (
'{
"name":"Sonia",
"spouse":
{
"name":"Alex",
"parents":
{
"father":"Rafael",
"mother":"Ofelia"
},
"phones":
[
{
"type":"work",
"number":"619-722-6719"
},
{
"type":"cell",
"number":"619-852-5083"
}
]
},
"children":
[
{
"name":"Brandon",
"gender":"M"
},
{
"name":"Azaleah",
"girl":true,
"phones": []
}
]
}'
);
([DPSOH3RSXODWLQJD-621ILHOG
([DPSOHLQVHUWV-621GDWD3RVWJUH64/DXWRPDWLFDOO\YDOLGDWHVWKHLQSXW
WRPDNHVXUHZKDW\RXDUHDGGLQJLVYDOLG-6215HPHPEHUWKDW\RXFDQߞWVWRUH
LQYDOLG-621LQD-621FROXPQQRUFDQ\RXFDVWLQYDOLG-621WRD-621GDWD
W\SH
CREATE TABLE persons (id serial PRIMARY KEY, person json);
CREATE OR REPLACE FUNCTION write_to_log(param_user_name varchar,
param_description text)
RETURNS integer AS
$$
INSERT INTO logs(user_name, description) VALUES($1, $2)
RETURNING log_id;
$$
LANGUAGE 'sql' VOLATILE;
([DPSOH64/IXQFWLRQWKDWUHWXUQVWKHLGHQWLILHURIDQLQVHUWHGUHFRUG
([DPSOHVKRZVDSULPLWLYH64/IXQFWLRQWKDWLQVHUWVDURZLQWRDWDEOHDQG
UHWXUQVDVFDODUYDOXH
%DVLF64/)XQFWLRQ
2QWKHSRVLWLYHVLGHWKHTXHU\SODQQHUFDQSHHNLQWRDQ64/IXQFWLRQDQG
RSWLPL]HH[HFXWLRQߚDSURFHVVFDOOHGLQOLQLQJ4XHU\SODQQHUVWUHDWRWKHU
ODQJXDJHVDVEODFNER[HV2QO\64/IXQFWLRQVFDQEHLQOLQHGZKLFKOHWVWKHP
WDNHDGYDQWDJHRILQGH[HVDQGFROODSVHUHSHWLWLYHFRPSXWDWLRQV
$OWKRXJK64/LVPRVWO\DODQJXDJHIRULVVXLQJTXHULHVLWFDQDOVREHXVHGWR
ZULWHIXQFWLRQV,Q3RVWJUH64/XVLQJDQH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XOZLWKZLQGRZIXQFWLRQVWKDWPRYHWKHZLQGRZ
6HH3RVWJUH64/&5($7($**5(*$7(IRUGHWDLOV
7,3
SELECT json_array_elements(person->'children')->>'name' As name FROM persons;
name
------Brandon
([DPSOHMVRQBDUUD\BHOHPHQWVWRH[SDQG-621DUUD\
7KHMVRQBDUUD\BHOHPHQWVIXQFWLRQWDNHVD-621DUUD\DQGUHWXUQVHDFKHOHPHQW
RIWKHDUUD\DVDVHSDUDWHURZDVLQ([DPSOH
,I\RXDUHFKDLQLQJWKH!RSHUDWRURQO\WKHYHU\ODVWRQHFDQEHD!!RSHUDWRU
SELECT person->'spouse'->'parents'->>'father' FROM persons;
SELECT person#>>array['children','0','name'] FROM persons;
$OOTXHULHVLQWKHSULRUH[DPSOHVUHWXUQWKHYDOXHDV-621SULPLWLYHV QXPEHUV
VWULQJVERROHDQV 7RUHWXUQWKHWH[WUHSUHVHQWDWLRQDGGDQRWKHUJUHDWHUWKDQ
VLJQDVLQWKHIROORZLQJH[DPSOHV
SELECT person#>array['children','0','name'] FROM persons;
$QGWKHSDWKDUUD\HTXLYDOHQW
SELECT person->'children'->0->'name' FROM persons;
7RSHQHWUDWH-621DUUD\VVSHFLI\WKHDUUD\LQGH[-621DUUD\VLV]HURLQGH[HG
XQOLNH3RVWJUH64/DUUD\VZKRVHLQGH[HVVWDUWDW
1RWLFHWKDW\RXPXVWXVHWKH!SRLQWHUV\PEROLIZKDWFRPHVDIWHULVDSDWK
DUUD\
SELECT person#>array['spouse','parents','father'] FROM persons;
<RXFDQDOVRZULWHWKHTXHU\XVLQJDSDWKDUUD\DVLQWKHIROORZLQJH[DPSOH
SELECT person->'name' FROM persons;
SELECT person->'spouse'->'parents'->'father' FROM persons;
([DPSOH4XHU\LQJWKH-621ILHOG
7KHHDVLHVWZD\WRWUDYHUVHWKHKLHUDUFK\RID-621REMHFWLVE\XVLQJSRLQWHU
V\PEROV([DPSOHVKRZVVRPHFRPPRQXVDJH
4XHU\LQJ-621
127(
7KHXVHRIDURZDVDQRXWSXWILHOGLQDTXHU\LVDIHDWXUHXQLTXHWR3RVWJUH64/
,WߞVKDQG\IRUFUHDWLQJFRPSOH[-621REMHFWV:HGHVFULEHLWIXUWKHULQ
ߡ&RPSRVLWH7\SHVLQ4XHULHVߢDQG([DPSOHGHPRQVWUDWHVWKHXVHRI
SELECT row_to_json(f) As jsoned_row FROM persons As f;
7RRXWSXWHDFKURZLQRXUpersonsWDEOHDV-621
SELECT row_to_json(f) As x
FROM (
SELECT id, json_array_elements(person->'children')->>'name' As cname FROM persons
) As f;
x
-------------------------{"id":1,"cname":"Brandon"}
{"id":1,"cname":"Azaleah"}
(2 rows)
([DPSOH&RQYHUWLQJURZVWRLQGLYLGXDO-621REMHFWV UHTXLUHVYHUVLRQ
RUODWHU
([DPSOHGHPRQVWUDWHVWKHXVHRIrow_to_jsonWRFRQYHUWDVXEVHWRI
FROXPQVLQHDFKUHFRUGIURPWKHWDEOHZHFUHDWHGDQGORDGHGLQ([DPSOH
,QDGGLWLRQWRTXHU\LQJ-621GDWD\RXFDQFRQYHUWRWKHUGDWDWR-621,QWKHVH
QH[WH[DPSOHVZHߞOOGHPRQVWUDWHWKHXVHRI-621EXLOWLQIXQFWLRQVWRFUHDWH
-621REMHFWV
2XWSXWWLQJ-621
:HVWURQJO\HQFRXUDJH\RXWRXVHSRLQWHUV\PEROVZKHQGULOOLQJGRZQLQWRD-621REMHFW
7KHV\QWD[LVPRUHVXFFLQFWDQG\RXFDQXVHWKHVDPHRSHUDWRUVDVIRU-621% ZKLFKZHߞOO
FRYHUVKRUWO\ 3RVWJUH64/GRHVRIIHUIXQFWLRQDOHTXLYDOHQWVLI\RXQHHGWKHP
MVRQBH[WUDFWBSDWKLVDYDULDGLFIXQFWLRQ IXQFWLRQVZLWKDQXQOLPLWHGQXPEHURIDUJXPHQWV 7KHILUVWDUJXPHQWLVDOZD\VWKH-621REMHFW\RXDUHWU\LQJWRQDYLJDWHVXEVHTXHQW
SDUDPHWHUVDUHWKHNH\YDOXHIRUHDFKWLHURIWKHKLHUDUFK\7KHHTXLYDOHQWWR!!DQG!!LV
MVRQBH[WUDFWBSDWKBWH[W
Azaleah
(2 rows)
SELECT col FROM sometable ORDER BY col USING sortop LIMIT 1;
SELECT agg(col) FROM sometable;
7KHRSWLRQDOVRUWRSHUDWRUFDQVHUYHDVWKHDVVRFLDWHGVRUWRSHUDWRUIRUDMINRU
MAXOLNHDJJUHJDWH,WLVXVHGWRWDNHDGYDQWDJHRILQGH[HV,WLVMXVWDQRSHUDWRU
QDPHVXFKDV>DQG<,WVKRXOGEHXVHGRQO\ZKHQWKHWZRIROORZLQJVWDWHPHQWV
DUHHTXLYDOHQW
7KHILQDOIXQFWLRQLVRSWLRQDOEXWLIVSHFLILHGLWPXVWWDNHDVLQSXWWKHUHVXOWRI
WKHVWDWHIXQFWLRQ7KHVWDWHIXQFWLRQDOZD\VWDNHVDGDWDW\SHDVWKHLQSXWDORQJ
ZLWKWKHUHVXOWRIWKHODVWFDOOWRWKHVWDWHIXQFWLRQ6RPHWLPHVWKLVUHVXOWLVZKDW
\RXZDQWDVWKHUHVXOWRIWKHDJJUHJDWHIXQFWLRQDQGVRPHWLPHV\RXZDQWWRUXQ
DILQDOIXQFWLRQWRPDVVDJHWKHUHVXOW7KHLQLWLDOFRQGLWLRQLVDOVRRSWLRQDO
:KHQWKHLQLWLDOFRQGLWLRQYDOXHLVSUHVHQWWKHFRPPDQGXVHVLWWRLQLWLDOL]HWKH
VWDWHYDOXH
CREATE AGGREGATE my_agg (input data type) (
SFUNC=state function name,
STYPE=state type,
FINALFUNC=final function name,
INITCOND=initial state value, SORTOP=sort_operator
);
5HJDUGOHVVRIZKLFKODQJXDJH\RXXVHWRFRGHWKHIXQFWLRQVWKHJOXHWKDWEULQJV
WKHPDOOWRJHWKHULVWKHCREATE AGGREGATEFRPPDQG
<RXFDQZULWHDJJUHJDWHVLQDOPRVWDQ\ODQJXDJH64/LQFOXGHG$QDJJUHJDWHLV
J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HDQXPEHULQSXWDVe-5ZRXOGEHFRQYHUWHGWRLWVGHFLPDO
UHSUHVHQWDWLRQ
1HZLQ3RVWJUH64/LVWKHjsonbGDWDW\SH,WLVKDQGOHGWKURXJKWKHVDPH
RSHUDWRUVDVWKRVHIRUWKHjsonW\SHDQGVLPLODUO\QDPHGIXQFWLRQVSOXVVHYHUDO
DGGLWLRQDORQHVjsonbSHUIRUPDQFHLVPXFKEHWWHUWKDQjsonSHUIRUPDQFH
EHFDXVHjsonbGRHVQߞWQHHGWREHUHSDUVHGGXULQJRSHUDWLRQV7KHUHDUHDFRXSOH
RINH\GLIIHUHQFHVEHWZHHQWKHjsonbDQGjsonGDWDW\SHV
%LQDU\-621MVRQE
array_aggDQGarray_to_jsonWRRXWSXWDVHWRIURZVDVDVLQJOH-621REMHFW
,QYHUVLRQZHKDYHDWRXUGLVSRVDOWKHjson_aggIXQFWLRQ:HGHPRQVWUDWH
LWVXVHLQ([DPSOH
SELECT person As b FROM persons_b WHERE id = 1;
SELECT person As j FROM persons WHERE id = 1;
b
--------------------------------------------------------------------------------{"name": "Sonia",
"spouse": {"name": "Alex", "phones": [{"type": "work", "number": "619-722-6719"},
{"type": "cell", "number": "619-852-5083"}],
"parents": {"father": "Rafael", "mother": "Ofelia"}},
"children": [{"name": "Brandon", "gender": "M"},
{"girl": true, "name": "Azaleah", "phones": []}]}
(1 row)
j
--------------------------------------------{
"name":"Sonia",
"spouse":
{
"name":"Alex",
"parents":
{
"father":"Rafael",
"mother":"Ofelia"
},
"phones":
[
{
"type":"work",
"number":"619-722-6719"+
},
{
"type":"cell",
"number":"619-852-5083"+
}
]
},
"children":
[
{
"name":"Brandon",
"gender":"M"
},
{
"name":"Azaleah",
"girl":true,
"phones": []
}
]
}
(1 row)
7ULJJHUIXQFWLRQVWKDWFKDQJHYDOXHVRIDURZVKRXOGEHFDOOHGRQO\LQWKH
BEFOREHYHQWEHFDXVHLQWKHAFTERHYHQWDOOXSGDWHVWRWKHNEWUHFRUGZL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7KHVHDGGLWLRQDORSHUDWRUVSURYLGHYHU\IDVWFKHFNVZKHQ\RXFRPSOHPHQWWKHP
ZLWKD*,1LQGH[RQWKHjsonbFROXPQ
SELECT person->>'name' As name
FROM persons_b
WHERE person @> '{"children":[{"name":"Brandon"}]}';
name
----Sonia
([DPSOHMVRQEFRQWDLQVRSHUDWRU
6RIRUH[DPSOHWROLVWDOOSHRSOHWKDWKDYHDFKLOGQDPHG%UDQGRQXVHWKH
FRQWDLQVRSHUDWRUDVGHPRQVWUDWHGLQ([DPSOH
,QDGGLWLRQWRWKHRSHUDWRUVVXSSRUWHGE\jsonjsonbKDVDGGLWLRQDOFRPSDUDWRU
RSHUDWRUVIRUHTXDOLW\ = FRQWDLQV @> FRQWDLQHG <@ NH\H[LVWV ? DQ\RI
DUUD\RINH\VH[LVWV ?| DQGDOORIDUUD\RINH\VH[LVWV ?& jsonbKDVVLPLODUO\QDPHGIXQFWLRQVDVjsonSOXVVRPHDGGLWLRQDORQHV6RIRU
H[DPSOHWKHjsonIDPLO\RIIXQFWLRQVVXFKDVjson_extract_path_textDQG
json_eachDUHPDWFKHGLQjsonbE\jsonb_extract_path_textjsonb_each
HWF+RZHYHUWKHHTXLYDOHQWRSHUDWRUVDUHWKHVDPHVR\RXZLOOILQGWKDWWKH
H[DPSOHVLQߡ4XHU\LQJ-621ߢZRUNODUJHO\WKHVDPHZLWKRXWFKDQJHIRUWKH
jsonbW\SHߚMXVWUHSODFHWKHWDEOHQDPHDQGjson_array_elementsZLWK
jsonb_array_elements
jsonbUHIRUPDWVLQSXWDQGUHPRYHVZKLWHVSDFH$OVRWKHRUGHURIDWWULEXWHV
LVQRWPDLQWDLQHGIURPWKHLQVHUW
jsonPDLQWDLQVLQSXWZKLWHVSDFHDQGWKHRUGHURIDWWULEXWHV
UPDATE persons_b
SET person = person #- '{children,1,girl}'::text[]
WHERE person @> '{"name":"Sonia"}'
RETURNING person->'children'->1;
([DPSOH8VLQJ-621%WRUHPRYHQHVWHGHOHPHQW
7KHVLPSOH-RSHUDWRUZRUNVIRUILUVWOHYHOHOHPHQWVEXWZKDWLI\RXZDQWHGWR
UHPRYHDQDWWULEXWHIURPDSDUWLFXODUPHPEHU"7KLVLVZKHQ\RXߞGXVHWKH#RSHUDWRU#-WDNHVDQDUUD\RIWH[WYDOXHVWKDWGHQRWHVWKHSDWKRIWKHHOHPHQW\RX
ZDQWWRUHPRYH,Q([DPSOHZHUHPRYHWKHgirlGHVLJQDWRURI$]DOHDK
UPDATE persons_b
SET person = person - 'address'
WHERE person @> '{"name":"Sonia"}';
([DPSOH8VLQJ-621%WRUHPRYHDQHOHPHQW
,IZHGHFLGHGZHQRORQJHUZDQWHGDQDGGUHVVZHFRXOGXVHWKH-DVVKRZQLQ
([DPSOH
%HFDXVH-621%UHTXLUHVWKDWNH\VEHXQLTXHLI\RXWU\WRDGGDGXSOLFDWHNH\
WKHRULJLQDOYDOXHZLOOEHUHSODFHGLQVWHDG6RWRXSGDWHZLWKDQHZDGGUHVVZH
ZRXOGUHSHDWWKHH[HUFLVHLQ([DPSOHEXWUHSODFLQJ6RPHZKHUHLQ6DQ
'LHJR&$ZLWKVRPHWKLQJHOVH
UPDATE persons_b
SET person = person || '{"address": "Somewhere in San Diego, CA"}'::jsonb
WHERE person @> '{"name":"Sonia"}'
RETURNING person;
profile
------------------------------------------------------------------------------{"name": "Sonia", ... "address": "Somewhere in San Diego, CA", "children": ...}
(1 row)
UPDATE 1
([DPSOH8VLQJ-621%__WRDGGDGGUHVV
7KHFRQFDWHQDWLRQRSHUDWRUFDQEHXVHGWRDGGDQGUHSODFHDWWULEXWHVRIDMVRQE
REMHFW,Q([DPSOHZHDGGDQDGGUHVVDWWULEXWHWRWKH*RPH]IDPLO\DQG
XVHWKHRETURNINGFRQVWUXFWFRYHUHGLQߡ5HWXUQLQJ$IIHFWHG5HFRUGVWRWKH
8VHUߢWRUHWXUQWKHXSGDWHGYDOXH7KHQHZYDOXHKDVDQDGGUHVVDWWULEXWH
RSHU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DOOLQJWKHIXQFWLRQ
<RXFDQUHIHUWRWKHDUJXPHQWVE\QDPH ear_sizeskin_colorHWF LQVLGHWKH
ERG\RIWKHIXQFWLRQ,IWKH\DUHQRWQDPHG\RXQHHGWRUHIHUWRWKHDUJXPHQWV
LQVLGHWKHIXQFWLRQE\WKHLURUGHULQWKHDUJXPHQWOLVWDQG
big_elephant(ear_size numeric, skin_color text DEFAULT 'blue',
name text DEFAULT 'Dumbo')
$UJXPHQWQDPHVDUHRSWLRQDOEXWDUHXVHIXOEHFDXVHWKH\OHW\RXUHIHUWRDQ
DUJXPHQWE\QDPHLQVLGHWKHIXQFWLRQERG\)RUH[DPSOHWKLQNRIDIXQFWLRQWKDW
LVGHILQHGWRWDNHWKUHHLQSXWDUJXPHQWV WZREHLQJRSWLRQDO $UJXPHQWVFDQKDYHGHIDXOWYDOXHVZKLFKDOORZWKHFDOOHURIWKHIXQFWLRQWR
RPLWWKHP2SWLRQDODUJXPHQWVPXVWEHSRVLWLRQHGDIWHUQRQRSWLRQDODUJXPHQWV
LQWKHIXQFWLRQGHILQLWLRQ
RETURNS some type | set of some type | TABLE (..) AS
$$
BODY of function
$$
LANGUAGE language_of_function
:KHQ\RXFUHDWHDFROXPQRIWKHxmlGDWDW\SH3RVWJUH64/DXWRPDWLFDOO\
HQVXUHVWKDWRQO\YDOLG;0/YDOXHVSRSXODWHWKHURZV7KLVLVZKDWGLVWLQJXLVKHV
DQ;0/FROXPQIURPMXVWDQ\WH[WFROXPQ+RZHYHUWKH;0/LVQRWYDOLGDWHG
DJDLQVWDQ\'RFXPHQW7\SH'HILQLWLRQ '7' RU;0/6FKHPD'HILQLWLRQ
,QVHUWLQJ;0/'DWD
7KH;0/GDWDW\SHVLPLODUWR-621LVߡFRQWURYHUVLDOߢLQDUHODWLRQDOGDWDEDVH
EHFDXVHLWYLRODWHVWKHSULQFLSOHVRIQRUPDOL]DWLRQ1RQHWKHOHVVDOORIWKHKLJK
HQGUHODWLRQDOGDWDEDVHSURGXFWV ,%0'%2UDFOH64/6HUYHU VXSSRUW;0/
3RVWJUH64/DOVRMXPSHGRQWKHEDQGZDJRQDQGRIIHUVSOHQW\RIIXQFWLRQVWR
ERRW :HߞYHDXWKRUHGPDQ\DUWLFOHVRQZRUNLQJZLWK;0/LQ3RVWJUH64/
3RVWJUH64/FRPHVSDFNDJHGZLWKIXQFWLRQVIRUJHQHUDWLQJPDQLSXODWLQJDQG
SDUVLQJ;0/GDWD7KHVHDUHRXWOLQHGLQ;0/)XQFWLRQV8QOLNHWKHjsonbW\SH
WKHUHLVFXUUHQWO\QRGLUHFWLQGH[VXSSRUWIRULW6R\RXQHHGWRXVHIXQFWLRQDO
LQGH[HVWRLQGH[VXESDUWVVLPLODUWRZKDW\RXFDQGRZLWKWKHSODLQjsonW\SH
;0/
jsonb_setWDNHVWKUHHDUJXPHQWVRIIRUPjsonb_set(jsonb_to_update,
text_array_path, new_jsonb_value,allow_creation),I\RXVHW
allow_creationWRfalseZKHQWKHSURSHUW\GLGQRWDOUHDG\H[LVWWKHVWDWHPHQW
ZLOOUHWXUQDQHUURU
UPDATE persons_b
SET person = jsonb_set(person,'{children,1,gender}'::text[],'"F"'::jsonb, true)
WHERE person @> '{"name":"Sonia"}';
([DPSOH8VLQJWKHMVRQEBVHWIXQFWLRQWRFKDQJHDQHVWHGYDOXH
7RDGGDJHQGHUDWWULEXWHRUUHSODFHRQHWKDWZDVSUHYLRXVO\VHWZHFDQXVHWKH
jsonb_setIXQFWLRQDVVKRZQLQ([DPSOH
:KHQUHPRYLQJHOHPHQWVIURPDQDUUD\\RXQHHGWRGHQRWHWKHLQGH[%HFDXVH
-DYD6FULSWLQGH[HVVWDUWDWWRUHPRYHDQHOHPHQWIURPWKHVHFRQGFKLOGZHXVH
1LQVWHDGRI,IZHZDQWHGWRUHPRYH$]DOHDKHQWLUHO\ZHZRXOGKDYHXVHG
'{children,1}'::text[]
{"name": "Azaleah", "phones": []}
CREATE OR REPLACE FUNCTION func_name(arg1 arg1_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FHGXUDOIXQFWLRQVLQ-DYD6FULSW3/9LVDIDYRULWHIRUZHEGHYHORSHUVDQGD
GDUOLQJFRPSDQLRQWRWKHEXLOWLQ-621DQG-621%GDWDW\SHVFRYHUHGLQ
ߡ-621ߢ
,Q3RVWJUH64/DVLQPRVWGDWDEDVHV\RXFDQVWULQJDVHULHVRI64/VWDWHPHQWV
WRJHWKHUDQGWUHDWWKHPDVDXQLWHYHQFXVWRPL]LQJHDFKUXQE\SDVVLQJ
DUJXPHQWV'LIIHUHQWGDWDEDVHVDVFULEHGLIIHUHQWQDPHVIRUWKLVXQLWVWRUHG
SURFHGXUHVXVHUGHILQHGIXQFWLRQVDQGVRRQ3RVWJUH64/VLPSO\UHIHUVWRWKHP
DVIXQFWLRQV
&KDSWHU:ULWLQJ)XQFWLRQV
)RUPRUHLQYROYHGFKHFNVWKDWUHTXLUHFKHFNLQJDJDLQVW'7'RU;6'\RXߞOO
QHHGWRUHVRUWWRZULWLQJIXQFWLRQVDQGXVLQJWKRVHLQWKHFKHFNFRQVWUDLQW
EHFDXVH3RVWJUH64/GRHVQߞWKDYHEXLOWLQIXQFWLRQVWRKDQGOHWKRVHNLQGVRI
FKHFNV
ZHZLOOJHWWKLVHUURUERROR: new row for relation "families" violates
check constraint "chk_has_relation"
INSERT INTO families (profile) VALUES ('<family name="HsuObe"></family>');
,IZHWKHQWU\WRLQVHUWVRPHWKLQJOLNH
ALTER TABLE families ADD CONSTRAINT chk_has_relation
CHECK (xpath_exists('/family/member/relation', profile));
([DPSOH(QVXUHWKDWDOOUHFRUGVKDYHDWOHDVWRQHPHPEHUUHODWLRQ
(DFK;0/YDOXHFRXOGKDYHDGLIIHUHQW;0/VWUXFWXUH7RHQIRUFHXQLIRUPLW\
\RXFDQDGGDFKHFNFRQVWUDLQWFRYHUHGLQߡ&KHFN&RQVWUDLQWVߢWRWKH;0/
FROXPQ([DPSOHHQVXUHVWKDWDOOfamilyKDVDWOHDVWRQHrelation
HOHPHQW7KH'/family/member/relation'LV;3DWKV\QWD[DEDVLFZD\WR
UHIHUWRHOHPHQWVDQGRWKHUSDUWVRI;0/
CREATE TABLE families (id serial PRIMARY KEY, profile xml);
INSERT INTO families(profile)
VALUES (
'<family name="Gomez">
<member><relation>padre</relation><name>Alex</name></member>
<member><relation>madre</relation><name>Sonia</name></member>
<member><relation>hijo</relation><name>Brandon</name></member>
<member><relation>hija</relation><name>Azaleah</name></member>
</family>');
([DPSOH3RSXODWHDQ;0/ILHOG
;6' HYHQLILWLVVSHFLILHGLQWKH;0/GRFXPHQW7RIUHVKHQXSRQZKDW
FRQVWLWXWHVYDOLG;0/([DPSOHVKRZV\RXKRZWRDSSHQG;0/GDWDWRD
WDEOHE\GHFODULQJDFROXPQDVxmlDQGLQVHUWLQJLQWRLWDVXVXDO
physics
algebra
calculus
chemistry
economics
physics
algebra
calculus
chemistry
economics
physics
NULL
|
|
|
|
|
|
|
|
|
|
|
|
72.00
72.50
64.50
73.50
90.00
84.00
77.25
65.00
74.50
90.00
78.00
74.65
SELECT student, subject, AVG(score)::numeric(10,2)
FROM test_scores
WHERE student IN ('leo','regina')
GROUP BY CUBE (student, subject)
ORDER BY student NULLS LAST, subject NULLS LAST;
student | subject | avg
---------+-----------+------leo
| algebra
| 82.00
leo
| calculus | 65.50
leo
| chemistry | 75.50
leo
| physics
| 72.00
leo
| NULL
| 73.75
regina | algebra
| 72.50
regina | calculus | 64.50
regina | chemistry | 73.50
regina | economics | 90.00
regina | physics
| 84.00
regina | NULL
| 75.44
NULL
| algebra
| 77.25
NULL
| calculus | 65.00
NULL
| chemistry | 74.50
NULL
| economics | 90.00
NULL
| physics
| 78.00
NULL
| NULL
| 74.65
(17 rows)
([DPSOH$YJVFRUHIRUHDFKVWXGHQWVWXGHQWLQVXEMHFWVXEMHFWDQG
RYHUDOO
,IZHDOVRZDQWHGWRLQFOXGHVXEWRWDOVIRUMXVWWKHVXEMHFWDQGMXVWWKHVWXGHQW
ZHߞGXVHGROUPING SETS ( (student), (student, subject), (subject),
() )RUWKHVKRUWKDQGCUBE (student, subject)LQ([DPSOH
leo
|
regina |
regina |
regina |
regina |
regina |
NULL
|
NULL
|
NULL
|
NULL
|
NULL
|
NULL
|
(15 rows)
([DPSOH4XHU\;0/XVLQJ;0/7$%/(
1HZLQYHUVLRQLVWKH$16,64/VWDQGDUG;0/7$%/(FRQVWUXFW
;0/7$%/(FRQYHUWVWH[WRI;0/LQWRLQGLYLGXDOURZVDQGFROXPQVEDVHGRQ
VRPHGHILQHGWUDQVIRUPDWLRQ:HߞOOUHSHDW([DPSOHXVLQJ;0/7$%/(
*HWWKHWH[WHOHPHQWLQWKHrelationDQGnameWDJVRIHDFKmemberHOHPHQW
:HQHHGWRXVHDUUD\VXEVFULSWLQJEHFDXVHxpathDOZD\VUHWXUQVDQDUUD\
HYHQLIRQO\RQHHOHPHQWLVUHWXUQHG
*HWWKHQDPHDWWULEXWHIURPfamilyURRW)RUWKLVZHXVH@attribute_name
%UHDNWKHUHVXOWRIWKH6(/(&7LQWRWKHVXEHOHPHQWV<member>
<relation></relation><name></name>DQG</member>WDJV7KH
VODVKLVDZD\RIJHWWLQJDWVXEWDJHOHPHQWV)RUH[DPSOH
xpath('/family/member', 'profile')ZLOOUHWXUQDQDUUD\RIDOO
PHPEHUVLQHDFKIDPLO\WKDWLVGHILQHGLQDSURILOH7KH#VLJQLVXVHGWR
VHOHFWDWWULEXWHVRIDQHOHPHQW6RIRUH[DPSOHfamily/@nameUHWXUQVWKH
QDPHDWWULEXWHRIDfamily%\GHIDXOWxpathDOZD\VUHWXUQVDQHOHPHQW
LQFOXGLQJWKHWDJSDUW7KHtext()IRUFHVDUHWXUQRIMXVWWKHWH[WERG\RIDQ
HOHPHQW
SELECT ordinality AS id, family,
(xpath('/member/relation/text()', f))[1]::text As relation,
(xpath('/member/name/text()', f))[1]::text As mem_name
FROM (
SELECT
(xpath('/family/@name', profile))[1]::text As family,
f.ordinality, f.f
FROM families, unnest(xpath('/family/member', profile)) WITH ORDINALITY AS f
) x;
id | family | relation | mem_name
----+--------+----------+---------1 | Gomez | padre
| Alex
2 | Gomez | madre
| Sonia
3 | Gomez | hijo
| Brandon
4 | Gomez | hija
| Azaleah
(4 rows)
([DPSOH4XHU\;0/ILHOG
HOHPHQWVWKDWVDWLVILHVWKH;3DWKTXHU\([DPSOHFRPELQHVxpathZLWK
unnestWRUHWXUQDOOWKHIDPLO\PHPEHUVunnestXQUDYHOVWKHDUUD\LQWRDURZ
VHW:HWKHQFDVWWKH;0/IUDJPHQWWRWH[W
7RVHDUFKWH[WXDOGDWDE\NH\ZRUGV\RXKDYHDW\RXUGLVSRVDOWKHlikeRUilike
,ߞPVXUH\RXߞYHVHHQZHEVLWHVZKHUH\RXFDQVHDUFKE\W\SLQJLQNH\ZRUGV$Q
HFRPPHUFHVLWHZLOOEULQJXSDOLVWRIPDWFKLQJSURGXFWVDILOPVLWHZLOOEULQJ
XSDOLVWRIPDWFKLQJPRYLHVDNQRZOHGJHEDVHVLWHZLOOEULQJXSPDWFKLQJ
TXHVWLRQVDQGDQVZHUVHWF
)XOO7H[W6HDUFK
7KHILUVWSDUWLVDQ;0/SDWKHOHPHQWWKDWGHILQHVWKHURZ7KHZRUG
3$66,1*LVIROORZHGE\WKHWDEOHFROXPQWRSDUVHRXWURZV7KLVFROXPQ
KDVWREHRIW\SHxml:HXVHWKHfamilies.profileFROXPQRIRXUIDPLOLHV
WDEOH
7KH&2/8016FRPSRQHQWVKRXOGGHILQHWKHOLVWRIFROXPQVWREHSDUVHG
RXWRIWKH[PO
6LPLODUWR:,7+25',1$/,7<LQFRQMXQFWLRQZLWKVHWUHWXUQLQJIXQFWLRQV
\RXFDQXVH)2525',1$/,7<WRDVVLJQQXPHULFRUGHUWRHDFKUHFRUG
<RXFDQXVHWRPRYHXSDOHYHODERYHWKHEDVHRIWKHURZ,QWKLVFDVHZH
XVH#QDPHWRJHWWKHIDPLO\QDPHZKLFKLVRQHOHYHODERYH
IDPLO\PHPEHU7KH#LVXVHGWRGHQRWHWKLVLVDQDWWULEXWH VRPHWKLQJRI
IRUPname='a value' DQGQRWDQHOHPHQW
,IDSDWKHOHPHQWPDWFKHVWKHQDPHRI\RXUGHILQHGFROXPQ\RXGRQߞWQHHG
WRVSHFLI\WKH3$7+,QWKLVFDVHEHFDXVHIDPLO\PHPEHUUHODWLRQPDWFKHV
RXUFROXPQQDPHrelationZHFDQVNLSWKH3$7+FODXVH
SELECT xt.*
FROM families,
XMLTABLE ('/family/member' PASSING profile
COLUMNS
id FOR ORDINALITY ,
family text PATH '../@name' ,
relation text NOT NULL ,
member_name text PATH 'name' NOT NULL
) AS xt;
id | family | relation | mem_name
----+--------+----------+---------1 | Gomez | padre
| Alex
2 | Gomez | madre
| Sonia
3 | Gomez | hijo
| Brandon
4 | Gomez | hija
| Azaleah
(4 rows)
SELECT student, subject, AVG(score)::numeric(10,2)
FROM test_scores
WHERE student IN ('leo','regina')
GROUP BY ROLLUP (subject,student)
ORDER BY student NULLS LAST, subject NULLS LAST;
student | subject | avg
---------+-----------+------leo
| algebra
| 82.00
leo
| calculus | 65.50
leo
| chemistry | 75.50
([DPSOH$YJVFRUHIRUHDFKVWXGHQWLQVXEMHFWVXEMHFWDQGRYHUDOO
,IZHUHYHUVHWKHRUGHURIFROXPQVLQ52//83ZHJHWWKHVFRUHIRUHDFK
VWXGHQWVXEMHFWSDLUDYHUDJHIRUHDFKVXEMHFWDQGRYHUDOODYHUDJHDVVKRZQLQ
([DPSOH
SELECT student, subject, AVG(score)::numeric(10,2)
FROM test_scores
WHERE student IN ('leo','regina')
GROUP BY ROLLUP (student,subject)
ORDER BY student NULLS LAST, subject NULLS LAST;
student | subject | avg
---------+-----------+------leo
| algebra
| 82.00
leo
| calculus | 65.50
leo
| chemistry | 75.50
leo
| physics
| 72.00
leo
| NULL
| 73.75
regina | algebra
| 72.50
regina | calculus | 64.50
regina | chemistry | 73.50
regina | economics | 90.00
regina | physics
| 84.00
regina | NULL
| 75.44
NULL
| NULL
| 74.65
(12 rows)
([DPSOH$YJVFRUHIRUHDFKVWXGHQWLQVXEMHFWVWXGHQWDQGRYHUDOO
:KDWLIZHZDQWHGWRKDYHWRWDOEUHDNGRZQVIRUVWXGHQWVWXGHQWSOXVVXEMHFW
DQGRYHUDOODYHUDJH":HFRXOGUHYLVHRXUTXHU\WRDGGDXQLYHUVDOJURXSLQJVHW
GROUPING SETS ((student),(student, subject),())7KLVLVHTXLYDOHQWWR
WKHVKRUWKDQGROLLUP (student, subject)6HH([DPSOH
NULL
| economics | 90.00
NULL
| physics
| 78.00
(16 rows)
SELECT student, subject, AVG(score)::numeric(10,2)
FROM test_scores
WHERE student IN ('leo','regina')
GROUP BY GROUPING SETS ((student,subject),(student),(subject))
ORDER BY student NULLS LAST, subject NULLS LAST;
student | subject | avg
---------+-----------+------leo
| algebra | 82.00
leo
| calculus | 65.50
leo
| chemistry | 75.50
leo
| physics | 72.00
leo
| NULL
| 73.75
regina | algebra | 72.50
regina | calculus | 64.50
regina | chemistry | 73.50
regina | economics | 90.00
regina | physics | 84.00
regina | NULL
| 75.44
NULL
| algebra | 77.25
NULL
| calculus | 65.00
NULL
| chemistry | 74.50
([DPSOH$YJVFRUHIRUHDFKVWXGHQWVWXGHQWLQVXEMHFWDQGVXEMHFW
:HFDQHYHQLQFOXGHDWRWDOIRUHDFKVXEMHFWDFURVVDOOVWXGHQWVE\KDYLQJ
PXOWLSOHJURXSLQJVHWVDVVKRZQLQ([DPSOH
,QDVLQJOHTXHU\([DPSOHJLYHVXVERWKWKHDYHUDJHRIHDFKVWXGHQWDFURVV
DOOVXEMHFWVDQGKLVRUKHUDYHUDJHLQHDFKVXEMHFW
SELECT student, subject, AVG(score)::numeric(10,2)
FROM test_scores
WHERE student IN ('leo','regina')
GROUP BY GROUPING SETS ((student),(student,subject))
ORDER BY student, subject NULLS LAST;
student | subject | avg
---------+-----------+------leo
| algebra
| 82.00
leo
| calculus | 65.50
leo
| chemistry | 75.50
leo
| physics
| 72.00
leo
| NULL
| 73.75
reg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FDWHDFRQILJXUDWLRQWKDW\RXߞGOLNHDGGHGWR\RXUDUVHQDO
LQVWDOODWLRQLVTXLWHVLPSOHDQGXVXDOO\GRHVQߞWUHTXLUHDGGLWLRQDOFRPSLODWLRQ
:HGHPRQVWUDWHE\LQVWDOOLQJWKHSRSXODUKXQVSHOOFRQILJXUDWLRQ
<RXߞUHQRWOLPLWHGWREXLOWLQ)76FRQILJXUDWLRQV<RXFDQFUHDWH\RXURZQ%XW
EHIRUH\RXGR\RXPD\ZLVKWRVHHZKDWRWKHUXVHUVKDYHDOUHDG\FUHDWHGWKDW
PD\VXLW\RXUQHHGV,I\RXUWH[WLVPHGLFDOUHODWHG\RXPD\EHDEOHWRILQGD
FRQILJXUDWLRQZLWKGLFWLRQDULHVFKRFNIXOORIVSHFLDOL]HGDQDWRP\WHUPV,I\RXU
WH[WLVLQ6SDQLVKILQGDFRQILJXUDWLRQWKDWWDLORUVWR\RXUSDUWLFXODUGLDOHFWRI
6SDQLVK
,I\RXQHHGWRFUHDWH\RXURZQFRQILJXUDWLRQVRUGLFWLRQDULHVUHIHUWR
3RVWJUH64/0DQXDO)XOO7H[W6HDUFK&RQILJXUDWLRQDQG3RVWJUH64/0DQXDO
)XOO7H[W6HDUFK'LFWLRQDULHV
cfgname
---------simple
danish
dutch
english
finnish
french
german
hungarian
italian
norwegian
portuguese
romanian
russian
spanish
swedish
turkish
(16 rows)
7RVHHWKHOLVWLQJRILQVWDOOHG)76FRQILJXUDWLRQVUXQWKHTXHU\SELECT
cfgname FROM pg_ts_config;2UXVHWKH\dFFRPPDQGLQSVTO$W\SLFDOOLVW
IROORZV
0RVW3RVWJUH64/GLVWULEXWLRQVFRPHSDFNDJHGZLWKRYHU)76FRQILJXUDWLRQV
$OOWKHVHDUHLQVWDOOHGLQWKHSJBFDWDORJVFKHPD
)76&RQILJXUDWLRQV
([DPSOH$YJVFRUHIRUHDFKVWXGHQWDQGVWXGHQWLQVXEMHFW
)RURXUWDEOHRIWHVWVFRUHVLIZHQHHGWRILQGERWKWKHRYHUDOODYHUDJHSHU
VWXGHQWDQGWKHDYHUDJHSHUVWXGHQWE\VXEMHFWZHFRXOGZULWHDTXHU\DVVKRZQ
LQ([DPSOHWDNLQJDGYDQWDJHRIJURXSLQJVHWV
,I\RXߞYHHYHUWULHGWRFUHDWHDVXPPDU\UHSRUWWKDWLQFOXGHVERWKWRWDOVDQG
VXEWRWDOV\RXߞOODSSUHFLDWHWKHFDSDELOLW\WRSDUWLWLRQ\RXUGDWDRQWKHIO\
*URXSLQJVHWVOHW\RXGRH[DFWO\WKDW
*5283,1*6(76&8%(52//83
,IZHGLGQߞWKDYHWKHWHEREFRQGLWLRQH[FOXGLQJWKHGD\UHFRUG
ZHZRXOGKDYHURZVZLWKWKHWKURZEHLQJ1 | 132 days | 2012-01-01
00:00:00-04
,Q([DPSOHWITH ORDINALITYJHWVDSSOLHGWRWKHUHVXOWRIWKHVHW
UHWXUQLQJIXQFWLRQ,WDOZD\VJHWVDSSOLHGEHIRUHWKHWHEREFRQGLWLRQ$VD
UHVXOWWKHUHLVDJDSLQQXPEHULQJLQWKHILQDOUHVXOW WKHQXPEHULVODFNLQJIRU
WKHGD\LQWHUYDO EHFDXVHWKHQXPEHUZDVILOWHUHGRXWE\RXUWHERE
FRQGLWLRQ
SELECT d.ord, i_type, d.dt
FROM
interval_periods CROSS JOIN LATERAL
generate_series('2012-01-01'::date, '2012-12-31'::date, i_type)
WITH ORDINALITY AS d(dt,ord)
WHERE NOT (dt = '2012-01-01' AND i_type = '132 days'::interval);
ord | i_type
| dt
----+------------+----------------------1 | 5 mons
| 2012-01-01 00:00:00-05
2 | 5 mons
| 2012-06-01 00:00:00-04
3 | 5 mons
| 2012-11-01 00:00:00-04
2 | 132 days | 2012-05-12 00:00:00-04
3 | 132 days | 2012-09-21 00:00:00-04
1 | 4862:00:00 | 2012-01-01 00:00:00-05
2 | 4862:00:00 | 2012-07-21 15:00:00-04
(7 rows)
([DPSOH8VLQJ:,7+25',1$/,7<ZLWK/$7(5$/
([DPSOHZHUHSHDWWKHLATERALLQ([DPSOHEXWDGGRQDVHTXHQWLDO
QXPEHUWRHDFKVHW
<RXߞOORIWHQILQGWITH ORDINALITYSDLUHGZLWKWKHLATERALFRQVWUXFW,Q
WITH ORDINALITYDOZD\VDGGVDQDGGLWLRQDOFROXPQDWWKHHQGRIWKHUHVXOW
FDOOHGRUGLQDOLW\DQGWITH ORDINALITYFDQRQO\DSSHDULQWKHFROMFODXVHRI
DQ64/VWDWHPHQW<RXDUHIUHHWRUHQDPHWKHRUGLQDOLW\FROXPQ
SELECT dt.*
FROM generate_series('2016-01-01'::date,'2016-12-31'::date,interval '1 month')
WITH ORDINALITY As dt;
dt
| ordinality
-----------------------+----------2016-01-01 00:00:00-05 |
1
2016-02-01 00:00:00-05 |
2
2016-03-01 00:00:00-05 |
3
2016-04-01 00:00:00-04 |
4
2016-05-01 00:00:00-04 |
5
2016-06-01 00:00:00-04 |
6
2016-07-01 00:00:00-04 |
7
2016-08-01 00:00:00-04 |
8
2016-09-01 00:00:00-04 |
9
2016-10-01 00:00:00-04 |
10
2016-11-01 00:00:00-04 |
11
2016-12-01 00:00:00-05 |
12
(12 rows)
([DPSOH1XPEHULQJUHVXOWVIURPVHWUHWXUQLQJIXQFWLRQV
([DPSOHGHPRQVWUDWHVWITH ORDINALITYXVHGLQFRQMXQFWLRQZLWKWKH
WHPSRUDOYDULDQWRIWKHgenerate_seriesIXQFWLRQ
<RXߞOOILQGWITH ORDINALITYRIWHQXVHGZLWKIXQFWLRQVOLNHgenerate_series
unnestDQGRWKHUIXQFWLRQVWKDWH[SDQGRXWFRPSRVLWHW\SHVDQGDUUD\V,WFDQ
EHXVHGZLWKDQ\VHWUHWXUQLQJIXQFWLRQLQFOXGLQJRQHV\RXFUHDWH\RXUVHOI
$OWKRXJK\RXFDQߞWXVHWITH ORDINALITYZLWKWDEOHVDQGVXETXHULHV\RXFDQDFKLHYHWKH
VDPHUHVXOWIRUWKRVHE\XVLQJWKHZLQGRZIXQFWLRQ52:B180%(5
127(
,QWURGXFHGLQYHUVLRQWKHWITH ORDINALITYFODXVHLVDQ64/$16,
VWDQGDUGFRQVWUXFWWITH ORDINALITYDGGVDVHTXHQWLDOQXPEHUFROXPQWRDVHW
UHWXUQLQJIXQFWLRQUHVXOW
\dF+ english_hunspell;
Text search configuration "pg_catalog.english_hunspell"
Parser: "pg_catalog.default"
Token
| Dictionaries
----------------+------------------------------asciihword
| english_hunspell,english_stem
asciiword
| english_hunspell,english_stem
email
| simple
file
| simple
float
| simple
host
| simple
hword
| english_hunspell,english_stem
hword_asciipart | english_hunspell,english_stem
hword_numpart | simple
hword_part
| english_hunspell,english_stem
int
| simple
numhword
| simple
numword
| simple
sfloat
| simple
uint
| simple
url
| simple
url_path
| simple
version
| simple
word
| english_hunspell,english_stem
([DPSOH)76FRQILJXUDWLRQKXQVSHOO
)URPSVTOLI\RXQRZUXQ([DPSOH\RXߞOOVHHGHWDLOVRIWKHKXQVSHOO
FRQILJXUDWLRQDQGGLFWLRQDU\ZHMXVWLQVWDOOHG
CREATE EXTENSION hunspell_en_us SCHEMA pg_catalog;
1H[WUXQ
&RS\WKHKXQVSHOOBHQBXV VTODQGKXQVSHOOBHQBXVFRQWUROILOHVWR\RXU
3RVWJUH64/LQVWDOODWLRQGLUHFWRU\VKDUHH[WHQVLRQIROGHU
&RS\HQBXVDIIL[DQGHQBXVGLFWWR\RXU3RVWJUH64/LQVWDOODWLRQGLUHFWRU\
VKDUHWVHDUFKBGDWD
'RZQORDGHYHU\WKLQJLQWKHIROGHU
6WDUWE\GRZQORDGLQJKXQVSHOOFRQILJXUDWLRQVIURPKXQVSHOOBGLFWV<RXߞOOEH
JUHHWHGE\KXQVSHOOIRUPDQ\GLIIHUHQWODQJXDJHV:HߞOOJRZLWKKXQVSHOOBHQBXV
ALTER DATABASE postgresql_book
SET default_text_search_config = 'pg_catalog.english';
7RUHSODFHWKHGHIDXOWZLWKDQRWKHUUXQ
SHOW default_text_search_config;
1RWVXUHZKLFKFRQILJXUDWLRQLVWKHGHIDXOW"5XQ
7KHRQO\GLIIHUHQFHEHWZHHQWKHWZRLVWKDWKXQVSHOOGUDZVIURPDQDGGLWLRQDO
GLFWLRQDU\
\dF+ english;
Text search configuration "pg_catalog.english"
Parser: "pg_catalog.default"
Token
| Dictionaries
----------------+-------------asciihword
| english_stem
asciiword
| english_stem
email
| simple
file
| simple
float
| simple
host
| simple
hword
| english_stem
hword_asciipart | english_stem
hword_numpart
| simple
hword_part
| english_stem
int
| simple
numhword
| simple
numword
| simple
sfloat
| simple
uint
| simple
url
| simple
url_path
| simple
version
| simple
word
| english_stem
([DPSOH)76(QJOLVKFRQILJXUDWLRQ
&RQWUDVWWKDWRXWSXWWRWKHEXLOWLQ(QJOLVKFRQILJXUDWLRQLQ([DPSOHZKLFK
JLYHV\RXWKHGLFWLRQDULHVXVHGE\WKH(QJOLVKFRQILJXUDWLRQ
.HHSLQPLQGWKDWQRWDOO)76FRQILJXUDWLRQVLQVWDOOLQWKHVDPHZD\5HDGWKHLQVWUXFWLRQV
:$51,1*
|
|
|
|
|
2012-11-01
2012-05-12
2012-09-21
2012-01-01
2012-07-21
00:00:00-04
00:00:00-04
00:00:00-04
00:00:00-05
15:00:00-04
:,7+25',1$/,7<
2WKHUGDWDEDVHSURGXFWVDOVRRIIHUODWHUDOMRLQVDOWKRXJKWKH\GRQߞWDELGHE\WKH
$16,PRQLNHU,Q2UDFOH\RXߞGXVHDWDEOHSLSHOLQHFRQVWUXFW,Q64/6HUYHU
\RXߞGXVHCROSS APPLYRUOUTER APPLY
<RXFDQXVHPXOWLSOHODWHUDOMRLQVLQ\RXU64/DQGHYHQFKDLQWKHPLQVHTXHQFH
DV\RXZRXOGZKHQMRLQLQJPRUHWKDQWZRVXETXHULHV<RXFDQVRPHWLPHVJHW
DZD\ZLWKRPLWWLQJWKHLATERALNH\ZRUGWKHTXHU\SDUVHULVVPDUWHQRXJKWR
ILJXUHRXWDODWHUDOMRLQLI\RXKDYHDFRUUHODWHGH[SUHVVLRQ%XWZHDGYLVHWKDW
\RXDOZD\VLQFOXGHWKHNH\ZRUGIRUWKHVDNHRIFODULW\$OVR\RXߞOOJHWDQHUURU
LI\RXZULWH\RXUVWDWHPHQWDVVXPLQJWKHXVHRIDODWHUDOMRLQEXWUXQWKH
VWDWHPHQWRQDSUHODWHUDOYHUVLRQ3RVWJUH64/:LWKRXWWKHNH\ZRUG3RVWJUH64/
PLJKWHQGXSSHUIRUPLQJDMRLQZLWKXQLQWHQGHGUHVXOWV
$OWKRXJK\RXFDQDFKLHYHWKHVDPHUHVXOWVE\XVLQJZLQGRZIXQFWLRQVODWHUDO
MRLQV\LHOGIDVWHUUHVXOWVZLWKPRUHVXFFLQFWV\QWD[
SELECT u.user_name, l.description, l.log_ts
FROM
super_users AS u CROSS JOIN LATERAL (
SELECT description, log_ts
FROM logs
WHERE
log_ts > CURRENT_TIMESTAMP - interval '100 days' AND
logs.user_name = u.user_name
ORDER BY log_ts DESC LIMIT 5
) AS l;
([DPSOH8VLQJ/$7(5$/WROLPLWURZVIURPDMRLQHGWDEOH
/DWHUDOLVDOVRKHOSIXOIRUXVLQJYDOXHVIURPWKHOHIWKDQGVLGHWROLPLWWKHQXPEHU
RIURZVUHWXUQHGIURPWKHULJKWKDQGVLGH([DPSOHXVHVLATERALWRUHWXUQ
IRUHDFKVXSHUXVHUZKRKDVXVHGRXUVLWHZLWKLQWKHODVWGD\VWKHODVWILYH
ORJLQVDQGZKDWWKH\ZHUHXSWR7DEOHVXVHGLQWKLVH[DPSOHZHUHFUHDWHGLQ
ߡ7<3(2)ߢDQGߡ%DVLF7DEOH&UHDWLRQߢ
5 mons
132 days
132 days
4862:00:00
4862:00:00
SELECT i_type, dt
FROM
interval_periods CROSS JOIN LATERAL
generate_series('2012-01-01'::date, '2012-12-31'::date, i_type) AS dt
WHERE NOT (dt = '2012-01-01' AND i_type = '132 days'::interval);
i_type
| dt
------------+----------------------5 mons
| 2012-01-01 00:00:00-05
5 mons
| 2012-06-01 00:00:00-04
([DPSOH8VLQJ/$7(5$/ZLWKJHQHUDWHBVHULHV
CREATE TABLE interval_periods(i_type interval);
INSERT INTO interval_periods (i_type)
VALUES ('5 months'), ('132 days'), ('4862 hours');
7KHUHDUHVLWXDWLRQVZKHQ\RXVKRXOGDYDLO\RXUVHOIRILATERALWRDYRLG
H[WUHPHO\FRQYROXWHGV\QWD[,Q([DPSOHDFROXPQRQWKHOHIWVHUYHVDVD
SDUDPHWHULQWKHgenerate_seriesIXQFWLRQRQWKHULJKW
LATERALOHWV\RXVKDUHGDWDLQFROXPQVDFURVVWZRWDEOHVLQDFROMFODXVH
+RZHYHULWZRUNVRQO\LQRQHGLUHFWLRQWKHULJKWKDQGVLGHFDQGUDZIURPWKH
OHIWKDQGVLGHEXWQRWYLFHYHUVD
SELECT *
FROM
census.facts L INNER JOIN LATERAL
(
SELECT *
FROM census.lu_fact_types
WHERE category = CASE WHEN L.yr = 2011
THEN 'Housing' ELSE category END
) R
ON L.fact_type_id = R.fact_type_id;
1RZDGGWKHLATERALNH\ZRUGDQGWKHHUURULVJRQH
INNER JOIN
(
SELECT *
FROM census.lu_fact_types
WHERE category = CASE WHEN L.yr = 2011
THEN 'Housing' ELSE category END
) R
ON L.fact_type_id = R.fact_type_id;
([DPSOHGHPRQVWUDWHVKRZIRXUGLIIHUHQW)76FRQILJXUDWLRQVUHVXOWLQ
GLIIHUHQWYHFWRUV1RWHKRZWKH(QJOLVKDQG+XQVSHOOFRQILJXUDWLRQVUHPRYHDOO
SELECT
c.name,
CASE
WHEN c.name ='default' THEN to_tsvector(f.t)
ELSE to_tsvector(c.name::regconfig,f.t)
END As vect
FROM (
SELECT 'Just dancing in the rain. I like to dance.'::text) As f(t), (
VALUES ('default'),('english'),('english_hunspell'),('simple')
) As c(name);
name
| vect
-----------------+---------------------------------------------------------------------------default
| 'danc':2,9 'like':7 'rain':5
english
| 'danc':2,9 'like':7 'rain':5
english_hunspell | 'dance':2,9 'danc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HZFRQVWUXFWVWKH
setweightIXQFWLRQDQGWKHFRQFDWHQDWLRQRSHUDWRU || WRWVYHFWRU
([DPSOHYHFWRUL]HVWKHWLWOHDQGGHVFULSWLRQFROXPQVDQGVWRUHVWKHYHFWRU
LQDQHZO\FUHDWHGWVYHFWRUFROXPQ7RVSHHGXSVHDUFKHVZHDGGD*,1LQGH[
RQWKHWVYHFWRUFROXPQ*,1LVDORVVOHVVLQGH[<RXFDQDOVRDGGD*L67LQGH[
RQDYHFWRUFROXPQ*L67LVORVV\DQGVORZHUWRVHDUFKEXWEXLOGVTXLFNHUDQG
WDNHVXSOHVVGLVNVSDFH:HH[SORUHLQGH[HVLQPRUHGHWDLOLQߡ,QGH[HVߢ
ALTER TABLE film ADD COLUMN fts tsvector;
UPDATE film
SET fts =
setweight(to_tsvector(COALESCE(title,'')),'A') ||
setweight(to_tsvector(COALESCE(description,'')),'B');
CREATE INDEX ix_film_fts_gin ON film USING gin (fts);
([DPSOH$GGWVYHFWRUFROXPQDQGSRSXODWHZLWKZHLJKWV
1H[WZHDGGDQGFRPSXWHDWVYHFWRUFROXPQWRWKHILOPWDEOHDVVKRZQLQ
([DPSOH
\encoding utf8;
\i film.sql
)RURXUH[DPSOHVZHJDWKHUHGILFWLWLRXVPRYLHGDWD/RDGWKHWDEOHVIURPSVTO
XVLQJWKHILOHVTOVFULSWDVIROORZV
7RLQFRUSRUDWH)76LQWR\RXUGDWDEDVHDGGDWVYHFWRUFROXPQWR\RXUWDEOH<RX
WKHQHLWKHUVFKHGXOHWKHWVYHFWRUFROXPQWREHXSGDWHGUHJXODUO\RUDGGDWULJJHU
WRWKHWDEOHVRWKDWZKHQHYHUUHOHYDQWILHOGVXSGDWHWKHWVYHFWRUILHOG
UHFRPSXWHV
7KHWRBWVYHFWRUIXQFWLRQUHWXUQVZKHUHHDFKOH[HPHDSSHDUVLQWKHWH[W6RIRU
H[DPSOH'danc':2,9PHDQVWKDWGDQFLQJDQGGDQFHDSSHDUDVWKHVHFRQGDQG
WKHQLQWKZRUGV
VWRSZRUGVVXFKDVMXVWDQGWR(QJOLVKDQG+XQVSHOODOVRFRQYHUWZRUGVWRWKHLU
QRUPDOL]HGIRUPDVGLFWDWHGE\WKHLUGLFWLRQDULHVVRGDQFLQJEHFRPHVGDQFDQG
GDQFHUHVSHFWLYHO\7KHVLPSOHFRQILJXUDWLRQKDVQRFRQFHSWRIVWHPPLQJDQG
VWRSZRUGV
census.facts L
SELECT *
FROM
LATERALLVDQHZ$16,64/FRQVWUXFWLRQLQYHUVLRQ+HUHߞVWKHPRWLYDWLRQ
EHKLQGLWVXSSRVH\RXSHUIRUPMRLQVRQWZRWDEOHVRUVXETXHULHVQRUPDOO\WKH
SDLUSDUWLFLSDWLQJLQWKHMRLQDUHLQGHSHQGHQWXQLWVDQGFDQߞWUHDGGDWDIURPHDFK
RWKHU)RUH[DPSOHWKHIROORZLQJLQWHUDFWLRQZRXOGJHQHUDWHDQHUURUEHFDXVH
l.yr = 2011LVQRWDFROXPQRQWKHULJKWKDQGVLGHRIWKHMRLQ
/DWHUDO-RLQV
*HWDOLVWRIDOOWDEOHVWKDWKDYHFKLOGWDEOHVEXWQRSDUHQWWDEOH
7KLVLVWKHUHFXUVLYHSDUWLWJHWVDOOFKLOGUHQRIWDEOHVLQtbls
7KHQDPHVRIWKHFKLOGWDEOHVVWDUWZLWKWKHSDUHQWDOQDPH
5HWXUQSDUHQWVDQGDOOFKLOGWDEOHV%HFDXVHZHVRUWE\WDEOHQDPHZKLFK
SUHSHQGVWKHSDUHQWQDPHDOOFKLOGWDEOHVZLOOIROORZWKHLUSDUHQWVLQWKHLU
RXWSXW
)
SELECT * FROM tbls ORDER BY tablename;
tableoid | schemaname | tablename
---------+------------+--------------------------------------3152249 | public
| logs
3152260 | public
| logs->logs_2011
3152272 | public
| logs->logs_2011->logs_2011_01_02
pg_namespace n ON n.oid = c.relnamespace LEFT JOIN
pg_tablespace t ON t.oid = c.reltablespace LEFT JOIN
pg_inherits As th ON th.inhrelid = c.oid
WHERE
th.inhrelid IS NULL AND
c.relkind = 'r'::"char" AND c.relhassubclass
UNION ALL
SELECT
c.oid As tableoid,
n.nspname AS schemaname,
tbls.tablename || '->' || c.relname AS tablename
FROM
tbls INNER JOIN
pg_inherits As th ON th.inhparent = tbls.tableoid INNER JOIN
pg_class c ON th.inhrelid = c.oid LEFT JOIN
pg_namespace n ON n.oid = c.relnamespace LEFT JOIN
pg_tablespace t ON t.oid = c.reltablespace
WITH RECURSIVE tbls AS (
SELECT
c.oid As tableoid,
n.nspname AS schemaname,
c.relname AS tablename
FROM
pg_class c LEFT JOIN
([DPSOH5HFXUVLYH&7(
,Q([DPSOHZHTXHU\WKHV\VWHPFDWDORJWROLVWWKHFDVFDGLQJWDEOH
UHODWLRQVKLSVZHKDYHLQRXUGDWDEDVH
$FRPPRQXVHRIUHFXUVLYH&7(VLVWRUHSUHVHQWPHVVDJHWKUHDGVDQGRWKHUWUHH
OLNHVWUXFWXUHV:HKDYHDQH[DPSOHRIWKLVLQ5HFXUVLYH&7(WR'LVSOD\7UHH
6WUXFWXUHV
7RWXUQDEDVLF&7(WRDUHFXUVLYHRQHDGGWKHRECURSIVEPRGLILHUDIWHUWKH
WITHWITH RECURSIVEFDQFRQWDLQDPL[RIUHFXUVLYHDQGQRQUHFXUVLYHWDEOH
H[SUHVVLRQV,QPRVWRWKHUGDWDEDVHVWKHRECURSIVENH\ZRUGLVQRWQHFHVVDU\WR
GHQRWHUHFXUVLRQ
7KHRIILFLDOGRFXPHQWDWLRQIRU3RVWJUH64/GHVFULEHVLWEHVWߡ7KHRSWLRQDO
RECURSIVEPRGLILHUFKDQJHV&7(IURPDPHUHV\QWDFWLFFRQYHQLHQFHLQWRD
IHDWXUHWKDWDFFRPSOLVKHVWKLQJVQRWRWKHUZLVHSRVVLEOHLQVWDQGDUG64/ߢ$
PRUHLQWHUHVWLQJ&7(LVRQHWKDWXVHVDUHFXUVLYHO\GHILQLQJFRQVWUXFWWREXLOGDQ
H[SU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YHFWRUFROXPQV:H
QRZVKRZ\RXKRZWRYHFWRUL]HWKHVHDUFKWHUPV
764XHULHV
([DPSOHUHDFWVWRDQLQVHUWRUXSGDWHLQWKHWLWOHRUGHVFULSWLRQE\
UHYHFWRULQJWKHIWVFROXPQ2QHVKRUWFRPLQJWKRXJKWVYHFWRUBXSGDWHBWULJJHU
GRHVQRWVXSSRUWZHLJKWLQJ
CREATE TRIGGER trig_tsv_film_iu
BEFORE INSERT OR UPDATE OF title, description ON film FOR EACH ROW
EXECUTE PROCEDURE tsvector_update_trigger(fts,'pg_catalog.english',
title,description);
([DPSOH7ULJJHUWRDXWRPDWLFDOO\XSGDWHWVYHFWRU
6KRXOGGDWDFKDQJHLQRQHRIWKHEDVLVFROXPQVIRUPLQJWKHWVYHFWRU\RXPXVW
UHYHFWRUL]H7RDYRLGKDYLQJWRPDQXDOO\UXQWRBWVYHFWRUHYHU\WLPHGDWD
FKDQJHVFUHDWHDWULJJHUWKDWUHVSRQGVWRXSGDWHV,QWKHWULJJHUXVHWKHKDQG\
WVYHFWRUBXSGDWHBWULJJHUIXQFWLRQDVVKRZQLQ([DPSOH
769HFWRUVFDQEHIRUPHGIURPRWKHUWVYHFWRUVXVLQJWKHFRQFDWHQDWLRQ ||
RSHUDWRU:HXVHGLWKHUHWRFRPELQHWKHWLWOHDQGGHVFULSWLRQLQWRDVLQJOH
WVYHFWRU7KLVZD\ZKHQZHVHDUFKZHKDYHWRFRQWHQGZLWKRQO\DVLQJOH
FROXPQ
ZHLJKWWRHDFK7KHZHLJKWVPXVWEH$%&RU'ZLWK$UDQNLQJKLJKHVWLQ
LPSRUWDQFH,Q([DPSOHZHDVVLJQHG$WROH[HPHVFXOOHGIURPWKHWLWOHDQG
%WROH[HPHVIURPWKHGHVFULSWLRQ,IRXUVHDUFKWHUPPDWFKHVDOH[HPHIURPWKH
WLWOHZHGHHPWKHPDWFKWREHPRUHUHOHYDQWWKDQDPDWFKIURPWKHGHVFULSWLRQ
RIWKHPRYLH
WRBWVTXHU\DQGSODLQWRBWVTXHU\ORRNRQO\DWZRUGVQRWWKHLUVHTXHQFH6R
EXVLQHVVDQDO\WLFVDQGDQDO\WLFVEXVLQHVVSURGXFHWKHVDPHWVTXHU\7KLVLVD
VKRUWFRPLQJEHFDXVH\RXߞUHOLPLWHGWRVHDUFKLQJE\VLQJOHZRUGVRQO\9HUVLRQ
DGGUHVVHGWKLVZLWKWKHIXQFWLRQSKUDVHWRBWVTXHU\,Q([DPSOHWKH
SKUDVHWRBWVTXHU\YHFWRUL]HVWKHZRUGVLQVHUWLQJWKHGLVWDQFHRSHUDWRUEHWZHHQ
SELECT plainto_tsquery('business analytics');
plainto_tsquery
----------------'busi' & 'analyt'
([DPSOH764XHU\FRQVWUXFWLRQVSODLQWRBTXHU\
$VOLJKWYDULDQWRIWRBWVTXHU\LVSODLQBWRWVTXHU\7KLVIXQFWLRQDXWRPDWLFDOO\
LQVHUWVWKHDQGRSHUDWRUEHWZHHQZRUGVIRU\RXVDYLQJ\RXDIHZNH\FOLFNV6HH
([DPSOH
<RXVKRXOGXVHWKHVDPH)76FRQILJXUDWLRQDVWKHRQH\RXXVHGWREXLOGWKHWVYHFWRU
:$51,1*
%RWKH[DPSOHVDUHDNLQWRVHDUFKLQJIRUWH[WFRQWDLQLQJWKHZRUGVEXVLQHVVDQG
DQDO\WLFV7KHDQGRSHUDWRU & PHDQVWKDWERWKZRUGVPXVWDSSHDULQWKH
VHDUFKHGWH[W7KHRURSHUDWRU | PHDQVRQHRUERWKRIWKHZRUGVPXVWDSSHDULQ
WKHVHDUFKHGWH[W,IWKHFRQILJXUDWLRQLQXVHILQGVPXOWLSOHVWHPVIRUDZRUG
WKH\DUHVWLWFKHGWRJHWKHUE\WKHRURSHUDWRU
SELECT to_tsquery('business & analytics');
to_tsquery
----------------'busi' & 'analyt'
SELECT to_tsquery('english_hunspell','business & analytics');
to_tsquery
-------------------------------('business' | 'busy') & 'analyt'
([DPSOH764XHU\FRQVWUXFWLRQVWRBTXHU\
([DPSOHVKRZVWKHRXWSXWXVLQJWKHWRBWVTXHU\IXQFWLRQVDJDLQVWWZR
FRQILJXUDWLRQVWKHGHIDXOW(QJOLVKFRQILJXUDWLRQDQGWKH+XQVSHOOFRQILJXUDWLRQ
UXQWKHP\RXFRXOGGHILQHDWVTXHU\FROXPQLQDWDEOH
CREATE TABLE logs_2011_01_02 (
PRIMARY KEY (log_id),
CONSTRAINT chk
CHECK (log_ts >= '2011-01-01' AND log_ts < '2011-03-01')
)
INHERITS (logs_2011);
7KHZULWDEOH&7(H[WHQGVWKH&7(WRDOORZIRUXSGDWHGHOHWHDQGLQVHUW
VWDWHPHQWV:HߞOOUHYLVLWRXUORJVWDEOHVWKDWZHFUHDWHGLQ([DPSOHDGGLQJ
DQRWKHUFKLOGWDEOHDQGSRSXODWLQJLW
:ULWDEOH&7(V
cte1 AS (
SELECT
tract_id,
substring(tract_id,1, 5) As county_code,
COUNT(*) OVER (PARTITION BY substring(tract_id,1,5)) As cnt_tracts
FROM census.lu_tracts
),
cte2 AS (
SELECT
MAX(tract_id) As last_tract,
county_code,
cnt_tracts
FROM cte1
WHERE cnt_tracts < 8 GROUP BY county_code, cnt_tracts
)
SELECT c.last_tract, f.fact_type_id, f.val
FROM census.facts As f INNER JOIN cte2 c ON f.tract_id = c.last_tract;
WITH
([DPSOH0XOWLSOH&7(V
<RXFDQVWXIIDVPDQ\&7(VDV\RXOLNHVHSDUDWHGE\FRPPDVLQWRWKHWITH
FODXVHDVVKRZQLQ([DPSOH7KHRUGHURIWKH&7(VPDWWHUVLQWKDW&7(V
GHILQHGODWHUFDQFDOO&7(VGHILQHGHDUOLHUEXWQRWYLFHYHUVD
cteLVWKHQDPHRIWKH&7(LQ([DPSOHGHILQHGXVLQJDSELECTVWDWHPHQW
WRFRQWDLQWKUHHFROXPQVtract_idcounty_codeDQGcnt_tracts7KHPDLQ
SELECTUHIHUVWRWKH&7(
FROM cte
WHERE cnt_tracts > 100
GROUP BY county_code, cnt_tracts;
WITH cte AS (
SELECT
tract_id, substring(tract_id,1, 5) As county_code,
COUNT(*) OVER(PARTITION BY substring(tract_id,1, 5)) As cnt_tracts
FROM census.lu_tracts
)
SELECT MAX(tract_id) As last_tract, county_code, cnt_tracts
([DPSOH%DVLF&7(
7KHEDVLF&7(ORRNVOLNH([DPSOH7KHWITHNH\ZRUGLQWURGXFHVWKH&7(
%DVLF&7(V
3RVWJUH64/DOORZV\RXWRKDYHD&7(WKDWLVERWKZULWDEOHDQGUHFXUVLYH
7KLVSXWVDQHQWLUHO\QHZZKLUORQVWDQGDUG&7(7KHURZVUHWXUQHGE\D
UHFXUVLYH&7(YDU\GXULQJWKHH[HFXWLRQRIWKHTXHU\
5HFXUVLYH&7(
7KLVLVDQH[WHQVLRQRIWKHEDVLF&7(ZLWKUPDATEINSERTDQGDELETE
FRPPDQGV$FRPPRQILQDOVWHSLQWKH&7(LVWRUHWXUQFKDQJHGURZV
:ULWDEOH&7(
7KLVLV\RXUSODLQYDQLOOD&7(XVHGWRPDNH\RXU64/PRUHUHDGDEOHRUWR
HQFRXUDJHWKHSODQQHUWRPDWHULDOL]HDFRVWO\LQWHUPHGLDWHUHVXOWIRUEHWWHU
SHUIRUPDQFH
%DVLF&7(
7KHUHDUHWKUHHZD\VWRXVH&7(V
(VVHQWLDOO\FRPPRQWDEOHH[SUHVVLRQV &7(V DOORZ\RXWRGHILQHDTXHU\WKDW
FDQEHUHXVHGLQDODUJHUTXHU\&7(VDFWDVWHPSRUDU\WDEOHVGHILQHGZLWKLQWKH
VFRSHRIWKHVWDWHPHQWWKH\ߞUHJRQHRQFHWKHHQFORVLQJVWDWHPHQWKDVILQLVKHG
H[HFXWLQJ
&RPPRQ7DEOH([SUHVVLRQV
IXQFWLRQ2WKHUGDWDEDVHVWHQGWROLPLWZLQGRZIXQFWLRQVWRXVLQJEXLOWLQ
DJJUHJDWHVVXFKDVAVGSUMMINDQGMAX
:HKDYHFUHDWHGDWVYHFWRUIURPRXUWH[WZHKDYHFUHDWHGDWVTXHU\IURPRXU
VHDUFKWHUPV1RZZHFDQSHUIRUPDQ)76:HGRVRE\XVLQJWKH@@RSHUDWRU
([DPSOHGHPRQVWUDWHVLW
8VLQJ)XOO7H[W6HDUFK
WVTXHULHVDQGWVYHFWRUVKDYHDGGLWLRQDORSHUDWRUVIRUGRLQJWKLQJVOLNH
GHWHUPLQLQJLIRQHLVDVXEVHWRIDQRWKHUDQGVHYHUDORWKHUIXQFWLRQV$OOWKLVLV
GHWDLOHGLQ3RVWJUH64/0DQXDO7H[W6HDUFK)XQFWLRQVDQG2SHUDWRUV
SELECT plainto_tsquery('business analyst') || phraseto_tsquery('data scientist');
tsquery
------------------------------------------'busi' & 'analyst' | 'data' <-> 'scientist'
SELECT plainto_tsquery('business analyst') && phraseto_tsquery('data scientist');
tsquery
-------------------------------------------'busi' & 'analyst' & ('data' <-> 'scientist')
([DPSOH&RPELQLQJWVTXHULHV
([DPSOHVRIHDFKDUHVKRZQLQ([DPSOH
764XHULHVFDQEHFRPELQHGXVLQJWKHRURSHUDWRU || RUWKHDQGRSHUDWRU && 7KHH[SUHVVLRQtsquery1 || tsquery2PHDQVPDWFKLQJWH[WPXVWVDWLVI\HLWKHU
WVTXHU\RUWVTXHU\7KHH[SUHVVLRQtsquery1 && tsquery2PHDQVPDWFKLQJ
WH[WPXVWVDWLVI\ERWKWVTXHU\DQGWVTXHU\
<RXFDQDOVRFDVWWH[WWRWVTXHU\ZLWKRXWXVLQJDQ\IXQFWLRQVDVLQ'business &
analytics'::tsquery+RZHYHUZLWKFDVWVZRUGVDUHQRWUHSODFHGZLWK
OH[HPHVDQGDUHWDNHQOLWHUDOO\
SELECT phraseto_tsquery('business analytics');
phraseto_tsquery
------------------'busi' <-> 'analyt'
SELECT phraseto_tsquery('english_hunspell','business analytics');
phraseto_tsquery
--------------------------------------------'business' <-> 'analyt' | 'busy' <-> 'analyt'
([DPSOH764XHU\FRQVWUXFWLRQVSKUDVHWRBTXHU\
WKHZRUGV7KLVPHDQVWKDWWKHVHDUFKHGWH[WPXVWFRQWDLQWKHZRUGVEXVLQHVV
DQGDQDO\WLFVLQWKDWRUGHUXSJUDGLQJDZRUGVHDUFKWRDSKUDVHVHDUFK
)76LQFOXGHVIXQFWLRQVIRUUDQNLQJUHVXOWV7KHVHIXQFWLRQVDUHWVBUDQNDQG
WVBUDQNBFGWVBUDQNFRQVLGHUVRQO\WKHIUHTXHQF\RIWHUPVDQGZHLJKWVZKLOH
WVBUDQNBFG FGVWDQGVIRUFRYHUDJHGHQVLW\ DOVRFRQVLGHUVWKHSRVLWLRQRIWKH
VHDUFKWHUPZLWKLQWKHVHDUFKHGWH[W,IOH[HPHVDUHIRXQGFORVHUWRJHWKHUWKH
UHVXOWUDQNVKLJKHUWVBUDQNBFGLVPHDQLQJIXORQO\LI\RXKDYHSRVLWLRQPDUNHUV
LQ\RXUWVYHFWRURWKHUZLVHLWUHWXUQV]HUR7KHIUHTXHQF\ZLWKZKLFKDVHDUFK
5DQNLQJ5HVXOWV
([DPSOHUHTXLUHVWKDWWKHZRUGKXQWHUSUHFHGHVFLHQWLVWRUFKHIE\H[DFWO\
IRXUZRUGV
SELECT left(title,50) As title, left(description,50) as description
FROM film
WHERE fts @@ to_tsquery('hunter <4> (scientist | chef)') AND title > '';
title
| description
-----------------+--------------------------------------------------ALASKA PHANTOM | A Fanciful Saga of a Hunter And a Pastry Chef who
DAUGHTER MADIGAN | A Beautiful Tale of a Hunter And a Mad Scientist w
(2 rows)
([DPSOH)76ZLWKRUGHUDQGSUR[LPLW\
,I\RXDUHUXQQLQJ3RVWJUH64/\RXFDQVSHFLI\WKHSUR[LPLW\DQGRUGHURI
ZRUGV6HH([DPSOH
([DPSOHILQGVDOOILOPVZLWKDWLWOHRUGHVFULSWLRQFRQWDLQLQJWKHZRUG
KXQWHUDQGHLWKHUWKHZRUGVFLHQWLVWRUWKHZRUGFKHIRUERWK
SELECT left(title,50) As title, left(description,50) as description
FROM film
WHERE fts @@ to_tsquery('hunter & (scientist | chef)') AND title > '';
title
| description
-----------------------+--------------------------------------------------ALASKA PHANTOM
| A Fanciful Saga of a Hunter And a Pastry Chef who
CAUSE DATE
| A Taut Tale of a Explorer And a Pastry Chef who mu
CINCINATTI WHISPERER | A Brilliant Saga of a Pastry Chef And a Hunter who
COMMANDMENTS EXPRESS | A Fanciful Saga of a Student And a Mad Scientist w
DAUGHTER MADIGAN
| A Beautiful Tale of a Hunter And a Mad Scientist w
GOLDFINGER SENSIBILITY | A Insightful Drama of a Mad Scientist And a Hunter
HATE HANDICAP
| A Intrepid Reflection of a Mad Scientist And a Pio
INSIDER ARIZONA
| A Astounding Saga of a Mad Scientist And a Hunter
WORDS HUNTER
| A Action-Packed Reflection of a Composer And a Mad
(9 rows)
([DPSOH)76LQDFWLRQ
,Q3RVWJUH64/DQ\DJJUHJDWHIXQFWLRQ\RXFUHDWHFDQEHXVHGDVDZLQGRZ
%RWKLEADDQGLAGWDNHDQRSWLRQDOVWHSDUJXPHQWWKDWGHILQHVKRZPDQ\URZV
WRVNLSIRUZDUGRUEDFNZDUGWKHVWHSFDQEHSRVLWLYHRUQHJDWLYHLEADDQGLAG
UHWXUQNULLZKHQWU\LQJWRUHWULHYHURZVRXWVLGHWKHZLQGRZSDUWLWLRQ7KLVLVD
SRVVLELOLW\WKDW\RXDOZD\VKDYHWRDFFRXQWIRU
1DPLQJRXUZLQGRZwtZLQGRZ
8VLQJRXUZLQGRZQDPHLQVWHDGRIUHW\SLQJ
SELECT * FROM (
SELECT
ROW_NUMBER() OVER( wt ) As rnum,
substring(tract_id,1, 5) As county_code,
tract_id,
LAG(tract_id,2) OVER wt As tract_2_before,
LEAD(tract_id) OVER wt As tract_after
FROM census.lu_tracts
WINDOW wt AS (PARTITION BY substring(tract_id,1, 5) ORDER BY tract_id)
) As x
WHERE rnum BETWEEN 2 and 3 AND county_code IN ('25007','25025')
ORDER BY county_code, rnum;
rnum | county_code | tract_id | tract_2_before | tract_after
-----+-------------+-------------+----------------+-----------2
| 25007
| 25007200200 |
| 25007200300
3
| 25007
| 25007200300 | 25007200100
| 25007200400
2
| 25025
| 25025000201 |
| 25025000202
3
| 25025
| 25025000202 | 25025000100
| 25025000301
([DPSOH1DPLQJZLQGRZVGHPRQVWUDWLQJ/($'DQG/$*
3RVWJUH64/DOVRVXSSRUWVZLQGRZQDPLQJZKLFKLVXVHIXOLI\RXKDYHWKHVDPH
ZLQGRZIRUHDFKRI\RXUZLQGRZFROXPQV([DPSOHGHPRQVWUDWHVKRZWR
QDPHZLQGRZVDVZHOODVKRZWRXVHWKHLEADDQGLAGZLQGRZIXQFWLRQVWR
VKRZDUHFRUGYDOXHEHIRUHDQGDIWHUIRUDJLYHQSDUWLWLRQ
<RXFDQH[SOLFLWO\FRQWUROWKHURZVXQGHUFRQVLGHUDWLRQE\DGGLQJDRANGERU
ROWSFODXVHROWS BETWEEN CURRENT ROW AND 5 FOLLOWING
LQVWDQFHLI\RXUURZLVLQWKHILIWKURZLQWKHWKLUGSDUWLWLRQWKHVXPZLOOFRYHU
RQO\WKHILUVWILYHURZVLQWKHWKLUGSDUWLWLRQ:HSXWDQORDER BY
left(tract_id,5), valDWWKHHQGRIWKHTXHU\VR\RXFDQHDVLO\VHHWKH
SDWWHUQEXWNHHSLQPLQGWKDWWKHORDER BYRIWKHTXHU\LVLQGHSHQGHQWRIWKH
ORDER BYLQHDFKOVERFODXVH
7KHNH\REVHUYDWLRQWRPDNHLQWKHRXWSXWLVKRZWKHVXPFKDQJHVIURPURZWR
URZ7KHORDER BYFODXVHPHDQVWKDWWKHVXPZLOOEHWDNHQRQO\IURPWKH
EHJLQQLQJRIWKHSDUWLWLRQWRWKHFXUUHQWURZJLYLQJ\RXDUXQQLQJWRWDOZKHUH
WKHORFDWLRQRIWKHFXUUHQWURZLQWKHOLVWLVGLFWDWHGE\WKHORDER BYFODXVH)RU
SELECT tract_id, val,
SUM(val) OVER (PARTITION BY left(tract_id,5) ORDER BY val) As sum_county_ordered
FROM census.facts
WHERE fact_type_id = 2
ORDER BY left(tract_id,5), val;
tract_id
| val
| sum_county_ordered
-------------+----------+----------------25001014100 | 226.000 |
226.000
25001011700 | 971.000 |
1197.000
25001010208 | 984.000 |
2181.000
:
25003933200 | 564.000 |
564.000
25003934200 | 593.000 |
1157.000
25003931300 | 606.000 |
1763.000
:
([DPSOH&RPELQLQJ3$57,7,21%<DQG25'(5%<
<RXFDQFRPELQHORDER BYZLWKPARTITION BYUHVWDUWLQJWKHRUGHULQJIRUHDFK
SDUWLWLRQ([DPSOHUHWXUQVWRRXUH[DPSOHRIFRXQW\FRGHV
,Q([DPSOHZHDOVRKDYHDQORDER BYIRUWKHHQWLUHTXHU\'RQߞWJHW
FRQIXVHGEHWZHHQWKLVDQGWKHORDER BYWKDWߞVVSHFLILFWRWKHZLQGRZIXQFWLRQ
SELECT ROW_NUMBER() OVER (ORDER BY tract_name) As rnum, tract_name
FROM census.lu_tracts
ORDER BY rnum LIMIT 4;
rnum | tract_name
-----+------------------------------------------------1
| Census Tract 1, Suffolk County, Massachusetts
2
| Census Tract 1001, Suffolk County, Massachusetts
3
| Census Tract 1002, Suffolk County, Massachusetts
4
| Census Tract 1003, Suffolk County, Massachusetts
([DPSOH1XPEHULQJXVLQJWKH52:B180%(5ZLQGRZIXQFWLRQ
FXUUHQWURZLQWKHZLQGRZRUSDUWLWLRQ7KHFODVVLFH[DPSOHXVHVWKH
ROW_NUMBERIXQFWLRQWRVHTXHQWLDOO\QXPEHUURZV,Q([DPSOHZH
GHPRQVWUDWHKRZWRQXPEHURXUFHQVXVWUDFWVLQDOSKDEHWLFDORUGHU7RDUULYHDW
WKHURZQXPEHUROW_NUMBERFRXQWVDOOURZVXSWRDQGLQFOXGLQJWKHFXUUHQWURZ
EDVHGRQWKHRUGHUGLFWDWHGE\WKHORDER BY
,ISHUIRUPDQFHLVDFRQFHUQ\RXVKRXOGH[SOLFLWO\GHFODUHWKH)76FRQILJXUDWLRQLQTXHULHV
LQVWHDGRIDOORZLQJWKHGHIDXOWEHKDYLRU$VQRWHGLQ6RPH)767ULFNVE\2OHJ%DUWXQRY\RX
FDQDFKLHYHWZLFHWKHVSHHGE\XVLQJto_tsquery('english','social & (science |
scientist)')LQOLHXRIto_tsquery('social & (science | scientist)')
127(
1RWLFHKRZLQ([DPSOHWKHVHFRQGHQWU\KDVDUDQNLQJRI]HUREHFDXVHWKH
WLWOHGRHVQRWFRQWDLQDOOWKHZRUGVWRVDWLVI\WKHWVTXHU\
SELECT
left(title,40) As title,
ts_rank('{0,0,0,1}'::numeric[],fts,ts)::numeric(10,3) AS r,
ts_rank_cd('{0,0,0,1}'::numeric[],fts,ts)::numeric(10,3) As rcd
FROM film, to_tsquery('english', 'love & (wait | indian | mad )') AS ts
WHERE fts @@ ts AND title > ''
ORDER BY r DESC;
title
| r
| rcd
--------------+-------+-----INDIAN LOVE | 0.991 | 1.000
LAWRENCE LOVE | 0.000 | 0.000
(2 rows)
([DPSOH5DQNLQJVHDUFKUHVXOWVXVLQJFXVWRPZHLJKWV
/HWߞVVXSSRVHZHZLVKWRUHWULHYHDILHOGRQO\LIWKHVHDUFKWHUPVDSSHDULQWKH
WLWOH)RUWKLVVLWXDWLRQZHZRXOGDVVLJQWRWKHWLWOHILHOGDQGWRDOORWKHUV
([DPSOHUHSHDWV([DPSOHSDVVLQJLQDQDUUD\RIZHLJKWV
SELECT title, left(description,50) As description,
ts_rank(fts,ts)::numeric(10,3) AS r
FROM film, to_tsquery('english','love & (wait | indian | mad)') AS ts
WHERE fts @@ ts AND title > ''
ORDER BY r DESC;
title
| description
| r
--------------+----------------------------------------------------+-----INDIAN LOVE | A Insightful Saga of a Mad Scientist And a Mad Sci | 0.999
LAWRENCE LOVE | A Fanciful Yarn of a Database Administrator And a | 0.252
(2 rows)
([DPSOH5DQNLQJVHDUFKUHVXOWV
WHUPDSSHDUVDOVRGHSHQGVRQSRVLWLRQPDUNHUV6RWKHWVBUDQNIXQFWLRQZLOO
FRQVLGHURQO\ZHLJKWVLISRVLWLRQDOPDUNHUVDUHPLVVLQJ%\GHIDXOWWVBUDQNDQG
WVBUDQNBFGDSSO\WKHZHLJKWVDQGUHVSHFWLYHO\IRU'&%DQG
$([DPSOHIROORZVWKHGHIDXOWRUGHU
SELECT to_tsvector(person)
FROM persons WHERE id=1;
to_tsvector
---------------------------------------------------------------------------------'-5083':19 '-6719':13 '-722':12 '-852':18 '619':11,17 'alex':3 'azaleah':25
'brandon':21 'cell':15 'm':23 'ofelia':7 'rafael':5 'sonia':1 'work':9
([DPSOH&RQYHUWLQJMVRQMVRQEWRWVYHFWRU
1HZLQYHUVLRQDUHts_headlineDQGto_tsvectorZKLFKWDNHDVLQSXWMVRQ
DQGMVRQEGDWD7KHIXQFWLRQVZRUNMXVWOLNHWKHWH[WRQHVH[FHSWWKH\FRQVLGHU
RQO\WKHYDOXHVRIMVRQMVRQEGDWDDQGQRWWKHNH\VRUMVRQPDUNXS([DPSOH
DSSOLHVWKHIXQFWLRQWRWKHMVRQSHUVRQFROXPQRIWKHWDEOHZHFUHDWHGLQ
([DPSOH
)XOO7H[W6XSSRUWIRU-621DQG-621%
.HHSLQPLQGWKDWDOWKRXJKDVWULSSHGYHFWRULVIDVWHUWRVHDUFKDQGWDNHVXSOHVV
GLVNVSDFHPDQ\RSHUDWRUVDQGIXQFWLRQVFDQQRWEHXVHGLQFRQMXQFWLRQZLWK
WKHP)RULQVWDQFHEHFDXVHDVWULSSHGYHFWRUKDVQRPDUNHUVGLVWDQFHRSHUDWRUV
FDQQRWEHXVHG
SELECT fts
FROM film
WHERE film_id = 1;
'academi':1A 'battl':15B 'canadian':20B 'dinosaur':2A 'drama':5B 'epic':4B
'feminist':8B 'mad':11B 'must':14B 'rocki':21B 'scientist':12B 'teacher':17B
SELECT strip(fts)
FROM film
WHERE film_id = 1;
'academi' 'battl' 'canadian' 'dinosaur' 'drama' 'epic' 'feminist' 'mad'
'must' 'rocki' 'scientist' 'teacher'
([DPSOH8QVWULSSHGYHUVXVVWULSSHGYHFWRU
%\GHIDXOWYHFWRUL]DWLRQDGGVPDUNHUV ORFDWLRQRIWKHOH[HPHVZLWKLQWKH
YHFWRU DQGRSWLRQDOO\ZHLJKWV $%&' ,I\RXUVHDUFKHVFDUHRQO\ZKHWKHUD
SDUWLFXODUWHUPFDQEHIRXQGUHJDUGOHVVRIZKHUHLWLVLQWKHWH[WKRZIUHTXHQWO\
LWRFFXUVRULWVSURPLQHQFH\RXFDQGHFOXWWHU\RXUYHFWRUVXVLQJWKHVWULS
IXQFWLRQ7KLVVDYHVGLVNVSDFHDQGJDLQVVRPHVSHHG([DPSOHFRPSDUHV
ZKDWDQXQVWULSSHGYHUVXVVWULSSHGYHFWRUORRNVOLNH
)XOO7H[W6WULSSLQJ
:LQGRZIXQFWLRQVDOVRDOORZDQORDER BYLQWKHOVERFODXVH:LWKRXWJHWWLQJ
WRRDEVWUXVHWKHEHVWZD\WRWKLQNDERXWWKLVLVWKDWDOOWKHURZVLQWKHZLQGRZ
ZLOOEHRUGHUHGDVLQGLFDWHGE\ORDER BYDQGWKHZLQGRZIXQFWLRQZLOOFRQVLGHU
RQO\URZVWKDWUDQJHIURPWKHILUVWURZLQWKHZLQGRZXSWRDQGLQFOXGLQJWKH
25'(5%<
SELECT tract_id, val, AVG(val) OVER (PARTITION BY left(tract_id,5)) As val_avg_county
FROM census.facts
WHERE fact_type_id = 2 ORDER BY tract_id;
tract_id
| val
| val_avg_county
------------+----------+---------------------25001010100 | 1765.000 | 1709.9107142857142857
25001010206 | 1366.000 | 1709.9107142857142857
25001010208 | 984.000 | 1709.9107142857142857
:
25003900100 | 1920.000 | 1438.2307692307692308
25003900200 | 1968.000 | 1438.2307692307692308
25003900300 | 1211.000 | 1438.2307692307692308
:
([DPSOH3DUWLWLRQLQJRXUZLQGRZE\FRXQW\FRGH
<RXFDQUXQDZLQGRZIXQFWLRQRYHUURZVFRQWDLQLQJSDUWLFXODUYDOXHVLQVWHDGRI
XVLQJWKHZKROHWDEOH7KLVUHTXLUHVWKHDGGLWLRQR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val_avg
------------+----------+---------------------25001010100 | 2942.000 | 4430.0602165087956698
25001010206 | 2750.000 | 4430.0602165087956698
25001010208 | 2003.000 | 4430.0602165087956698
25001010304 | 2421.000 | 4430.0602165087956698
:
([DPSOH7KHEDVLFZLQGRZ
([DPSOHJLYHV\RXDTXLFNVWDUW8VLQJDZLQGRZIXQFWLRQZHFDQREWDLQ
ERWKWKHGHWDLOGDWDDQGWKHDYHUDJHYDOXHIRUDOOUHFRUGVZLWKfact_type_idRI
LQRQHVLQJOHSELECT1RWHWKDWWKHWHEREFODXVHLVDOZD\VHYDOXDWHGEHIRUH
WKHZLQGRZIXQFWLRQ
:LWKRXWZLQGRZIXQFWLRQV\RXߞGKDYHWRUHVRUWWRXVLQJMRLQVDQGVXETXHULHVWR
SROOQHLJKERULQJURZV2QWKHVXUIDFHZLQGRZIXQFWLRQVYLRODWHWKHVHWEDVHG
SULQFLSOHRI64/EXWZHPROOLI\WKHSXULVWE\FODLPLQJWKDWWKH\DUHPHUHO\
VKRUWKDQG<RXFDQILQGPRUHGHWDLOVDQGH[DPSOHVLQ:LQGRZ)XQFWLRQV
:LQGRZIXQFWLRQVDUHDFRPPRQ$16,64/IHDWXUH$ZLQGRZIXQFWLRQKDVWKH
SUHVFLHQFHWRVHHDQGXVHGDWDEH\RQGWKHFXUUHQWURZKHQFHWKHWHUPZLQGRZ$
ZLQGRZGHILQHVZKLFKRWKHUURZVQHHGWREHFRQVLGHUHGLQDGGLWLRQWRWKH
FXUUHQWURZ:LQGRZVOHW\RXDGGDJJUHJDWHLQIRUPDWLRQWRHDFKURZRI\RXU
RXWSXWZKHUHWKHDJJUHJDWLRQLQYROYHVRWKHUURZVLQWKHVDPHZLQGRZ:LQGRZ
IXQFWLRQVVXFKDVrow_numberDQGrankDUHXVHIXOIRURUGHULQJ\RXUGDWDLQ
VRSKLVWLFDWHGZD\VWKDWXVHURZVRXWVLGHWKHVHOHFWHGUHVXOWVEXWZLWKLQD
ZLQGRZ
:LQGRZ)XQFWLRQV
sonia
|
(4 rows)
CREATE TABLE chickens (id integer PRIMARY KEY);
CREATE TABLE ducks (id integer PRIMARY KEY, chickens chickens[]);
CREATE TABLE turkeys (id integer PRIMARY KEY, ducks ducks[]);
([DPSOH7XUGXFNHQ
3RVWJUH64/DXWRPDWLFDOO\FUHDWHVFXVWRPW\SHVIRUDOOWDEOHV)RUDOOLQWHQWVDQG
SXUSRVHV\RXFDQXVHFXVWRPW\SHVMXVWDV\RXZRXOGDQ\RWKHUEXLOWLQW\SH6R
ZHFRXOGFRQFHLYDEO\FUHDWHDWDEOHWKDWKDVDFROXPQW\SHWKDWLVDQRWKHUWDEOHߞV
FXVWRPW\SHDQGZHFDQJRHYHQIXUWKHUDQGPDNHDQDUUD\RIWKDWW\SH:H
GHPRQVWUDWHWKLVߡWXUGXFNHQߢLQ([DPSOH
$OO7DEOHV$UH&XVWRP'DWD7\SHV
7KLVVHFWLRQGHPRQVWUDWHVKRZWRGHILQHDQGXVHDFXVWRPW\SH7KHcomposite
DNDrecordrow REMHFWW\SHLVRIWHQXVHGWREXLOGDQREMHFWWKDWLVWKHQFDVWWR
DFXVWRPW\SHRUDVDUHWXUQW\SHIRUIXQFWLRQVQHHGLQJWRUHWXUQPXOWLSOH
FROXPQV
&XVWRPDQG&RPSRVLWH'DWD7\SHV
1RWHWKHEROG+70/WDJVDURXQGWKHPDWFKLQJYDOXH
SELECT ts_headline(person->'spouse'->'parents', 'rafael'::tsquery)
FROM persons_b WHERE id=1;
{"father": "<b>Rafael</b>", "mother": "Ofelia"}
(1 row)
([DPSOH7DJPDWFKLQJZRUGV
$OVRDYDLODEOHQRZIRUMVRQDQGMVRQELVWKHts_headlineIXQFWLRQZKLFKWDJV
DV+70/DOOPDWFKLQJWH[WLQWKHMVRQGRFXPHQW([DPSOHIODJVDOO
UHIHUHQFHVWR5DIDHOLQWKHGRFXPHQW
7RDSSO\WKLVIXQFWLRQWRWKHMVRQEWDEOHpersons_bVZDSRXWWKHpersonsWDEOH
IRUpersons_b6LPLODUWRWKHWRBWVYHFWRUIRUWH[WWKHVHIXQFWLRQVDOVRKDYHD
YDULDQWWKDWWDNHVWKH)76FRQILJXUDWLRQWRXVHDVWKHLUILUVWDUJXPHQW7RPDNH
EHVWXVHRIWKHVHIXQFWLRQVFUHDWHDWVYHFWRUFROXPQLQ\RXUWDEOHDQGSRSXODWH
WKHILHOGXVLQJHLWKHUDWULJJHURUXSGDWHDVQHHGHG
(1 row)
id | ducks_jsonb
---+-----------------------------------------------1 | [{"id": 1, "chickens": [{"id": 1}, {"id": 3}]}]
(1 row)
SELECT id, to_jsonb(ducks) AS ducks_jsonb
FROM turkeys;
$Q\FRPSOH[URZRUFROXPQUHJDUGOHVVRIKRZFRPSOH[FDQEHFRQYHUWHGWRD
MVRQRUMVRQEFROXPQOLNHVR
:HXVHGWKHRETURNINGFODXVHDVGLVFXVVHGLQߡ5HWXUQLQJ$IIHFWHG5HFRUGVWR
WKH8VHUߢWRRXWSXWWKHFKDQJHGUHFRUG
output
-------------------------id | ducks
---+---------------------1 | {"(1,\"{(1),(3)}\")"}
UPDATE turkeys SET ducks[1].chickens[2] = ROW(3)::chickens
WHERE id = 1 RETURNING *;
:HFDQDOVRUHSODFHVXEHOHPHQWVRIRXUWXUGXFNHQ7KLVQH[WH[DPSOHUHSODFHV
RXUVHFRQGFKLFNHQLQRXUILUVWWXUNH\ZLWKDGLIIHUHQWFKLFNHQ
output
-------------------------id | ducks
---+---------------------1 | {"(1,\"{(1),(1)}\")"}
SELECT * FROM turkeys;
)LQDOO\OHWߞVVHHZKDWZHKDYHLQRXUWXUNH\
:HFUHDWHDQLQVWDQFHRIDFKLFNHQZLWKRXWDGGLQJLWWRWKHchickenWDEOHLWVHOI
KHQFHZHߞUHDEOHWRUHSHDWidZLWKLPSXQLW\:HWDNHRXUDUUD\RIWZRFKLFNHQV
VWXIIWKHPLQWRRQHGXFNDQGDGGLWWRWKHducksWDEOH:HWDNHWKHGXFNZH
DGGHGDQGVWXIILWLQWRWKHturkeysWDEOH
INSERT INTO ducks VALUES (1, ARRAY[ROW(1)::chickens, ROW(1)::chickens]);
INSERT INTO turkeys VALUES (1, array(SELECT d FROM ducks d));
SELECT
student,
percentile_disc(0.5) WITHIN
FILTER (WHERE subject =
percentile_disc(0.5) WITHIN
FILTER (WHERE subject =
FROM test_scores
GROUP BY student
ORDER BY student;
student | algebra | physics
--------+---------+-------alex
|
74 |
79
leo
|
80 |
72
regina |
68 |
83
GROUP (ORDER BY score)
'algebra') AS algebra,
GROUP (ORDER BY score)
'physics') AS physics
([DPSOH&RPSXWHPHGLDQVFRUHIRUWZRVXEMHFWV
$VZLWKDOODJJUHJDWHV\RXFDQFRPELQHWKHVHIXQFWLRQVZLWKPRGLILHUV
([DPSOHFRPELQHVWITHIN GROUPZLWKFILTER
SELECT
student,
percentile_cont('{0.5,0.60,1}'::float[])
WITHIN GROUP (ORDER BY score) AS cont_median,
percentile_disc('{0.5,0.60,1}'::float[])
WITHIN GROUP (ORDER BY score) AS disc_median,
COUNT(*) As num_scores
FROM test_scores
GROUP BY student
ORDER BY student;
student | cont_median
| disc_median | num_scores
--------+----------------+-------------+-----------alex
| {78,79.2,84} | {77,79,84} |
8
leo
| {72,73.6,84} | {72,72,84} |
8
regina | {76,76.8,90} | {76,77,90} |
9
sonia | {73.5,75.6,86} | {72,75,86} |
8
(4 rows)
([DPSOH&RPSXWHPXOWLSOHSHUFHQWLOHV
7KHSHUFHQWLOHIXQFWLRQVKDYHDQRWKHUYDULDQWWKDWDFFHSWVDQDUUD\RISHUFHQWLOHV
OHWWLQJ\RXUHWULHYHPXOWLSOHSHUFHQWLOHVDOOLQRQHFDOO([DPSOHFRPSXWHV
WKHPHGLDQWKHSHUFHQWLOHDQGWKHKLJKHVWVFRUH
7KHLQSXWVRIWKHVHIXQFWLRQVGLIIHUIURPRWKHUDJJUHJDWHIXQFWLRQV7KHFROXPQ
EHLQJDJJUHJDWHGLVWKHFROXPQLQWKHORDER BYFODXVHVRIWKHWITHIN GROUP
PRGLILHUV7KHFROXPQLVQRWGLUHFWLQSXWWRWKHIXQFWLRQDVZHߞUHXVHGWR
VHHLQJ
|
|
|
|
{74,74}
{75,78}
{68,77}
{84,80}
|
|
|
|
{83,79}
{72,72}
{83,85}
{72,72}
([DPSOHFRPSXWHVERWKWKHGLVFUHWHDQGWKHFRQWLQXRXVPHGLDQVFRUH
ZKLFKFRXOGGLIIHUZKHQVWXGHQWVKDYHDQHYHQQXPEHURIVFRUHV
SELECT
student,
percentile_cont(0.5) WITHIN GROUP (ORDER BY score) As cont_median,
percentile_disc(0.5) WITHIN GROUP (ORDER BY score) AS disc_median,
mode() WITHIN GROUP (ORDER BY score) AS mode,
COUNT(*) As num_scores
FROM test_scores
GROUP BY student
ORDER BY student;
student | cont_median | disc_median | mode | num_scores
--------+-------------+-------------+------+-----------alex
|
78 |
77 | 74 |
8
leo
|
72 |
72 | 72 |
8
regina |
76 |
76 | 68 |
9
sonia
|
73.5 |
72 | 72 |
8
(4 rows)
([DPSOH&RPSXWHPHGLDQDQGPRGHVFRUHV
0HGLDQLVPHUHO\WKHSHUFHQWLOHWKHUHIRUHLWGRHVQRWGHVHUYHDVHSDUDWH
IXQFWLRQRILWVRZQ7KHPRGHIXQFWLRQILQGVWKHPRVWFRPPRQYDOXH6KRXOG
WKHUHEHPRUHWKDQRQHPRGHWKHILUVWRQHHQFRXQWHUHGLVUHWXUQHGWKHUHIRUH
RUGHULQJPDWWHUVDVVKRZQLQ([DPSOH
7KHWZRSHUFHQWLOHIXQFWLRQVGLIIHULQKRZWKH\KDQGOHHYHQFRXQWV)RUWKH
GLVFUHWHIXQFWLRQWKHILUVWYDOXHHQFRXQWHUHGLVWDNHQVRWKHRUGHULQJRIWKHGDWD
PDWWHUV)RUWKHFRQWLQXRXVFDVHYDOXHVZLWKLQWKHVDPHSHUFHQWLOHDUHDYHUDJHG
1HZLQ3RVWJUH64/DUHVWDWLVWLFDOIXQFWLRQVIRUFRPSXWLQJSHUFHQWLOH
PHGLDQ DNDSHUFHQWLOH DQGPRGH7KHVHIXQFWLRQVDUHSHUFHQWLOHBGLVF
SHUFHQWLOHGLVFUHWH SHUFHQWLOHBFRQW SHUFHQWLOHFRQWLQXRXV DQGPRGH
3HUFHQWLOHVDQG0RGH
FILTERZRUNVIRUDOODJJUHJDWHIXQFWLRQVQRWMXVWDJJUHJDWHIXQFWLRQVEXLOWLQWR
3RVWJUH64/
jojo
jdoe
robe
lhsu
18//LVDFRQIXVLQJFRQFHSWLQWKH$16,64/6WDQGDUGSULPDULO\EHFDXVH
NULL != NULL:KHQZRUNLQJZLWK18//VLQVWHDG\RXQHHGWRXVH,618//
&RPSRVLWHVDQG18//V
3X]]OHGE\WKHSDUHQWKHVHVVXUURXQGLQJac_volt",I\RXOHDYHWKHPRXW3RVWJUH64/ZLOO
UDLVHWKHHUURUmissing FROM-clause entry for table “ac_volt”EHFDXVHLWDVVXPHV
ac_voltZLWKRXWSDUHQWKHVHVUHIHUVWRDWDEOH
:$51,1*
SELECT circuit_id, (ac_volt).r, (ac_volt).i FROM circuits;
RUDQHTXLYDOHQW
SELECT circuit_id, (ac_volt).* FROM circuits;
:HFDQWKHQTXHU\RXUWDEOHZLWKVWDWHPHQWVVXFKDV
CREATE TABLE circuits (circuit_id serial PRIMARY KEY, ac_volt complex_number);
:HFDQWKHQXVHWKLVFRPSOH[QXPEHUDVDFROXPQW\SH
CREATE TYPE complex_number AS (r double precision, i double precision);
$OWKRXJK\RXFDQHDVLO\FUHDWHFRPSRVLWHW\SHVMXVWE\FUHDWLQJDWDEOHDWVRPH
SRLQW\RXߞOOSUREDEO\ZDQWWREXLOG\RXURZQIURPVFUDWFK)RUH[DPSOHOHWߞV
EXLOGDFRPSOH[QXPEHUGDWDW\SHZLWKWKHIROORZLQJVWDWHPHQW
%XLOGLQJ&XVWRP'DWD7\SHV
3RVWJUH64/LQWHUQDOO\NHHSVWUDFNRIREMHFWGHSHQGHQFLHV7KHducks.chickens
FROXPQLVGHSHQGHQWRQWKHchickensWDEOH7KHturkeys.ducksFROXPQLV
GHSHQGHQWRQWKHducksWDEOH<RXZRQߞWEHDEOHWRGURSWKHchickensWDEOH
ZLWKRXWVSHFLI\LQJCASCADERUILUVWGURSSLQJWKHducks.chickensFROXPQ,I
\RXGRDCASCADEWKHducks.chickensFROXPQZLOOEHJRQHDQGZLWKRXW
ZDUQLQJ\RXUWXUNH\VZLOOKDYHQRFKLFNHQVLQWKHLUGXFNV
7KHQH[WVWHSLVWRFUHDWHDV\PEROLFRSHUDWRUWRZUDSWKHIXQFWLRQDVLQ
([DPSOH
CREATE OR REPLACE FUNCTION add(complex_number, complex_number)
RETURNS complex_number AS
$$
SELECT
((COALESCE(($1).r,0) + COALESCE(($2).r,0)),
(COALESCE(($1).i,0) + COALESCE(($2).i,0)))::complex_number;
$$
language sql;
([DPSOH$GGIXQFWLRQIRUFRPSOH[QXPEHU
7KHILUVWVWHSWRFUHDWLQJDQRSHUDWRULVWRFUHDWHDIXQFWLRQDVVKRZQLQ
([DPSOH
,QDGGLWLRQWREHLQJDQDOLDVDQRSHUDWRUFRQWDLQVRSWLPL]DWLRQLQIRUPDWLRQWKDW
FDQEHXVHGE\WKHTXHU\RSWLPL]HUWRGHFLGHKRZLQGH[HVVKRXOGEHXVHGKRZ
EHVWWRQDYLJDWHWKHGDWDDQGZKLFKRSHUDWRUH[SUHVVLRQVDUHHTXLYDOHQW0RUH
GHWDLOVDERXWWKHVHRSWLPL]DWLRQVDQGKRZHDFKFDQKHOSWKHRSWLPL]HUDUHLQ
2SHUDWRU2SWLPL]DWLRQ
$IWHU\RXEXLOGDFXVWRPW\SHVXFKDVDFRPSOH[QXPEHUQDWXUDOO\\RXߞOOZDQW
WRFUHDWHIXQFWLRQVDQGRSHUDWRUVIRULW:HߞOOGHPRQVWUDWHEXLOGLQJD+RSHUDWRU
IRUWKHcomplex_numberZHFUHDWHG)RUPRUHGHWDLOVDERXWEXLOGLQJIXQFWLRQV
VHH&KDSWHU$VVWDWHGHDUOLHUDQRSHUDWRULVDV\PERODOLDVIRUDIXQFWLRQWKDW
WDNHVRQHRUWZRDUJXPHQWV<RXFDQILQGPRUHGHWDLOVDERXWZKDWV\PEROVDQG
VHWVRIV\PEROVDUHDOORZHGLQ&5($7(23(5$725
%XLOGLQJ2SHUDWRUVDQG)XQFWLRQVIRU&XVWRP7\SHV
3RVWJUH64/DELGHVE\WKH$16,64/VWDQGDUGVSHFVZKHQGHDOLQJZLWK18//V
7KHVSHFVUHTXLUHWKDWLQRUGHUIRUDFRPSRVLWHWREH,618//DOOHOHPHQWVRI
WKHFRPSRVLWHPXVWEH18//+HUHLVZKHUHFRQIXVLRQFDQHQWHU,QRUGHUIRUD
FRPSRVLWHWREHFRQVLGHUHG,612718//HYHU\HOHPHQWLQWKHFRPSRVLWHPXVW
UHWXUQWUXHIRU,612718//
,612718//RU127 somevalue,618// :LWKQRQFRPSRVLWHW\SHV
something IS NULLLVJHQHUDOO\WKHDQWLWKHVLVWRsomething IS NOT NULL
7KLVLVQRWWKHFDVHZLWKFRPSRVLWHVKRZHYHU
SELECT student,
array_agg(score) FILTER (WHERE subject ='algebra') As algebra,
array_agg(score) FILTER (WHERE subject ='physics') As physics
FROM test_scores
GROUP BY student;
student | algebra | physics
--------+---------+--------
([DPSOH),/7(5XVHGZLWKDUUD\BDJJ
2EVHUYHWKDWLQ([DPSOHZHJHWDEXQFKRINULLILHOGVLQRXUDUUD\V:H
FRXOGZRUNDURXQGWKLVLVVXHZLWKVRPHFOHYHUXVHRIVXEVHOHFWVEXWPRVWRI
WKRVHZLOOEHPRUHYHUERVHDQGVORZHUWKDQWKHFILTERDOWHUQDWLYHVKRZQLQ
([DPSOH
SELECT student,
array_agg(CASE WHEN subject ='algebra' THEN score ELSE NULL END) As algebra,
array_agg(CASE WHEN subject ='physics' THEN score ELSE NULL END) As physics
FROM test_scores
GROUP BY student;
student | algebra
| physics
--------+---------------------------+------------------------------jojo
| {74,NULL,NULL,NULL,74,..} | {NULL,83,NULL,NULL,NULL,79,..}
jdoe
| {75,NULL,NULL,NULL,78,..} | {NULL,72,NULL,NULL,NULL,72..}
robe
| {68,NULL,NULL,NULL,77,..} | {NULL,83,NULL,NULL,NULL,85,..}
lhsu
| {84,NULL,NULL,NULL,80,..} | {NULL,72,NULL,NULL,NULL,72,..}
(4 rows)
([DPSOH&$6(:+(1XVHGLQDUUD\BDJJ
,QWKHFDVHRIDYHUDJHVDQGVXPVDQGPDQ\RWKHUDJJUHJDWHVWKHCASEDQG
FILTERDUHHTXLYDOHQW7KHEHQHILWLVWKDWFILTERLVDOLWWOHFOHDUHULQSXUSRVH
DQGIRUODUJHGDWDVHWVLVIDVWHU+RZHYHUWKHUHDUHVRPHDJJUHJDWHVߚVXFKDV
array_aggZKLFKFRQVLGHUVNULLILHOGVߚZKHUHWKHCASEVWDWHPHQWJLYHV\RX
H[WUDNULLYDOXHV\RXGRQߞWZDQW,Q([DPSOHZHWU\WRJHWWKHOLVWRI
VFRUHVIRUHDFKVXEMHFWRILQWHUHVWIRUHDFKVWXGHQWXVLQJWKHCASE .. WHEN..
DSSURDFK
SELECT student,
AVG(score) FILTER (WHERE subject ='algebra') As algebra,
AVG(score) FILTER (WHERE subject ='physics') As physics
FROM test_scores
GROUP BY student;
([DPSOH),/7(5XVHGZLWK$9*DJJUHJDWH
7KHFILTERFODXVHHTXLYDOHQWIRU([DPSOHLVVKRZQLQ([DPSOH
8VHRIGROODUTXRWLQJVRZHGRQߞWQHHGWRHVFDSH LQ+RXVLQJ6LQFHWKH'2
FRPPDQGLVDOVRZUDSSHGLQGROODUVZHQHHGWRXVHDQDPHGGHOLPLWHU
LQVLGH:HFKRVH$sql$
8VHstring_aggWRIRUPDVHWRI64/VWDWHPHQWVDVDVLQJOHVWULQJRIWKH
IRUPINSERT INTO lu_fact_type(...) SELECT ... WHERE s01 ~ '[azA-Z]+';
([HFXWHWKH64/
SELECT student,
AVG(CASE WHEN subject ='algebra' THEN score ELSE NULL END) As algebra,
AVG(CASE WHEN subject ='physics' THEN score ELSE NULL END) As physics
FROM test_scores
GROUP BY student;
([DPSOH&$6(:+(1XVHGLQ$9*
1HZLQYHUVLRQLVWKHFILTERFODXVHIRUDJJUHJDWHVUHFHQWO\VWDQGDUGL]HGLQ
$16,64/7KLVUHSODFHVWKHVWDQGDUGCASE WHENFODXVHIRUUHGXFLQJWKH
QXPEHURIURZVLQFOXGHGLQDQDJJUHJDWLRQ)RUH[DPSOHVXSSRVH\RXXVHG
CASE WHENWREUHDNRXWDYHUDJHWHVWVFRUHVE\VWXGHQWDVVKRZQLQ([DPSOH
),/7(5&ODXVHIRU$JJUHJDWHV
,Q([DPSOHZHDUHXVLQJWKHGROODUTXRWLQJV\QWD[FRYHUHGLQߡ'ROODU
4XRWLQJߢIRUWKHERG\RIWKH'2IXQFWLRQDQGVRPHIUDJPHQWVRIWKH64/
VWDWHPHQWVLQVLGHWKHIXQFWLRQ6LQFHZHXVHGROODUTXRWLQJWRGHILQHWKHZKROH
ERG\RIWKH'2DVZHOODVLQWHUQDOO\ZHQHHGWRXVHQDPHGGROODUTXRWLQJIRUDW
OHDVWRQHSDUW7KHVDPHGROODUTXRWLQJQHVWHGDSSURDFKFDQEHXVHGIRUIXQFWRQ
GHILQLWLRQVDVZHOO
END
$$;
)
FROM generate_series(1,51) As I;
EXECUTE var_sql;
SELECT
'Housing',
array_agg(s$sql$ || lpad(i::text,2,'0')
|| ') As fact_subcats,'
|| quote_literal('s' || lpad(i::text,2,'0')) || ' As short_name
FROM staging.factfinder_import
WHERE s' || lpad(I::text,2,'0') || $sql$ ~ '^[a-zA-Z]+' $sql$, ';'
7KHDELOLW\WREXLOGFXVWRPW\SHVDQGRSHUDWRUVSXVKHV3RVWJUH64/WRWKH
ERXQGDU\RIDIXOOIOHGJHGGHYHORSPHQWHQYLURQPHQWEULQJLQJXVHYHUFORVHUWR
RXUXWRSLDZKHUHHYHU\WKLQJLVWDEOHGULYHQ
$OWKRXJKZHGLGQߞWGHPRQVWUDWHLWKHUH\RXFDQR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QGYLHZV
VREHFDXWLRXVZKHQXVLQJ&$6&$'(
set search_path=census;
DROP TABLE IF EXISTS lu_fact_types CASCADE;
CREATE TABLE lu_fact_types (
fact_type_id serial,
category varchar(100),
fact_subcats varchar(255)[],
short_name varchar(50),
CONSTRAINT pk_lu_fact_types PRIMARY KEY (fact_type_id)
);
)LUVWZHߞOOFUHDWHWKHWDEOH
7KHDOFRPPDQGDOORZV\RXWRLQMHFWDSLHFHRISURFHGXUDOFRGHLQWR\RXU64/
RQWKHIO\<RXFDQWKLQNRILWDVDRQHWLPHDQRQ\PRXVIXQFWLRQ$VDQH[DPSOH
ZHߞOOORDGWKHGDWDFROOHFWHGLQ([DPSOHLQWRSURGXFWLRQWDEOHVIURPRXU
VWDJLQJWDEOH:HߞOOXVH3/SJ64/IRURXUSURFHGXUDOVQLSSHWEXW\RXߞUHIUHHWR
XVHRWKHUODQJXDJHV
'2
$YDULDQWRIGROODUTXRWLQJLVQDPHGGROODUTXRWLQJ:HFRYHUWKLVLQWKH
IROORZLQJVHFWLRQ
7KHSDLURIGROODUVLJQVUHSODFHVWKHVLQJOHTXRWHDQGHVFDSHVDOOVLQJOHTXRWHV
ZLWKLQ
SELECT $$It's O'Neil's play. $$ || $$It'll start at two o'clock.$$
:LWKGROODUTXRWLQJ
SELECT 'It''s O''Neil''s play. ' || 'It''ll start at two o''clock.'
,I\RXDUHZULWLQJDQ64/VWDWHPHQWWKDWJOXHVWZRVHQWHQFHVZLWKPDQ\VLQJOH
TXRWHVWKH$16,VWDQGDUGZD\ZRXOGEHWRHVFDSHDVLQWKHIROORZLQJ
'ROODUTXRWLQJLVDOVRXVHIXOLQVLWXDWLRQVZKHUH\RXߞUHWU\LQJWRH[HFXWHDSLHFH
RI64/G\QDPLFDOO\VXFKDVexec(some sql),Q([DPSOHZHHQFORVHGWKH
ERG\RIDWULJJHUXVLQJGROODUTXRWLQJ
3RVWJUH64/OHWV\RXHVFDSHVLQJOHTXRWHVLQVWULQJVRIDQ\OHQJWKE\VXUURXQGLQJ
WKHPZLWKWZRVHTXHQWLDOGROODUVLJQV KHQFHWKHQDPHGROODUTXRWLQJ
,QVWDQGDUG$16,64/VLQJOHTXRWHV VXUURXQGVWULQJOLWHUDOV6KRXOG\RX
KDYHDVLQJOHTXRWHLQWKHVWULQJLWVHOIVXFKDVODVWQDPHVOLNH2ߞ1DQSRVVHVLYHV
OLNHPRQߞVSODFHRUFRQWUDFWLRQVOLNHFDQߞW\RXQHHGWRHVFDSHLWZLWKDQRWKHU
7KHHVFDSHFKDUDFWHULVDQRWKHUVLQJOHTXRWHSODFHGLQIURQWRIWKHVLQJOHTXRWH
\RXߞUHWU\LQJWRHVFDSH6D\\RXߞUHZULWLQJDQLQVHUWVWDWHPHQWZKHUH\RXFRSLHG
DODUJHSDVVDJHIURPDQRYHO$IIL[LQJ\HWDQRWKHUVLQJOHTXRWHWRDOOH[LVWLQJ
VLQJOHTXRWHVLVERWKWHGLRXVWRDGGDQGFKDOOHQJLQJWRUHDG$IWHUDOOWZRVLQJOH
TXRWHVORRNDZIXOO\OLNHRQHGRXEOHTXRWHZKLFKLVDQRWKHUFKDUDFWHUHQWLUHO\
'ROODU4XRWLQJ
SELECT json_agg(f) As cats
FROM (
SELECT MAX(fact_type_id) As max_type, category
FROM census.lu_fact_types
GROUP BY category
) As f;
([DPSOH4XHU\WR-621XVLQJMVRQBDJJ
ALTER TABLE logs
ALTER COLUMN log_id RESTART WITH 2000;
,IZHDOUHDG\KDGGDWDLQWKHWDEOHZHߞGQHHGWRSUHYHQWWKHQXPEHULQJIURP
VWDUWLQJDWZLWKDVWDWHPHQWOLNHWKLV
DROP SEQUENCE logs_log_id_seq CASCADE;
ALTER TABLE logs
ALTER COLUMN log_id ADD GENERATED BY DEFAULT AS IDENTITY;
<RXFRXOGWXUQWKHH[LVWLQJORJBLGFROXPQWRWKHQHZ,'(17,7<FRQVWUXFW
XVLQJDVHTXHQFHREMHFW
1HZLQYHUVLRQLVWKH,'(17,7<TXDOLILHUIRUDFROXPQ,'(17,7<LVD
PRUHVWDQGDUGFRPSOLDQWZD\RIJHQHUDWLQJDQDXWRQXPEHUIRUDWDEOHFROXPQ
serialLVWKHGDWDW\SHXVHGWRUHSUHVHQWDQLQFUHPHQWLQJDXWRQXPEHU
$GGLQJDVHULDOFROXPQDXWRPDWLFDOO\DGGVDQDFFRPSDQ\LQJVHTXHQFH
REMHFWWRWKHGDWDEDVHVFKHPD$VHULDOGDWDW\SHLVDOZD\VDQLQWHJHUZLWK
WKHGHIDXOWYDOXHVHWWRWKHQH[WYDOXHRIWKHVHTXHQFHREMHFW(DFKWDEOH
XVXDOO\KDVMXVWRQHVHULDOFROXPQZKLFKRIWHQVHUYHVDVWKHSULPDU\NH\
)RUYHU\ODUJHWDEOHV\RXVKRXOGRSWIRUWKHUHODWHGbigserial
varcharLVVKRUWKDQGIRUߡFKDUDFWHUYDU\LQJߢDYDULDEOHOHQJWKVWULQJVLPLODU
WRZKDW\RXZLOOILQGLQRWKHUGDWDEDVHV<RXGRQߞWQHHGWRVSHFLI\D
PD[LPXPOHQJWKLI\RXGRQߞWvarcharZLOOEHDOPRVWLGHQWLFDOWRWKHWH[W
GDWDW\SH
textLVDVWULQJRILQGHWHUPLQDWHOHQJWK,WߞVQHYHUIROORZHGE\DOHQJWK
UHVWULFWLRQ
timestamp with time zone VKRUWKDQGtimestamptz LVDGDWHDQGWLPH
GDWDW\SHDOZD\VVWRUHGLQ87&,WGLVSOD\VGDWHDQGWLPHLQWKHVHUYHUߞV
RZQWLPH]RQHXQOHVV\RXWHOOLWWRRWKHUZLVH6HHߡ7LPH=RQHV:KDW7KH\
$UHDQG$UH1RWߢIRUDPRUHWKRURXJKGLVFXVVLRQ
log_id serial PRIMARY KEY,
user_name varchar(50),
description text,
log_ts timestamp with time zone NOT NULL DEFAULT current_timestamp
);
CREATE INDEX idx_logs_log_ts ON logs USING btree (log_ts);
3RVWJUH64/VWDQGVDORQHDVWKHRQO\GDWDEDVHSURGXFWRIIHULQJLQK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DEOHVRLQFUHPHQWLQJDQGUHVHWWLQJWKHYDOXHLVPDQDJHGZLWKWKHWDEOH$VHULDO
RQWKHRWKHUKDQGFUHDWHVDVHTXHQFHREMHFWWKDWPD\RUPD\QRWEHUHXVHGE\
RWKHUWDEOHVDQGQHHGVWREHGURSSHGPDQXDOO\ZKHQLWߞVQRORQJHUQHHGHG,I
\RXZDQWHGWRUHVHWWKHQXPEHURIDVHULDO\RXߞGQHHGWRPRGLI\WKHUHODWHG
6(48(1&(REMHFWZKLFKPHDQVNQRZLQJZKDWWKHQDPHRILWLV
7KHVWUXFWXUHRI([DPSOHLVPXFKWKHVDPHDVZKDWZHVDZLQ([DPSOH
EXWPRUHYHUERVH
CREATE TABLE logs (
log_id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
user_name varchar(50),
description text,
log_ts timestamp with time zone NOT NULL DEFAULT current_timestamp
);
([DPSOH%DVLFWDEOHFUHDWLRQXVLQJ,'(17,7<
,IZHZHUHVWDUWLQJZLWKDQHZWDEOHZHߞGFUHDWHLWDVVKRZQLQ([DPSOH
XVLQJ,'(17,7<LQVWHDGRIVHULDO
,QYHUVLRQWKHjson_aggIXQFWLRQUHSODFHVWKHFKDLQRIarray_to_jsonDQG
array_aggRIIHULQJERWKFRQYHQLHQFHDQGVSHHG,Q([DPSOHZHUHSHDW
([DPSOHXVLQJjson_aggDQGERWKH[DPSOHVZLOOKDYHWKHVDPHRXWSXW
'HILQHVDVXETXHU\ZLWKQDPHffFDQWKHQEHXVHGWRUHIHUHQFHHDFKURZLQ
WKHVXETXHU\
$JJUHJDWHHDFKURZRIVXETXHU\LQJXVLQJDUUD\BDJJDQGWKHQFRQYHUWWKH
DUUD\WRMVRQZLWKDUUD\BWRBMVRQ
cats
---------------------------------------------------[{"max_type":102,"category":"Population"},
{"max_type":153,"category":"Housing"}]
7KLVZLOOJLYH\RXDQRXWSXWRI
SELECT array_to_json(array_agg(f)) As cat
FROM (
SELECT MAX(fact_type_id) As max_type, category
FROM census.lu_fact_types
GROUP BY category
) As f;
([DPSOH4XHU\WR-621RXWSXW
,I\RXDUHEXLOGLQJZHEDSSOLFDWLRQV\RXFDQWDNHDGYDQWDJHRIWKHEXLOWLQ
-621DQG-621%VXSSRUWZHFRYHUHGLQߡ-621ߢDQGXVHDFRPELQDWLRQRI
array_aggDQGarray_to_jsonWRRXWSXWDTXHU\DVDVLQJOH-621REMHFWDV
VKRZQLQ([DPSOH,Q3RVWJUH64/\RXFDQXVHjson_agg6HH
([DPSOH
,QVWHDGRIHUURULQJRXWWKHSUHFHGLQJH[DPSOHUHWXUQVWKHFDQRQLFDO
UHSUHVHQWDWLRQRIDlu_fact_typeGDWDW\SH&RPSRVLWHVFDQVHUYHDVLQSXWWR
VHYHUDOXVHIXOIXQFWLRQVDPRQJZKLFKDUHarray_aggDQGhstore DIXQFWLRQ
SDFNDJHGZLWKWKHKVWRUHH[WHQVLRQWKDWFRQYHUWVDURZLQWRDNH\YDOXHSDLU
REMHFW x
-----------------------------------------------------------------(86,Population,"{D001,Total:}",d001)
(87,Population,"{D002,Total:,""Not Hispanic or Latino:""}",d002)
WKHUHVXOW
$WILUVWJODQFH\RXPLJKWWKLQNWKDWZHOHIWRXWD.*E\DFFLGHQWEXWFKHFNRXW
SELECT x FROM census.lu_fact_types As x LIMIT 2;
3RVWJUH64/DXWRPDWLFDOO\FUHDWHVGDWDW\SHVRIDOOWDEOHV%HFDXVHGDWDW\SHV
GHULYHGIURPWDEOHVFRQWDLQRWKHUGDWDW\SHVWKH\DUHRIWHQFDOOHGFRPSRVLWHGDWD
W\SHVRUMXVWFRPSRVLWHV7KHILUVWWLPH\RXVHHDTXHU\ZLWKFRPSRVLWHV\RX
PLJKWEHVXUSULVHG,QIDFW\RXPLJKWFRPHDFURVVWKHLUYHUVDWLOLW\E\DFFLGHQW
ZKHQPDNLQJDW\SRLQDQ64/VWDWHPHQW7U\WKHIROORZLQJTXHU\
&RPSRVLWH7\SHVLQ4XHULHV
7KH'2SDUWRIWKH,16(57FRQVWUXFWZLOORQO\KDSSHQLIWKHUHLVDSULPDU\NH\
XQLTXHLQGH[RUXQLTXHNH\FRQVWUDLQWHUURUWULJJHUHG+RZHYHUHUURUVVXFKDV
GDWDW\SHRQHVRUFKHFNFRQVWUDLQWVZLOOIDLODQGQHYHUEHSURFHVVHGE\'2
83'$7(
INSERT INTO colors(color, hex)
VALUES('Blue', '0000FF'), ('Red', 'FF0000'), ('Green', '00FF00')
ON CONFLICT ON CONSTRAINT colors_pkey
DO UPDATE SET color = EXCLUDED.color, hex = EXCLUDED.hex;;
([DPSOH21&21)/,&7'283'$7(
,QWKHFDVHRI,16(5721&21)/,&7'283'$7(\RXQHHGWRVSHFLI\WKH
FRQIOLFWLQJFRQGLWLRQRU&21675$,17QDPH,IXVLQJDFRQVWUDLQW\RXߞGXVH
ON CONFLICT ON CONSTRAINT constraint_name_hereDVVKRZQLQ
([DPSOH
,Q([DPSOHZHVSHFLILHGWKHFRQIOLFWZKLFKPDWFKHVWKHH[SUHVVLRQRID
FRQVWUDLQWRUXQLTXHLQGH[VRXVLQJVRPHWKLQJOLNHupper(color)ZRXOGQRW
ZRUNVLQFHWKHFRORUVWDEOHKDVQRPDWFKLQJLQGH[IRUWKDWH[SUHVVLRQ
INSERT INTO colors(color, hex)
VALUES('Blue', '0000FF'), ('Red', 'FF0000'), ('Green', '00FF00')
ON CONFLICT(lower(color))
DO UPDATE SET color = EXCLUDED.color, hex = EXCLUDED.hex;
([DPSOH21&21)/,&7'283'$7(
UHDOO\ZDQWHGWRVSHOOWKHFRORUVDVJLYHQWRXVZHFRXOGXVHFRGHOLNHWKDWJLYHQ
LQ([DPSOH
$OOWDEOHVLQDSDUWLWLRQPXVWKDYHWKHVDPHH[DFWFROXPQV7KLVLVXQOLNH
:KHQSDUWLWLRQVDUHXVHGGDWDFDQEHLQVHUWHGLQWRWKHFRUHWDEOHDQGLV
UHURXWHGDXWRPDWLFDOO\WRWKHPDWFKLQJSDUWLWLRQ7KLVLVQR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TABLE logs_2011
ADD CONSTRAINT chk_y2011
CHECK (
log_ts >= '2011-1-1'::timestamptz AND log_ts < '2012-1-1'::timestamptz
);
([DPSOH,QKHULWHGWDEOHFUHDWLRQ
FRQVWUDLQWVDQGLQGH[HVDUHQHYHULQKHULWHG&KHFNFRQVWUDLQWVDUHLQKHULWHGEXW
FKLOGUHQFDQKDYHWKHLURZQFKHFNFRQVWUDLQWVLQDGGLWLRQWRWKHRQHVWKH\LQKHULW
IURPWKHLUSDUHQWV VHH([DPSOH 6LPLODUWRLQKHULWDQFHZHFUHDWHFKLOGWDEOHVRIWKHSDUWLWLRQH[FHSWLQVWHDGRI
XVLQJ&+(&.FRQVWUDLQWVWRGHQRWHDOORZHGGDWDLQWKHFKLOGWDEOHZHXVHWKH
CREATE TABLE logs (
log_id int GENERATED BY DEFAULT AS IDENTITY,
user_name varchar(50),
description text,
log_ts timestamp with time zone NOT NULL DEFAULT current_timestamp
) PARTITION BY RANGE (log_ts);
([DPSOH%DVLFWDEOHFUHDWLRQIRUSDUWLWLRQ
)RUDSDUWLWLRQHGWDEOHVHWWKHSDUHQWWDEOHPXVWEHQRWHGDVDSDUWLWLRQHGWDEOH
WKURXJKWKHPARTITION BYV\QWD[DVVKRZQLQ([DPSOH&RQWUDVWWKDWWR
([DPSOHZKHUHZHMXVWVWDUWZLWKDUHJXODUWDEOHGHILQLWLRQ$OVRQRWHWKDW
ZHGRQRWGHILQHDSULPDU\NH\EHFDXVHSULPDU\NH\VDUHQRWVXSSRUWHGIRUWKH
SDUHQWSDUWLWLRQWDEOH
DROP TABLE IF EXISTS logs CASCADE;
)LUVWZHߞOOGURSRXUH[LVWLQJORJVWDEOHDQGDOOLWVFKLOGWDEOHV
:HߞOOUHFUHDWHWKHlogsWDEOHIURP([DPSOHDVDSDUWLWLRQHGWDEOHDQGFUHDWH
WKHFKLOGWDEOHVXVLQJSDUWLWLRQV\QWD[LQVWHDGRIWKHLQKHULWDQFHVKRZQLQ
([DPSOH
8QOLNHLQKHULWHGWDEOHVWKHSDUHQWSDUWLWLRQHGWDEOHFDQߞWKDYHDQ\URZVRILWV
RZQ$OOLQVHUWVDUHUHGLUHFWHGWRDPDWFKLQJFKLOGSDUWLWLRQDQGZKHQQR
PDWFKLQJFKLOGSDUWLWLRQLVDYDLODEOHDQHUURULVWKURZQ
7KHSDUHQWRIWKHSDUWLWLRQFDQߞWKDYHSULPDU\NH\VXQLTXHNH\VRULQGH[HV
DOWKRXJKWKHFKLOGSDUWLWLRQVFDQ7KLVLVGLIIHUHQWIURPWKHLQKHULWDQFHWDEOHV
ZKHUHWKHSDUHQWDQGHDFKFKLOGFDQKDYHDSULPDU\NH\WKDWQHHGVRQO\WREH
XQLTXHZLWKLQWKHWDEOHQRWQHFHVVDULO\DFURVVDOOWKHLQKHULWHGFKLOGUHQ
(DFKSDUWLWLRQHGWDEOHEHORQJVWRDVLQJOHSDUWLWLRQHGJURXS,QWHUQDOO\WKDW
PHDQVLWFDQKDYHRQO\RQHSDUHQWWDEOH,QKHULWHGWDEOHVRQRWKHUKDQGFDQ
LQKHULWFROXPQVIURPPXOWLSOHWDEOHV
LQKHULWHGWDEOHVZKHUHFKLOGWDEOHVDUHDOORZHGWRKDYHDGGLWLRQDOFROXPQV
WKDWDUHQRWLQWKHSDUHQWWDEOHV
|
|
|
|
asian_alone
native_hawaiian_and_other_pacific_islander_alone
some_other_race_alone
two_or_more_races
$VEHIRUHLIZHWULHGWRLQVHUWD'Blue'ZHߞGEHSUHYHQWHGIURPGRLQJVRDQG
WKH21&21)/,&7'2127+,1*ZRXOGUHVXOWLQQRWKLQJKDSSHQLQJ,IZH
CREATE UNIQUE INDEX uidx_colors_lcolor ON colors USING btree(lower(color));
6RPHRQHFRXOGFRPHDQGSXWLQDGLIIHUHQWFDVH'Blue'LQRXUV\VWHPDQGZHߞG
WKHQKDYHWZRGLIIHUHQWFDVHGEOXHV7RUHPHG\WKLVZHFDQSXWDXQLTXHLQGH[
RQRXUWDEOH
INSERT INTO colors(color, hex)
VALUES('blue', '0000FF'), ('red', 'FF0000'), ('green', '00FF00')
ON CONFLICT DO NOTHING ;
([DPSOH21&21)/,&7'2127+,1*
:HWKHQJHWDQHZEDWFKRIFRORUVWRDGGWRRXUWDEOHEXWVRPHPD\EHSUHVHQW
DOUHDG\,IZHGRDUHJXODULQVHUWZHߞGJHWDSULPDU\NH\YLRODWLRQZKHQZHWULHG
WRDGGFRORUVDOUHDG\LQWKHWDEOH:KHQZHUXQ([DPSOHZHJHWRQO\RQH
UHFRUGLQVHUWHGWKHgreenWKDWLVQRWDOUHDG\LQRXUWDEOHDQGHDFKVXEVHTXHQW
UXQZRXOGUHVXOWLQQRUHFRUGVEHLQJLQVHUWHG
CREATE TABLE colors(color varchar(50) PRIMARY KEY, hex varchar(6));
INSERT INTO colors(color, hex)
VALUES('blue', '0000FF'), ('red', 'FF0000');
7KLVIHDWXUHUHTXLUHVDXQLTXHNH\SULPDU\NH\XQLTXHLQGH[RUH[FOXVLRQ
FRQVWUDLQWLQSODFHWKDWZKHQYLRODWHG\RXߞGZDQWGLIIHUHQWEHKDYLRUOLNH
XSGDWLQJWKHH[LVWLQJUHFRUGRUQRWGRLQJDQ\WKLQJ7RGHPRQVWUDWHLPDJLQHZH
KDYHDWDEOHRIFRORUVWRFUHDWH
1HZLQYHUVLRQLVWKH,16(5721&21)/,&7FRQVWUXFWZKLFKLVRIWHQ
UHIHUUHGWRDVDQ836(577KLVIHDWXUHLVXVHIXOLI\RXGRQߞWNQRZDUHFRUG
DOUHDG\H[LVWVLQDWDEOHDQGUDWKHUWKDQKDYLQJWKHLQVHUWIDLO\RXZDQWLWWR
HLWKHUXSGDWHWKHH[LVWLQJUHFRUGRUGRQRWKLQJ
836(57V,16(5721&21)/,&783'$7(
99
100
101
102
UPDATE census.lu_fact_types AS f
SET short_name = replace(replace(lower(f.fact_subcats[4]),' ','_'),':','')
WHERE f.fact_subcats[3] = 'Hispanic or Latino:' AND f.fact_subcats[4] > ''
RETURNING fact_type_id, short_name;
fact_type_id | short_name
-------------+------------------------------------------------96
| white_alone
97
| black_or_african_american_alone
98
| american_indian_and_alaska_native_alone
([DPSOH5HWXUQLQJFKDQJHGUHFRUGVRIDQ83'$7(ZLWK5(7851,1*
7KHRETURNINGSUHGLFDWHLVVXSSRUWHGE\$16,64/VWDQGDUGVEXWQRW
FRPPRQO\IRXQGLQRWKHUUHODWLRQDOGDWDEDVHV:HVKRZDQH[DPSOHLQ
([DPSOHZKHUHZHUHWXUQWKHUHFRUGVGHOHWHGRETURNINGFDQDOVREHXVHG
IRULQVHUWVDQGXSGDWHV)RULQVHUWVLQWRWDEOHVZLWKDVHULDONH\RETURNINGLV
LQYDOXDEOHEHFDXVHLWUHWXUQVWKHNH\YDOXHRIWKHQHZURZVߚVRPHWKLQJ\RX
ZRXOGQߞWNQRZSULRUWRWKHTXHU\H[HFXWLRQ$OWKRXJKRETURNINGLVRIWHQ
DFFRPSDQLHGE\*IRUDOOILHOGV\RXFDQOLPLWWKHILHOGVDVZHGRLQ([DPSOH
5HWXUQLQJ$IIHFWHG5HFRUGVWRWKH8VHU
7KHVWDQGDUGVFRPSOLDQWZD\ZRXOGEHWRXVHDFOXQNLHUINH[SUHVVLRQLQWKH
WHERE
DELETE FROM census.facts
USING census.lu_fact_types As ft
WHERE facts.fact_type_id = ft.fact_type_id AND ft.short_name = 's01';
([DPSOH'(/(7(86,1*
2IWHQZKHQ\RXGHOHWHGDWDIURPDWDEOH\RXߞOOZDQWWRGHOHWHWKHGDWDEDVHGRQ
LWVSUHVHQFHLQDQRWKHUVHWRIGDWD6SHFLI\WKLVDGGLWLRQDOVHWZLWKWKHUSING
SUHGLFDWH7KHQLQWKHWHEREFODXVH\RXFDQXVHERWKGDWDVHWVLQWKHUSINGDQG
LQWKHFROMWRGHILQHFRQGLWLRQVIRUGHOHWLRQ0XOWLSOHWDEOHVFDQEHLQFOXGHGLQ
USINGVHSDUDWHGE\FRPPDV([DPSOHGHOHWHVDOOUHFRUGVIURPFHQVXVIDFWV
WKDWFRUUHVSRQGWRDIDFWW\SHRIshort_name = 's01'
'(/(7(86,1*
DOUHDG\EHHQPRYHGSUHYLRXVO\
;
6LPLODUWRWKHZD\LQKHULWDQFHZRUNVZKHQZHTXHU\WKHSDUHQWWDEOHDOO
,QWKHUHDOZRUOG\RXZRXOGQHHGWRFUHDWHLQGH[HVDQGSULPDU\NH\VRQWKHQHZ
FKLOGIRUTXHU\HIILFLHQF\
5HSHDWLQJRXULQVHUWQRZZHFDQVHHE\SELECT * FROM logs_gt_2011;WKDW
RXUGDWDJRWUHURXWHGWRWKHQHZSDUWLWLRQ
8QOLNH([DPSOHZHRSWHGWRXVHWKH3$57,7,21UDQJHNH\ZRUG
unboundedZKLFKDOORZVRXUSDUWLWLRQWREHXVHGIRUIXWXUHGDWHV
CREATE TABLE logs_gt_2011 PARTITION OF logs
FOR VALUES FROM ('2012-1-1') TO (unbounded);
,IZHWKHQFUHDWHDSDUWLWLRQWDEOHIRUWKHFXUUHQW\HDU
ERROR: no partition of relation "logs" found for row
DETAIL: Partition key of the failing row contains
(log_ts) = (2017-05-25 02:58:28.057101-04).
:HߞGJHWDQHUURUVXFKDV
INSERT INTO logs(user_name, description ) VALUES ('regina', 'Sleeping');
1RZLIZHZHUHWRLQVHUWGDWDDVIROORZV
'HILQHWKHQHZWDEOHDVDSDUWLWLRQRIlogs
'HILQHWKHVHWRIGDWDWREHVWRUHGLQWKLVSDUWLWLRQ&KLOGSDUWLWLRQVPXVWQRW
KDYHRYHUODSSLQJUDQJHVVRLI\RXWU\WRGHILQHDUDQJHWKDWRYHUODSVDQ
H[LVWLQJUDQJHWKH&5($7(7$%/(FRPPDQGZLOOIDLOZLWKDQHUURU
&KLOGSDUWLWLRQVFDQKDYHLQGH[HVDQGSULPDU\NH\V$VZLWKLQKHULWDQFHWKH
SULPDU\NH\LVQRWHQIRUFHGDFURVVWKHZKROHSDUWLWLRQVHWRIWDEOHV
CREATE TABLE logs_2011 PARTITION OF logs
FOR VALUES FROM ('2011-1-1') TO ('2012-1-1') ;
CREATE INDEX idx_logs_2011_log_ts ON logs_2011 USING btree(log_ts);
ALTER TABLE logs_2011 ADD CONSTRAINT pk_logs_2011 PRIMARY KEY (log_id)
([DPSOH&UHDWHDFKLOGSDUWLWLRQ
FOR VALUES FROM''/FRQVWUXFW:HUHSHDWWKHH[HUFLVHIURP([DPSOHLQ
([DPSOHEXWXVLQJWKHFOR VALUES FROMFRQVWUXFWLQVWHDGRIINHERITS
([DPSOH8QORJJHGWDEOHFUHDWLRQ
,I\RXDFFLGHQWDOO\XQSOXJWKHSRZHUFRUGRQWKHVHUYHUDQGWKHQWXUQWKHSRZHU
EDFNRQWKHUROOEDFNSURFHVVZLOOZLSHFOHDQDOOGDWDLQXQORJJHGWDEOHV
$QRWKHUFRQVHTXHQFHRIPDNLQJDWDEOHXQORJJHGLVWKDWLWVGDWDZRQߞWEHDEOHWR
SDUWLFLSDWHLQ3RVWJUH64/UHSOLFDWLRQ$SJBGXPSRSWLRQDOVRDOORZV\RXWRVNLS
WKHEDFNLQJXSRIXQORJJHGGDWD
)RUHSKHPHUDOGDWDWKDWFRXOGEHUHEXLOWLQWKHHYHQWRIDGLVNIDLOXUHRUGRHVQߞW
QHHGWREHUHVWRUHGDIWHUDFUDVK\RXPLJKWSUHIHUKDYLQJPRUHVSHHGWKDQ
UHGXQGDQF\7KHUNLOGGEDPRGLILHUDOORZV\RXWRFUHDWHXQORJJHGWDEOHVDV
VKRZQLQ([DPSOH7KHVHWDEOHVZLOOQRWEHSDUWRIDQ\ZULWHDKHDGORJV
7KHELJDGYDQWDJHRIDQXQORJJHGWDEOHLVWKDWZULWLQJGDWDWRLWLVPXFKIDVWHU
WKDQWRDORJJHGWDEOHߚ࠺WLPHVIDVWHULQRXUH[SHULHQFH
8QORJJHG7DEOHV
Table "public.logs"
:
Partition key: RANGE (log_ts)
Partitions: logs_2011
FOR VALUES FROM ('2011-01-01 00:00:00-05') TO ('2012-01-01 00:00:00-05'),
logs_gt_2011
FOR VALUES FROM ('2012-01-01 00:00:00-05') TO (UNBOUNDED)
\d+ logs
,I\RXDUHXVLQJWKH364/SDFNDJHGZLWK3RVWJUH64/\RXZLOOJHWPRUH
LQIRUPDWLRQZKHQ\RXXVHWKHGHVFULEHWDEOHFRPPDQGWKDWGHWDLOVWKHSDUWLWLRQ
UDQJHVRIWKHSDUHQWWDEOH
7KLVLVZKHUHWKHONLYNH\ZRUGFRPHVLQKDQG\:HVKRZDQH[DPSOHRILWVXVH
LQ([DPSOHZKHUHZHZDQWWRGHOHWHRQO\WKRVHUHFRUGVIURPWKH
SURGXFWLRQWDEOHWKDWKDYHQߞWPLJUDWHGWRWKHORJWDEOH:LWKRXWWKHONLY
PRGLILHUZHߞGHQGXSGHOHWLQJUHFRUGVIURPWKHFKLOGWDEOHWKDWPLJKWKDYH
:KHQ\RXTXHU\IURPDWDEOHWKDWKDVFKLOGWDEOHVWKHTXHU\DXWRPDWLFDOO\GULOOV
GRZQLQWRWKHFKLOGUHQFUHDWLQJDXQLRQRIDOOWKHFKLOGUHFRUGVVDWLVI\LQJWKH
TXHU\FRQGLWLRQDELETEDQGUPDATEZRUNWKHVDPHZD\GULOOLQJGRZQWKH
KLHUDUFK\IRUYLFWLPV6RPHWLPHVWKLVLVQRWGHVLUDEOHEHFDXVH\RXZDQWGDWDWR
FRPHRQO\IURPWKHWDEOH\RXVSHFLILHGZLWKRXWWKHNLGVWDJJLQJDORQJ
5HVWULFWLQJ'(/(7(83'$7(DQG6(/(&7IURP,QKHULWHG
7DEOHV
SELECT i_type,
generate_series('2012-01-01'::date,'2012-12-31'::date,i_type) As dt
FROM interval_periods;
i_type
| dt
-----------+----------------------5 months | 2012-01-01 00:00:00-05
5 months | 2012-06-01 00:00:00-04
5 months | 2012-11-01 00:00:00-04
132 days | 2012-01-01 00:00:00-05
132 days | 2012-05-12 00:00:00-04
132 days | 2012-09-21 00:00:00-04
4862 hours | 2012-01-01 00:00:00-05
4862 hours | 2012-07-21 15:00:00-04
([DPSOH6HWUHWXUQLQJIXQFWLRQLQ6(/(&7
CREATE TABLE interval_periods (i_type interval);
INSERT INTO interval_periods (i_type)
VALUES ('5 months'), ('132 days'), ('4862 hours');
,QWHUZHDYLQJVRPHVHWUHWXUQLQJIXQFWLRQVLQWRDQDOUHDG\FRPSOLFDWHGTXHU\
FRXOGSURGXFHUHVXOWVEH\RQGZKDW\RXH[SHFWEHFDXVHWKHVHIXQFWLRQVXVXDOO\
UHVXOWLQWKHFUHDWLRQRIQHZURZV<RXPXVWDQWLFLSDWHWKLVLI\RXߞOOEHXVLQJWKH
UHVXOWVDVDVXETXHU\,Q([DPSOHZHGHPRQVWUDWHURZFUHDWLRQUHVXOWLQJ
IURPXVLQJDWHPSRUDOYHUVLRQRIgenerate_series7KHH[DPSOHXVHVDWDEOH
WKDWZHFRQVWUXFWZLWK
([DPSOH3ODQQHUVNLSSLQJRWKHUSDUWLWLRQV
EXPLAIN ANALYZE SELECT * FROM logs WHERE log_ts > '2017-05-01';
Append (cost=0.00..15.25 rows=140 width=162)
(actual time=0.008..0.009 rows=1 loops=1)
-> Seq Scan on logs_gt_2011 (cost=0.00..15.25 rows=140 width=162)
(actual time=0.008..0.008 rows=1 loops=1)
Filter: (log_ts > '2017-05-01 00:00:00-04'::timestamp with time zone)
Planning time: 0.152 ms
Execution time: 0.022 ms
IXQFWLRQVFDQDSSHDULQWKHSELECT
SDUWLWLRQVWKDWGRQߞWVDWLVI\WKHGDWHILOWHUDUHVNLSSHGDVVKRZQLQ([DPSOH
3RVWJUH64/DOORZVVHWUHWXUQLQJIXQFWLRQVWRDSSHDULQWKHSELECTFODXVHRIDQ
64/VWDWHPHQW7KLVLVQRWWUXHRIPRVWRWKHUGDWDEDVHVLQZKLFKRQO\VFDODU
$VHWUHWXUQLQJIXQFWLRQLVDIXQFWLRQWKDWFRXOGUHWXUQPRUHWKDQRQHURZ
6HW5HWXUQLQJ)XQFWLRQVLQ6(/(&7
ANYFDQEHXVHGZLWKDQ\GDWDW\SHVDQGFRPSDULVRQRSHUDWRUV RSHUDWRUVWKDW
UHWXUQD%RROHDQ LQFOXGLQJRQHV\RXEXLOW\RXUVHOIRULQVWDOOHGYLDH[WHQVLRQV
7KHH[DPSOHMXVWVKRZQLVDVKRUWKDQGZD\RIXVLQJPXOWLSOHILIKEORFODXVHV
<RXFDQXVHANYZLWKRWKHUFRPSDUDWRUVVXFKDVLIKE=DQG~ WKHUHJH[OLNH
RSHUDWRU tract_name
----------------------------------------------------Census Tract 102.06, Barnstable County, Massachusetts
Census Tract 103.06, Barnstable County, Massachusetts
Census Tract 106, Barnstable County, Massachusetts
Census Tract 9900, Dukes County, Massachusetts
(4 rows)
SELECT tract_name
FROM census.lu_tracts
WHERE tract_name ILIKE ANY(ARRAY['%99%duke%','%06%Barnstable%']::text[]);
+HUHLVDQH[DPSOH
3RVWJUH64/KDVDFRQVWUXFWFDOOHG$1<WKDWFDQEHXVHGLQFRQMXQFWLRQZLWK
DUUD\VFRPELQHGZLWKDFRPSDUDWRURSHUDWRURUFRPSDUDWRUNH\ZRUG,IDQ\
HOHPHQWRIWKHDUUD\PDWFKHVDURZWKDWURZLVUHWXUQHG
$1<$UUD\6HDUFK
tract_name
-----------------------------------------------Census Tract 2001, Dukes County, Massachusetts
Census Tract 2002, Dukes County, Massachusetts
Census Tract 2003, Dukes County, Massachusetts
Census Tract 2004, Dukes County, Massachusetts
Census Tract 9900, Dukes County, Massachusetts
SELECT tract_name FROM census.lu_tracts WHERE tract_name ILIKE '%duke%';
/HWߞVVD\ZHQRZQHHGWRDGGDSKRQHQXPEHUWRRXUsuper_usersWDEOHIURP
$IWHUFUHDWLQJWDEOHVIURPGDWDW\SHV\RXFDQߞWDOWHUWKHFROXPQVRIWKHWDEOH
,QVWHDGDGGRUUHPRYHFROXPQVWRWKHFRPSRVLWHGDWDW\SHDQG3RVWJUH64/ZLOO
DXWRPDWLFDOO\SURSDJDWHWKHFKDQJHVWRWKHWDEOHVWUXFWXUH0XFKOLNH
LQKHUL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REATE UNLOGGED TABLE web_sessions (
session_id text PRIMARY KEY,
add_ts timestamptz,
upd_ts timestamptz,
session_state xml);
SET search_path=census, public;
ALTER TABLE facts ADD CONSTRAINT fk_facts_1 FOREIGN KEY (fact_type_id)
REFERENCES lu_fact_types (fact_type_id) ON UPDATE CASCADE ON DELETE RESTRICT;
([DPSOH%XLOGLQJIRUHLJQNH\FRQVWUDLQWVDQGFRYHULQJLQGH[HV
3RVWJUH64/IROORZVWKHVDPHFRQYHQWLRQDVPRVWGDWDEDVHVWKDWVXSSRUW
UHIHUHQWLDOLQWHJULW\<RXFDQVSHFLI\FDVFDGHXSGDWHDQGGHOHWHUXOHVWRDYRLG
SHVN\RUSKDQHGUHFRUGV:HVKRZ\RXKRZWRDGGIRUHLJQNH\FRQVWUDLQWVLQ
([DPSOH
)RUHLJQ.H\&RQVWUDLQWV
1DPHVRISULPDU\NH\DQGXQLTXHNH\FRQVWUDLQWVPXVWEHXQLTXHZLWKLQDJLYHQVFKHPD$
JRRGSUDFWLFHLVWRLQFOXGHWKHQDPHRIWKHWDEOHDQGFROXPQDVSDUWRIWKHQDPHRIWKHNH\)RU
WKHVDNHRIEUHYLW\RXUH[DPSOHVPLJKWQRWDELGHE\WKLVSUDFWLFH
:$51,1*
3RVWJUH64/FRQVWUDLQWVDUHWKHPRVWDGYDQFHG DQGPRVWFRPSOH[ RIDQ\
GDWDEDVHZHߞYHZRUNHGZLWK<RXFDQFRQWURODOOIDFHWVRIKRZDFRQVWUDLQW
KDQGOHVH[LVWLQJGDWDDOOFDVFDGHRSWLRQVKRZWRSHUIRUPWKHPDWFKLQJZKLFK
LQGH[HVWRLQFRUSRUDWHFRQGLWLRQVXQGHUZKLFKWKHFRQVWUDLQWFDQEHYLRODWHG
DQGPRUH2QWRSRILWDOO\RXFDQSLFN\RXURZQQDPHIRUHDFKFRQVWUDLQW)RU
WKHIXOOWUHDWPHQWZHVXJJHVW\RXUHYLHZWKHRIILFLDOGRFXPHQWDWLRQ<RXߞOOILQG
FRPIRUWLQNQRZLQJWKDWXVLQJWKHGHIDXOWVHWWLQJVXVXDOO\ZRUNVRXWILQH:HߞOO
VWDUWRIIZLWKVRPHWKLQJIDPLOLDUWRPRVWUHODWLRQDOIRONVIRUHLJQNH\XQLTXH
DQGFKHFNFRQVWUDLQWV7KHQZHߞOOPRYHRQWRH[FOXVLRQFRQVWUDLQWV
&RQVWUDLQWV
1RUPDOO\\RXFDQߞWFKDQJHWKHGHILQLWLRQRIDW\SHLIWDEOHVGHSHQGRQWKDWW\SH
7KHCASCADEPRGLILHURYHUULGHVWKLVUHVWULFWLRQDSSO\LQJWKHVDPHFKDQJHWRDOO
GHSHQGHQWWDEOHV
ALTER TYPE basic_user ADD ATTRIBUTE phone varchar(10) CASCADE;
([DPSOH$OOZHKDYHWRGRLVH[HFXWHWKHIROORZLQJFRPPDQG
3RVWJUH64/LVFDVHVHQVLWLYH+RZHYHULWGRHVKDYHPHFKDQLVPVLQSODFHWR
LJQRUHFDVLQJ<RXFDQDSSO\WKHupperIXQFWLRQWRERWKVLGHVRIWKH$16,LIKE
RSHUDWRURU\RXFDQVLPSO\XVHWKHILIKE aa RSHUDWRU
,/,.(IRU&DVH,QVHQVLWLYH6HDUFK
:KHQ\RXXVHVALUESDVDVWDQGLQIRUDYLUWXDOWDEOH\RXQHHGWRVSHFLI\WKH
QDPHVIRUWKHFROXPQV<RXDOVRQHHGWRH[SOLFLWO\FDVWWKHYDOXHVWRWKHGDWD
W\SHVLQWKHWDEOHLIWKHSDUVHUFDQߞWLQIHUWKHGDWDW\SHIURPWKHGDWD7KH
PXOWLURZ9$/8(6FRQVWUXFWDOVRH[LVWVLQ0\64/DQG64/6HUYHU
SELECT *
FROM (
VALUES
('robe', 'logged in', '2011-01-10 10:15 AM EST'::timestamptz),
('lhsu', 'logged out', '2011-01-11 10:20 AM EST'::timestamptz)
) AS l (user_name, description, log_ts);
([DPSOH8VLQJDPXOWLURZFRQVWUXFWRUDVDYLUWXDOWDEOH
7KHODWWHUSRUWLRQRIWKHPXOWLURZFRQVWUXFWRUVWDUWLQJZLWKWKHVALUESNH\ZRUG
LVRIWHQUHIHUUHGWRDVDYDOXHVOLVW$YDOXHVOLVWFDQVWDQGDORQHDQGHIIHFWLYHO\
FUHDWHVDWDEOHRQWKHIO\DVLQ([DPSOH
INSERT INTO logs_2011 (user_name, description, log_ts)
VALUES
('robe', 'logged in', '2011-01-10 10:15 AM EST'),
('lhsu', 'logged out', '2011-01-11 10:20 AM EST');
([DPSOH8VLQJDPXOWLURZFRQVWUXFWRUWRLQVHUWGDWD
3RVWJUH64/VXSSRUWVWKHPXOWLURZFRQVWUXFWRUWRLQVHUWPRUHWKDQRQHUHFRUGDW
DWLPH([DPSOHGHPRQVWUDWHVKRZWRXVHDPXOWLURZFRQVWUXFWLRQWRLQVHUW
GDWDLQWRWKHWDEOHZHFUHDWHGLQ([DPSOH
0XOWLURZ,QVHUW
FRORQVDVLQ'2011-1-1'::date7KLVV\QWD[LVVKRUWHUDQGHDVLHUWRDSSO\IRU
FDVHVLQZKLFK\RXFDQߞWGLUHFWO\FDVWIURPRQHW\SHWRDQRWKHUDQGKDYHWR
LQWHUFHGHZLWKRQHRUPRUHLQWHUPHGLDU\W\SHVVXFKDV
someXML::text::integer
$16,64/GHILQHVDFRQVWUXFWFDOOHGCASTWKDWDOORZV\RXWRPRUSKRQHGDWD
W\SHWRDQRWKHU)RUH[DPSOHCAST('2011-1-11' AS date)FDVWVWKHWH[W
2011-1-1WRDGDWH3RVWJUH64/KDVVKRUWKDQGIRUGRLQJWKLVXVLQJDSDLURI
6KRUWKDQG&DVWLQJ
SELECT DISTINCT ON (left(tract_id, 5))
left(tract_id, 5) As county, tract_id, tract_name
FROM census.lu_tracts
ORDER BY county, tract_id LIMIT 3 OFFSET 2;
county | tract_id
| tract_name
-------+-------------+------------------------------------------------25005 | 25005600100 | Census Tract 6001, Bristol County, Massachusetts
25007 | 25007200100 | Census Tract 2001, Dukes County, Massachusetts
25009 | 25009201100 | Census Tract 2011, Essex County, Massachusetts
(3 rows)
([DPSOH)LUVWWUDFWIRUFRXQWLHVWKURXJK
/LPLWVDQGRIIVHWVDUHQRWXQLTXHWR3RVWJUH64/DQGDUHLQIDFWFRSLHGIURP
0\64/DOWKRXJKLPSOHPHQWDWLRQGLIIHUVZLGHO\DPRQJGDWDEDVHSURGXFWV
LIMITUHWXUQVRQO\WKHQXPEHURIURZVLQGLFDWHGOFFSETLQGLFDWHVWKHQXPEHU
RIURZVWRVNLS<RXFDQXVHWKHPLQWDQGHPRUVHSDUDWHO\<RXDOPRVWDOZD\V
XVHWKHPLQFRQMXQFWLRQZLWKDQORDER BY,Q([DPSOHZHGHPRQVWUDWH
XVHRIDSRVLWLYHRIIVHW/HDYLQJRXWWKHRIIVHW\LHOGVWKHVDPHUHVXOWDVVHWWLQJ
WKHRIIVHWWR]HUR
/,0,7DQG2))6(7
7KHONPRGLILHUDFFHSWVPXOWLSOHFROXPQVFRQVLGHULQJDOORIWKHPWRGHWHUPLQH
GLVWLQFWQHVV7KHORDER BYFODXVHKDVWRVWDUWZLWKWKHVHWRIFROXPQVLQWKH
DISTINCT ONWKHQ\RXFDQIROORZZLWK\RXUSUHIHUUHGRUGHULQJ
county | tract_id | tract_name
-------+-------------+--------------------------------------------------25001 | 25001010100 | Census Tract 101, Barnstable County, Massachusetts
25003 | 25003900100 | Census Tract 9001, Berkshire County, Massachusetts
25005 | 25005600100 | Census Tract 6001, Bristol County, Massachusetts
25007 | 25007200100 | Census Tract 2001, Dukes County, Massachusetts
25009 | 25009201100 | Census Tract 2011, Essex County, Massachusetts
:
(14 rows)
2IWHQ\RXߞOOILQG\RXUVHOIQHHGLQJWRHQVXUHXQLTXHQHVVIRURQO\DVXEVHWRI\RXU
URZV3RVWJUH64/GRHVQRWRIIHUFRQGLWLRQDOXQLTXHFRQVWUDLQWVEXW\RXFDQ
DFKLHYHWKHVDPHHIIHFWE\XVLQJDSDUWLDOXQLTXHQHVVLQGH[6HHߡ3DUWLDO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NH\ZRUGWR\RXU
5()5(6+FRPPDQGSURYLGHGWKDW\RXKDYHHVWDEOLVKHGDXQLTXHLQGH[RQ
\RXUYLHZ7KHWUDGHRIILVFRQFXUUHQWUHIUHVKHVFRXOGWDNHORQJHUWR
FRPSOHWH
LQGH[HV
&KHFN&RQVWUDLQWV
<RXFDQߞWXVHCREATE OR REPLACEWRHGLWDQH[LVWLQJPDWHULDOL]HGYLHZ<RX
PXVWGURSDQGUHFUHDWHWKHYLHZHYHQIRUWKHPRVWWULYLDORIFKDQJHV8VH
DROP MATERIALIZED VIEW name_of_view$QQR\LQJO\\RXߞOOORVHDOO\RXU
&XUUHQWOLPLWDWLRQVRIPDWHULDOL]HGYLHZVLQFOXGH
REFRESH MATERIALIZED VIEW CONCURRENTLY census.vw_facts_2011_materialized;
,Q3RVWJUH64/WRDOORZWKHYLHZWREHTXHULHGZKLOHLWߞVUHIUHVKLQJ\RXFDQ
XVH
7KHYLHZFDQQRWEHTXHULHGZKLOHWKH5()5(6+0$7(5,$/,=('9,(:VWHS
LVUXQQLQJ
REFRESH MATERIALIZED VIEW census.vw_facts_2011_materialized;
7RUHIUHVKWKHYLHZLQ3RVWJUH64/XVH
7KHDGYDQWDJHRIXVLQJORDER BYLQWKHPDWHULDOL]HGYLHZRYHUXVLQJWKH
&/867(5DSSURDFKLVWKDWWKHVRUWLVPDLQWDLQHGZLWKHDFKREFRESH
MATERIALIZED VIEWFDOODOOHYLDWLQJWKHQHHGWRUHFOXVWHU7KHGRZQVLGHLVWKDW
ORDER BYJHQHUDOO\DGGVPRUHSURFHVVLQJWLPHWRWKHREFRESHVWHSRIWKHYLHZ
<RXVKRXOGWHVWWKHHIIHFWRIORDER BYRQSHUIRUPDQFHRIREFRESHEHIRUHXVLQJ
LW2QHZD\WRWHVWLVMXVWWRUXQWKHXQGHUO\LQJTXHU\RIWKHYLHZZLWKDQORDER
BYFODXVH
1DPHWKHLQGH[WRFOXVWHURQ1HHGHGRQO\GXULQJYLHZFUHDWLRQ
(DFKWLPH\RXUHIUHVK\RXPXVWUHFOXVWHUWKHGDWD
CLUSTER census.vw_facts_2011_materialized USING ix;
CLUSTER census.vw_facts_2011_materialized;
([DPSOH&OXVWHULQJDQGUHFOXVWHULQJDYLHZRQDQLQGH[
)RUVSHHGLHUDFFHVVWRDPDWHULDOL]HGYLHZZLWKDODUJHQXPEHURIUHFRUGV\RX
PD\ZDQWWRFRQWUROWKHSK\VLFDOVRUWRIWKHGDWD7KHHDVLHVWZD\LVWRLQFOXGH
DQORDER BYZKHQ\RXFUHDWHWKHYLHZ$OWHUQDWLYHO\\RXFDQDGGDFOXVWHU
LQGH[WRWKHYLHZ)LUVWFUHDWHDQLQGH[LQWKHSK\VLFDOVRUWRUGHU\RXZDQWWR
KDYH7KHQUXQWKHCLUSTERFRPPDQGSDVVLQJLWWKHLQGH[DVVKRZQLQ
([DPSOH
3RVWJUH64/DOVRDOORZV\RXWRPL[DQGPDWFKGLIIHUHQWLQGH[W\SHVLQWKHVDPH
3RVWJUH64/FRPHVZLWKDODYLVKIUDPHZRUNIRUFUHDWLQJDQGILQHWXQLQJLQGH[HV
7KHDUWRI3RVWJUH64/LQGH[LQJFRXOGILOODWRPHDOOE\LWVHOI3RVWJUH64/LV
SDFNDJHGZLWKVHYHUDOW\SHVRILQGH[HV,I\RXILQGWKHVHLQDGHTXDWH\RXFDQ
GHILQHQHZLQGH[RSHUDWRUVDQGPRGLILHUVWRVXSSOHPHQW,IVWLOOXQVDWLVILHG
\RXߞUHIUHHWRLQYHQW\RXURZQLQGH[W\SH
,QGH[HV
7KHLQWDUUD\H[WHQVLRQSURYLGHV*L67LQGH[VXSSRUWIRULQWHJHUDUUD\V LQW
LQW $IWHULQWDUUD\LVLQVWDOOHG\RXFDQWKHQXVH*L67ZLWKDUUD\VDQGFUHDWH
H[FOXVLRQFRQVWUDLQWVRQLQWHJHUDUUD\V
CREATE EXTENSION IF NOT EXISTS intarray;
ALTER TABLE room_blocks
ADD CONSTRAINT ex_room_blocks_rooms
EXCLUDE USING gist(rooms WITH &&);
([DPSOH3UHYHQWRYHUODSSLQJDUUD\EORFNV
7RHQVXUHWKDWQRWZREORFNVKDYHDURRPLQFRPPRQ\RXFDQVHWXSDQ
H[FOXVLRQFRQVWUDLQWSUHYHQWLQJEORFNVIURPRYHUODSSLQJ WZREORFNVKDYLQJWKH
VDPHURRP ([FOXVLRQFRQVWUDLQWVXQIRUWXQDWHO\ZRUNRQO\ZLWK*L67LQGH[HV
DQGEHFDXVH*,67LQGH[HVGRQߞWH[LVWIRUDUUD\VRXWRIWKHER[\RXQHHGWR
LQVWDOODQDGGLWLRQDOH[WHQVLRQEHIRUH\RXFDQGRWKLVDVVKRZQLQ([DPSOH
CREATE TABLE room_blocks(block_id integer primary key, rooms int[]);
$UUD\VDUHDQRWKHUSRSXODUW\SHZKHUH(;&/86,21FRQVWUDLQWVFRPHLQKDQG\
/HWߞVVXSSRVH\RXKDYHDVHWRIURRPVWKDW\RXQHHGWRDVVLJQWRDJURXSRI
SHRSOH:HߞOOFDOOWKHVHURRPߡEORFNVߢ)RUH[SHGLHQF\\RXGHFLGHWRVWRUHRQH
UHFRUGSHUSDUW\EXW\RXZDQWWRHQVXUHWKDWWZRSDUWLHVDUHQHYHUJLYHQWKH
VDPHURRP6R\RXVHWXSDWDEOHDVIROORZV
-XVWDVZLWKXQLTXHQHVVFRQVWUDLQWV3RVWJUH64/DXWRPDWLFDOO\FUHDWHVD
FRUUHVSRQGLQJLQGH[RIWKHW\SHVSHFLILHGLQWKHFRQVWUDLQWGHFODUDWLRQ
CREATE TABLE schedules(id serial primary key, room int, time_slot tstzrange);
ALTER TABLE schedules ADD CONSTRAINT ex_schedules
EXCLUDE USING gist (room WITH =, time_slot WITH &&);
*HQHUDOL]HG6HDUFK7UHH *L67 LVDQLQGH[RSWLPL]HGIRU)76VSDWLDOGDWD
*L67
%ORFNUDQJHLQGH[ %5,1 LVDQLQGH[W\SHLQWURGXFHGLQ3RVWJUH64/
,WߞVGHVLJ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PQXVHVD*L67
LQGH[ZLWKERWKLQGH[HVFRQWULEXWLQJWRVSHHGXSWKHTXHULHV7RGHOYHPRUHLQWR
WKHPHFKDQLFVRIKRZWKHSODQQHUWDNHVDGYDQWDJHRILQGH[HVYLVLW%LWPDS,QGH[
6FDQ6WUDWHJ\
CREATE UNIQUE INDEX ix
ON census.vw_facts_2011_materialized (tract_id, fact_type_id, yr);
([DPSOH$GGLQGH[WRPDWHULDOL]HGYLHZ
&UHDWHDQLQGH[RQDPDWHULDOL]HGYLHZDV\RXZRXOGGRRQDUHJXODUWDEOHDV
VKRZQLQ([DPSOH
CREATE MATERIALIZED VIEW census.vw_facts_2011_materialized AS
SELECT fact_type_id, val, yr, tract_id FROM census.facts WHERE yr = 2011;
([DPSOH0DWHULDOL]HGYLHZ
([DPSOHGHPRQVWUDWHVKRZWRPDNHDPDWHULDOL]HGYHUVLRQRIWKHYLHZLQ
([DPSOH
8QOLNHQRQPDWHULDOL]HGYLHZV\RXFDQDGGLQGH[HVWRPDWHULDOL]HGYLHZVWR
VSHHGXSWKHUHDG
7KHPRVWFRQYLQFLQJFDVHVIRUXVLQJPDWHULDOL]HGYLHZVDUHZKHQWKHXQGHUO\LQJ
TXHU\WDNHVDORQJWLPHDQGZKHQKDYLQJWLPHO\GDWDLVQRWFULWLFDO<RXRIWHQ
HQFRXQWHUWKHVHVFHQDULRVZKHQEXLOGLQJRQOLQHDQDO\WLFDOSURFHVVLQJ 2/$3
DSSOLFDWLRQV
0DWHULDOL]HGYLHZVFDFKHWKHIHWFKHGGDWD7KLVKDSSHQVZKHQ\RXILUVWFUHDWH
WKHYLHZDVZHOODVZKHQ\RXUXQWKHREFRESH MATERIALIZED VIEWFRPPDQG
7RXVHPDWHULDOL]HGYLHZV\RXQHHGDWOHDVWYHUVLRQ
0DWHULDOL]HG9LHZV
<RXFDQVWLOOXVHUXOHVWRXSGDWHYLHZGDWDEXWINSTEAD OFWULJJHUVDUH
SUHIHUUHGQRZ,QWHUQDOO\3RVWJUH64/VWLOOXVHVUXOHVWRGHILQHWKHYLHZ DYLHZ
LVQRWKLQJEXWDQINSTEAD OF SELECTUXOHRQDYLUWXDOWDEOH DQGWRLPSOHPHQW
VLQJOHWDEOHXSGDWDEOHYLHZV7KHGLIIHUHQFHEHWZHHQXVLQJDWULJJHUDQGDUXOHLV
WKDWDUXOHUHZULWHVWKHXQGHUO\LQJTXHU\DQGDWULJJHUJHWVFDOOHGIRUHDFKYLUWXDO
URZ$VVXFKUXOHVEHFRPHRYHUZKHOPLQJO\GLIILFXOWWRZULWH DQGXQGHUVWDQG
ZKHQPDQ\WDEOHVDUHLQYROYHG5XOHVDUHDOVROLPLWHGEHFDXVHWKH\FDQEH
ZULWWHQRQO\LQ64/QRWLQRWKHUSURFHGXUDOODQJXDJHV
3RVWJUH64/KDVDQRWKHUDSSURDFKIRUXSGDWLQJYLHZVFDOOHGUXOHVZKLFK
SUHGDWHVWKHLQWURGXFWLRQRIINSTEAD OFWULJJHUVYLHZVXSSRUW<RXFDQVHHDQ
H[DPSOHXVLQJUXOHVLQ'DWDEDVH$EVWUDFWLRQZLWK8SGDWDEOH9LHZV
$OWKRXJKWKLVH[DPSOHFUHDWHGDVLQJOHWULJJHUIXQFWLRQWRKDQGOHPXOWLSOH
HYHQWVZHFRXOGKDYHMXVWDVHDVLO\FUHDWHGDVHSDUDWHWULJJHUDQGWULJJHU
IXQFWLRQIRUHDFKHYHQW
Query returned successfully: 0 rows affected, 931 ms execution time.
:LWKDPHVVDJH
UPDATE census.vw_facts SET short_name = 'test';
,IZHWU\WRXSGDWHDILHOGQRWLQRXUXSGDWHURZFRPSDULVRQWKHXSGDWHZLOOQRW
WDNHSODFH
Query returned successfully: 56 rows affected, 40 ms execution time.
8SRQDVXFFHVVIXOXSGDWH3RVWJUH64/UHWXUQVWKHIROORZLQJPHVVDJH
UPDATE census.vw_facts SET yr = 2012
WHERE yr = 2011 AND tract_id = '25027761200';
1RZZKHQZHXSGDWHGHOHWHRULQVHUWLQWR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DLQLQJERWKVLPSOHDQGFRPSOH[W\SHV)RUH[DPSOH
\RXFDQKDYHDFRPSRXQGLQGH[WKDWFRQVLVWVRIDFROXPQRISODLQWH[WDQGD
FROXPQRIIXOOWH[W1RUPDOO\FRPSOH[W\SHVVXFKDVIXOOWH[WOWUHH
JHRPHWULFDQGVSDWLDOW\SHVFDQXVHRQO\*,1RU*L67LQGH[HVDQGWKXVFDQ
QHYHUEHFRPELQHGZLWKVLPSOHUW\SHVWKDWFDQRQO\XVH%7UHH7KHVH
FRPERPHWKRGVDOORZ\RXWRFRPELQHFROXPQVLQGH[HGZLWK*,67ZLWK
FROXPQVLQGH[HGZLWK%7UHHLQDVLQJOHLQGH[
,I\RXZDQWWRH[SORUHLQGH[HVEH\RQGZKDW3RVWJUH64/LQVWDOOVE\GHIDXOW
HLWKHURXWRIQHHGRUFXULRVLW\VWDUWZLWKWKHFRPSRVLWH%7UHH*L67RU%
7UHH*,1LQGH[HVERWKDYDLODEOHDVH[WHQVLRQVDQGLQFOXGHGZLWKPRVW
3RVWJUH64/GLVWULEXWLRQV
%7UHH*L67%7UHH*,1
KDVKLQGH[HVWKHUHIRUH\RXFRXOGQߞWXVHWKHPLQVWUHDPLQJUHSOLFDWLRQ
VHWXSV$OWKRXJKKDVKLQGH[HVZHUHUHOHJDWHGWROHJDF\VWDWXVIRUVRPHWLPH
WKH\JRWVRPHORYHLQ3RVWJUH64/,QWKDWYHUVLRQWKH\JDLQHG
WUDQVDFWLRQDOVDIHW\DQGVRPHSHUIRUPDQFHLPSURYHPHQWVWKDWPDGHWKHP
PRUHHIILFLHQWWKDQ%7UHHLQVRPHFDVHV
CREATE OR REPLACE FUNCTION census.trig_vw_facts_ins_upd_del() RETURNS trigger AS
$$
BEGIN
IF (TG_OP = 'DELETE') THEN
DELETE FROM census.facts AS f
WHERE
f.tract_id = OLD.tract_id AND f.yr = OLD.yr AND
f.fact_type_id = OLD.fact_type_id;
RETURN OLD;
END IF;
IF (TG_OP = 'INSERT') THEN
INSERT INTO census.facts(tract_id, yr, fact_type_id, val, perc)
SELECT NEW.tract_id, NEW.yr, NEW.fact_type_id, NEW.val, NEW.perc;
RETURN NEW;
END IF;
IF (TG_OP = 'UPDATE') THEN
IF
ROW(OLD.fact_type_id, OLD.tract_id, OLD.yr, OLD.val, OLD.perc) !=
ROW(NEW.fact_type_id, NEW.tract_id, NEW.yr, NEW.val, NEW.perc)
THEN
UPDATE census.facts AS f
SET
tract_id = NEW.tract_id,
yr = NEW.yr,
fact_type_id = NEW.fact_type_id,
val = NEW.val,
perc = NEW.perc
WHERE
f.tract_id = OLD.tract_id AND
f.yr = OLD.yr AND
f.fact_type_id = OLD.fact_type_id;
RETURN NEW;
ELSE
RETURN NULL;
END IF;
END IF;
END;
$$
([DPSOH7ULJJHUIXQFWLRQIRUYZBIDFWVWRLQVHUWXSGDWHGHOHWH
7RPDNHWKLVYLHZXSGDWDEOHZLWKDWULJJHU\RXFDQGHILQHRQHRUPRUHINSTEAD
OFWULJJHUV:HILUVWGHILQHWKHWULJJHUIXQFWLRQWRKDQGOHWKHWULIHFWDINSERT
UPDATEDELETE,QDGGLWLRQ3RVWJUH64/VXSSRUWVWULJJHUVRQWKHTRUNCATE
HYHQW<RXFDQXVHDQ\ODQJXDJHWRZULWHWKHIXQFWLRQH[FHSWSQLDQG\RXߞUH
IUHHWRQDPHLWZKDWHYHU\RXOLNH:HFKRVH3/SJ64/LQ([DPSOH
ON x.fact_type_id = y.fact_type_id;
CREATE OR REPLACE VIEW census.vw_facts AS
SELECT
y.fact_type_id, y.category, y.fact_subcats, y.short_name,
x.tract_id, x.yr, x.val, x.perc
FROM census.facts As x INNER JOIN census.lu_fact_types As y
([DPSOH&UHDWLQJYLHZYZBIDFWV
/HWߞVVWDUWE\FUHDWLQJDYLHZWKDWSXOOVURZVIURPWKHIDFWVWDEOHDQGDORRNXS
WDEOHDVVKRZQLQ([DPSOH
9LHZVFDQHQFDSVXODWHMRLQVDPRQJWDEOHV:KHQDYLHZGUDZVIURPPRUHWKDQ
RQHWDEOHXSGDWLQJWKHXQGHUO\LQJGDWDZLWKDVLPSOHFRPPDQGLVQRORQJHU
SRVVLEOH'UDZLQJGDWDIURPPRUHWKDQRQHWDEOHLQWURGXFHVLQKHUHQWDPELJXLW\
ZKHQ\RXߞUHWU\LQJWRXSGDWHWKHXQGHUO\LQJGDWDDQG3RVWJUH64/LVQRWDERXW
WRPDNHDQDUELWUDU\GHFLVLRQIRU\RX)RULQVWDQFHLI\RXKDYHDYLHZWKDWMRLQV
DWDEOHRIFRXQWULHVZLWKDWDEOHRISURYLQFHVDQGWKHQGHFLGHWRGHOHWHRQHRIWKH
URZV3RVWJUH64/ZRQߞWNQRZZKHWKHU\RXLQWHQGWRGHOHWHRQO\DFRXQWU\D
SURYLQFHRUDSDUWLFXODUFRXQWU\SURYLQFHSDLULQJ1RQHWKHOHVV\RXFDQVWLOO
PRGLI\WKHXQGHUO\LQJGDWDWKURXJKWKHYLHZXVLQJWULJJHUV
8VLQJ7ULJJHUVWR8SGDWH9LHZV
ERROR: New row violates WITH CHECK OPTION for view "vw_facts_2011"
DETAIL: Failing row contains (1, 25001010500, 2012, 2985.000, 100.00).
<RXߞOOJHWDQHUURU
UPDATE census.vw_facts_2011 SET yr = 2012 WHERE val > 2942;
1RZWU\WRUXQDQXSGDWHVXFKDV
CREATE OR REPLACE VIEW census.vw_facts_2011 AS
SELECT fact_type_id, val, yr, tract_id FROM census.facts
WHERE yr = 2011 WITH CHECK OPTION;
([DPSOH6LQJOHWDEOHYLHZ:,7+&+(&.237,21
DWDQ\DWWHPSWVWRDGGUHFRUGVRXWVLGHWKHYLHZDQGWRXSGDWHUHFRUGVWKDWZLOO
SXWWKHPRXWVLGHWKHYLHZ,QRXUH[DPSOHYLHZRXUJRDOLVWROLPLW
YZBIDFWVBWRDOORZLQVHUWVRQO\RIGDWDDQGGLVDOORZXSGDWHVRIWKH\U
WRVRPHWKLQJRWKHUWKDQ7RDGGWKLVUHVWULFWLRQZHUHYLVHRXUYLHZ
GHILQLWLRQDVVKRZQLQ([DPSOH
SELECT am.amname AS index_method, opc.opcname AS opclass_name,
opc.opcintype::regtype AS indexed_type, opc.opcdefault AS is_default
FROM pg_am am INNER JOIN pg_opclass opc ON opc.opcmethod = am.oid
WHERE am.amname = 'btree'
ORDER BY index_method, indexed_type, opclass_name;
index_method | opclass_name
| indexed_type | is_default
-------------+---------------------+--------------+-----------btree
| bool_ops
| boolean
| t
൲
btree
| text_ops
| text
| t
btree
| text_pattern_ops
| text
| f
btree
| varchar_ops
| text
| f
btree
| varchar_pattern_ops | text
| f
([DPSOH:KLFKGDWDW\SHVDQGRSHUDWRUFODVVHVGRHV%7UHHVXSSRUW"
3RVWJUH64/JURXSVRSHUDWRUVLQWRRSHUDWRUFODVVHV)RUH[DPSOHWKHint4_ops
RSHUDWRUFODVVLQFOXGHVWKHRSHUDWRUV= < > > <WREHDSSOLHGDJDLQVWWKHGDWD
W\SHRIint4 FRPPRQO\NQRZQDVDQLQWHJHU 7KHSJBRSFODVVV\VWHPWDEOH
SURYLGHVDFRPSOHWHOLVWLQJRIDYDLODEOHRSHUDWRUFODVVHVERWKIURP\RXURULJLQDO
LQVWDOODQGIURPH[WHQVLRQV$SDUWLFXODULQGH[ZLOOZRUNRQO\DJDLQVWDJLYHQVHW
RIRSFODVVHV7RVHHWKLVFRPSOHWHOLVW\RXFDQHLWKHURSHQXSSJ$GPLQDQGORRN
XQGHURSHUDWRUFODVVHVRUH[HFXWHWKHTXHU\LQ([DPSOHWRJHWD
FRPSUHKHQVLYHYLHZ
,QGH[DUFKLWHFWVLQWHQGIRUWKHLULQGH[HVWRZRUNRQO\DJDLQVWFHUWDLQGDWDW\SHV
DQGZLWKVSHFLILFFRPSDULVRQRSHUDWRUV$QH[SHUWLQLQGH[LQJUDQJHVFRXOG
REVHVVRYHUWKHRYHUODSRSHUDWRU && ZKHUHDVDQH[SHUWLQLQGH[LQJWH[W
VHDUFKHVPD\ILQGOLWWOHPHDQLQJLQD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GH[RQDFROXPQWRFRYHUPXOWLSOHRSFODVVHV\RX
PXVWFUHDWHVHSDUDWHLQGH[HV7RDGGWKHGHIDXOWLQGH[text_opsWRDWDEOHUXQ
<RXZLOOQRWLFHWKDWWKHOLVWFRQWDLQVERWKvarchar_opsDQGtext_opsEXWWKH\PDSRQO\WR
textcharacter varyingGRHVQߞWKDYH%7UHHRSHUDWRUVRILWVRZQEHFDXVHLWLVHVVHQWLDOO\
WH[WZLWKDOHQJWKFRQVWUDLQWvarchar_opsDQGvarchar_pattern_opsDUHMXVWDOLDVHVIRU
text_opsDQGtext_pattern_opsWRVDWLVI\WKHGHVLUHRIVRPHWRPDLQWDLQWKLVV\PPHWU\RI
RSFODVVHVVWDUWLQJZLWKWKHQDPHRIWKHW\SHWKH\VXSSRUW
127(
CREATE INDEX idx1 ON census.lu_tracts USING btree (tract_name text_pattern_ops);
)RULQVWDQFH%7UHHDJDLQVWtext_ops DNDvarchar_ops GRHVQߞWLQFOXGHWKH
~~RSHUDWRU WKHLIKERSHUDWRU VRQRQHRI\RXULIKEVHDUFKHVFDQXVHDQLQGH[
LQWKHtext_opsRSFODVV,I\RXSODQRQGRLQJPDQ\ZLOGFDUGVHDUFKHVRQ
varcharRUtextFROXPQV\RXߞGEHEHWWHURIIH[SOLFLWO\FKRRVLQJWKH
text_pattern_opsvarchar_pattern_opsRSFODVVIRU\RXULQGH[7RVSHFLI\
WKHRSFODVVMXVWDSSHQGWKHRSFODVVDIWHUWKHFROXPQQDPHDVLQ
,Q([DPSOHZHOLPLWRXUUHVXOWWR%7UHH1RWLFHWKDWRQHRSFODVVSHU
LQGH[HGGDWDW\SHLVPDUNHGDVWKHGHIDXOW:KHQ\RXFUHDWHDQLQGH[ZLWKRXW
VSHFLI\LQJWKHRSFODVV3RVWJUH64/FKRRVHVWKHGHIDXOWRSFODVVIRUWKHLQGH[
*HQHUDOO\WKLVLVJRRGHQRXJKEXWQRWDOZD\V
:
7KHXSGDWHRI([DPSOHGRHVQRWYLRODWHWKHWHEREFRQGLWLRQ%XWRQFH
H[HFXWHG\RXZRXOGKDYHHPSWLHG\RXUYLHZ)RUWKHVDNHRIVDQLW\\RXPD\
ILQGLWGHVLUDEOHWRSUHYHQWXSGDWHVRULQVHUWVWKDWOHDYHGDWDLQYLVLEOHWRIXUWKHU
TXHULHV9HUVLRQLQWURGXFHGWKHWITH CHECK OPTIONWRDFFRPSOLVKWKLV
,QFOXGHWKLVPRGLILHUZKHQFUHDWLQJWKHYLHZDQG3RVWJUH64/ZLOOIRUHYHUEDON
UPDATE census.vw_facts_2011 SET yr = 2012 WHERE yr = 2011;
([DPSOH9LHZXSGDWHWKDWUHVXOWVLQGDWDQRORQJHUYLVLEOHLQYLHZ
%HDZDUHWKDW\RXFDQLQVHUWGDWDWKDWSODFHVLWRXWVLGHRIWKHYLHZߞVWHERERU
XSGDWHGDWDVRLWLVQRORQJHUYLVLEOHIURPWKHYLHZDVVKRZQLQ([DPSOH
UPDATE census.vw_facts_2011 SET val = 1 WHERE yr = 2012;
$QGWKHIROORZLQJZLOOQRWXSGDWHDQ\UHFRUGVEHFDXVHWKHYLHZH[SOLFLWO\
LQFOXGHVRQO\UHFRUGVIRU
DELETE FROM census.vw_facts_2011 WHERE val = 0;
$VRIYHUVLRQ\RXFDQDOWHUWKHGDWDLQWKLVYLHZE\XVLQJINSERTUPDATE
RUDELETEFRPPDQGV8SGDWHVDQGGHOHWHVZLOODELGHE\DQ\WHEREFRQGLWLRQ
\RXKDYHDVSDUWRI\RXUYLHZ)RUH[DPSOHWKHIROORZLQJTXHU\ZLOOGHOHWHRQO\
UHFRUGVZKRVHvalueLV
CREATE OR REPLACE VIEW census.vw_facts_2011 AS
SELECT fact_type_id, val, yr, tract_id FROM census.facts WHERE yr = 2011;
([DPSOH6LQJOHWDEOHYLHZ
7KHVLPSOHVWYLHZGUDZVIURPDVLQJOHWDEOH$OZD\VLQFOXGHWKHSULPDU\NH\LI
\RXLQWHQGWRZULWHGDWDEDFNWRWKHWDEO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HGZKRKDYHVWRSSHGJHWWLQJQHZVSDSHUVEHFDXVHWKH\ߞUHQRWRQWKH
FDUULHUVߞOLVWDQ\ZD\<RXUWDEOHORRNVOLNHWKLV
3DUWLDOLQGH[HV VRPHWLPHVFDOOHGILOWHUHGLQGH[HV DUHLQGH[HVWKDWFRYHURQO\
URZVILWWLQJDSUHGHILQHGWHEREFRQGLWLRQ)RULQVWDQFHLI\RXKDYHDWDEOHRI
URZVEXW\RXFDUHDERXWDIL[HGVHWRI\RXߞUHEHWWHURII
FUHDWLQJSDUWLDOLQGH[HV7KHUHVXOWLQJLQGH[HVFDQEHIDVWHUEHFDXVHPRUHFDQILW
LQWR5$0SOXV\RXߞOOVDYHDELWRIGLVNVSDFHRQWKHLQGH[LWVHOI
3DUWLDO,QGH[HV
$OZD\VXVHWKHVDPHIXQFWLRQDOH[SUHVVLRQZKHQTXHU\LQJWRHQVXUHXVHRIWKHLQGH[
:$51,1*
SELECT fullname FROM featnames_short WHERE upper(fullname) LIKE 'S%';
7KLVQH[WH[DPSOHXVHVWKHVDPHIXQFWLRQWRXSSHUFDVHWKHIXOOQDPHFROXPQ
EHIRUHFRPSDULQJ6LQFHZHFUHDWHGWKHLQGH[ZLWKWKHVDPHupper(fullname)
H[SUHVVLRQWKHSODQQHUZLOOEHDEOHWRXVHWKHLQGH[IRUWKLVTXHU\
CREATE INDEX idx ON featnames_short
USING btree (upper(fullname) varchar_pattern_ops);
3RVWJUH64/OHWV\RXDGGLQGH[HVWRIXQFWLRQVRIFROXPQV)XQFWLRQDOLQGH[HV
SURYHWKHLUXVHIXOQHVVLQPL[HGFDVHWH[WXDOGDWD3RVWJUH64/LVDFDVHVHQVLWLYH
GDWDEDVH7RSHUIRUPDFDVHLQVHQVLWLYHVHDUFK\RXFRXOGFUHDWHDIXQFWLRQDO
LQGH[
)XQFWLRQDO,QGH[HV
SELECT * FROM vw_subscribers_current WHERE name = 'sandy';
7KHQDOZD\VTXHU\WKHYLHZLQVWHDGRIWKHWDE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EOH7KLVPHDQV\RXFDQߞWXVH
WLPHIXQFWLRQVOLNHCURRENT_DATERUGDWDIURPRWKHUWDEOHV RURWKHUURZVRIWKHLQGH[HG
WDEOH WRGHWHUPLQHZKHWKHUDUHFRUGVKRXOGEHLQGH[HG
:$51,1*
CREATE UNIQUE INDEX uq ON subscribers USING btree(lower(name)) WHERE is_active;
:HDGGDSDUWLDOLQGH[WRJXDUDQWHHXQLTXHQHVVRQO\IRUFXUUHQWVXEVFULEHUV
id serial PRIMARY KEY,
name varchar(50) NOT NULL, type varchar(50),
is_active boolean);
7KHSODQQHUFDQDOVRHPSOR\DVWUDWHJ\FDOOHGDQLQGH[RQO\VFDQZKLFKHQDEOHV
WKHSODQQHUWRXVHMXVWWKHLQGH[DQGQRWWKHWDEOHLIWKHLQGH[FRQWDLQVDOOWKH
FROXPQVQHHGHGWRVDWLVI\DTXHU\6RLI\RXFRPPRQO\ILOWHUE\WKHVDPHVHWRI
ILHOGVDQGRXWSXWWKRVHDFRPSRXQGLQGH[FDQLPSURYHVSHHGVLQFHLWFDQVNLS
WKHWDEOH.HHSLQPLQGWKDWWKHPRUHFROXPQV\RXKDYHLQDQLQGH[WKHIDWWHU
\RXULQGH[DQGWKHOHVVRILWWKDWFDQHDVLO\ILWLQ5$0'RQߞWJRRYHUERDUGZLWK
FRPSRXQGLQGH[HV
,I\RXKDYHDPXOWLFROXPQ%7UHHLQGH[RQtypeDQGupper(name)WKHUHLVQR
QHHGIRUDQLQGH[RQMXVWtypeEHFDXVHWKHSODQQHUFDQVWLOOXVHWKHFRPSRXQG
LQGH[IRUFDVHVLQZKLFK\RXMXVWQHHGWRILOWHUE\type$OWKRXJKWKHSODQQHU
FDQXVHWKHLQGH[HYHQLIWKHFROXPQV\RXDUHTXHU\LQJDUHQRWWKHILUVWLQWKH
LQGH[TXHU\LQJE\WKHILUVWFROXPQLQDQLQGH[LVPXFKPRUHHIILFLHQWWKDQ
TXHU\LQJE\MXVWVHFRQGDU\FROXPQV
7KH3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