SlideShare a Scribd company logo
PostgreSQL
introduction
Introduction Installation The psql client Authentication and privileges Backup and restoration
I
Part 1
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 2/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Part 1
1 Introduction
2 Installation
3 The psql client
4 Authentication and privileges
5 Backup and restoration
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 3/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Introduction
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 4/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
What is PostgreSQL
What is PostgreSQL
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 5/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
What is PostgreSQL
What is PostgreSQL ?
Open Source
BSD-like licence
Many commercial derivatives (EnterpriseDB,. . . )
Uncompromised
Slow evolution but rock solid
No for-profit company behind
Goes much further than standards asks for.
Relational
ACID
Object-Relational
Stored procedures
Cross-platform
Linux
Windows
Most *NIX
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 6/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
What is PostgreSQL
History
Evolved from Ingres
Postgres = Ingres + SQL
Postgres released in 1995
1999 : support for real ACID and PL/pgSQL
2005 : optimized enough to become a real contender
2009 : PostgreSQL 8.4
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 7/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
What is PostgreSQL
How does PostgreSQL compare to MySQL ?
Respects the standards strictly.
Strict value checking. Example: MySQL 4.X considers
2012-01-00 a valid date , same for 2012-02-31, MySQL 5 fixes
the later example only.
Really ACID. Exemples
In PostgreSQL NOW() is the start of the transaction not
really the current timestamp.
Transactional DDL : In MySQL, an ALTER will silently COMMIT
any transaction.
In MySQL, foreign key cascades do not fire triggers,API’s
don’t fire triggers
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 8/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
What is PostgreSQL
Maximum sizes of objects
Object PostgreSQL MyISAM InnoDB
Database Unlimited 1 Unlimited2 Adressable space3
Table size 32TB Max file size 4 Max file size 4
Row size 400GB 64kB 5 64kB 5
Field size 1GB 8kB 6 8kB 6
Columns up to 16007 ?8 10009
Indexes unlimited 256 256
1
some 32TB databases exist
2
but databases over 200GB are quite rare, or quite slow
3
innodbPageSize * 224
with default innodbPageSize = 16kB, for all tables
unless innodb file per table=1
4
4TB on typical linux
5
not counting BLOBs
6
not counting BLOBs, VARCHAR and, VARBINARY
7
250 to 1600, depending on field types
8
constraint is row size
9
row size constraint still applies
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 9/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
What is PostgreSQL
How does PostgreSQL compare to MySQL ?
Deeply object-oriented (database creation is a
fork+inheritance, tables can inherit from one another...)
Storage of large objects (BLOB) is automagically ”put aside”
to not clobber tables.
PostgreSQL is renowned in the geographical field :
PostGIS
PostgreSQL can be used for datawarehousing :
OpenStreetMap has a 2.4TB database and it’s a small one.
PostgreSQL does not have a query cache : you need an
external tool for that
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 10/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Basic concepts
Basic concepts
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 11/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Basic concepts
Cluster
A database cluster is a single PostgreSQL server instance
If you want multiple clusters you will need :
Different config files
A different data directory
A different network port
Usually, servers only have one cluster
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 12/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Basic concepts
Database
A database is a set of objects that can be used together
Tables
Functions
Custom data types
Views
Triggers
...
Once connected to a database, you stay inside it, and cannot
use objects from other databases
A database cluster can have multiple databases
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 13/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Basic concepts
Schema
A schema is a namespace within a database
All databases have a default, public schema
It can be used to separate things
It can also help with access control
A database can have multiple schemas
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 14/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Basic concepts
Tables
Your regular DB table
myschema.mytable : explicit table indication
mytable : look for table in the search path (contains public)
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 15/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Basic concepts
Tablespaces
The physical location of files on disk. Your friendly sysadmin
can choose to put one tablespace on a faster medium (SSD)
to increase performance of this tablespace’s tables.
Can be defined per database or per user
Or explicitely on table creation
Any object in the whole cluster can use any tablespace if
permissions allow it
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 16/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Installation
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 17/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Installing PostgreSQL
Installing PostgreSQL
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 18/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Installing PostgreSQL
Grabbing the bits
Debian : aptitude install postgresql
RedHat :
yum install postgresql-server
chkconfig postgresql on
service postgresql initdb
service postgresql start
You might want to install postgresql-contrib, which
contains may community-developped tools : benchmark tools,
additional diagnostic tools, . . .
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 19/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Installing PostgreSQL
First steps : processes
Look at the running processes
postgres
-D /var/lib/postgresql/8.4/main
-c config file=/etc/.../postgresql.conf
Master database process
postgres: writer process
Writes data blocks back to disk
postgres: wal writer process
Writes transaction logs
postgres: autovacuum launcher process
Database housekeeping
postgres: stats collector process
Stats
postgres: postgres postgres [local] idle
Each connection to the DB spawns a process
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 20/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Installing PostgreSQL
First steps : config files
Each cluster has its own set of configuration files and data files
environment : environment variables for the server
pg ctl.conf : startup command-line options
pg hba.conf : authentication
pg ident.conf : ident maps for authentication
postgresql.conf : main config file
start.conf : whether this cluster is auto started or not
When changing the configuration, there is usually no need to
restart, a reload sould be enough
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 21/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Installing PostgreSQL
First steps : files
That -D parameter was the $datadir : by default, everything
is there
On RedHat, everything is there
On Debian, as usual, conf goes to /etc/postgresql and logs
go to /var/log/postgresql
Debian has a version/cluster convention that allows you
to run as many instances of as many versions of
PostgreSQL as you want easily
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 22/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Installing PostgreSQL
First steps : files
base : default tablespace
global : global tablespace
pg tblspc : other tablespaces (can contain symlinks)
pg log : application logs (admin-readable)
pg clog, pg multixact, pg stat tmp, pg subtrans,
pg twophase, pg xlog : various binary logs and technical
directories
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 23/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Basic administration
Basic administration
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 24/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Basic administration
Basic administration
su - postgres : required to do any administrative operation
on the database with the default configuration
createuser -P myuser : create a new user
You should answer ”no” to all its questions : access users
should not be able to create anything global
createdb -O myuser mydb : create a database that belongs
to the user, he will have full privileges on it
you can now psql -hlocalhost -Umyuser mydb
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 25/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
The psql client
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 26/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
psql basic usage
Uses unix-socket by default to connect to localhost, check
your ident or adapt pg hba.conf
Connect with psql DBNAME USERNAME -W for interactive
auth
Use psql -h 10.1.2.3 to connect to a remote postgres.
psql uses less as a result pager by default (usefull)
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 27/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
psql basic usage
Once connected,
l : list database (equivalent to MySQL’s SHOW DATABASES)
c : connect to database (equivalent to MySQL’s USE)
dt : list tables in current database (equivalent to MySQL’s
SHOW TABLES).
dt+ : shows more information like ownerships and comments
on each table.
di : show indexes
d table : lists columns of a table (equivalent to MySQL’s
DESC table)
x : Print one column per line
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 28/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Autocommit
By default, autocommit is set, this means any command you
enter in the psql client will be automatically commited as it it
were a mini-transaction
You can set AUTOCOMMIT off
With autocommit set to off, even if you do not explicitely
start a transaction with BEGIN, no permanent change will be
made until you COMMIT
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 29/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Passwords
psql will prompt automatically for a password if required
You cannot give the password on the command line !
.pgpass file in the homedirectory :
hostname:port:database:username:password
eg : *:*:mydb:myuser:mypw
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 30/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Authentication and privileges
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 31/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Authentification
Authentification
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 32/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Authentification
Authentification
PostgreSQL allows you to choose between many authentication
schemes :
md5 : regular user/password based on embedded account info
ident : get identity from local socket (only for local auth !)
krb5
ldap
cert : SSL client auth
gss/sspi/pam : delegate to external framework
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 33/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Authentification
Authentification
Authentication is configured in pg hba.conf:
# Database a d m i n i s t r a t i v e l o g i n by UNIX s o c k e t s
l o c a l a l l p o s t g r e s i d e n t
# ” l o c a l ” i s f o r Unix domain socket c o n n e c t i o n s only
l o c a l a l l a l l i d e n t
# IPv4 l o c a l c o n n e c t i o n s :
host a l l a l l 1 2 7 . 0 . 0 . 1 / 3 2 md5
# IPv6 l o c a l c o n n e c t i o n s :
host a l l a l l : : 1 / 1 2 8 md5
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 34/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Authentification
Authentification
Allowing external connections :
First, change the listen address in postgresql.conf
l i s t e n a d d r e s s e s = ’∗ ’
Then, add in pg hba.conf
# From j u s t one f r o n t s e r v e r
host a l l a l l 172.16.0.54/32 md5
# From a pool of f r o n t s e r v e r s
host a l l a l l 172.16.0.0/24 md5
# From everywhere ( don ’ t )
host a l l a l l 0 . 0 . 0 . 0 / 0 md5
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 35/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Roles : users and groups
Roles : users and groups
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 36/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Roles : users and groups
Roles : users and groups
Role management :
CREATE ROLE something; : not very useful
CREATE ROLE foobar LOGIN PASSWORD "toto"; : role can
login
CREATE USER foobar; : same thing
CREATE ROLE foobar CREATEDB; : this role can create
databases
CREATE ROLE foobar CREATEROLE; : this role can admin
roles
CREATE ROLE foobar SUPERUSER; : this role is not
submitted to permission checks
ALTER ROLE, DROP ROLE
GRANT group role TO user; : add a user to a group
SET ROLE group role; : gain the privileges of the group
Role display :
SELECT * FROM pg roles;
du
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 37/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Privileges
Privileges
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 38/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Privileges
Privileges
SELECT, INSERT, UPDATE, DELETE
TRUNCATE
REFERENCES, TRIGGER, TEMPORARY: create foreign
keys/triggers/temp tambes
CREATE : create tables, indexes, etc.
CONNECT : allow a user to connect to the database
USAGE, EXECUTE : allow a user to define and execute functions
ALL/ALL PRIVILEGES : allow everything
rights to DROP and ALTER are not grantable, and given to
object owners and superusers
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 39/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Privileges
Privileges
GRANT rights ON object TO role;
REVOKE rights ON object FROM role;
dp : display permissions
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 40/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Ownership
Ownership
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 41/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Ownership
Ownership
Owners have default privileges
Create any table in a db
read any column in a table
etc..
When using createdb to create a new database for your
application do not forget to set the owner
You can see the owner with dt
You can change the owner with ALTER
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 42/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Backup and restoration
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 43/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Backups
Backups
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 44/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Backups
dumps
pg dump
pg dump is the standard database dump program :
pg dump dbname > outfile (as superuser)
pg dump -t mytable dbname > outfile : just one table
(-b for blobs)
You can use -Fc to produce a optimized, more flexible dump
using an internal format (not SQL)
pg dumpall
pg dumpall dumps all databases
pg dumpall > outfile (as superuser)
Backups will be coherent, and will not lock tables (MVCC) except
if you do a heavy modification like ALTER
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 45/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Restoration
Restoration
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 46/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Restoration
Restoration
psql < mydump
pg restore -d mydb mydump if your dump is in
PostgreSQL ’s custom format
pg restore mydump converts the dump into SQL format for
later use
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 47/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Physical backup and PITR
Physical backup and PITR
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 48/110
Introduction Installation The psql client Authentication and privileges Backup and restoration
Physical backup and PITR
Physical backup and PITR
Instead of doing dumps, you can ”freeze” the database and do
a filesystem backup
Then, the WALs are archived
Using the filesystem backup + the WAL, you can restore to
any point in time
This requires a specific, complicated config
Stick to good old dumps if you don’t really need that
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 49/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
II
Part 2
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 50/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Part 2
6 Internal Architecture
7 Performance optimization
8 Stats and monitoring
9 Logs
10 Replication
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 51/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Internal Architecture
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 52/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Storage engine
Storage engine
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 53/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Storage engine
Filesystem
The tablespace is a folder that contains oddly-named files
Those files are data blocks (multiples of 8k)
They are mapped in memory in various caches
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 54/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Storage engine
Cache
PostgreSQL uses two levels of cache
Shared buffers contain a copy of data blocks
Since PostgreSQL knows what they are used for, it can
invalidate only some of them during certain operations
The OS cache (not PostgreSQL memory) also contains a
copy of the data blocks
PostgreSQL relies on it for performance considerations
(query planner)
Invalidation is controlled by the OS, and not smart from a DB
point of view
It is however, smart from a hardware point of view
Both level are designed to work together
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 55/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Storage engine
Write-ahead log
The WAL is an essential component
stored in pg xlog
Contains every modification of the database
Optimized for write performance
Pre-allocated
sequential writes
Used to guarantee integrity : on COMMIT (or autocommit),
the modification is written and fsync-ed to the log
If a crash occurs, the server re-synchronises the data from the
xlog
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 56/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Storage engine
Checkpoints and background writer
The background writer’s task is to commit dirty buffers to the
disk (or OS cache)
Checkpointing is the operation that writes all data back to the
hard drive (bypassing OS cache)
throttled to avoid IO peaks
It occurs automatically
When the transation logs are full
After a certain time
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 57/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Transactions
Transactions
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 58/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Transactions
MVCC : Multi Version Concurrency Control
MVCC is a method used in many databases, relational or
others
InnoDB, Oracle Database, Berkeley DB, DB2, Sybase (and
MSSQL)
CouchDB
Subversion
EHcache
and of course, PostgreSQL
rows have a xmin, and xmax value, containing a xid, or
transaction ID
Every transaction increments the current xid,
Is is an internal column, but you can display it with
SELCT txid current();
SELECT *,xmin,xmax from mytable;
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 59/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Transactions
MVCC and vaccuuming
UPDATE does not modify rows, it creates a new row with a
xmin set to the current transaction and sets xmax to the
current transaction on the old row
DELETE does not delete rows, it sets the row’s xmax to the
current transaction
The operation that removes unneeded rows is VACCUUM
regular VACCUUM simply removes unneeded rows by marking
them free in the Free Space Map (FSM)
VACUUM FULL actually disallocates unneeded rows from the
tablespace, it is a heavy operation, but can help of very active
tables (sessions)
regular VACCUUM is automatically performed by the
autovaccuum process
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 60/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Transactions
Transaction isolation level
PostgreSQL automatically manages row-level locking as
needed
READ COMMITED (default)
In the same transaction, you can see a different image of the
DB depending if another transaction has been validated in the
meantime
You will never, however, see a transaction that has not been
commited yet
But you can lock lines explicitely with SELECT FOR UPDATE
SERIALIZABLE
If a transaction detects that a value was modified since the
beginning of the transaction, it will fail
The other standard levels (READ UNCOMMITED, and
REPEATABLE READ) are mapped to the closest level of those
two
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 61/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Performance optimization
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 62/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Memory
Memory
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 63/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Memory
shared buffers
The main memory pool used by PostgreSQL
Usually : shared buffers = totalram/4
more isn’t better
SHM : need to tweak sysctl.conf
kernel.shmmax : maximum size (in bytes) of a memory
segment
kernel.shmall : maximum size (in pages) of all memory
segments
getconf PAGE SIZE
effective cache size : give it the actual cache size,
including OS cache (50 − 70% of memory) to help the query
planner. This will not allocate anything, it’s just helping the
query planner make the right decisions.
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 64/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Memory
work memory
work mem : memory for sorts, hash tables, etc.
The more work mem, the less you need to store temporary
tables to disk
Any single session can run multiple operations concurrently
(complex queries)
so don’t put too much there, or you’ll run out of memory on
high loads
maintenance work mem : memory available to maintenance
operation (INDEX, VACUUM, ALTER, . . . )
You can give more than work mem, not many maintenance
operations will occur simultaneously
a good value is totalram/20
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 65/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Disk
Disk
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 66/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Disk
Disk
Checkpoints are the main source of IO
checkpoint segments : number of 16Mb log files to keep
before ckecpointing
recommended value : 10
checkpoint timeout : maximum time between two
checkpoint
Default : 5min, but you can put more if you need to
checkpoint completion target : helps spread the IO load
default : 0.5
you can try : 0.9 or something in-between if you want to
spread the load more
wal buffers
a good value is 16Mb, which is the size of a WAL
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 67/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Network
Network
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 68/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Network
Network
max connections : maximum amount of connections
superuser reserved connections : maximum amount of
connections for the superuser
If you use PHP, you’ll need
max connections =
frontends
maxclients+superuser reserved connections
If you have connection pools :
max connections =
frontends
poolsize+superuser reserved connections
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 69/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Query planner
Query planner
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 70/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Query planner
Query planner
The query planner is used by PostgreSQL to find the most
efficient way to process a request
Can we use indexes
Which one is the best
In which order do we execute complex statements
Can we reformulate the statement in a more efficient one
(SELECT WHERE IN becomes an JOIN, etc.)
It uses a cost-based model
And predictions from ANALYZE (autoanalyze is a part of
autovaccuum)
Sometimes it is wrong (but not very often!)
Don’t try to configure it or you will shoot yourself in the foot
You cant, however, hint it not to use certain operations (no
full scans, no indexes, etc.) and see what happens
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 71/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Query planner
What it looks like
EXPLAIN will yield :
A tree of actions
Indication on
estimated cost for first row and for all rows
amount of rows expected to retrieve
estimated size of a row
EXPLAIN ANALYZE will actually execute the request, yield the
query planner info, and :
time it took to produce first row and all rows (but actual time
wihtout the profiler would be much faster)
actual amount of rows returned
how many times the node was executed
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 72/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Query planner
Sequential scans and index scans
# A query using a t a b l e scan ( bad )
root=> EXPLAIN SELECT ∗ FROM customer WHERE a c t i v e =0;
QUERY PLAN
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Seq Scan on customer ( c o s t =0.00..16.49 rows=15 width =70)
F i l t e r : ( a c t i v e = 0)
# A query using an index ( good )
root=> EXPLAIN SELECT ∗ FROM customer WHERE last name =’SMITH ’ ;
QUERY PLAN
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Index Scan using i d x l a s t n a m e on customer ( c o s t = 0 . 0 0 . . 8 . 2 7 rows=1 width =70)
Index Cond : (( last name ) : : t e x t = ’SMITH ’ : : t e x t )
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 73/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Query planner
Query optimization
root=> EXPLAIN SELECT DISTRICT FROM a dd r es s WHERE a d d r e s s i d IN
(SELECT a d d r e s s i d FROM customer WHERE a c t i v e =0);
QUERY PLAN
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Hash Semi Join ( c o s t =16.68..32.45 rows=15 width =9)
Hash Cond : ( a dd r es s . a d d r e s s i d = customer . a d d r e s s i d )
−> Seq Scan on a dd r es s ( c o s t =0.00..14.03 rows=603 width =13)
−> Hash ( c o s t =16.49..16.49 rows=15 width =2)
−> Seq Scan on customer ( c o s t =0.00..16.49 rows=15 width =2)
F i l t e r : ( a c t i v e = 0)
root=> EXPLAIN SELECT a dd r es s . d i s t r i c t FROM address , customer WHERE
a dd r es s . a d d r e s s i d=customer . a d d r e s s i d AND customer . a c t i v e =0;
QUERY PLAN
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
Hash Join ( c o s t =16.68..33.12 rows=15 width =9)
Hash Cond : ( a dd r es s . a d d r e s s i d = customer . a d d r e s s i d )
−> Seq Scan on a dd r es s ( c o s t =0.00..14.03 rows=603 width =13)
−> Hash ( c o s t =16.49..16.49 rows=15 width =2)
−> Seq Scan on customer ( c o s t =0.00..16.49 rows=15 width =2)
F i l t e r : ( a c t i v e = 0)
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 74/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Query planner
Complex queries
root=> EXPLAIN SELECT COUNT(∗) , ad d re ss . d i s t r i c t FROM address , customer
WHERE a d dr e ss . a d d r e s s i d=customer . a d d r e s s i d AND customer . a c t i v e=1
GROUP BY a dd r es s . d i s t r i c t ;
QUERY PLAN
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
HashAggregate ( c o s t =49.01..53.73 rows=378 width =9)
−> Hash Join ( c o s t =21.57..46.09 rows=584 width =9)
Hash Cond : ( customer . a d d r e s s i d = a d dr e ss . a d d r e s s i d )
−> Seq Scan on customer ( c o s t =0.00..16.49 rows=584 width =2)
F i l t e r : ( a c t i v e = 1)
−> Hash ( c o s t =14.03..14.03 rows=603 width =13)
−> Seq Scan on a dd r es s ( c o s t =0.00..14.03 rows=603 width =13)
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 75/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Query planner
Complex queries (for real)
root=> EXPLAIN ANALYZE SELECT COUNT(∗) , ad d re s s . d i s t r i c t FROM address , customer
WHERE a d dr e ss . a d d r e s s i d=customer . a d d r e s s i d AND customer . a c t i v e=1
GROUP BY a dd r es s . d i s t r i c t ;
QUERY PLAN
−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
HashAggregate ( c o s t =49.01..53.73 rows=378 width =9)
( a c t u a l time =4.939..5.384 rows=369 l o o p s =1)
−> Hash Join ( c o s t =21.57..46.09 rows=584 width =9)
( a c t u a l time =1.632..4.045 rows=584 l o o p s =1)
Hash Cond : ( customer . a d d r e s s i d = a d dr e ss . a d d r e s s i d )
−> Seq Scan on customer ( c o s t =0.00..16.49 rows=584 width =2)
( a c t u a l time =0.016..0.935 rows=584 l o o p s =1)
F i l t e r : ( a c t i v e = 1)
−> Hash ( c o s t =14.03..14.03 rows=603 width =13)
( a c t u a l time =1.602..1.602 rows=603 l o o p s =1)
−> Seq Scan on a dd r es s ( c o s t =0.00..14.03 rows=603 width =13)
( a c t u a l time =0.007..0.796 rows=603 l o o p s =1)
Total runtime : 5.883 ms
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 76/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Stats and monitoring
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 77/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Table stats
Table stats
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 78/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Table stats
Table statistics
SELECT * from pg stat user table
root=> d p g s t a t u s e r t a b l e s ;
View ” p g c a t a l o g . p g s t a t u s e r t a b l e s ”
Column | Type
−−−−−−−−−−−−−−−−−−+−−−−−−−−−−
r e l i d | oid | Table oid
schemaname | name | Schema name
relname | name | Table name
seq scan | b i g i n t | Number of s e q u e n t i a l scans
s e q t u p r e a d | b i g i n t | Number of rows r e t u r n e by seq scans
i d x s c a n | b i g i n t | Number of index scans
i d x t u p f e t c h | b i g i n t | Number of rows r e t u r n e d by i d x scans
n t u p i n s | b i g i n t | Number of i n s e r t e d rows
n tup upd | b i g i n t | Number of updated rows
n t u p d e l | b i g i n t | Number of d e l e t e d rows
n tup hot upd | b i g i n t | Number of updated rows ( with HOT)
n l i v e t u p | b i g i n t | Number of v a l i d rows
n dead tup | b i g i n t | Number of i n v a l i d (MVCC) rows
last vacuum | timestamp | Last time VACCUUM was s t a r t e d manually
last autovacuum | timestamp | Last time VACCUUM was s t a r t e d a u t o m a t i c a l l y
l a s t a n a l y z e | timestamp | Last time ANALYZE was s t a r t e d manually
l a s t a u t o a n a l y z e | timestamp | Last time ANALYZE was s t a r t e d a u t o m a t i c a l l y
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 79/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Table stats
IO stats
SELECT * from pg statio user tables
Show you the amount of cache reads and page reads
Show how efficient the buffer pool is
Does not show information about the OS cache
SELECT * from pg statio user indexes
Same with indexes
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 80/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Table stats
IO stats example
SELECT relname , h e a p b l k s r e a d , h e a p b l k s h i t , i d x b l k s r e a d , i d x b l k s h i t
FROM p g s t a t i o u s e r t a b l e s ;
relname | h e a p b l k s r e a d | h e a p b l k s h i t | i d x b l k s r e a d | i d x b l k s h i t
−−−−−−−−−−−+−−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−
payment | 52 | 297 | 54 | 28123
f i l m | 58 | 170 | 34 | 8646
i n v e n t o r y | 27 | 71 | 35 | 17552
SELECT relname ,
c a s t ( h e a p b l k s h i t as numeric )/( h e a p b l k s h i t+h e a p b l k s r e a d ) AS h i t r a t e
FROM p g s t a t i o u s e r t a b l e s
WHERE ( h e a p b l k s h i t+h e a p b l k s r e a d )>0 ;
relname | h i t r a t e
−−−−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−−
country | 0.50000000000000000000
payment p2007 04 | 0.85100286532951289398
f i l m | 0.74561403508771929825
payment p2007 02 | 0.83898305084745762712
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 81/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Table stats
Table sizes
SELECT schemaname , relname ,
p g s i z e p r e t t y ( p g r e l a t i o n s i z e ( r e l i d )) AS s i z e ,
p g s i z e p r e t t y ( p g t o t a l r e l a t i o n s i z e ( r e l i d )) AS t o t a l s i z e
FROM p g s t a t u s e r t a b l e s ;
or dt+
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 82/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Activity
Activity
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 83/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Activity
The pg stat activity view
root=> d p g s t a t a c t i v i t y ;
View ” p g c a t a l o g . p g s t a t a c t i v i t y ”
Column | Type
−−−−−−−−−−−−−−−+−−−−−−−−−−−−
d a t i d | oid | Database OID
datname | name | Database name
procpid | i n t e g e r | PID of s e r v e r p r o c e s s
u s e s y s i d | oid | User OID
usename | name | User name
c u r r e n t q u e r y | t e x t | Query
w a i t i n g | boolean | I s the query w a i t i n g
x a c t s t a r t | timestamp | Transaction s t a r t time
q u e r y s t a r t | timestamp | Query s t a r t time
b a c k e n d s t a r t | timestamp | Process s t a r t time
c l i e n t a d d r | i n e t | C l i e n t IP a dd r es s
c l i e n t p o r t | i n t e g e r | C l i e n t source port
A slightly better view :
SELECT datname , usename , c u r r e n t q u e r y , waiting , c l i e n t a d d r ,
now()− q u e r y s t a r t AS r u n n i n g f o r
FROM p g s t a t a c t i v i t y ORDER BY r u n n i n g f o r DESC;
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 84/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Locks
Locks
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 85/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Locks
Finding locks
Let’s see which queries are blocked
SELECT ∗ FROM p g s t a t a c t i v i t y WHERE w a i t i n g=t r u e ;
−[ RECORD 1 ]−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
d a t i d | 16392
datname | root
procpid | 1530
u s e s y s i d | 16390
usename | root
c u r r e n t q u e r y | SELECT i FROM t WHERE s=1 FOR UPDATE;
w a i t i n g | t
x a c t s t a r t | 2012−02−27 14:21:34.723872+01
q u e r y s t a r t | 2012−02−27 14:21:36.195808+01
b a c k e n d s t a r t | 2012−02−27 14:16:49.203923+01
c l i e n t a d d r |
c l i e n t p o r t | −1
PID 1530 is waiting for something
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 86/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Locks
Let’s find who is blocking it
SELECT ∗ FROM p g l o c k s WHERE pid =1530 AND granted=f a l s e ;
−[ RECORD 1 ]−−−−−−+−−−−−−−−−−−−−−
l o c k t y p e | t r a n s a c t i o n i d
database |
r e l a t i o n |
page |
t u p l e |
v i r t u a l x i d |
t r a n s a c t i o n i d | 688
c l a s s i d |
o b j i d |
o b j s u b i d |
v i r t u a l t r a n s a c t i o n | 2/39
pid | 1530
mode | ShareLock
granted | f
We are locked by transaction 688
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 87/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Locks
Let’s find who is transaction 688
SELECT ∗ FROM p g l o c k s WHERE t r a n s a c t i o n i d =688 AND granted=t r u e ;
−[ RECORD 1 ]−−−−−−+−−−−−−−−−−−−−−
l o c k t y p e | t r a n s a c t i o n i d
database |
r e l a t i o n |
page |
t u p l e |
v i r t u a l x i d |
t r a n s a c t i o n i d | 688
c l a s s i d |
o b j i d |
o b j s u b i d |
v i r t u a l t r a n s a c t i o n | 1/1099
pid | 1519
mode | E x c l u s i v e L o c k
granted | t
Now you can SELECT * FROM pg stat activity WHERE
procpid=1519; to find more about it
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 88/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
System monitoring
System monitoring
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 89/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
System monitoring
Munin
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 90/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Logs
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 91/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Error logs
Error logs
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 92/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Error logs
Error logs
You can find error logs in /var/log/postgresql
syntax errors, server errors, etc.
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 93/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Finding slow queries
Finding slow queries
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 94/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Finding slow queries
Finding slow queries
log min duration statement : define which queries should
be logged
−1 (default) : Do not log anything
0 : log all queries (aka: ruin your HDD)
500 : log queries that run for longer than 500ms
Set it globally in postgresql.conf then reload
Or set it just for one user :
alter role ’myuser’ set log min duration statement
= 500;
Will only start logging next time myuser opens a session
Then, disable it with :
alter role ’myuser’ set log min duration statement
= DEFAULT;
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 95/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Replication
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 96/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Basics
Basics
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 97/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Basics
Basics
PostgreSQL replication is asynchronous by default
synchronous replication is possible, but not usually
recommended
log shipping : copy transaction log files to the server and
replay them
Delay can be high
You can set a timeout to make sure you get a new log every X
minutes
You have to setup the copy (using rsync, NFS, etc.)
streaming : direct connection to the master server (like
MySQL)
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 98/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Basics
Basics
warm standby : the replica is not usable, but can be rapidly
enabled during failover
hot standby : the replica can be used for reads
beware of conflicts due to locking
must compromise between the ability to run long queries and
the freshness of data
On 8.4 : log shipping, warm standby
On 9.1 : log shipping, streaming, hot standby
Many 3rd party products : slony, etc.
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 99/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Log Shipping
Log Shipping
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 100/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Log Shipping
Log Shipping: master
Setup SSH keys, a NFS shared server, or any other mean to
share the logs
In postgresql.conf, enable archiving :
wal level = archive
archive mode = on
archive command = ’rsync -a %p host:/dir/%f’
archive timeout = 60 (if you want one)
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 101/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Log Shipping
Log Shipping: initializing the replication
Do a hot backup on the master :
SELECT pg start backup(’label’);
rsync -av --delete $datadir slave:$datadir
SELECT pg stop backup();
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 102/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Log Shipping
Log Shipping: slave
create a recovery.conf file in the datadir
restore command = ’/ usr / l i b / p o s t g r e s q l /8.4/ bin / pg standby −d −t
/tmp/ p g s q l s t o p s t a n d b y / var / l i b / p o s t g r e s q l /8.4/ main/ a r c h i v e l o g %f %p %r
2>>/var / log / p o s t g r e s q l / standby . log ’
recovery end command = ’rm −f /tmp/ p gsq l st op st and by ’
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 103/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Log Shipping
Log Shipping: failing over
remove the trigger file /tmp/pgsql stopstandby to finish
restoration
recovery.conf is renamed to recovery.done
You can now access the slave
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 104/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Streaming
Streaming
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 105/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Streaming
Streaming: master
You can combine streaming and log shipping
Enable WAL senders :
max wal senders = 2
You should make sure to have a few old log files if you don’t
use archiving :
wal keep segments = 10
Create a REPLICATION account :
CREATE ROLE myuser REPLICATION LOGIN PASSWORD
’mypass’
In pg hba.conf
host replication myuser slave/32 md5
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 106/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Streaming
Streaming: slave
standby mode = ’on’
primary conninfo = ’host=pgmaster port=5432
user=X password=Y’
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 107/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Streaming
Streaming: failing over
pg ctlcluster 9.1 main promote
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 108/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Hot standby
Hot standby
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 109/110
Internal Architecture Performance optimization Stats and monitoring Logs Replication
Hot standby
Hot standby
hot standby = on
wal level = hot standby
max standby archive delay = 30
max standby streaming delay = 30
It’s the time we allow queries running on the slave to block
WAL replay
www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 110/110
Ad

More Related Content

What's hot (20)

PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PGConf APAC
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
Reuven Lerner
 
PostgreSQL HA
PostgreSQL   HAPostgreSQL   HA
PostgreSQL HA
haroonm
 
Getting started with postgresql
Getting started with postgresqlGetting started with postgresql
Getting started with postgresql
botsplash.com
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
Command Prompt., Inc
 
Connection Pooling in PostgreSQL using pgbouncer
Connection Pooling in PostgreSQL using pgbouncer Connection Pooling in PostgreSQL using pgbouncer
Connection Pooling in PostgreSQL using pgbouncer
Sameer Kumar
 
PostgreSQL replication
PostgreSQL replicationPostgreSQL replication
PostgreSQL replication
NTT DATA OSS Professional Services
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
metsarin
 
OpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQLOpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQL
Open Gurukul
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuning
elliando dias
 
Get to know PostgreSQL!
Get to know PostgreSQL!Get to know PostgreSQL!
Get to know PostgreSQL!
Oddbjørn Steffensen
 
PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System Administrators
Command Prompt., Inc
 
Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.
Alexey Lesovsky
 
MySQL Shell for DBAs
MySQL Shell for DBAsMySQL Shell for DBAs
MySQL Shell for DBAs
Frederic Descamps
 
Microsoft SQL Server - Files and Filegroups
Microsoft SQL Server - Files and FilegroupsMicrosoft SQL Server - Files and Filegroups
Microsoft SQL Server - Files and Filegroups
Naji El Kotob
 
MySQL Database Architectures - InnoDB ReplicaSet & Cluster
MySQL Database Architectures - InnoDB ReplicaSet & ClusterMySQL Database Architectures - InnoDB ReplicaSet & Cluster
MySQL Database Architectures - InnoDB ReplicaSet & Cluster
Kenny Gryp
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
Smita Prasad
 
MySQL 상태 메시지 분석 및 활용
MySQL 상태 메시지 분석 및 활용MySQL 상태 메시지 분석 및 활용
MySQL 상태 메시지 분석 및 활용
I Goo Lee
 
MySQL Data Encryption at Rest
MySQL Data Encryption at RestMySQL Data Encryption at Rest
MySQL Data Encryption at Rest
Mydbops
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
Universidade de São Paulo
 
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PGConf APAC
 
PostgreSQL HA
PostgreSQL   HAPostgreSQL   HA
PostgreSQL HA
haroonm
 
Getting started with postgresql
Getting started with postgresqlGetting started with postgresql
Getting started with postgresql
botsplash.com
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
Command Prompt., Inc
 
Connection Pooling in PostgreSQL using pgbouncer
Connection Pooling in PostgreSQL using pgbouncer Connection Pooling in PostgreSQL using pgbouncer
Connection Pooling in PostgreSQL using pgbouncer
Sameer Kumar
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
metsarin
 
OpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQLOpenGurukul : Database : PostgreSQL
OpenGurukul : Database : PostgreSQL
Open Gurukul
 
PostgreSQL Performance Tuning
PostgreSQL Performance TuningPostgreSQL Performance Tuning
PostgreSQL Performance Tuning
elliando dias
 
PostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System Administrators
Command Prompt., Inc
 
Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.
Alexey Lesovsky
 
Microsoft SQL Server - Files and Filegroups
Microsoft SQL Server - Files and FilegroupsMicrosoft SQL Server - Files and Filegroups
Microsoft SQL Server - Files and Filegroups
Naji El Kotob
 
MySQL Database Architectures - InnoDB ReplicaSet & Cluster
MySQL Database Architectures - InnoDB ReplicaSet & ClusterMySQL Database Architectures - InnoDB ReplicaSet & Cluster
MySQL Database Architectures - InnoDB ReplicaSet & Cluster
Kenny Gryp
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
Smita Prasad
 
MySQL 상태 메시지 분석 및 활용
MySQL 상태 메시지 분석 및 활용MySQL 상태 메시지 분석 및 활용
MySQL 상태 메시지 분석 및 활용
I Goo Lee
 
MySQL Data Encryption at Rest
MySQL Data Encryption at RestMySQL Data Encryption at Rest
MySQL Data Encryption at Rest
Mydbops
 

Similar to PostgreSQL : Introduction (20)

Presentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - WebinarPresentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - Webinar
Orient Technologies
 
Azure SQL - more or/and less than SQL Server
Azure SQL - more or/and less than SQL ServerAzure SQL - more or/and less than SQL Server
Azure SQL - more or/and less than SQL Server
Rafał Hryniewski
 
Bare Metal to OpenStack with Razor and Chef
Bare Metal to OpenStack with Razor and ChefBare Metal to OpenStack with Razor and Chef
Bare Metal to OpenStack with Razor and Chef
Matt Ray
 
linux installation.pdf
linux installation.pdflinux installation.pdf
linux installation.pdf
MuhammadShoaibHussai2
 
Squid
SquidSquid
Squid
Syeda Javeria
 
Why Managed Service Providers Should Embrace Container Technology
Why Managed Service Providers Should Embrace Container TechnologyWhy Managed Service Providers Should Embrace Container Technology
Why Managed Service Providers Should Embrace Container Technology
Sagi Brody
 
Ceph Day Bring Ceph To Enterprise
Ceph Day Bring Ceph To EnterpriseCeph Day Bring Ceph To Enterprise
Ceph Day Bring Ceph To Enterprise
Alex Lau
 
[Devconf.cz][2017] Understanding OpenShift Security Context Constraints
[Devconf.cz][2017] Understanding OpenShift Security Context Constraints[Devconf.cz][2017] Understanding OpenShift Security Context Constraints
[Devconf.cz][2017] Understanding OpenShift Security Context Constraints
Alessandro Arrichiello
 
Ceph Day Taipei - Bring Ceph to Enterprise
Ceph Day Taipei - Bring Ceph to EnterpriseCeph Day Taipei - Bring Ceph to Enterprise
Ceph Day Taipei - Bring Ceph to Enterprise
Ceph Community
 
Ceph Day Tokyo - Bring Ceph to Enterprise
Ceph Day Tokyo - Bring Ceph to Enterprise Ceph Day Tokyo - Bring Ceph to Enterprise
Ceph Day Tokyo - Bring Ceph to Enterprise
Ceph Community
 
18587936 squid-proxy-configuration-guide - [the-xp.blogspot.com]
18587936 squid-proxy-configuration-guide - [the-xp.blogspot.com]18587936 squid-proxy-configuration-guide - [the-xp.blogspot.com]
18587936 squid-proxy-configuration-guide - [the-xp.blogspot.com]
Krisman Tarigan
 
LISA18: Hidden Linux Metrics with Prometheus eBPF Exporter
LISA18: Hidden Linux Metrics with Prometheus eBPF ExporterLISA18: Hidden Linux Metrics with Prometheus eBPF Exporter
LISA18: Hidden Linux Metrics with Prometheus eBPF Exporter
Ivan Babrou
 
Containers with systemd-nspawn
Containers with systemd-nspawnContainers with systemd-nspawn
Containers with systemd-nspawn
Gábor Nyers
 
CoreOS, or How I Learned to Stop Worrying and Love Systemd
CoreOS, or How I Learned to Stop Worrying and Love SystemdCoreOS, or How I Learned to Stop Worrying and Love Systemd
CoreOS, or How I Learned to Stop Worrying and Love Systemd
Richard Lister
 
Gab17 lyon SQL on Linux - David BARBARIN
Gab17 lyon SQL on Linux - David BARBARINGab17 lyon SQL on Linux - David BARBARIN
Gab17 lyon SQL on Linux - David BARBARIN
AZUG FR
 
Ch 22: Web Hosting and Internet Servers
Ch 22: Web Hosting and Internet ServersCh 22: Web Hosting and Internet Servers
Ch 22: Web Hosting and Internet Servers
webhostingguy
 
containerD
containerDcontainerD
containerD
strikr .
 
Introduction to PostgreSQL for System Administrators
Introduction to PostgreSQL for System AdministratorsIntroduction to PostgreSQL for System Administrators
Introduction to PostgreSQL for System Administrators
Jignesh Shah
 
PVS-Studio in the Clouds: Travis CI
PVS-Studio in the Clouds: Travis CIPVS-Studio in the Clouds: Travis CI
PVS-Studio in the Clouds: Travis CI
Andrey Karpov
 
How to Become Cloud Backup Provider
How to Become Cloud Backup ProviderHow to Become Cloud Backup Provider
How to Become Cloud Backup Provider
Cloudian
 
Presentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - WebinarPresentation of OrientDB v2.2 - Webinar
Presentation of OrientDB v2.2 - Webinar
Orient Technologies
 
Azure SQL - more or/and less than SQL Server
Azure SQL - more or/and less than SQL ServerAzure SQL - more or/and less than SQL Server
Azure SQL - more or/and less than SQL Server
Rafał Hryniewski
 
Bare Metal to OpenStack with Razor and Chef
Bare Metal to OpenStack with Razor and ChefBare Metal to OpenStack with Razor and Chef
Bare Metal to OpenStack with Razor and Chef
Matt Ray
 
Why Managed Service Providers Should Embrace Container Technology
Why Managed Service Providers Should Embrace Container TechnologyWhy Managed Service Providers Should Embrace Container Technology
Why Managed Service Providers Should Embrace Container Technology
Sagi Brody
 
Ceph Day Bring Ceph To Enterprise
Ceph Day Bring Ceph To EnterpriseCeph Day Bring Ceph To Enterprise
Ceph Day Bring Ceph To Enterprise
Alex Lau
 
[Devconf.cz][2017] Understanding OpenShift Security Context Constraints
[Devconf.cz][2017] Understanding OpenShift Security Context Constraints[Devconf.cz][2017] Understanding OpenShift Security Context Constraints
[Devconf.cz][2017] Understanding OpenShift Security Context Constraints
Alessandro Arrichiello
 
Ceph Day Taipei - Bring Ceph to Enterprise
Ceph Day Taipei - Bring Ceph to EnterpriseCeph Day Taipei - Bring Ceph to Enterprise
Ceph Day Taipei - Bring Ceph to Enterprise
Ceph Community
 
Ceph Day Tokyo - Bring Ceph to Enterprise
Ceph Day Tokyo - Bring Ceph to Enterprise Ceph Day Tokyo - Bring Ceph to Enterprise
Ceph Day Tokyo - Bring Ceph to Enterprise
Ceph Community
 
18587936 squid-proxy-configuration-guide - [the-xp.blogspot.com]
18587936 squid-proxy-configuration-guide - [the-xp.blogspot.com]18587936 squid-proxy-configuration-guide - [the-xp.blogspot.com]
18587936 squid-proxy-configuration-guide - [the-xp.blogspot.com]
Krisman Tarigan
 
LISA18: Hidden Linux Metrics with Prometheus eBPF Exporter
LISA18: Hidden Linux Metrics with Prometheus eBPF ExporterLISA18: Hidden Linux Metrics with Prometheus eBPF Exporter
LISA18: Hidden Linux Metrics with Prometheus eBPF Exporter
Ivan Babrou
 
Containers with systemd-nspawn
Containers with systemd-nspawnContainers with systemd-nspawn
Containers with systemd-nspawn
Gábor Nyers
 
CoreOS, or How I Learned to Stop Worrying and Love Systemd
CoreOS, or How I Learned to Stop Worrying and Love SystemdCoreOS, or How I Learned to Stop Worrying and Love Systemd
CoreOS, or How I Learned to Stop Worrying and Love Systemd
Richard Lister
 
Gab17 lyon SQL on Linux - David BARBARIN
Gab17 lyon SQL on Linux - David BARBARINGab17 lyon SQL on Linux - David BARBARIN
Gab17 lyon SQL on Linux - David BARBARIN
AZUG FR
 
Ch 22: Web Hosting and Internet Servers
Ch 22: Web Hosting and Internet ServersCh 22: Web Hosting and Internet Servers
Ch 22: Web Hosting and Internet Servers
webhostingguy
 
containerD
containerDcontainerD
containerD
strikr .
 
Introduction to PostgreSQL for System Administrators
Introduction to PostgreSQL for System AdministratorsIntroduction to PostgreSQL for System Administrators
Introduction to PostgreSQL for System Administrators
Jignesh Shah
 
PVS-Studio in the Clouds: Travis CI
PVS-Studio in the Clouds: Travis CIPVS-Studio in the Clouds: Travis CI
PVS-Studio in the Clouds: Travis CI
Andrey Karpov
 
How to Become Cloud Backup Provider
How to Become Cloud Backup ProviderHow to Become Cloud Backup Provider
How to Become Cloud Backup Provider
Cloudian
 
Ad

More from Open Source School (13)

Linux Administrateur
Linux AdministrateurLinux Administrateur
Linux Administrateur
Open Source School
 
Linux Scripting
Linux Scripting Linux Scripting
Linux Scripting
Open Source School
 
PHP & Initiation Symfony
PHP & Initiation SymfonyPHP & Initiation Symfony
PHP & Initiation Symfony
Open Source School
 
Strategie de Virtualisation Open Source
Strategie de Virtualisation Open SourceStrategie de Virtualisation Open Source
Strategie de Virtualisation Open Source
Open Source School
 
IPSEC
IPSECIPSEC
IPSEC
Open Source School
 
GIT pour développeur
GIT pour développeurGIT pour développeur
GIT pour développeur
Open Source School
 
Kerberos : The network authentification protocol
Kerberos : The network authentification protocolKerberos : The network authentification protocol
Kerberos : The network authentification protocol
Open Source School
 
LDAP : Theory and OpenLDAP implementation
LDAP : Theory and OpenLDAP implementationLDAP : Theory and OpenLDAP implementation
LDAP : Theory and OpenLDAP implementation
Open Source School
 
Monitoring : The art of knowing when and why things go wrong
Monitoring : The art of knowing when and why things go wrongMonitoring : The art of knowing when and why things go wrong
Monitoring : The art of knowing when and why things go wrong
Open Source School
 
Redhat enterprise Linux
Redhat enterprise LinuxRedhat enterprise Linux
Redhat enterprise Linux
Open Source School
 
Redis : NoSQL key-data store
Redis : NoSQL key-data storeRedis : NoSQL key-data store
Redis : NoSQL key-data store
Open Source School
 
SSL : Theory and practice
SSL : Theory and practiceSSL : Theory and practice
SSL : Theory and practice
Open Source School
 
Varnish : Advanced and high-performance HTTP caching
Varnish : Advanced and high-performance HTTP cachingVarnish : Advanced and high-performance HTTP caching
Varnish : Advanced and high-performance HTTP caching
Open Source School
 
Strategie de Virtualisation Open Source
Strategie de Virtualisation Open SourceStrategie de Virtualisation Open Source
Strategie de Virtualisation Open Source
Open Source School
 
Kerberos : The network authentification protocol
Kerberos : The network authentification protocolKerberos : The network authentification protocol
Kerberos : The network authentification protocol
Open Source School
 
LDAP : Theory and OpenLDAP implementation
LDAP : Theory and OpenLDAP implementationLDAP : Theory and OpenLDAP implementation
LDAP : Theory and OpenLDAP implementation
Open Source School
 
Monitoring : The art of knowing when and why things go wrong
Monitoring : The art of knowing when and why things go wrongMonitoring : The art of knowing when and why things go wrong
Monitoring : The art of knowing when and why things go wrong
Open Source School
 
Varnish : Advanced and high-performance HTTP caching
Varnish : Advanced and high-performance HTTP cachingVarnish : Advanced and high-performance HTTP caching
Varnish : Advanced and high-performance HTTP caching
Open Source School
 
Ad

Recently uploaded (20)

AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
How to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and TrendsHow to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and Trends
Nascenture
 
MEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptxMEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptx
IC substrate Shawn Wang
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
ICT Frame Magazine Pvt. Ltd.
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025
Damco Salesforce Services
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
How to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and TrendsHow to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and Trends
Nascenture
 
MEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptxMEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptx
IC substrate Shawn Wang
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdfICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
ICDCC 2025: Securing Agentic AI - Eryk Budi Pratama.pdf
Eryk Budi Pratama
 
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
MULTI-STAKEHOLDER CONSULTATION PROGRAM On Implementation of DNF 2.0 and Way F...
ICT Frame Magazine Pvt. Ltd.
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025
Damco Salesforce Services
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 

PostgreSQL : Introduction

  • 2. Introduction Installation The psql client Authentication and privileges Backup and restoration I Part 1 www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 2/110
  • 3. Introduction Installation The psql client Authentication and privileges Backup and restoration Part 1 1 Introduction 2 Installation 3 The psql client 4 Authentication and privileges 5 Backup and restoration www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 3/110
  • 4. Introduction Installation The psql client Authentication and privileges Backup and restoration Introduction www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 4/110
  • 5. Introduction Installation The psql client Authentication and privileges Backup and restoration What is PostgreSQL What is PostgreSQL www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 5/110
  • 6. Introduction Installation The psql client Authentication and privileges Backup and restoration What is PostgreSQL What is PostgreSQL ? Open Source BSD-like licence Many commercial derivatives (EnterpriseDB,. . . ) Uncompromised Slow evolution but rock solid No for-profit company behind Goes much further than standards asks for. Relational ACID Object-Relational Stored procedures Cross-platform Linux Windows Most *NIX www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 6/110
  • 7. Introduction Installation The psql client Authentication and privileges Backup and restoration What is PostgreSQL History Evolved from Ingres Postgres = Ingres + SQL Postgres released in 1995 1999 : support for real ACID and PL/pgSQL 2005 : optimized enough to become a real contender 2009 : PostgreSQL 8.4 www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 7/110
  • 8. Introduction Installation The psql client Authentication and privileges Backup and restoration What is PostgreSQL How does PostgreSQL compare to MySQL ? Respects the standards strictly. Strict value checking. Example: MySQL 4.X considers 2012-01-00 a valid date , same for 2012-02-31, MySQL 5 fixes the later example only. Really ACID. Exemples In PostgreSQL NOW() is the start of the transaction not really the current timestamp. Transactional DDL : In MySQL, an ALTER will silently COMMIT any transaction. In MySQL, foreign key cascades do not fire triggers,API’s don’t fire triggers www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 8/110
  • 9. Introduction Installation The psql client Authentication and privileges Backup and restoration What is PostgreSQL Maximum sizes of objects Object PostgreSQL MyISAM InnoDB Database Unlimited 1 Unlimited2 Adressable space3 Table size 32TB Max file size 4 Max file size 4 Row size 400GB 64kB 5 64kB 5 Field size 1GB 8kB 6 8kB 6 Columns up to 16007 ?8 10009 Indexes unlimited 256 256 1 some 32TB databases exist 2 but databases over 200GB are quite rare, or quite slow 3 innodbPageSize * 224 with default innodbPageSize = 16kB, for all tables unless innodb file per table=1 4 4TB on typical linux 5 not counting BLOBs 6 not counting BLOBs, VARCHAR and, VARBINARY 7 250 to 1600, depending on field types 8 constraint is row size 9 row size constraint still applies www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 9/110
  • 10. Introduction Installation The psql client Authentication and privileges Backup and restoration What is PostgreSQL How does PostgreSQL compare to MySQL ? Deeply object-oriented (database creation is a fork+inheritance, tables can inherit from one another...) Storage of large objects (BLOB) is automagically ”put aside” to not clobber tables. PostgreSQL is renowned in the geographical field : PostGIS PostgreSQL can be used for datawarehousing : OpenStreetMap has a 2.4TB database and it’s a small one. PostgreSQL does not have a query cache : you need an external tool for that www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 10/110
  • 11. Introduction Installation The psql client Authentication and privileges Backup and restoration Basic concepts Basic concepts www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 11/110
  • 12. Introduction Installation The psql client Authentication and privileges Backup and restoration Basic concepts Cluster A database cluster is a single PostgreSQL server instance If you want multiple clusters you will need : Different config files A different data directory A different network port Usually, servers only have one cluster www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 12/110
  • 13. Introduction Installation The psql client Authentication and privileges Backup and restoration Basic concepts Database A database is a set of objects that can be used together Tables Functions Custom data types Views Triggers ... Once connected to a database, you stay inside it, and cannot use objects from other databases A database cluster can have multiple databases www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 13/110
  • 14. Introduction Installation The psql client Authentication and privileges Backup and restoration Basic concepts Schema A schema is a namespace within a database All databases have a default, public schema It can be used to separate things It can also help with access control A database can have multiple schemas www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 14/110
  • 15. Introduction Installation The psql client Authentication and privileges Backup and restoration Basic concepts Tables Your regular DB table myschema.mytable : explicit table indication mytable : look for table in the search path (contains public) www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 15/110
  • 16. Introduction Installation The psql client Authentication and privileges Backup and restoration Basic concepts Tablespaces The physical location of files on disk. Your friendly sysadmin can choose to put one tablespace on a faster medium (SSD) to increase performance of this tablespace’s tables. Can be defined per database or per user Or explicitely on table creation Any object in the whole cluster can use any tablespace if permissions allow it www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 16/110
  • 17. Introduction Installation The psql client Authentication and privileges Backup and restoration Installation www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 17/110
  • 18. Introduction Installation The psql client Authentication and privileges Backup and restoration Installing PostgreSQL Installing PostgreSQL www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 18/110
  • 19. Introduction Installation The psql client Authentication and privileges Backup and restoration Installing PostgreSQL Grabbing the bits Debian : aptitude install postgresql RedHat : yum install postgresql-server chkconfig postgresql on service postgresql initdb service postgresql start You might want to install postgresql-contrib, which contains may community-developped tools : benchmark tools, additional diagnostic tools, . . . www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 19/110
  • 20. Introduction Installation The psql client Authentication and privileges Backup and restoration Installing PostgreSQL First steps : processes Look at the running processes postgres -D /var/lib/postgresql/8.4/main -c config file=/etc/.../postgresql.conf Master database process postgres: writer process Writes data blocks back to disk postgres: wal writer process Writes transaction logs postgres: autovacuum launcher process Database housekeeping postgres: stats collector process Stats postgres: postgres postgres [local] idle Each connection to the DB spawns a process www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 20/110
  • 21. Introduction Installation The psql client Authentication and privileges Backup and restoration Installing PostgreSQL First steps : config files Each cluster has its own set of configuration files and data files environment : environment variables for the server pg ctl.conf : startup command-line options pg hba.conf : authentication pg ident.conf : ident maps for authentication postgresql.conf : main config file start.conf : whether this cluster is auto started or not When changing the configuration, there is usually no need to restart, a reload sould be enough www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 21/110
  • 22. Introduction Installation The psql client Authentication and privileges Backup and restoration Installing PostgreSQL First steps : files That -D parameter was the $datadir : by default, everything is there On RedHat, everything is there On Debian, as usual, conf goes to /etc/postgresql and logs go to /var/log/postgresql Debian has a version/cluster convention that allows you to run as many instances of as many versions of PostgreSQL as you want easily www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 22/110
  • 23. Introduction Installation The psql client Authentication and privileges Backup and restoration Installing PostgreSQL First steps : files base : default tablespace global : global tablespace pg tblspc : other tablespaces (can contain symlinks) pg log : application logs (admin-readable) pg clog, pg multixact, pg stat tmp, pg subtrans, pg twophase, pg xlog : various binary logs and technical directories www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 23/110
  • 24. Introduction Installation The psql client Authentication and privileges Backup and restoration Basic administration Basic administration www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 24/110
  • 25. Introduction Installation The psql client Authentication and privileges Backup and restoration Basic administration Basic administration su - postgres : required to do any administrative operation on the database with the default configuration createuser -P myuser : create a new user You should answer ”no” to all its questions : access users should not be able to create anything global createdb -O myuser mydb : create a database that belongs to the user, he will have full privileges on it you can now psql -hlocalhost -Umyuser mydb www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 25/110
  • 26. Introduction Installation The psql client Authentication and privileges Backup and restoration The psql client www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 26/110
  • 27. Introduction Installation The psql client Authentication and privileges Backup and restoration psql basic usage Uses unix-socket by default to connect to localhost, check your ident or adapt pg hba.conf Connect with psql DBNAME USERNAME -W for interactive auth Use psql -h 10.1.2.3 to connect to a remote postgres. psql uses less as a result pager by default (usefull) www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 27/110
  • 28. Introduction Installation The psql client Authentication and privileges Backup and restoration psql basic usage Once connected, l : list database (equivalent to MySQL’s SHOW DATABASES) c : connect to database (equivalent to MySQL’s USE) dt : list tables in current database (equivalent to MySQL’s SHOW TABLES). dt+ : shows more information like ownerships and comments on each table. di : show indexes d table : lists columns of a table (equivalent to MySQL’s DESC table) x : Print one column per line www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 28/110
  • 29. Introduction Installation The psql client Authentication and privileges Backup and restoration Autocommit By default, autocommit is set, this means any command you enter in the psql client will be automatically commited as it it were a mini-transaction You can set AUTOCOMMIT off With autocommit set to off, even if you do not explicitely start a transaction with BEGIN, no permanent change will be made until you COMMIT www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 29/110
  • 30. Introduction Installation The psql client Authentication and privileges Backup and restoration Passwords psql will prompt automatically for a password if required You cannot give the password on the command line ! .pgpass file in the homedirectory : hostname:port:database:username:password eg : *:*:mydb:myuser:mypw www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 30/110
  • 31. Introduction Installation The psql client Authentication and privileges Backup and restoration Authentication and privileges www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 31/110
  • 32. Introduction Installation The psql client Authentication and privileges Backup and restoration Authentification Authentification www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 32/110
  • 33. Introduction Installation The psql client Authentication and privileges Backup and restoration Authentification Authentification PostgreSQL allows you to choose between many authentication schemes : md5 : regular user/password based on embedded account info ident : get identity from local socket (only for local auth !) krb5 ldap cert : SSL client auth gss/sspi/pam : delegate to external framework www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 33/110
  • 34. Introduction Installation The psql client Authentication and privileges Backup and restoration Authentification Authentification Authentication is configured in pg hba.conf: # Database a d m i n i s t r a t i v e l o g i n by UNIX s o c k e t s l o c a l a l l p o s t g r e s i d e n t # ” l o c a l ” i s f o r Unix domain socket c o n n e c t i o n s only l o c a l a l l a l l i d e n t # IPv4 l o c a l c o n n e c t i o n s : host a l l a l l 1 2 7 . 0 . 0 . 1 / 3 2 md5 # IPv6 l o c a l c o n n e c t i o n s : host a l l a l l : : 1 / 1 2 8 md5 www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 34/110
  • 35. Introduction Installation The psql client Authentication and privileges Backup and restoration Authentification Authentification Allowing external connections : First, change the listen address in postgresql.conf l i s t e n a d d r e s s e s = ’∗ ’ Then, add in pg hba.conf # From j u s t one f r o n t s e r v e r host a l l a l l 172.16.0.54/32 md5 # From a pool of f r o n t s e r v e r s host a l l a l l 172.16.0.0/24 md5 # From everywhere ( don ’ t ) host a l l a l l 0 . 0 . 0 . 0 / 0 md5 www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 35/110
  • 36. Introduction Installation The psql client Authentication and privileges Backup and restoration Roles : users and groups Roles : users and groups www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 36/110
  • 37. Introduction Installation The psql client Authentication and privileges Backup and restoration Roles : users and groups Roles : users and groups Role management : CREATE ROLE something; : not very useful CREATE ROLE foobar LOGIN PASSWORD "toto"; : role can login CREATE USER foobar; : same thing CREATE ROLE foobar CREATEDB; : this role can create databases CREATE ROLE foobar CREATEROLE; : this role can admin roles CREATE ROLE foobar SUPERUSER; : this role is not submitted to permission checks ALTER ROLE, DROP ROLE GRANT group role TO user; : add a user to a group SET ROLE group role; : gain the privileges of the group Role display : SELECT * FROM pg roles; du www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 37/110
  • 38. Introduction Installation The psql client Authentication and privileges Backup and restoration Privileges Privileges www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 38/110
  • 39. Introduction Installation The psql client Authentication and privileges Backup and restoration Privileges Privileges SELECT, INSERT, UPDATE, DELETE TRUNCATE REFERENCES, TRIGGER, TEMPORARY: create foreign keys/triggers/temp tambes CREATE : create tables, indexes, etc. CONNECT : allow a user to connect to the database USAGE, EXECUTE : allow a user to define and execute functions ALL/ALL PRIVILEGES : allow everything rights to DROP and ALTER are not grantable, and given to object owners and superusers www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 39/110
  • 40. Introduction Installation The psql client Authentication and privileges Backup and restoration Privileges Privileges GRANT rights ON object TO role; REVOKE rights ON object FROM role; dp : display permissions www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 40/110
  • 41. Introduction Installation The psql client Authentication and privileges Backup and restoration Ownership Ownership www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 41/110
  • 42. Introduction Installation The psql client Authentication and privileges Backup and restoration Ownership Ownership Owners have default privileges Create any table in a db read any column in a table etc.. When using createdb to create a new database for your application do not forget to set the owner You can see the owner with dt You can change the owner with ALTER www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 42/110
  • 43. Introduction Installation The psql client Authentication and privileges Backup and restoration Backup and restoration www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 43/110
  • 44. Introduction Installation The psql client Authentication and privileges Backup and restoration Backups Backups www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 44/110
  • 45. Introduction Installation The psql client Authentication and privileges Backup and restoration Backups dumps pg dump pg dump is the standard database dump program : pg dump dbname > outfile (as superuser) pg dump -t mytable dbname > outfile : just one table (-b for blobs) You can use -Fc to produce a optimized, more flexible dump using an internal format (not SQL) pg dumpall pg dumpall dumps all databases pg dumpall > outfile (as superuser) Backups will be coherent, and will not lock tables (MVCC) except if you do a heavy modification like ALTER www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 45/110
  • 46. Introduction Installation The psql client Authentication and privileges Backup and restoration Restoration Restoration www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 46/110
  • 47. Introduction Installation The psql client Authentication and privileges Backup and restoration Restoration Restoration psql < mydump pg restore -d mydb mydump if your dump is in PostgreSQL ’s custom format pg restore mydump converts the dump into SQL format for later use www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 47/110
  • 48. Introduction Installation The psql client Authentication and privileges Backup and restoration Physical backup and PITR Physical backup and PITR www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 48/110
  • 49. Introduction Installation The psql client Authentication and privileges Backup and restoration Physical backup and PITR Physical backup and PITR Instead of doing dumps, you can ”freeze” the database and do a filesystem backup Then, the WALs are archived Using the filesystem backup + the WAL, you can restore to any point in time This requires a specific, complicated config Stick to good old dumps if you don’t really need that www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 49/110
  • 50. Internal Architecture Performance optimization Stats and monitoring Logs Replication II Part 2 www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 50/110
  • 51. Internal Architecture Performance optimization Stats and monitoring Logs Replication Part 2 6 Internal Architecture 7 Performance optimization 8 Stats and monitoring 9 Logs 10 Replication www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 51/110
  • 52. Internal Architecture Performance optimization Stats and monitoring Logs Replication Internal Architecture www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 52/110
  • 53. Internal Architecture Performance optimization Stats and monitoring Logs Replication Storage engine Storage engine www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 53/110
  • 54. Internal Architecture Performance optimization Stats and monitoring Logs Replication Storage engine Filesystem The tablespace is a folder that contains oddly-named files Those files are data blocks (multiples of 8k) They are mapped in memory in various caches www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 54/110
  • 55. Internal Architecture Performance optimization Stats and monitoring Logs Replication Storage engine Cache PostgreSQL uses two levels of cache Shared buffers contain a copy of data blocks Since PostgreSQL knows what they are used for, it can invalidate only some of them during certain operations The OS cache (not PostgreSQL memory) also contains a copy of the data blocks PostgreSQL relies on it for performance considerations (query planner) Invalidation is controlled by the OS, and not smart from a DB point of view It is however, smart from a hardware point of view Both level are designed to work together www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 55/110
  • 56. Internal Architecture Performance optimization Stats and monitoring Logs Replication Storage engine Write-ahead log The WAL is an essential component stored in pg xlog Contains every modification of the database Optimized for write performance Pre-allocated sequential writes Used to guarantee integrity : on COMMIT (or autocommit), the modification is written and fsync-ed to the log If a crash occurs, the server re-synchronises the data from the xlog www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 56/110
  • 57. Internal Architecture Performance optimization Stats and monitoring Logs Replication Storage engine Checkpoints and background writer The background writer’s task is to commit dirty buffers to the disk (or OS cache) Checkpointing is the operation that writes all data back to the hard drive (bypassing OS cache) throttled to avoid IO peaks It occurs automatically When the transation logs are full After a certain time www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 57/110
  • 58. Internal Architecture Performance optimization Stats and monitoring Logs Replication Transactions Transactions www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 58/110
  • 59. Internal Architecture Performance optimization Stats and monitoring Logs Replication Transactions MVCC : Multi Version Concurrency Control MVCC is a method used in many databases, relational or others InnoDB, Oracle Database, Berkeley DB, DB2, Sybase (and MSSQL) CouchDB Subversion EHcache and of course, PostgreSQL rows have a xmin, and xmax value, containing a xid, or transaction ID Every transaction increments the current xid, Is is an internal column, but you can display it with SELCT txid current(); SELECT *,xmin,xmax from mytable; www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 59/110
  • 60. Internal Architecture Performance optimization Stats and monitoring Logs Replication Transactions MVCC and vaccuuming UPDATE does not modify rows, it creates a new row with a xmin set to the current transaction and sets xmax to the current transaction on the old row DELETE does not delete rows, it sets the row’s xmax to the current transaction The operation that removes unneeded rows is VACCUUM regular VACCUUM simply removes unneeded rows by marking them free in the Free Space Map (FSM) VACUUM FULL actually disallocates unneeded rows from the tablespace, it is a heavy operation, but can help of very active tables (sessions) regular VACCUUM is automatically performed by the autovaccuum process www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 60/110
  • 61. Internal Architecture Performance optimization Stats and monitoring Logs Replication Transactions Transaction isolation level PostgreSQL automatically manages row-level locking as needed READ COMMITED (default) In the same transaction, you can see a different image of the DB depending if another transaction has been validated in the meantime You will never, however, see a transaction that has not been commited yet But you can lock lines explicitely with SELECT FOR UPDATE SERIALIZABLE If a transaction detects that a value was modified since the beginning of the transaction, it will fail The other standard levels (READ UNCOMMITED, and REPEATABLE READ) are mapped to the closest level of those two www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 61/110
  • 62. Internal Architecture Performance optimization Stats and monitoring Logs Replication Performance optimization www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 62/110
  • 63. Internal Architecture Performance optimization Stats and monitoring Logs Replication Memory Memory www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 63/110
  • 64. Internal Architecture Performance optimization Stats and monitoring Logs Replication Memory shared buffers The main memory pool used by PostgreSQL Usually : shared buffers = totalram/4 more isn’t better SHM : need to tweak sysctl.conf kernel.shmmax : maximum size (in bytes) of a memory segment kernel.shmall : maximum size (in pages) of all memory segments getconf PAGE SIZE effective cache size : give it the actual cache size, including OS cache (50 − 70% of memory) to help the query planner. This will not allocate anything, it’s just helping the query planner make the right decisions. www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 64/110
  • 65. Internal Architecture Performance optimization Stats and monitoring Logs Replication Memory work memory work mem : memory for sorts, hash tables, etc. The more work mem, the less you need to store temporary tables to disk Any single session can run multiple operations concurrently (complex queries) so don’t put too much there, or you’ll run out of memory on high loads maintenance work mem : memory available to maintenance operation (INDEX, VACUUM, ALTER, . . . ) You can give more than work mem, not many maintenance operations will occur simultaneously a good value is totalram/20 www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 65/110
  • 66. Internal Architecture Performance optimization Stats and monitoring Logs Replication Disk Disk www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 66/110
  • 67. Internal Architecture Performance optimization Stats and monitoring Logs Replication Disk Disk Checkpoints are the main source of IO checkpoint segments : number of 16Mb log files to keep before ckecpointing recommended value : 10 checkpoint timeout : maximum time between two checkpoint Default : 5min, but you can put more if you need to checkpoint completion target : helps spread the IO load default : 0.5 you can try : 0.9 or something in-between if you want to spread the load more wal buffers a good value is 16Mb, which is the size of a WAL www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 67/110
  • 68. Internal Architecture Performance optimization Stats and monitoring Logs Replication Network Network www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 68/110
  • 69. Internal Architecture Performance optimization Stats and monitoring Logs Replication Network Network max connections : maximum amount of connections superuser reserved connections : maximum amount of connections for the superuser If you use PHP, you’ll need max connections = frontends maxclients+superuser reserved connections If you have connection pools : max connections = frontends poolsize+superuser reserved connections www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 69/110
  • 70. Internal Architecture Performance optimization Stats and monitoring Logs Replication Query planner Query planner www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 70/110
  • 71. Internal Architecture Performance optimization Stats and monitoring Logs Replication Query planner Query planner The query planner is used by PostgreSQL to find the most efficient way to process a request Can we use indexes Which one is the best In which order do we execute complex statements Can we reformulate the statement in a more efficient one (SELECT WHERE IN becomes an JOIN, etc.) It uses a cost-based model And predictions from ANALYZE (autoanalyze is a part of autovaccuum) Sometimes it is wrong (but not very often!) Don’t try to configure it or you will shoot yourself in the foot You cant, however, hint it not to use certain operations (no full scans, no indexes, etc.) and see what happens www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 71/110
  • 72. Internal Architecture Performance optimization Stats and monitoring Logs Replication Query planner What it looks like EXPLAIN will yield : A tree of actions Indication on estimated cost for first row and for all rows amount of rows expected to retrieve estimated size of a row EXPLAIN ANALYZE will actually execute the request, yield the query planner info, and : time it took to produce first row and all rows (but actual time wihtout the profiler would be much faster) actual amount of rows returned how many times the node was executed www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 72/110
  • 73. Internal Architecture Performance optimization Stats and monitoring Logs Replication Query planner Sequential scans and index scans # A query using a t a b l e scan ( bad ) root=> EXPLAIN SELECT ∗ FROM customer WHERE a c t i v e =0; QUERY PLAN −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Seq Scan on customer ( c o s t =0.00..16.49 rows=15 width =70) F i l t e r : ( a c t i v e = 0) # A query using an index ( good ) root=> EXPLAIN SELECT ∗ FROM customer WHERE last name =’SMITH ’ ; QUERY PLAN −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Index Scan using i d x l a s t n a m e on customer ( c o s t = 0 . 0 0 . . 8 . 2 7 rows=1 width =70) Index Cond : (( last name ) : : t e x t = ’SMITH ’ : : t e x t ) www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 73/110
  • 74. Internal Architecture Performance optimization Stats and monitoring Logs Replication Query planner Query optimization root=> EXPLAIN SELECT DISTRICT FROM a dd r es s WHERE a d d r e s s i d IN (SELECT a d d r e s s i d FROM customer WHERE a c t i v e =0); QUERY PLAN −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Hash Semi Join ( c o s t =16.68..32.45 rows=15 width =9) Hash Cond : ( a dd r es s . a d d r e s s i d = customer . a d d r e s s i d ) −> Seq Scan on a dd r es s ( c o s t =0.00..14.03 rows=603 width =13) −> Hash ( c o s t =16.49..16.49 rows=15 width =2) −> Seq Scan on customer ( c o s t =0.00..16.49 rows=15 width =2) F i l t e r : ( a c t i v e = 0) root=> EXPLAIN SELECT a dd r es s . d i s t r i c t FROM address , customer WHERE a dd r es s . a d d r e s s i d=customer . a d d r e s s i d AND customer . a c t i v e =0; QUERY PLAN −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Hash Join ( c o s t =16.68..33.12 rows=15 width =9) Hash Cond : ( a dd r es s . a d d r e s s i d = customer . a d d r e s s i d ) −> Seq Scan on a dd r es s ( c o s t =0.00..14.03 rows=603 width =13) −> Hash ( c o s t =16.49..16.49 rows=15 width =2) −> Seq Scan on customer ( c o s t =0.00..16.49 rows=15 width =2) F i l t e r : ( a c t i v e = 0) www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 74/110
  • 75. Internal Architecture Performance optimization Stats and monitoring Logs Replication Query planner Complex queries root=> EXPLAIN SELECT COUNT(∗) , ad d re ss . d i s t r i c t FROM address , customer WHERE a d dr e ss . a d d r e s s i d=customer . a d d r e s s i d AND customer . a c t i v e=1 GROUP BY a dd r es s . d i s t r i c t ; QUERY PLAN −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− HashAggregate ( c o s t =49.01..53.73 rows=378 width =9) −> Hash Join ( c o s t =21.57..46.09 rows=584 width =9) Hash Cond : ( customer . a d d r e s s i d = a d dr e ss . a d d r e s s i d ) −> Seq Scan on customer ( c o s t =0.00..16.49 rows=584 width =2) F i l t e r : ( a c t i v e = 1) −> Hash ( c o s t =14.03..14.03 rows=603 width =13) −> Seq Scan on a dd r es s ( c o s t =0.00..14.03 rows=603 width =13) www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 75/110
  • 76. Internal Architecture Performance optimization Stats and monitoring Logs Replication Query planner Complex queries (for real) root=> EXPLAIN ANALYZE SELECT COUNT(∗) , ad d re s s . d i s t r i c t FROM address , customer WHERE a d dr e ss . a d d r e s s i d=customer . a d d r e s s i d AND customer . a c t i v e=1 GROUP BY a dd r es s . d i s t r i c t ; QUERY PLAN −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− HashAggregate ( c o s t =49.01..53.73 rows=378 width =9) ( a c t u a l time =4.939..5.384 rows=369 l o o p s =1) −> Hash Join ( c o s t =21.57..46.09 rows=584 width =9) ( a c t u a l time =1.632..4.045 rows=584 l o o p s =1) Hash Cond : ( customer . a d d r e s s i d = a d dr e ss . a d d r e s s i d ) −> Seq Scan on customer ( c o s t =0.00..16.49 rows=584 width =2) ( a c t u a l time =0.016..0.935 rows=584 l o o p s =1) F i l t e r : ( a c t i v e = 1) −> Hash ( c o s t =14.03..14.03 rows=603 width =13) ( a c t u a l time =1.602..1.602 rows=603 l o o p s =1) −> Seq Scan on a dd r es s ( c o s t =0.00..14.03 rows=603 width =13) ( a c t u a l time =0.007..0.796 rows=603 l o o p s =1) Total runtime : 5.883 ms www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 76/110
  • 77. Internal Architecture Performance optimization Stats and monitoring Logs Replication Stats and monitoring www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 77/110
  • 78. Internal Architecture Performance optimization Stats and monitoring Logs Replication Table stats Table stats www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 78/110
  • 79. Internal Architecture Performance optimization Stats and monitoring Logs Replication Table stats Table statistics SELECT * from pg stat user table root=> d p g s t a t u s e r t a b l e s ; View ” p g c a t a l o g . p g s t a t u s e r t a b l e s ” Column | Type −−−−−−−−−−−−−−−−−−+−−−−−−−−−− r e l i d | oid | Table oid schemaname | name | Schema name relname | name | Table name seq scan | b i g i n t | Number of s e q u e n t i a l scans s e q t u p r e a d | b i g i n t | Number of rows r e t u r n e by seq scans i d x s c a n | b i g i n t | Number of index scans i d x t u p f e t c h | b i g i n t | Number of rows r e t u r n e d by i d x scans n t u p i n s | b i g i n t | Number of i n s e r t e d rows n tup upd | b i g i n t | Number of updated rows n t u p d e l | b i g i n t | Number of d e l e t e d rows n tup hot upd | b i g i n t | Number of updated rows ( with HOT) n l i v e t u p | b i g i n t | Number of v a l i d rows n dead tup | b i g i n t | Number of i n v a l i d (MVCC) rows last vacuum | timestamp | Last time VACCUUM was s t a r t e d manually last autovacuum | timestamp | Last time VACCUUM was s t a r t e d a u t o m a t i c a l l y l a s t a n a l y z e | timestamp | Last time ANALYZE was s t a r t e d manually l a s t a u t o a n a l y z e | timestamp | Last time ANALYZE was s t a r t e d a u t o m a t i c a l l y www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 79/110
  • 80. Internal Architecture Performance optimization Stats and monitoring Logs Replication Table stats IO stats SELECT * from pg statio user tables Show you the amount of cache reads and page reads Show how efficient the buffer pool is Does not show information about the OS cache SELECT * from pg statio user indexes Same with indexes www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 80/110
  • 81. Internal Architecture Performance optimization Stats and monitoring Logs Replication Table stats IO stats example SELECT relname , h e a p b l k s r e a d , h e a p b l k s h i t , i d x b l k s r e a d , i d x b l k s h i t FROM p g s t a t i o u s e r t a b l e s ; relname | h e a p b l k s r e a d | h e a p b l k s h i t | i d x b l k s r e a d | i d x b l k s h i t −−−−−−−−−−−+−−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−+−−−−−−−−−−−−−− payment | 52 | 297 | 54 | 28123 f i l m | 58 | 170 | 34 | 8646 i n v e n t o r y | 27 | 71 | 35 | 17552 SELECT relname , c a s t ( h e a p b l k s h i t as numeric )/( h e a p b l k s h i t+h e a p b l k s r e a d ) AS h i t r a t e FROM p g s t a t i o u s e r t a b l e s WHERE ( h e a p b l k s h i t+h e a p b l k s r e a d )>0 ; relname | h i t r a t e −−−−−−−−−−−−−−−−−−+−−−−−−−−−−−−−−−−−−−−−−−− country | 0.50000000000000000000 payment p2007 04 | 0.85100286532951289398 f i l m | 0.74561403508771929825 payment p2007 02 | 0.83898305084745762712 www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 81/110
  • 82. Internal Architecture Performance optimization Stats and monitoring Logs Replication Table stats Table sizes SELECT schemaname , relname , p g s i z e p r e t t y ( p g r e l a t i o n s i z e ( r e l i d )) AS s i z e , p g s i z e p r e t t y ( p g t o t a l r e l a t i o n s i z e ( r e l i d )) AS t o t a l s i z e FROM p g s t a t u s e r t a b l e s ; or dt+ www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 82/110
  • 83. Internal Architecture Performance optimization Stats and monitoring Logs Replication Activity Activity www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 83/110
  • 84. Internal Architecture Performance optimization Stats and monitoring Logs Replication Activity The pg stat activity view root=> d p g s t a t a c t i v i t y ; View ” p g c a t a l o g . p g s t a t a c t i v i t y ” Column | Type −−−−−−−−−−−−−−−+−−−−−−−−−−−− d a t i d | oid | Database OID datname | name | Database name procpid | i n t e g e r | PID of s e r v e r p r o c e s s u s e s y s i d | oid | User OID usename | name | User name c u r r e n t q u e r y | t e x t | Query w a i t i n g | boolean | I s the query w a i t i n g x a c t s t a r t | timestamp | Transaction s t a r t time q u e r y s t a r t | timestamp | Query s t a r t time b a c k e n d s t a r t | timestamp | Process s t a r t time c l i e n t a d d r | i n e t | C l i e n t IP a dd r es s c l i e n t p o r t | i n t e g e r | C l i e n t source port A slightly better view : SELECT datname , usename , c u r r e n t q u e r y , waiting , c l i e n t a d d r , now()− q u e r y s t a r t AS r u n n i n g f o r FROM p g s t a t a c t i v i t y ORDER BY r u n n i n g f o r DESC; www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 84/110
  • 85. Internal Architecture Performance optimization Stats and monitoring Logs Replication Locks Locks www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 85/110
  • 86. Internal Architecture Performance optimization Stats and monitoring Logs Replication Locks Finding locks Let’s see which queries are blocked SELECT ∗ FROM p g s t a t a c t i v i t y WHERE w a i t i n g=t r u e ; −[ RECORD 1 ]−+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− d a t i d | 16392 datname | root procpid | 1530 u s e s y s i d | 16390 usename | root c u r r e n t q u e r y | SELECT i FROM t WHERE s=1 FOR UPDATE; w a i t i n g | t x a c t s t a r t | 2012−02−27 14:21:34.723872+01 q u e r y s t a r t | 2012−02−27 14:21:36.195808+01 b a c k e n d s t a r t | 2012−02−27 14:16:49.203923+01 c l i e n t a d d r | c l i e n t p o r t | −1 PID 1530 is waiting for something www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 86/110
  • 87. Internal Architecture Performance optimization Stats and monitoring Logs Replication Locks Let’s find who is blocking it SELECT ∗ FROM p g l o c k s WHERE pid =1530 AND granted=f a l s e ; −[ RECORD 1 ]−−−−−−+−−−−−−−−−−−−−− l o c k t y p e | t r a n s a c t i o n i d database | r e l a t i o n | page | t u p l e | v i r t u a l x i d | t r a n s a c t i o n i d | 688 c l a s s i d | o b j i d | o b j s u b i d | v i r t u a l t r a n s a c t i o n | 2/39 pid | 1530 mode | ShareLock granted | f We are locked by transaction 688 www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 87/110
  • 88. Internal Architecture Performance optimization Stats and monitoring Logs Replication Locks Let’s find who is transaction 688 SELECT ∗ FROM p g l o c k s WHERE t r a n s a c t i o n i d =688 AND granted=t r u e ; −[ RECORD 1 ]−−−−−−+−−−−−−−−−−−−−− l o c k t y p e | t r a n s a c t i o n i d database | r e l a t i o n | page | t u p l e | v i r t u a l x i d | t r a n s a c t i o n i d | 688 c l a s s i d | o b j i d | o b j s u b i d | v i r t u a l t r a n s a c t i o n | 1/1099 pid | 1519 mode | E x c l u s i v e L o c k granted | t Now you can SELECT * FROM pg stat activity WHERE procpid=1519; to find more about it www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 88/110
  • 89. Internal Architecture Performance optimization Stats and monitoring Logs Replication System monitoring System monitoring www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 89/110
  • 90. Internal Architecture Performance optimization Stats and monitoring Logs Replication System monitoring Munin www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 90/110
  • 91. Internal Architecture Performance optimization Stats and monitoring Logs Replication Logs www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 91/110
  • 92. Internal Architecture Performance optimization Stats and monitoring Logs Replication Error logs Error logs www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 92/110
  • 93. Internal Architecture Performance optimization Stats and monitoring Logs Replication Error logs Error logs You can find error logs in /var/log/postgresql syntax errors, server errors, etc. www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 93/110
  • 94. Internal Architecture Performance optimization Stats and monitoring Logs Replication Finding slow queries Finding slow queries www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 94/110
  • 95. Internal Architecture Performance optimization Stats and monitoring Logs Replication Finding slow queries Finding slow queries log min duration statement : define which queries should be logged −1 (default) : Do not log anything 0 : log all queries (aka: ruin your HDD) 500 : log queries that run for longer than 500ms Set it globally in postgresql.conf then reload Or set it just for one user : alter role ’myuser’ set log min duration statement = 500; Will only start logging next time myuser opens a session Then, disable it with : alter role ’myuser’ set log min duration statement = DEFAULT; www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 95/110
  • 96. Internal Architecture Performance optimization Stats and monitoring Logs Replication Replication www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 96/110
  • 97. Internal Architecture Performance optimization Stats and monitoring Logs Replication Basics Basics www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 97/110
  • 98. Internal Architecture Performance optimization Stats and monitoring Logs Replication Basics Basics PostgreSQL replication is asynchronous by default synchronous replication is possible, but not usually recommended log shipping : copy transaction log files to the server and replay them Delay can be high You can set a timeout to make sure you get a new log every X minutes You have to setup the copy (using rsync, NFS, etc.) streaming : direct connection to the master server (like MySQL) www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 98/110
  • 99. Internal Architecture Performance optimization Stats and monitoring Logs Replication Basics Basics warm standby : the replica is not usable, but can be rapidly enabled during failover hot standby : the replica can be used for reads beware of conflicts due to locking must compromise between the ability to run long queries and the freshness of data On 8.4 : log shipping, warm standby On 9.1 : log shipping, streaming, hot standby Many 3rd party products : slony, etc. www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 99/110
  • 100. Internal Architecture Performance optimization Stats and monitoring Logs Replication Log Shipping Log Shipping www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 100/110
  • 101. Internal Architecture Performance optimization Stats and monitoring Logs Replication Log Shipping Log Shipping: master Setup SSH keys, a NFS shared server, or any other mean to share the logs In postgresql.conf, enable archiving : wal level = archive archive mode = on archive command = ’rsync -a %p host:/dir/%f’ archive timeout = 60 (if you want one) www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 101/110
  • 102. Internal Architecture Performance optimization Stats and monitoring Logs Replication Log Shipping Log Shipping: initializing the replication Do a hot backup on the master : SELECT pg start backup(’label’); rsync -av --delete $datadir slave:$datadir SELECT pg stop backup(); www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 102/110
  • 103. Internal Architecture Performance optimization Stats and monitoring Logs Replication Log Shipping Log Shipping: slave create a recovery.conf file in the datadir restore command = ’/ usr / l i b / p o s t g r e s q l /8.4/ bin / pg standby −d −t /tmp/ p g s q l s t o p s t a n d b y / var / l i b / p o s t g r e s q l /8.4/ main/ a r c h i v e l o g %f %p %r 2>>/var / log / p o s t g r e s q l / standby . log ’ recovery end command = ’rm −f /tmp/ p gsq l st op st and by ’ www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 103/110
  • 104. Internal Architecture Performance optimization Stats and monitoring Logs Replication Log Shipping Log Shipping: failing over remove the trigger file /tmp/pgsql stopstandby to finish restoration recovery.conf is renamed to recovery.done You can now access the slave www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 104/110
  • 105. Internal Architecture Performance optimization Stats and monitoring Logs Replication Streaming Streaming www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 105/110
  • 106. Internal Architecture Performance optimization Stats and monitoring Logs Replication Streaming Streaming: master You can combine streaming and log shipping Enable WAL senders : max wal senders = 2 You should make sure to have a few old log files if you don’t use archiving : wal keep segments = 10 Create a REPLICATION account : CREATE ROLE myuser REPLICATION LOGIN PASSWORD ’mypass’ In pg hba.conf host replication myuser slave/32 md5 www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 106/110
  • 107. Internal Architecture Performance optimization Stats and monitoring Logs Replication Streaming Streaming: slave standby mode = ’on’ primary conninfo = ’host=pgmaster port=5432 user=X password=Y’ www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 107/110
  • 108. Internal Architecture Performance optimization Stats and monitoring Logs Replication Streaming Streaming: failing over pg ctlcluster 9.1 main promote www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 108/110
  • 109. Internal Architecture Performance optimization Stats and monitoring Logs Replication Hot standby Hot standby www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 109/110
  • 110. Internal Architecture Performance optimization Stats and monitoring Logs Replication Hot standby Hot standby hot standby = on wal level = hot standby max standby archive delay = 30 max standby streaming delay = 30 It’s the time we allow queries running on the slave to block WAL replay www.opensourceschool.fr – Licence Creative Commons (CC BY-SA 3.0 FR) – 110/110
  翻译: