Professional Documents
Culture Documents
Abstract POSTGRESQL is an open-source, full-featured relational database. This presentation covers advanced administration topics.
Creative Commons Attribution License http://momjian.us/presentations
Introduction
Installation
Click-Through installers
Ports
Source
Initialization (initdb)
$ initdb The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale C. The default database encoding has accordingly been set to SQL_ASCII. The default text search configuration will be set to "english". creating directory /u/pg/data ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers/max_fsm_pages ... 32MB/204800 creating configuration files ... ok creating template1 database in /u/pg/data/base/1 ... ok initializing pg_authid ... ok initializing dependencies ... ok creating system views ... ok loading system objects descriptions ... ok creating conversions ... ok creating dictionaries ... ok setting privileges on built-in objects ... ok creating information schema ... ok vacuuming database template1 ... ok copying template1 to template0 ... ok copying template1 to postgres ... ok
Mastering PostgreSQL Administration 3
Initialization (continued)
WARNING: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the -A option the next time you run initdb.
Success. You can now start the database server using: postgres -D /u/pg/data or pg_ctl -D /u/pg/data -l logfile start
pg_controldata
pg_control version number: Catalog version number: Database system identifier: Database cluster state: pg_control last modified: Current log file ID: Next log file segment: Latest checkpoint location: Prior checkpoint location: Latest checkpoints REDO location: Latest checkpoints UNDO location: Latest checkpoints TimeLineID: Latest checkpoints NextXID: Latest checkpoints NextOID: Time of latest checkpoint: Database block size: Blocks per segment of large relation: Bytes per WAL segment: Maximum length of identifiers: Maximum number of function arguments: Date/time type storage: Maximum length of locale name: LC_COLLATE: LC_CTYPE: 74 200502281 4766833642862247929 shut down 03/03/05 10:49:18 0 1 0/A34010 0/A2D5C0 0/A34010 0/0 1 545 17233 03/03/05 10:49:18 8192 131072 16777216 64 32 floating-point numbers 128 C C
System Architecture
Main Libpq Postmaster
Postgres
Postgres
Utility Command
e.g. CREATE TABLE, COPY
Rewrite Query
Utilities
Catalog
Storage Managers
Access Methods
Nodes / Lists
Starting Postmaster
database system was shut down at 2009-01-27 10:32:05 EST database system is ready to accept connections autovacuum launcher started
Stopping Postmaster
received smart shutdown request autovacuum launcher shutting down shutting down database system is shut down
Connections
hostssl only
Authentication
trust passwords md5 crypt password remote authentication host ident using pg_ident.conf kerberos gss sspi
10
Authentication (continued)
local socket ident host ident using local identd socket permissions pam ldap reject
11
Access
hostname and network mask database name role name (user or group) lename or list of databases, role
IPv6
12
Pg_hba.conf
# TYPE DATABASE USER CIDR-ADDRESS # "local" is for Unix domain socket connections only local all all # IPv4 local connections: host all all 127.0.0.1/32 # IPv6 local connections: host all all ::1/128
13
Permissions
host connection permissions role permissions create roles create databases table permissions Database creation template1 customization system tables disk space computations
14
Data Directory
$ ls -CF
15
Database Directories
$ ls -CF global/ 1136 1137 1213 1214 1232 1233 1260 1261 1262 2396 2397 2671 2672 2676 2677 2694 2695 2697 2698 2842 2843 2844 2845 2846 2847 pg_auth pg_control pg_database pgstat.stat
$ ls -CF base/ 1/ 11510/ 11511/ 16384/ $ ls -CF base/16384 11429 11431 ...
Mastering PostgreSQL Administration
2617 2618
2696 2699
16
Transaction/WAL Directories
archive_status/
17
Conguration Directories
$ ls -CF share/
18
Conguration Postgresql.conf
19
PostgreSQL.Conf
# # # # # # # # # # # # # # #
----------------------------PostgreSQL configuration file ----------------------------This file consists of lines of the form: name = value (The "=" is optional.) Whitespace may be used. Comments are introduced with "#" anywhere on a line. The complete list of parameter names and allowed values can be found in the PostgreSQL documentation. The commented-out settings shown in this file represent the default values. Re-commenting a setting is NOT sufficient to revert it to the default value; you need to reload the server.
20
PostgreSQL.Conf (Continued)
# # # # # # # # # # # #
This file is read on server startup and when the server receives a SIGHUP signal. If you edit the file on a running system, you have to SIGHUP the server for the changes to take effect, or use "pg_ctl reload". Some parameters, which are marked below, require a server shutdown and restart to take effect. Any parameter can also be given as a command-line option to the server, e.g., "postgres -c log_connections=on". Some paramters can be changed at run time with the "SET" SQL command. Memory units: kB = kilobytes MB = megabytes GB = gigabytes Time units: ms = milliseconds s = seconds min = minutes h = hours d = days
21
# use data in another directory # (change requires restart) #hba_file = ConfigDir/pg_hba.conf # host-based authentication file # (change requires restart) #ident_file = ConfigDir/pg_ident.conf # ident configuration file # (change requires restart) # If external_pid_file is not explicitly set, no extra PID file is written. #external_pid_file = (none) # write an extra PID file # (change requires restart)
#data_directory = ConfigDir
22
#listen_addresses = localhost
# what IP address(es) to listen on; # comma-separated list of addresses; # defaults to localhost, * = all # (change requires restart) #port = 5432 # (change requires restart) max_connections = 100 # (change requires restart) # Note: Increasing max_connections costs ~400 bytes of shared memory per # connection slot, plus lock space (see max_locks_per_transaction). You might # also need to raise shared_buffers to support more connections. #superuser_reserved_connections = 3 # (change requires restart) #unix_socket_directory = # (change requires restart) #unix_socket_group = # (change requires restart) #unix_socket_permissions = 0777 # begin with 0 to use octal notation # (change requires restart) #bonjour_name = # defaults to the computer name # (change requires restart)
23
#authentication_timeout = 1min # 1s-600s #ssl = off # (change requires restart) #ssl_ciphers = ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH # allowed SSL ciphers # (change requires restart) #password_encryption = on #db_user_namespace = off # Kerberos and GSSAPI #krb_server_keyfile = # (change requires restart) #krb_srvname = postgres # (change requires restart, Kerberos only) #krb_server_hostname = # empty string matches any keytab entry # (change requires restart, Kerberos only) #krb_caseins_users = off # (change requires restart) #krb_realm = # (change requires restart)
24
TCP/IP Control
# # # # # #
TCP_KEEPIDLE, in seconds; 0 selects the system default TCP_KEEPINTVL, in seconds; 0 selects the system default TCP_KEEPCNT; 0 selects the system default
25
Memory Usage
# min 128kB or max_connections*16kB # (change requires restart) #temp_buffers = 8MB # min 800kB #max_prepared_transactions = 5 # can be 0 or more # (change requires restart) # Note: Increasing max_prepared_transactions costs ~600 bytes of shared memory # per transaction slot, plus lock space (see max_locks_per_transaction). #work_mem = 1MB # min 64kB #maintenance_work_mem = 16MB # min 1MB #max_stack_depth = 2MB # min 100kB
shared_buffers = 32MB
26
Postgres Backend Postgres Backend R A M Postgres Backend PostgreSQL Shared Buffer Cache Kernel Disk Buffer Cache Free Page In Kernel Page Out Swap
27
# min max_fsm_relations*16, 6 bytes each # (change requires restart) # min 100, ~70 bytes each # (change requires restart)
28
# # # #
# 1-10000 credits
# - Background Writer #bgwriter_delay = 200ms # 10-10000ms between rounds #bgwriter_lru_maxpages = 100 # 0-1000 max buffers written/round #bgwriter_lru_multiplier = 2.0 # 010.0 multipler on buffers scanned/round
29
30
Disk Blocks
31
# in logfile segments, min 1, 16MB each # range 30s-1h # checkpoint target duration, 0.0 - 1.0 # 0 is off
# # # # #
allows archiving to be done (change requires restart) command to use to archive a logfile segment force a logfile segment switch after this time; 0 is off
32
Query Tuning
# - Planner Method Configuration #enable_bitmapscan = on #enable_hashagg = on #enable_hashjoin = on #enable_indexscan = on #enable_mergejoin = on #enable_nestloop = on #enable_seqscan = on #enable_sort = on #enable_tidscan = on # - Planner Cost Constants #seq_page_cost = 1.0 #random_page_cost = 4.0 #cpu_tuple_cost = 0.01 #cpu_index_tuple_cost = 0.005 #cpu_operator_cost = 0.0025 #effective_cache_size = 128MB
Mastering PostgreSQL Administration
# # # # #
measured on an arbitrary scale same scale as above same scale as above same scale as above same scale as above
33
#geqo = on #geqo_threshold = 12 #geqo_effort = 5 #geqo_pool_size = 0 #geqo_generations = 0 #geqo_selection_bias = 2.0 # - Other Planner Options #default_statistics_target = 10 #constraint_exclusion = off #from_collapse_limit = 8 #join_collapse_limit = 8
# range 1-10 # selects default based on effort # selects default based on effort # range 1.5-2.0
# range 1-1000
34
Where To Log
#log_destination = stderr
# # # #
Valid values are combinations of stderr, csvlog, syslog and eventlog, depending on platform. csvlog requires logging_collector to be on.
# This is used when logging to stderr: #logging_collector = off # Enable capturing of stderr and csvlog # into log files. Required to be on for # csvlogs. # (change requires restart) # These are only used if logging_collector is on: #log_directory = pg_log # directory where log files are written, # can be absolute or relative to PGDATA #log_filename = postgresql-%Y-%m-%d_%H%M%S.log # log file name pattern, # can include strftime() escapes
35
#log_truncate_on_rotation = off
# # # # # # # # # # # #
If on, an existing log file of the same name as the new log file will be truncated rather than appended to. But such truncation only occurs on time-driven rotation, not on restarts or size-driven rotation. Default is off, meaning append to existing files in all cases. Automatic rotation of logfiles will happen after that time. 0 to disable. Automatic rotation of logfiles will happen after that much log output.
# 0 to disable. # These are relevant when logging to syslog: #syslog_facility = LOCAL0 #syslog_ident = postgres
Mastering PostgreSQL Administration 36
When to Log
#client_min_messages = notice # values in order of decreasing detail: # debug5 # debug4 # debug3 # debug2 # debug1 # log # notice # warning # error # values in order of decreasing detail: # debug5 # debug4 # debug3 # debug2 # debug1 # info # notice # warning # error # log # fatal # panic
37
#log_min_messages = notice
#log_min_duration_statement = -1
#silent_mode = off
38
What to Log
#debug_print_parse = off #debug_print_rewritten = off #debug_print_plan = off #debug_pretty_print = off #log_checkpoints = off #log_connections = off #log_disconnections = off #log_duration = off #log_hostname = off
39
#log_line_prefix =
# special values: # %u = user name # %d = database name # %r = remote host and port # %h = remote host # %p = process ID # %t = timestamp without milliseconds # %m = timestamp with milliseconds # %i = command tag # %c = session ID # %l = session line number # %s = session start timestamp # %v = virtual transaction ID # %x = transaction ID (0 if none) # %q = stop here in non-session # processes # %% = % # e.g. <%u%%%d>
40
#log_timezone = unknown
# # # # # # #
log lock waits >= deadlock_timeout none, ddl, mod, all log temporary files equal or larger than specified size; -1 disables, 0 logs all temp files actually, defaults to TZ environment setting
41
Runtime Statistics
# - Query/Index Statistics Collector #track_activities = on #track_counts = on #update_process_title = on # - Statistics Monitoring #log_parser_stats = off #log_planner_stats = off #log_executor_stats = off #log_statement_stats = off
42
Autovacuum
# Enable autovacuum subprocess? on # requires track_counts to also be on. #log_autovacuum_min_duration = -1 # -1 disables, 0 logs all actions and # their durations, > 0 logs only # actions running at least that time. #autovacuum_max_workers = 3 # max number of autovacuum subprocesses #autovacuum_naptime = 1min # time between autovacuum runs #autovacuum_vacuum_threshold = 50 # min number of row updates before # vacuum #autovacuum_analyze_threshold = 50 # min number of row updates before # analyze #autovacuum_vacuum_scale_factor = 0.2 # fraction of table size before vacuum #autovacuum_analyze_scale_factor = 0.1 # fraction of table size before analyze #autovacuum_freeze_max_age = 200000000 # maximum XID age before forced vacuum # (change requires restart) #autovacuum_vacuum_cost_delay = 20 # default vacuum cost delay for # autovacuum, -1 means use # vacuum_cost_delay #autovacuum_vacuum_cost_limit = -1 # default vacuum cost limit for # autovacuum, -1 means use # vacuum_cost_limit #autovacuum = on
43
Statement Behavior
# # # #
schema names a tablespace name, uses the default a list of tablespace names, uses only default tablespace
#check_function_bodies = on #default_transaction_isolation = read committed #default_transaction_read_only = off #session_replication_role = origin #statement_timeout = 0 # 0 is disabled #vacuum_freeze_min_age = 100000000 #xmlbinary = base64 #xmloption = content
44
46
Other Defaults
47
Lock Management
# min 10 # (change requires restart) # Note: Each lock table slot uses ~270 bytes of shared memory, and there are # max_locks_per_transaction * (max_connections + max_prepared_transactions) # lock table slots.
#deadlock_timeout = 1s #max_locks_per_transaction = 64
48
Version/Platform Compatibility
# - Previous PostgreSQL Versions #add_missing_from = off #array_nulls = on #backslash_quote = safe_encoding #default_with_oids = off #escape_string_warning = on #regex_flavor = advanced #sql_inheritance = on #standard_conforming_strings = off #synchronize_seqscans = on # - Other Platforms and Clients #transform_null_equals = off
49
Customization
#custom_variable_classes =
50
Interfaces
Installing Compiled Languages (C, ecpg) Scripting Language (Perl, Python) SPI Connection Pooling
51
Include Files
$ ls -CF include/
52
Library Files
$ ls -CF lib/ ascii_and_mic.so* cyrillic_and_mic.so* dict_snowball.so* euc_cn_and_mic.so* euc_jis_2004_and_shift_ euc_jp_and_sjis.so* euc_kr_and_mic.so* euc_tw_and_big5.so* latin2_and_win1250.so* latin_and_mic.so* libecpg.a libecpg.so@ libecpg.so.6@ libecpg.so.6.0* libecpg_compat.a libecpg_compat.so@
Mastering PostgreSQL Administration
libecpg_compat.so.3@ libecpg_compat.so.3.0* libpgport.a libpgtypes.a libpgtypes.so@ libpgtypes.so.3@ libpgtypes.so.3.0* libpq.a libpq.so@ libpq.so.5@ libpq.so.5.1* pgxs/ plperl.so* plpgsql.so* pltcl.so* utf8_and_ascii.so*
utf8_and_big5.so* utf8_and_cyrillic.so* utf8_and_euc_cn.so* utf8_and_euc_jis_2004.s utf8_and_euc_jp.so* utf8_and_euc_kr.so* utf8_and_euc_tw.so* utf8_and_gb18030.so* utf8_and_gbk.so* utf8_and_iso8859.so* utf8_and_iso8859_1.so* utf8_and_johab.so* utf8_and_shift_jis_2004 utf8_and_sjis.so* utf8_and_uhc.so* utf8_and_win.so*
53
Maintenance
54
Backup
File system-level tar, cpio while shutdown le system snapshot rsync, shutdown, rsync, restart pg_dump/pg_dumpall restore/pg_restore with custom format
55
00
00
00
02
09
11
WAL
W
AL
AL
Continuous Backup
56
13
00
1. archive_command = cp %p /mnt/server/pgsql/%f 2. SELECT pg_start_backup(label); 3. Perform le system-level backup (can be inconsistent) 4. SELECT pg_stop_backup();
57
PITR Recovery
00
30
40
17
17
17
WAL
W
AL
AL
Continuous Backup
58
17
55
1. Stop postmaster 2. Restore le system-level backup 3. Make adjustments as outlined in the documentation 4. Create recovery.conf 5. Add restore_command = cp /mnt/server/pgsql/%f %p 6. Start the postmaster
59
)( )( & '& '& '& $ %$ %$ %$ %$ %$ 97 9 )7 97 9 '7 9 '7 9 '7 97 97 97 9% 7 9% 7 9% 7 9% 7 8( 7 8( 7 8&7 8&7 8&7 8&7 87 87 8$ 7 8$ 7 8$ 7 8$ 7 8$ 9% 8$ )7 6 ( )7 6( 7 6 & '7 6 & '7 6 & '7 6& 7 6 7 6 7 6$ % 7 6$ % 7 6$ % 7 6$ % 7 6$ % 6$ )7 )( )( & '& '& '& $ %$ %$ %$ %$ %$ $ %$ %$ %$ %$ %$
#" #" ! ! ! H7 G7H7 G7H7 G7H7 G7 G7 G7 G7 G7 H7 H7 H7 H7 H G # G7 # G7 " H7 " H7 ! G7H7 ! G7H7 ! G7H7 T7 S7 S7 S7 S7 S7T7 S7T7 S7T7 S7 T7 T7 T7 T7 T S # S7 # S7 " T7 " T7 ! S7T7 ! S7T7 ! S7T7 #" #" ! ! !
60
Other Solutions
61
Data Maintenance
VACUUM
VACUUM FULL
ANALYZE
62
Vacuum
Free Space Map
Block #
Block #
Block #
Block #
Block #
Table Hashed
Block #
Block #
Block #
Shared Memory
63
Vacuum Full
Original Heap With Expired Rows Identified
A C T I V E A C T I V E E X P I R E A C T I V E A C T I V E A C T I V E E X P I R E A C T I V E A C T I V E A C T I V E A C T I V E A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
Truncate File
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
A C T I V E
64
Checkpoints
Write all dirty shared buffers Sync all dirty kernel buffers Recycle WAL les Check for server messages indicating too-frequent checkpoints If so, increase checkpoint_segments
65
Automating Tasks
66
67
ps
TIME COMMAND
?? ?? ?? ?? p6
Ss S S I I+
./bin/postmaster -i stats buffer process (postmaster) stats collector process (postmaster) postgres test [local] idle (postmaster) psql test
68
top
$ 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 23785 postgres 57 0 11M 5336K run/0 23784 postgres 2 0 10M 11M sleep TIME WCPU CPU COMMAND 0:07 30.75% 30.66% postmaster 0:00 2.25% 2.25% psql
69
pgmonitor
70
Query Monitoring
-[ RECORD 1 ]-+-------------------------------datid | 16384 datname | test procpid | 2373 usesysid | 10 usename | postgres current_query | select * from pg_stat_activity; waiting | f xact_start | 2009-01-27 12:12:59.917695-05 query_start | 2009-01-27 12:12:59.917695-05 backend_start | 2009-01-27 12:12:33.422001-05 client_addr | client_port | -1
Mastering PostgreSQL Administration 71
pgmonitor
72
Access Statistics
pg_stat_all_indexes pg_stat_all_tables pg_stat_database pg_stat_sys_indexes pg_stat_sys_tables pg_stat_user_indexes pg_stat_user_tables pg_statio_all_indexes pg_statio_all_sequences pg_statio_all_tables pg_statio_sys_indexes pg_statio_sys_sequences pg_statio_sys_tables pg_statio_user_indexes pg_statio_user_sequences pg_statio_user_tables
Mastering PostgreSQL Administration
| | | | | | | | | | | | | | | |
view view view view view view view view view view view view view view view view
| | | | | | | | | | | | | | | |
postgres postgres postgres postgres postgres postgres postgres postgres postgres postgres postgres postgres postgres postgres postgres postgres
73
Database Statistics
test=> SELECT * FROM pg_stat_database; ... -[ RECORD 4 ]-+---------datid | 16384 datname | test numbackends | 1 xact_commit | 188 xact_rollback | 0 blks_read | 95 blks_hit | 11832 tup_returned | 64389 tup_fetched | 2938 tup_inserted | 0 tup_updated | 0 tup_deleted | 0
Mastering PostgreSQL Administration 74
Table Activity
test=> SELECT * FROM pg_stat_all_tables; ... -[ RECORD 10 ]---+-----------------------relid | 2616 schemaname | pg_catalog relname | pg_opclass seq_scan | 2 seq_tup_read | 2 idx_scan | 99 idx_tup_fetch | 99 n_tup_ins | 0 n_tup_upd | 0 n_tup_del | 0 n_tup_hot_upd | 0 n_live_tup | 0
75
| 0 | | | |
76
est=> SELECT * FROM pg_statio_all_tables; -[ RECORD 50 ]--+-----------------------relid | 2602 schemaname | pg_catalog relname | pg_amop heap_blks_read | 3 heap_blks_hit | 114 idx_blks_read | 5 idx_blks_hit | 303 toast_blks_read | toast_blks_hit | tidx_blks_read | tidx_blks_hit |
t
77
Analyzing Activity
usage
78
CPU
$ vmstat 5 procs memory r b w avm fre 1 0 0 501820 48520 3 0 0 512796 46812 3 0 0 542260 44356 4 0 0 539708 41868 4 0 0 547200 32964 4 0 0 556140 23884 1 0 0 535136 46280
page flt re 1234 86 1422 201 788 137 576 65 454 0 461 0 1056 141
pi po fr sr 2 0 0 3 12 0 0 0 6 0 0 0 13 0 0 0 0 0 0 0 0 0 0 0 25 0 0 0
disks s0 s0 5 0 3 0 8 0 4 0 5 0 2 0 2 0
faults in sy cs 263 2881 599 259 6483 827 286 5698 741 273 5721 819 253 5736 948 249 5917 959 261 6417 890
cpu us sy 10 4 4 7 2 5 16 4 50 4 52 3 24 6
id 86 88 94 80 46 44 70
79
I/O
$ iostat 5 tty sd0 tin tout sps tps msps 7 119 244 11 6.1 0 86 20 1 1.4 0 82 61 4 3.6 0 65 6 0 0.0 12 90 31 2 5.4 24 173 6 0 4.9 0 91 3594 63 4.6
sps tps 0 0 0 0 0 0 0 0 0 0 0 0 0 0
sps tps 0 0 0 0 0 0 0 0 0 0 0 0 0 0
sd2 % msps usr nic sys int 18.1 9 1 4 0 0.0 2 0 2 0 0.0 2 0 2 0 0.0 1 0 2 0 0.0 4 0 3 0 0.0 48 0 3 0 0.0 11 0 4 0
cpu idl 86 96 97 97 93 49 85
80
Disk Usage
play=# SELECT relfilenode, relpages * 8 AS kilobytes play-# FROM pg_class play-# WHERE relname = customer; relfilenode | kilobytes -------------+---------16806 | 480 (1 row)
81
TOAST Usage
SELECT relname, relpages * 8 AS kilobytes FROM pg_class WHERE relname = pg_toast_16806 OR relname = pg_toast_16806_index ORDER BY relname; relname | kilobytes ----------------------+---------pg_toast_16806 | 0 pg_toast_16806_index | 1
82
Index Usage
SELECT c2.relname, c2.relpages * 8 AS kilobytes FROM pg_class c, pg_class c2, pg_index i WHERE c.relname = customer AND c.oid = i.indrelid AND c2.oid = i.indexrelid ORDER BY c2.relname; relname | kilobytes ----------------------+---------customer_id_indexdex | 26
83
Largest Tables
play=# SELECT relname, relpages * 8 play-# FROM pg_class play-# ORDER BY relpages DESC; relname | kilobytes ----------------------+---------bigtable | 3290 customer | 3144
84
$ oid2name All databases: --------------------------------18720 = test1 1 = template1 18719 = template0 18721 = test 18735 = postgres 18736 = cssi
85
$ 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
86
$ oid2name -d test -o 18737 Tablename of oid 18737 from database "test": --------------------------------18737 = ips $ oid2name -d test -t ips Oid of table ips from database "test": --------------------------------18737 = ips $ # show disk space for every db object $ du * | while read SIZE RELFILENODE > do > echo "$SIZE oid2name -q -d test -o $RELFILENODE" > done 24 18737 = ips 36 18722 = cities ...
Mastering PostgreSQL Administration 87
$ # same as above, but sort by largest first $ du * | while read SIZE OID > do > echo "$SIZE oid2name -q -d test -o $OID" > done | > sort -rn 2048 19324 = bigtable 1950 23903 = customers ... $ # show disk usage per database $ cd /usr/local/pgsql/data/base $ du -s * | > while read SIZE OID > do > echo "$SIZE oid2name -q | grep ^$OID " > done | > sort -rn 2256 18721 = test 2135 18735 = postgres
88
Disk Balancing
89
Per-Database Tablespaces
DB1
DB2
DB3
DB4
Disk 1
Mastering PostgreSQL Administration
Disk 2
Disk 3
90
Per-Object Tablespaces
tab1
tab2
index constraint
Disk 1
Mastering PostgreSQL Administration
Disk 2
Disk 3
91
Analyzing Locking
$ ps -Upostgres PID TT STAT 9874 ?? I 9835 ?? S 10295 ?? S TIME 0:00.07 0:00.05 0:00.05 COMMAND postgres test [local] idle in transaction (postmaster) postgres test [local] UPDATE waiting (postmaster) postgres test [local] DELETE waiting (postmaster)
test=> SELECT * FROM pg_locks; relation | database | transaction | pid | mode | granted ----------+----------+-------------+------+------------------+--------17143 | 17142 | | 9173 | AccessShareLock | t 17143 | 17142 | | 9173 | RowExclusiveLock | t | | 472 | 9380 | ExclusiveLock | t | | 468 | 9338 | ShareLock | f | | 470 | 9338 | ExclusiveLock | t 16759 | 17142 | | 9380 | AccessShareLock | t 17143 | 17142 | | 9338 | AccessShareLock | t 17143 | 17142 | | 9338 | RowExclusiveLock | t | | 468 | 9173 | ExclusiveLock | t (9 rows)
Mastering PostgreSQL Administration 92
Miscellaneous Tasks
93
Administration Tools
pgadmin phppgadmin
94
Recovery
95
96
97
98
Nothing Required. Transactions in progress are rolled back. Partial page writes are repaired.
99
Disk Failure
100
Accidental DELETE
Recover table from previous backup, perhaps using pg_restore. It is possible to modify the backend code to make deleted tuples visible, dump out the deleted table and restore the original code. All tuples in the table since the previous vacuum will be visible. It is possible to restrict that so only tuples deleted by a specic transaction are visible.
101
See pg_resetxlog. Review recent transactions and identify any damage, including partially committed transactions.
102
File Deletion
It may be necessary to create an empty le with the deleted le name so the object can be deleted, and then the object restored from backup.
103
104
Recreate index.
105
106
Non-Starting Installation
Restart problems are usually caused by write-ahead log problems. See pg_resetxlog. Review recent transactions and identify any damage, including partially committed transactions.
107
Index Corruption
Use REINDEX.
108
Table Corruption
Try reindexing the table. Try identifying the corrupt OID of the row and transfer the valid rows into another table using SELECTINTOWHERE oid != ###. Use http://sources.redhat.com/rhdb/tools.html to analyze the internal structure of the table.
109
Conclusion
110