0292 Introduction Postgresql
0292 Introduction Postgresql
Introduction to PostgreSQL
The Open Source Object-Relational Database Management
System
Agenda
PostgreSQL Features
Installation and Configuration
Maintenance and Monitoring
Command Line Interface
Database Basics in PostgreSQL
4
Client Interfaces
Inheritance
Comparisons to other Databases
Replication, Point in Time Recovery
Full Text Search
5
History of Postgres
1986 1992 1995
Miró/Illustra I postgres95
UCB
1996 PostgreSQL
IIInformix Global Dev
Informix 2000
IUS 9
2001
IBM
2006
6
What is PostgreSQL?
Relational Database Management System
Object-Relational Database
Ability to add First Class simple and complex objects,
with methods, that can be used in a Relational
Context (SQL)
7
Data Centricity
Data stands on its own
Data is money
Many applications one database
Database centric logic
Integrity cannot be circumvented by applications
9
ACID Compliance
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
10
Downloading PostgreSQL
http://www.postgresql.org
By Source: ftp, bittorrent
By CVS tree
In Packages: RPM, Debian
Company Distributions
18
Configuration Points
Build Time
Build directives
Installation directory
PL Language options
Server Environment
postgresql.conf, pg_hba.conf
Runtime/Client Environment
PG environment variables
20
Configuration Points
Build Time
As user postgres ...
$ ./configure \
--prefix=/local/pgsql81 \
--with-perl \
--with-python \
--with-tcl \
--enable-depend
$ make
$ sudo make install
21
Initdb -D $PGDATA
Creates Data Directory with:
configuration files
postgresql.conf
pg_hba.conf
template databases
template0
template1
super user database
22
Configuration Points
Server Environment
Global User Configuration
$PGDATA/postgresql.conf
Environment variables for server startup
Access Security
$PGDATA/pg_hba.conf
Host, user and database access.
23
Configuration Points
Global User Configuration
Environment Variables for Server Startup
postgresql.conf
See also:
www.varlena.com/GeneralBits/
Tidbits/#Performance
24
Configuration Points
Global User Configuration
V a ria b le D e fa u lt @ 2G RAM
m ax_ c o n n ec tio n s 100 100
sh ared _ b u ffers 1000 25000
w o rk _ m em 1024 16384
m ain ten an c e_ w o rk _ m em 16384 16384
m ax_ fsm _ p ag es 20000 *
m ax_ fsm _ relatio n s 1000 *
effec tive_ c ac h e_ size 1000 82500
lo g _ d estin atio n std err std err
red irect_ std err o ff on
25
Configuration Points
Global User Configuration
V a ria b le D e fa u lt @ 2G RAM
lo g _ d irecto ry p g _ lo g /varl/lo g /p g sq l
lo g _ m in _ d u ratio n _ statem -1 500
lo g _ lin e_ p refix [% p - % t ]
lo g _ statem en t none ddl
stats_ start_ co llecto r on on
stats_ co m m an d _ strin g o ff on
stats_ b lo ck_ level o ff on
stats_ ro w _ level o ff on
au to vacu u m o ff on
26
Backup! Now!
No excuses! Really!
33
Monitoring PostgreSQL
Client Server Architecture
pg_stat_activity
Set pg_stats_command in postgresql.conf
ps -alx
Log files
check pgfoundry for log parsers
34
Creating Databases
$ createdb accounts
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
37
psql Basics
Always learn help first.
Command Line options
$ psql --help
Backslash Command Help
$ psql db
db=# \?
SQL Help
$ psql db
db=# \help [SQL command]
38
Create Table
AS, LIKE
WITH OIDS
Current default WITH may change
See default_with_oids
Temporary Tables
PRESERVE ROWS, DELETE ROWS, DROP
INHERITS
CONSTRAINTS
TABLESPACE
40
Create Table
Data Types
Integers, big and small Boolean
Serials Geometric
Arbitrary precision–numeric Network Addresses
Floating points Bit Types
Serial Types–Identity Arrays
Character Types Oids
Binary Data, big and small Pseudo Types
Date/Time/Timestamp
42
Data Type Mapping
Integers................. 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
43
Keys
Primary Keys
Implemented as B-Tree 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, deletes
Nulls OK
44
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
46
TableSpaces
Creating a tablespace
CREATE TABLESPACE bd LOCATION '/bigdisk';
Using a tablespace
CREATE TABLE FOO (...) TABLESPACE bd;
Altering a tablespace
alter owner, alter name
Alter a table's tablespace
ALTER TABLE SET TABLE SPACE TO bd;
47
SELECT
Target List – list of columns to be returned
any expression,
aggregate,
subquery,
function,
columns from FROM clause data sources
48
SELECT
FROM – data sources
Tables,
Views,
Set Returning Functions,
SubQueries,
JOINS,
UNIONS
49
SELECT
WHERE – boolean expression qualifying data
Expressions,
Columns,
Functions,
SubQueries
50
SELECT
GROUP BY – scope of Aggregate
Elements of Target List not involved in aggregation.
Determines Break columns
Conditional Statements
COALESCE
coalesce( description,
short_description, 'N/A')
CASE
(select case when $1 is null then
'#ffffff'
else
'#000000'
end)
IN
WHERE thisid IN (select id from bigtable)
ANY (SOME)
name = ANY (select user from users)
ALL
due_date > ALL (select milestones from
projects)
54
SELECT ...
FROM matches m JOIN events e
ON (m.matchid = e.m_id)
SELECT ...
FROM matches m, events e
WHERE m.matchid = e.matchid
56
INSERT
Target Table
(Column Names)
VALUES
(Column Values)
Expressions
Simple Domains
Subtype Inherits Parent Type
Attributes and
Operators, Functions
May Over Ride
DEFAULT, CHECK
CONSTRAINT, [NOT] NULL
Operators, Functions
64
Simple Domains
May Not Over Ride
Casts
LIKE
AS PRIMARY KEY use UNIQUE INDEX
Input/Output Functions
Output Format
to_char( ----, text)
timestamp, integer, double precision, numeric
Input/Output Functions
Input Format
to_date(text, text)
to_timestamp(text,text)
to_number(text, text)
age( timestamp )
age('12/31/1959');
72
Functions & Operators
Interval Arithmetic
(start, end) OVERLAPS (start2, end2)
(proposed_start, proposed_end)
OVERLAPS
('12/23/06'::date, '1/4/06'::date)
SIMILAR TO or ~, ~*
name SIMILAR TO
'(Mr.|Ms.) [A-Z]([ a-z])*'
74
Indexing Operators
create index uname_idx
on users (user_name);
Functional Indexing
Functional indexes
Result of any immutable procedure
create index tsdate_idx on
log_table date(createtimestamp);
create name_idx on
users lower(user_name);
Expressional indexes
Result of any immutable expression
create overdue_idx
on books duedate + '30 days'
76
Partial Indexing
Indexes over parts of tables
create index active_clients on clients
where status = 'A';
create index currentyear on accounts
where reg_date = '2005';
77
Rules
Re-Write a Query
Action On a Table or View
Select Rules Implement Views
Updateable Views Implemented via Rules
82
Rules View
Example View:
CREATE VIEW matches_v
SELECT m.matchname, m.matchid,
t1.team AS team1, t2.team AS team2,
t1.teamid as t1id, t2.teamid as
t2id,
e.eventname, m.eventid
FROM matches m JOIN teams t1 USING
(t1.id=teamid)
JOIN teams t2 USING (t2.id=teamid)
JOIN event e ON (eventid);
83
Rules
CREATE 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);
86
Rules
CREATE RULE del_matches
AS ON DELETE TO matches_v
DO INSTEAD
DELETE FROM tmatches
WHERE matchid=OLD.matchid
87
Operators
Tuning Queries
The usual suspects
DID YOU VACUUM?
Type mismatch
Indexing Expressions
GUC configurations
Explaining Explain
[email protected]
89
Explain
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 for
Seq Scan, Hash Join,
Subquery, Hash,
Index Scan
Index usage
90
Replication Products
SLONY-1
Mammoth Replicator Command Prompt, Inc.
pgpool (client side)
postgres-r, dbmirror async, Rserv async, clustgres,
pglcluster, osogres (client side replication)
91
References
www.postgresql.org
www.varlena.com/GeneralBits
Mailing Lists
general, sql, novice, interfaces
hackers
advocacy
performance, bugs
docs
IRC #postgresql freenode.net