Mastering PostgreSQL Administration
Mastering PostgreSQL Administration
PostgreSQL Administration
BRUCE MOMJIAN,
ENTERPRISEDB
March, 2005
Abstract
POSTGRESQL is an open-source, full-featured relational database. This
presentation covers advanced administration topics.
Installation
source
– obtaining
– installing
build options
RPM
– obtaining
– installing
MS Windows
– obtaining
– installing
Mastering PostgreSQL Administration 1
Initialization (initdb)
$ initdb
The files belonging to this database sys-
tem will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale C.
creating directory /u/pg/data ... ok
creating directory /u/pg/data/global ... ok
creating directory /u/pg/data/pg_xlog ... ok
creating directory /u/pg/data/pg_xlog/archive_status ... ok
creating directory /u/pg/data/pg_clog ... ok
creating directory /u/pg/data/pg_subtrans ... ok
creating directory /u/pg/data/base ... ok
creating directory /u/pg/data/base/1 ... ok
creating directory /u/pg/data/pg_tblspc ... ok
selecting default max_connections ... 100
Mastering PostgreSQL Administration 2
Initialization (Continued)
$ pg_controldata
pg_control version number: 74
Catalog version number: 200502281
Database system identifier: 4766833642862247929
Database cluster state: shut down
pg_control last modified: 03/03/05 10:49:18
Current log file ID: 0
Next log file segment: 1
Latest checkpoint location: 0/A34010
Prior checkpoint location: 0/A2D5C0
Latest checkpoint’s REDO location: 0/A34010
Latest checkpoint’s UNDO location: 0/0
Latest checkpoint’s TimeLineID: 1
Latest checkpoint’s NextXID: 545
Latest checkpoint’s NextOID: 17233
Time of latest checkpoint: 03/03/05 10:49:18
Database block size: 8192
Blocks per segment of large relation: 131072
Bytes per WAL segment: 16777216
Maximum length of identifiers: 64
Maximum number of function arguments: 32
Date/time type storage: floating-point numbers
Maximum length of locale name: 128
LC_COLLATE: C
LC_CTYPE: C
Main
Libpq
Postmaster
Postgres Postgres
Parse Statement
utility Utility
Traffic Cop
Command
Query e.g. CREATE TABLE, COPY
SELECT, INSERT, UPDATE, DELETE
Rewrite Query
Generate Paths
Optimal Path
Generate Plan
Plan
Execute Plan
manually
pg_ctl
on boot
manually
pg_ctl
on shutdown
host — TCP/IP
hostssl
passwords
– md5
– crypt
– password
remote authentication
local ident
socket permissions
pam
reject
Mastering PostgreSQL Administration 10
Access
dbname
username
groupname
IPv6
user/group permissions
– create users
– create databases
– table permissions
Database creation
– template1 customization
– system tables
– disk space computations
$ ls -CF
PG_VERSION pg_hba.conf postmaster.opts
base/ pg_ident.conf postmaster.pid
global/ pg_xlog/
pg_clog/ postgresql.conf
$ ls -CF global/
1260 16432 16454 16475 pg_group
1261 16434 16467 16485 pg_pwd
1262 16435 16469 16487 pgstat.stat
16431 16453 16473 pg_control
$ ls -CF base/
1/ 16569/ 16640/ 16652/
$ ls -CF base/16569
1247 16422 16450
1249 16423 16451
...
$ ls -CF pg_xlog/
000000010000000000000000 archive_status/
$ ls -CF pg_clog/
0000
$ ls -CF share/
conversion_create.sql pg_ident.conf.sample postgresql.conf.sample system_views.sql
information_schema.sql pg_service.conf.sample psqlrc.sample timezone/
locale/ postgres.bki recovery.conf.sample unknown.pltcl
pg_hba.conf.sample postgres.description sql_features.txt
# -----------------------------
# PostgreSQL configuration file
# -----------------------------
#
# This file consists of lines of the form:
#
# name = value
#
# (The ’=’ is optional.) White space may be used. Comments are introduced
# with ’#’ anywhere on a line. The complete list of option names and
# allowed values can be found in the PostgreSQL documentation. The
# commented-out settings shown in this file represent the default values.
# The default values of these variables are driven from the -D command line
# switch or PGDATA environment variable, represented here as ConfigDir.
# data_directory = ’ConfigDir’ # use data in another directory
# hba_file = ’ConfigDir/pg_hba.conf’ # the host-based authentication file
# ident_file = ’ConfigDir/pg_ident.conf’ # the IDENT configuration file
# If external_pid_file is not explicitly set, no extra pid file is written.
# external_pid_file = ’(none)’ # write an extra pid file
# - Memory -
shared_buffers = 1000 # min 16, at least max_connections*2, 8KB each
#work_mem = 1024 # min 64, size in KB
#maintenance_work_mem = 16384 # min 1024, size in KB
#max_stack_depth = 2048 # min 100, size in KB
Postgres Backend
Postgres Backend
Postgres Backend
R
PostgreSQL Shared Buffer Cache
A
Kernel Disk Buffer Cache Page Out
M
Swap
Free
Page In
Kernel
# - Background writer -
# - Settings -
# - Checkpoints -
# - Archiving -
#enable_hashagg = true
#enable_hashjoin = true
#enable_indexscan = true
#enable_mergejoin = true
#enable_nestloop = true
#enable_seqscan = true
#enable_sort = true
#enable_tidscan = true
#geqo = true
#geqo_threshold = 12
#geqo_effort = 5 # range 1-10
#geqo_pool_size = 0 # selects default based on effort
#geqo_generations = 0 # selects default based on effort
#geqo_selection_bias = 2.0 # range 1.5-2.0
# - Where to Log -
#debug_print_parse = false
#debug_print_rewritten = false
#debug_print_plan = false
#debug_pretty_print = false
#log_connections = false
#log_disconnections = false
#log_duration = false
#log_line_prefix = ’’ # e.g. ’<%u%%%d> ’
# %u=user name %d=database name
# %r=remote host and port
# %p=PID %t=timestamp %i=command tag
# %c=session id %l=session line number
# %s=session start timestamp %x=transaction id
# %q=stop here in non-session processes
# %%=’%’
#log_statement = ’none’ # none, mod, ddl, all
#log_hostname = false
Mastering PostgreSQL Administration 33
Runtime Statistics
# - Statistics Monitoring -
#log_parser_stats = false
#log_planner_stats = false
#log_executor_stats = false
#log_statement_stats = false
#stats_start_collector = true
#stats_command_string = false
#stats_block_level = false
#stats_row_level = false
#stats_reset_on_server_start = true
# - Statement Behavior -
#explain_pretty_print = true
#dynamic_library_path = ’$libdir’
#add_missing_from = true
#regex_flavor = advanced # advanced, extended, or basic
#sql_inheritance = true
#default_with_oids = true
Installing
– Compiled Languages
– Scripting Language
– SPI
Connection Pooling
$ ls -CF include/
ecpg_informix.h internal/ pg_config_os.h pgtypes_timestamp.h
ecpgerrno.h libpq/ pgtypes_date.h postgres_ext.h
ecpglib.h libpq-fe.h pgtypes_error.h server/
ecpgtype.h pg_config.h pgtypes_interval.h sql3types.h
informix/ pg_config_manual.h pgtypes_numeric.h sqlca.h
$ ls -CF lib/
ascii_and_mic.so* libecpg_compat.a libpq.so.3.2* utf8_and_gb18030.so*
cyrillic_and_mic.so* libecpg_compat.so@ pgxs/ utf8_and_gbk.so*
euc_cn_and_mic.so* libecpg_compat.so.1@ plperl.so* utf8_and_iso8859.so*
euc_jp_and_sjis.so* libecpg_compat.so.1.1* plpgsql.so* utf8_and_iso8859_1.so*
euc_kr_and_mic.so* libpgport.a pltcl.so* utf8_and_johab.so*
euc_tw_and_big5.so* libpgtypes.a utf8_and_ascii.so* utf8_and_sjis.so*
latin2_and_win1250.so* libpgtypes.so@ utf8_and_big5.so* utf8_and_tcvn.so*
latin_and_mic.so* libpgtypes.so.1@ utf8_and_cyrillic.so* utf8_and_uhc.so*
libecpg.a libpgtypes.so.1.2* utf8_and_euc_cn.so* utf8_and_win1250.so*
libecpg.so@ libpq.a utf8_and_euc_jp.so* utf8_and_win1256.so*
libecpg.so.4@ libpq.so@ utf8_and_euc_kr.so* utf8_and_win874.so*
libecpg.so.4.2* libpq.so.3@ utf8_and_euc_tw.so*
File system-level
– tar, cpio while shutdown
– file system snapshot
– rsync, shutdown, rsync, restart
pg_dump/pg_dumpall
00
00
00
02
09
11
13
WAL
AL AL
W W
2. SELECT pg_start_backup(’label’);
4. SELECT pg_stop_backup();
00
30
40
55
17
17
17
17
WAL
AL AL
W W
1. Stop postmaster
4. Create recovery.conf
#" #" ! ! !
*%# %
)( " %
* #%
)( " %
*%
)( %
* !%
)( %
* !%
)( %
* !%
)( %
*%
)( %*%)( %
* %)( % * %
)( %* %
)( %* %)( %
* %)( * )( '% %& %' %
& %'%
&% ' %& %' %
&% ' %& %'%
&%'%
& %'%& %' %
& %
' %
& %' %
& %
' %& ' &
#" #" ! ! ! %$ % $ % $ %$ % $ %$ % $ % $ %$ % $ %$ % $ %$ $
#" #" ! ! !
0% /% 0% /% 0%/%0% /% 0% /% 0% /% 0%/%0%/%0%/% 0% /% 0% /% 0% /% 0% /% 0 /
6% 5% 6% 5% 6%5%6% 5% 6% 5% 6% 5% 6%5%6%5%6%5% 6% 5% 6% 5% 6% 5% 6% 5% 6 5
4% %3
%
4 %
3
%4%
3%
4 %3 %4 %
3%
4 %3 %4%
3%
4%
3 %4%3 %
4 %
3 %
4 %
3 %4 %
3 %
4 %3 4 3
,%%+ %
, %
+ %
+, %
%, %
+%
, %
+%
, %
+%
+, %,%+%
% +, %
% , %
+ %, %
+ %, %+ %
, %+ , +
.% %
- %
. %
- %
.%-%
. %
-%
. %
-%
. %
-%
.%- %.%-%
. %- %
. %
- %. %
- %. %- %
. %- . -
Pooling: pgpool
VACUUM FULL
ANALYZE
DB oid
Relfilenode Table Block # Block #
Hashed
Shared Memory
A A E A A A E A A A A A
Original Heap C C X C C C X C C C C C
T T P T T T P T T T T T
With Expired I I I I I I I I I I I I
V V R V V V R V V V V V
Rows Identified E E E E E E E E E E E E
A A A A A A A A A A
Move Trailing C C C C C C C C C C
T T T T T T T T T T
Rows Into Expired I I I I I I I I I I
V V V V V V V V V V
Slots E E E E E E E E E E
A A A A A A A A A A
C C C C C C C C C C
T T T T T T T T T T
Truncate File I I I I I I I I I I
V V V V V V V V V V
E E E E E E E E E E
$ ps -Upostgres
PID TT STAT TIME COMMAND
2125 ?? Ss 0:00.26 ./bin/postmaster -i
2142 ?? S 0:00.03 stats buffer process (postmaster)
2143 ?? S 0:00.06 stats collector process (postmaster)
3341 ?? I 0:00.07 postgres test [local] idle (postmaster)
3340 p6 I+ 0:00.03 psql test
$ top
load averages: 0.56, 0.39, 0.36 18:25:58
138 processes: 5 running, 130 sleeping, 3 zombie
CPU states: 50.0% user, 0.0% nice, 0.0% system, 0.0% interrupt, 50.0% idle
Memory: Real: 96M/133M Virt: 535M/1267M Free: 76M
PID USERNAME PRI NICE SIZE RES STATE TIME WCPU CPU COMMAND
23785 postgres 57 0 11M 5336K run/0 0:07 30.75% 30.66% postmaster
23784 postgres 2 0 10M 11M sleep 0:00 2.25% 2.25% psql
stats_command_string = true
$ pg_ctl reload
Unnecessary indexes
Additional indexes
Index usage
TOAST usage
$ vmstat 5
procs memory page disks faults cpu
r b w avm fre flt re pi po fr sr s0 s0 in sy cs us sy id
1 0 0 501820 48520 1234 86 2 0 0 3 5 0 263 2881 599 10 4 86
3 0 0 512796 46812 1422 201 12 0 0 0 3 0 259 6483 827 4 7 88
3 0 0 542260 44356 788 137 6 0 0 0 8 0 286 5698 741 2 5 94
4 0 0 539708 41868 576 65 13 0 0 0 4 0 273 5721 819 16 4 80
4 0 0 547200 32964 454 0 0 0 0 0 5 0 253 5736 948 50 4 46
4 0 0 556140 23884 461 0 0 0 0 0 2 0 249 5917 959 52 3 44
1 0 0 535136 46280 1056 141 25 0 0 0 2 0 261 6417 890 24 6 70
$ iostat 5
tty sd0 sd1 sd2 % cpu
tin tout sps tps msps sps tps msps sps tps msps usr nic sys int idl
7 119 244 11 6.1 0 0 27.3 0 0 18.1 9 1 4 0 86
0 86 20 1 1.4 0 0 0.0 0 0 0.0 2 0 2 0 96
0 82 61 4 3.6 0 0 0.0 0 0 0.0 2 0 2 0 97
0 65 6 0 0.0 0 0 0.0 0 0 0.0 1 0 2 0 97
12 90 31 2 5.4 0 0 0.0 0 0 0.0 4 0 3 0 93
24 173 6 0 4.9 0 0 0.0 0 0 0.0 48 0 3 0 49
0 91 3594 63 4.6 0 0 0.0 0 0 0.0 11 0 4 0 85
$ oid2name
All databases:
---------------------------------
18720 = test1
1 = template1
18719 = template0
18721 = test
18735 = postgres
18736 = cssi
$ cd /usr/local/pgsql/data/base
$ oid2name
All databases:
---------------------------------
16817 = test2
16578 = x
16756 = test
1 = template1
16569 = template0
16818 = test3
16811 = floattest
$ cd 16756
$ ls 1873*
18730 18731 18732 18735 18736 18737 18738 18739
Tablespaces
$ ps -Upostgres
PID TT STAT TIME COMMAND
9874 ?? I 0:00.07 postgres test [local] idle in transaction (postmaster)
9835 ?? S 0:00.05 postgres test [local] UPDATE waiting (postmaster)
10295 ?? S 0:00.05 postgres test [local] DELETE waiting (postmaster)
Upgrading
Migration
pgadmin
pgphpadmin
pgaccess
It may be necessary to create an empty file with the deleted file name so the
object can be deleted, and then the object restored from backup.
Recreate index.
Use REINDEX.
Try reindexing the table. Try identifying the corrupt OID of the row and
transfer the valid rows into another table using SELECT…INTO…WHERE oid !=
###. Use http://sources.redhat.com/rhdb/tools.html to analyze the
internal structure of the table.