the open source objectrelational database management...
TRANSCRIPT
![Page 1: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/1.jpg)
1
Introduction to PostgreSQLThe Open Source ObjectRelational Database Management
System
![Page 2: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/2.jpg)
2
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:Redistribution in source or binary must maintain copyright and following disclaimer
Neither the name of the organization nor the names of its contributors may be used to endorse or promote products.
PostgreSQL BSD License
![Page 3: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/3.jpg)
3
PostgreSQL Features
Installation and Configuration
Maintenance and Monitoring
Command Line Interface
Database Basics in PostgreSQL
Agenda
![Page 4: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/4.jpg)
4
Client Interfaces
Inheritance
Comparisons to other Databases
Replication, Point in Time Recovery
Full Text Search
Not the Agenda
![Page 5: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/5.jpg)
5
History of Postgres
UCBMiró/Illustra II
1986
2001
1992
1996
1995
2006
2000Informix IUS 9
IBM
PostgreSQL Global Dev
postgres95
IInformixI
![Page 6: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/6.jpg)
6
Relational Database Management System
ObjectRelational DatabaseAbility to add First Class simple and complex objects, with methods, that can be used in a Relational Context (SQL)
What is PostgreSQL?
![Page 7: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/7.jpg)
7
Foreign keys
Triggers
Views
Transactional Integrity
ACID compliance
Complex Queries
PostgreSQL Relational Features
![Page 8: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/8.jpg)
8
Data stands on its own
Data is money
Many applications one database
Database centric logic
Integrity cannot be circumvented by applications
Data Centricity
![Page 9: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/9.jpg)
9
Atomic
transactions seen in full or not at all
Consistent
system enforced contraints
Isolated
transactions do not interfere with each other transactions
Durable
On Commit, result will not be lost
ACID Compliance
![Page 10: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/10.jpg)
10
Snapshot of data for command or transaction
Virtually eliminates need for locking
Reading does not block writing and vice versaSET TRANSACTION ISOLATION LEVEL
READ COMMITTED
SERIALIZABLE
MultiVersion Concurrency Control
![Page 11: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/11.jpg)
11
Excellent Standards Compliance
SQL89, SQL92, SQL98, SQL2003
Documentation includes Compliance
Design Issues decided by Standards
SQL and PostgreSQL
![Page 12: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/12.jpg)
12
Data types
Functions
Operators
Rules
Aggregates
Index Methods
Object Relational Features
![Page 13: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/13.jpg)
13
select hotel_name, hotel_address from hotels h, airports awhere a.name = 'OAK' and
h.loc @ Circle(a.loc, '5 miles');
select name, num_kids from people;
select pdf( doc, '/home/me') from doc d where dnameget(doc) = 'myresume';
PostgreSQL Queries with Objects
![Page 14: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/14.jpg)
14
PgAdmin III www.pgadmin.org
phppgadmin phppgadmin.sourceforge.net
DbVisualizer www.minq.se/products/dbvis/
Others, e.g. pgaccessSee sourceforge.net
Client GUI Interfaces
![Page 15: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/15.jpg)
15
psql Command Line
libpq – C library
ECPG – Embedded SQL
pgtcl – Tcl binding library
Drivers
JDBC
ODBC
DBI: Perl, Python, PHP, etc.
.NET
Client Programming Interfaces
![Page 16: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/16.jpg)
16
PL/pgsql
SQL
C
Other server side languages
PL/perl, PL/pythonu,
PL/R, PL/Tcl, PL/Ruby,
PL/bash, PL/Java
etc.
Server Side Languages
![Page 17: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/17.jpg)
17
http://www.postgresql.org
By Source: ftp, bittorrent
By CVS tree
In Packages: RPM, Debian
Company Distributions
Downloading PostgreSQL
![Page 18: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/18.jpg)
18
Most Linux like OS distributions
MacOSX:
www.entropy.ch/software/macosx/ postgresql
8.1 Native Win32 Version
pginstaller at pgfoundry.org
Cygwin:
www.cygwin.com
Operating System Distributions
![Page 19: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/19.jpg)
19
Build Time
Build directives
Installation directory
PL Language options
Server Environment
postgresql.conf, pg_hba.conf
Runtime/Client Environment
PG environment variables
Configuration Points
![Page 20: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/20.jpg)
20
As user postgres ...
$ ./configure \prefix=/local/pgsql81 \withperl \withpython \withtcl \enabledepend
$ make
$ sudo make install
Configuration PointsBuild Time
![Page 21: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/21.jpg)
21
Initdb D $PGDATA
Creates Data Directory with:
configuration files
postgresql.confpg_hba.conf
template databases
template0template1super user database
![Page 22: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/22.jpg)
22
Global User Configuration
$PGDATA/postgresql.conf
Environment variables for server startup
Access Security
$PGDATA/pg_hba.conf
Host, user and database access.
Configuration PointsServer Environment
![Page 23: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/23.jpg)
23
Environment Variables for Server Startup
postgresql.conf
See also:
Configuration PointsGlobal User Configuration
![Page 24: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/24.jpg)
24
Configuration PointsGlobal User Configuration
V ariab le D efau lt @ 2G R A Mm ax_connec tions 100 100shared_bu ffers 1000 25000w ork_m em 1024 16384m ain tenance_w ork_m em 16384 16384m ax_ fsm _pages 20000 *m ax_ fsm _relations 1000 *effec tive_cache_size 1000 82500log_destination stderr stderrred irec t_stderr o ff on
![Page 25: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/25.jpg)
25
Configuration PointsGlobal User Configuration
V ariab le D efau lt @ 2G R A Mlog_d irec to ry pg_ loglog_m in_duration_statem en t 1 500log_ line_p refix [% p % t ] log_statem en t nonestats_start_co llec to r on onstats_com m and_string o ff onstats_b lock_ level o ff onstats_ row _ level o ff on
o ff on
/varl/log /pgsq l
dd l
au tovacuum
![Page 26: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/26.jpg)
26
Configuration Points Basic Security
# Host DB USER ADDRESS METHOD# "local" is for Unix domain socket connections onlylocal all all trust# IPv4 local:host all all 127.0.0.1/32 trust# IPv6 local:host all all ::1/128 trust# bad berniehost all bernie 163.555.9.9 reject# demohost demo varlena163.555.9.9 trust# usershost all all 163.555.9.9 md5
![Page 27: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/27.jpg)
27
Environment VariablesPGHOST – default localhost
PGPORT – default 5432
PGUSER – default $USER
PGDATABASE – default $PGUSER
Different for multiple installations
Configuration PointsRuntime/Client Environment
![Page 28: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/28.jpg)
28
View: pg_settings
Show values and descriptionsSELECT name, setting, short_desc
FROM pg_settings
ORDER BY name;
What can be set in a session?SELECT name
FROM pg_settings
where context='user';
Configuration PointsSession Setting
![Page 29: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/29.jpg)
29
Starting & Stopping PostgreSQL
Installation Specific Script (/etc/init.d)$ pg_ctl start D $PGDATA
$ pg_ctl stop
Windows PostgreSQL> Programs
Housekeeping PostgreSQL Start and Stop
![Page 30: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/30.jpg)
30
Log Maintenance
Rotate Log Settings in postgresql.conf
Alternative:$ pg_ctl start D $PGDATA |\
rotatelogs $PGDATA/pglog 86400 2>&1;
Always know where your log file is!
Housekeeping PostgreSQL Logging
![Page 31: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/31.jpg)
31
Autovacuum
Configure in postgresql.conf
Vacuum$ vacuumdb analyze full
Updates Statistics
Improves Performance
Recovers Disk Space
Frequency tuning required
Housekeeping PostgreSQL Vacuuming
![Page 32: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/32.jpg)
32
Backup pg_dumpall > \
.../`date +%Y%m%d`dump.sql
Restorepsql f 20061231dump.sql
Backup! Now! No excuses! Really!
Housekeeping PostgreSQL Backing Up
![Page 33: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/33.jpg)
33
Client Server Architecture
pg_stat_activity
Set pg_stats_command in postgresql.conf
ps alx
Log files
check pgfoundry for log parsers
Monitoring PostgreSQL
![Page 34: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/34.jpg)
34
Online & Downloadable Docs
Mailing Lists: www.postgresql.org
IRC #postgresql freenode.net
PostgreSQL General Bits :)
Documentation and Help
![Page 35: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/35.jpg)
35
Creating Databases
$ createdb accounts
![Page 36: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/36.jpg)
36
Adding Users $ createuser bob
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
CREATE ROLE
![Page 37: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/37.jpg)
37
Always learn help first.Command Line options
$ psql help
Backslash Command Help $ psql db db=# \?
SQL Help $ psql db db=# \help [SQL command]
psql Basics
![Page 38: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/38.jpg)
38
Data Types & Sequences
Nulls
Keys
Constraints & Defaults
Triggers, Functions & Operators
Tablespaces
Simple domains
Rules
Database Design Elements
![Page 39: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/39.jpg)
39
AS, LIKE
WITH OIDS
Current default WITH may changeSee default_with_oids
Temporary Tables
PRESERVE ROWS, DELETE ROWS, DROPINHERITS
CONSTRAINTS
TABLESPACE
Create Table
![Page 40: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/40.jpg)
40
CREATE TABLE people (id SERIAL PRIMARY KEY,name text,dept_no int REFERENCES dept(dept_no));
CREATE temp TABLE ships_temp asSELECT ship_id, cargo_no, voyage FROM ships;
Create Table
![Page 41: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/41.jpg)
41
Data TypesIntegers, big and smallSerialsArbitrary precision–numericFloating pointsSerial Types–Identity Character TypesBinary Data, big and smallDate/Time/Timestamp
BooleanGeometricNetwork AddressesBit TypesArraysOidsPseudo Types
![Page 42: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/42.jpg)
42
Data Type MappingIntegers................. 2, 4, 8 bytes
Serials.................... Identity, Autoincrement
Numeric.................. Money
Floats..................... Arithmetic
Text....................... Character Types
Date/Time/Interval... Dates & Times
Timestamp.............. Timestamps
Boolean................... Boolean
bytea...................... Byte stream, images
![Page 43: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/43.jpg)
43
Keys
Primary Keys
Implemented as BTree Unique indexes
Foreign Keys
Implement Referential Integrity.A FK in table A says that this value references a unique value in table B.Cascading updates, deletesNulls OK
![Page 44: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/44.jpg)
44
Defaults & Constraints
Initialize column with constants
Check value for validity
UNIQUE, [NOT] NULL, KEYSCREATE TABLE players (
nick_name text PRIMARY KEY,
team_name text REFERENCES teams(team_name),
age integer CHECK (age > 15) NOT NULL,
games_played integer DEFAULT 0
);
![Page 45: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/45.jpg)
45
Nulls
A NULL is a NULL is a NULL
NULLS are not equal to each other
NULLS are not equivalent to an empty string
NULLS are not equivalent to 0
NULLS are not indexed
![Page 46: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/46.jpg)
46
Creating a tablespaceCREATE TABLESPACE bd LOCATION '/bigdisk';
Using a tablespaceCREATE TABLE FOO (...) TABLESPACE bd;
Altering a tablespace
alter owner, alter name
Alter a table's tablespaceALTER TABLE SET TABLE SPACE TO bd;
TableSpaces
![Page 47: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/47.jpg)
47
Target List – list of columns to be returned
any expression,
aggregate,
subquery,
function,
columns from FROM clause data sources
SELECT
![Page 48: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/48.jpg)
48
FROM – data sources
Tables,
Views,
Set Returning Functions,
SubQueries,
JOINS,
UNIONS
SELECT
![Page 49: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/49.jpg)
49
WHERE – boolean expression qualifying data
Expressions,
Columns,
Functions,
SubQueries
SELECT
![Page 50: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/50.jpg)
50
GROUP BY – scope of Aggregate
Elements of Target List not involved in aggregation.
Determines Break columns
select tname, count(match_id)
from tmatches
group by tname;
SELECT
![Page 51: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/51.jpg)
51
HAVING – boolean expression qualifying aggregates
Expressions usually involving aggregates
select team1, count(matid)
from tmatches
group by team1
having count(matid) > 5;
SELECT
![Page 52: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/52.jpg)
52
COALESCEcoalesce( description, short_description, 'N/A')
CASE(select case when $1 is null then '#ffffff'else '#000000'end)
NULLIF (value1, value2)
NULL if values are equal else value1
Conditional Statements
![Page 53: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/53.jpg)
53
Expressions and Lists
EXISTSWHERE EXISTS (select id from bigtable)
INWHERE thisid IN (select id from bigtable)
ANY (SOME)name = ANY (select user from users)
ALLdue_date > ALL (select milestones from projects)
SubQuery Expressions
![Page 54: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/54.jpg)
54
UNIONS & JOINS
![Page 55: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/55.jpg)
55
JOINS: ON, USING, WHERESELECT ... FROM matches m JOIN events e
USING (matchid)
SELECT ... FROM matches m JOIN events e
ON (m.matchid = e.m_id)
SELECT ... FROM matches m, events e
WHERE m.matchid = e.matchid
![Page 56: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/56.jpg)
56
Target Table
(Column Names)
VALUES
(Column Values)
Expressions
INSERT INTO tmatches(matid, team1, team2, score1, score2) VALUES (DEFAULT, 'Berkeley', 'KC', 40,2);
INSERT
![Page 57: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/57.jpg)
57
Target Table
SubQuery
INSERT INTO events (ename, year, descr) SELECT lower(ename), 2006, description FROM events2006 WHERE lower(ename) not in (select ename from events);
INSERT
![Page 58: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/58.jpg)
58
Target Table
SET Column_Name = Value, Column_Name = Value
expression, value from Target Table, FROM listFROM
Other TablesWHERE
DON'T FORGET THE WHERE CLAUSE!
UPDATE
![Page 59: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/59.jpg)
59
UPDATE teamsSET descr = nt.longdescrFROM newteam_names ntWHERE teams.sname = nt.sname;
UPDATE
![Page 60: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/60.jpg)
60
Table Name
USING
Data Sources (i.e. table list)WHERE
DON'T FORGET THE WHERE CLAUSE!
DELETE FROM daily_log where log_ts < (current_date 1) + '12:00pm'::time
DELETE
![Page 61: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/61.jpg)
61
Views
Named Queries
Implemented Using Rules
Can do Updates, Inserts, Deletes via Rules
Usability
CREATE OR REPLACE VIEW phonelist ASSELECT t.team, p.player, p.name, p.phone
FROM teams t, p.players WHERE t.team = p.team;
![Page 62: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/62.jpg)
62
Blobs, Slobs and TOAST
Large Objects
special interface lo_
seek, read, write
TOAST
automatic and invisible promotion
INSERT, UPDATE, DELETE
no seek
![Page 63: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/63.jpg)
63
Simple Domains
Subtype Inherits Parent Type
Attributes and
Operators, Functions
May Over Ride
DEFAULT, CHECK
CONSTRAINT, [NOT] NULL
Operators, Functions
![Page 64: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/64.jpg)
64
Simple Domains
May Not Over Ride
Casts
LIKE
AS PRIMARY KEY use UNIQUE INDEX
CREATE DOMAIN degrees float CHECK(degrees > 180 and degrees <= 180);
![Page 65: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/65.jpg)
65
Logical & Comparison Operators
Math Functions, Aggregates & Operators
Type Conversions
Date, Time & Interval Arithmetic
String and pattern matching
Conditional Statements
Builtin Functions & Operators
![Page 66: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/66.jpg)
66
SELECT (('1/1/' || 2006)+ 7*( week 1 ), SUM(cookies), scout_nameFROM cookie_sales c JOIN scouts s USING (s.name), generate_series(1,53) g(week)WHERE date_part('week',c.sales_date) = weekGROUP BY week, scout_name;
Functions & Operators
![Page 67: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/67.jpg)
67
INTERVAL '2 days 3 hours'
TIMESTAMP '12/31/59'
'gotta wanna'::text
16::bigint
'(1.5,2.7)'::point
123.456::numeric(6,3)
Functions & Operators: Casts
![Page 68: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/68.jpg)
68
Output Format
to_char( , text)
timestamp, integer, double precision, numeric
to_char( idate, 'ddMonYYYY');
to_char( price, '999D99');
Input/Output Functions
![Page 69: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/69.jpg)
69
Input Format
to_date(text, text)
to_timestamp(text,text)
to_number(text, text)
to_date( '31 Dec 2006', 'DD Mon YYYY')
to_timestamp( '5/24/06', 'DD/MM/YY');
to_number( '543', '999D99')
Input/Output Functions
![Page 70: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/70.jpg)
70
Regular Arithmetic Expressionscurrent_date + INTERVAL '5 days'
start_date + duration
Regular Comparison Operatorsitem_date > due_date
start_date + INTERVAL '5 days' <= due_date
logtime <> last_log
Functions & Operators Interval Arithmetic
![Page 71: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/71.jpg)
71
extract( field FROM src)extract(epoch FROM
TIMESTAMP '20041231 01:43:03');extract(hours FROM
INTERVAL '2 days 5 hours');
age( timestamp )age('12/31/1959');
Functions & Operators Date, Time Arithmetic
![Page 72: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/72.jpg)
72
(start, end) OVERLAPS (start2, end2)
(proposed_start, proposed_end) OVERLAPS ('12/23/06'::date, '1/4/06'::date)
(sessiontime, INTERVAL '1 hour') OVERLAPS
(breaktime, INTERVAL '15 minutes')
Functions & Operators Interval Arithmetic
![Page 73: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/73.jpg)
73
LIKE, ILIKE or ~~, ~~*city LIKE 'San_%'
city ~~ 'San_%'
city ILIKE 'oak%'
city ~~* 'oak%'
SIMILAR TO or ~, ~* name SIMILAR TO
'(Mr.|Ms.) [AZ]([ az])*'
Functions & Operators String and Pattern matching
![Page 74: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/74.jpg)
74
create index uname_idx on users (user_name);
create index ttnotes_idx on trouble_tickets(ticket_id, note_id);
create index range_idx on cows USING RTREE (range);
Indexing Operators
![Page 75: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/75.jpg)
75
Functional indexes
Result of any immutable procedurecreate index tsdate_idx on log_table date(createtimestamp);create name_idx on
users lower(user_name);
Expressional indexes
Result of any immutable expressioncreate overdue_idx on books duedate + '30 days'
Functional Indexing
![Page 76: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/76.jpg)
76
Indexes over parts of tablescreate index active_clients on clients where status = 'A';
create index currentyear on accounts where reg_date = '2005';
Partial Indexing
![Page 77: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/77.jpg)
77
PL/pgsql and SQL Primary languages
Query & Trigger enabled
Trusted vs. untrusted languages
Available server side languages
PL/perl, PL/pythonu,
PL/R, PL/Tcl, PL/Ruby,
PL/bash
C, etc.
Server Side Languages
![Page 78: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/78.jpg)
78
CREATE FUNCTION foo(text, integer)
RETURNS integer AS
$$
...
$$
LANGUAGE 'plpgsql' [OPTIONS...]
Server Side Functions
![Page 79: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/79.jpg)
79
Executes once per row
Often Used for
complex or dynamic defaults
logging
PlPgSQL Trigger Functions
![Page 80: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/80.jpg)
80
Triggers
Function executed per Row
Before or After Event
Insert, Update or Delete CREATE OR REPLACE FUNCTION lastmod RETURNS TRIGGER AS $$
BEGIN NEW.last_modified = now(); RETURN NEW;
END; $$ LANGUAGE 'plpgsql';
CREATE TRIGGER team_upd BEFORE INSERT OR UPDATE on teams FOR EACH ROW EXECUTE PROCEDURE lastmod();
![Page 81: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/81.jpg)
81
RulesReWrite a Query
Action On a Table or View
Select Rules Implement Views
Updateable Views Implemented via Rules
![Page 82: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/82.jpg)
82
Rules View
Example View:CREATE VIEW matches_vSELECT m.matchname, m.matchid, t1.team AS team1, t2.team AS team2, t1.teamid as t1id, t2.teamid as t2id,
e.eventname, m.eventidFROM matches m JOIN teams t1 USING (t1.id=teamid)JOIN teams t2 USING (t2.id=teamid) JOIN event e ON (eventid);
![Page 83: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/83.jpg)
83
Rules Implement a View(Implicit)CREATE RULE “_RETURN” AS ON SELECT TO matches_v DO INSTEAD SELECT...;
![Page 84: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/84.jpg)
84
Rules Implement a ViewCREATE RULE upd_matches AS ON UPDATE TO matches_v DO INSTEAD UPDATE matches SET matchname=NEW.matchname, eventid=NEW.eventid,t1id=NEW.t1id, t2id=NEW.t2id
WHERE matchid=OLD.matchid;
![Page 85: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/85.jpg)
85
RulesCREATE RULE ins_matches AS ON INSERT TO matches_v DO INSTEAD INSERT INTO matches (matchid, eventid, t1id, t2id, matchname)
VALUES(default, NEW.eventid, NEW.t1id, NEW.t2id, NEW.matchname);
![Page 86: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/86.jpg)
86
RulesCREATE RULE del_matches AS ON DELETE TO matches_v DO INSTEAD DELETE FROM tmatches WHERE matchid=OLD.matchid
![Page 87: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/87.jpg)
87
Create first class operators
Implemented by functions
Use the same way as ordinary builtin operators.
Natural cost overhead.
Operators
![Page 88: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/88.jpg)
88
The usual suspects
DID YOU VACUUM?
Type mismatch
Indexing Expressions
GUC configurations
Explaining Explain
plpgsql[email protected]
Tuning Queries
![Page 89: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/89.jpg)
89
Explain [analyze] [verbose]OP (cost=n...n rows=n width=n)
(actual time=t..t rows=n loops=n) OP cond: (...)> OP (cost=...)(actual time=...)
OP cond: (...)
Look forSeq Scan, Hash Join, Subquery, Hash, Index ScanIndex usage
Explain
![Page 90: The Open Source ObjectRelational Database Management Systemoobdata.com/wp-content/uploads/2019/06/postsql-intro-brief.pdf · Relational Database Management System ObjectRelational](https://reader030.vdocument.in/reader030/viewer/2022040712/5e1698f23a06b03a3320b451/html5/thumbnails/90.jpg)
90
Replication Products
SLONY1
Mammoth Replicator Command Prompt, Inc.
pgpool (client side)
postgresr, dbmirror async, Rserv async, clustgres, pglcluster, osogres (client side replication)