Generalities
One "root" directory per DB.
Find physical file names from the pg_class table.
Initialize files with the initdb script. |
Config files
pg_hba.conf |
Access control |
pg_ident.conf |
Identity manager configuration |
postgresql.conf |
Engine configuration |
postgresql.auto.conf |
Online engine configuration |
postmaster.pid |
Instance lock file |
postmaster.opts |
Last startup options |
By default in /var/lib/pgsql/<version>/data
Tools
initdb |
Create file/dir structure (aka cluster) for instance |
pg_ctl |
Control instance state |
pg_controldata |
View config |
pg_isready |
Check if instance is up w/o opening a full connection |
pg_resetwal |
DANGEROUS Wipe transaction logs |
createdb, dropdb |
Create/delete a DB |
By default in /usr/pgsql-<version>/bin (on redhat)
Create new instance
initdb \
-A auth_method \
-D data_directory \
-E encoding \
-X tlog_directory \
--locale=locale \
[-k]
# Start from service
systemctl enable postgres
systemctl start postgres
# Start standalone
/usr/pgsql-16/bin/pg_ctl -D data_directory start
# Connect as postgres w/o passwd
sudo -u postgres psql -w
|
Check instance state with "systemctl postgres-16 status" or pg_isready
Contribs
pg_freespacemap |
View free space maps |
pg_prewarm |
Preload caches from last run at boot |
pg_stat_statements |
Track SQL execution statistics |
auto_explain |
Trace costly statements automatically |
pgstattuple |
Get table stats (live/dead rows, volume of data) |
View available extensions with pg_available_extensions view or \dx[+ extension]
Install modules via postgres.conf: shared_preload_libraries (permanent) or LOAD statement (volatile).
Users and permissions
Users and groups are both roles. User = can login, group = cannot login.
Roles can be granted to other roles.
Roles can impersonate roles granted to them.
GRANT adminrole to userrole
means userrole can SET ROLE adminrole
and impersonate. |
Access checklist
Can connect according to pg_hba |
Has LOGIN permission |
Has CONNECT on DB |
Has USAGE on schema/namespace |
Has <operation> on table/column of queryable |
GRANT/REVOKE: object permissions
Everyone has CONNECT and TEMP permissions on DBs through public
group.
DROP and ALTER belong to the owner role only.
Change default permissions with:
ALTER DEFAULT PRIVILEGES
FOR ROLE object_creator
permission_stmt` |
See psql section to view permissions.
pg_hba: authentication management
TYPE |
|
DATABASE |
all | replication | db_name
|
USER |
role
for specific role, +role
for group |
ADDRESS |
Origin address |
METHOD |
Accepted auth method, see man page |
Space-separated file, one rule per line with above fields.
Configuration reload required to apply.
|
|
"Special" data files
pg_wal |
Write-ahead log |
pg_xact |
Commit data |
pg_commit_ts |
Commit timestamps |
pg_multixacts |
States of multiple transactions |
pg_serial |
States of serializable transactions |
pg_twophase |
States of prepared transactions |
pg_dynshmem |
Dynamic shared memory |
pg_logical |
Logical replication |
pg_notify |
Listen/Notify states |
pg_repslot |
Replication slots |
pg_snapshots |
Exported snapshots |
"Private" files, should not be edited in normal operation
Data files
"Normal" files |
Tables, indexes |
TOAST |
Oversized object storage |
FSM |
Free space map |
VM |
Visibility map |
Filesize is max 1 GB except TOAST files.
Files over 1 GB are split into .1, .2, .3, etc.
WAL
Filename split into timeline, logical file, physical file (segment).
Logical filesize max 4 GB.
LSN stored is only determinable with 16 MB physical WAL files (offset at end of filename).
pg_current_wal_lsn()
pg_walfile_name(lsn) to get WAL file containing LSN |
Storage best practices
Recommended: ext4 first, xfs second
zfs possible but not well-known yet
ext4 recommended parameters: noatime, data=writeback
On Linux, consider scheduler config changes depending on hardware
RAID 10 preferable to raid 5 for controller load reasons |
Better hardware badly configured usually outperforms well-configured worse hardware.
Performance tracking
Use extension pg_stat_statements to track execution times. ⚠️Requires restart to install⚠️
Note parameters pg_stat_statements.track and pg_stat_statements.track_planning (<- costly)
Query pg_stat_statements to view results. |
External utilities
pg_activity |
|
dbeaver |
GUI tool for DDL visualization |
pgadmin |
Web-based tool (slow) |
pgloader |
Data Migration tool from other psql instance, CSV or other DBMS |
pgHero |
Performance dashboard for Postgres |
pgTune |
Performance configuration tuning tool |
|
|
psql things
Connect from system shell |
psql -h hostname -p port -U role -d DB |
Connect from psql shell |
\c db user host port |
Execute sql script |
\i file |
Execute shell command |
\! command |
Edit psql options |
\set [variable=value] |
Execute command every 3 seconds 2 times |
\watch i=2 c=3 |
Get help on SQL command |
\h command |
Edit command in external editor and execute |
\e |
Make wide tables readable |
\x on|auto |
Profile script |
Located in ~/.psqlrc |
Re-run last command |
\g (\gx to output as \x on) |
Can supply connection string instead of connection arguments
List DB objects with psql
\l |
Databases |
\dt |
Tables |
\dn |
Namespaces (aka schemas) |
\d name |
Describe queryable or index |
\d |
List everything |
\di |
Indexes |
\ds |
Sequences |
\dp |
Permissions |
\du |
Roles (users/groups) |
\dv |
Views |
\dx |
Extensions |
\dn |
Namespaces (schemas) |
See psql manual
All commands can take a filter pattern as argument.
See "search path" above.
Search path
Alter for this session |
SET search_path TO my_db; |
Persist for this DB |
ALTER DATABASE SET search_path to ... ; ALTER DATABASE SET search_path FROM CURRENT; |
Persist for this instance |
postgresql.conf |
"List" psql commands ( \d
) only show what is in the search path by default.
postgresql.conf
listen_addresses |
interfaces to listen on (ipv4 and v6) |
port |
TCP port |
max_connections |
Max simultaneous sessions |
ssl |
Enable encrypted transport |
shared_buffers |
Instance-wide shared memory (see oracle sga) |
temp_buffers |
Dedicated memory per session (see oracle pga) |
work_mem |
Dedicated memory per query |
maintenance_work_mem |
Memory reserved for maintenance ops (vacuum, index creation, etc) |
autovacuum_work_mem |
Memory dedicated to autovacuum processes |
postgresql.conf: planner/optimizer
seq_page_cost |
Sequential page read cost |
random_page_cost |
Random page read cost |
cpu_operator_cost |
Operator call cost |
effective_cache_size |
Estimated system cache size |
default_statistics_target |
"Target quality" of the optimizer: higher values takes longer to calculate plan but yields better results |
from_collapse_limit |
Merge nested FROM (to avoid "reorder experimentation" from the planner) if sub-FROM would yield less than this setting |
join_collapse_limit |
See from_collapse_limit but with JOINs instead of FROMs |
postgres.conf: autovacuum
autovacuum |
Enable/disable autovacuum |
autovacuum_max_workers |
Max parallel autovac jobs |
autovacuum_naptime |
Max delay before autovac |
autovacuum_vacuum_cost_limit |
Accumulated I/O cost of autovac before pausing |
autovacuum_vacuum_cost_delay |
Duration of autovacuum pause (see previous) |
autovacuum_vacuum_threshold |
Insert/update threshold to trigger autovac (absolute) |
autovacuum_vacuum_scale_factor |
Insert/update threshold to trigger autovac (relative) |
Autovacuum keeps count of I/O (as "cost"). Once cost has reached autovacuum_vacuum_cost_limit, it pauses for autovacuum_vacuum_cost_delay.
Vacuum threshold is the sum of vacuum_threshold and vacuum_scale_factor.
Autovacuum trigger thresholds have separate, identical parameters for inserts.
postgres.conf: WAL
wal_level |
Transaction log level: minimal (no log), replica, logical |
fsync |
Enable write syncs |
synchronous_commit |
Wait for writes to be committed to disk before sending commit to client |
wal_sync_method |
Method used to invalidate WAL caches |
full_page_writes |
Write complete pages to WAL |
wal_buffers |
Size of WAL buffers in RAM |
checkpoint_timeout |
Max delay between checkpoints |
checkpoint_completion_target |
Spread checkpoint over n times checkpoint_timeout |
min/max_wal_size |
Soft limits (targets) for WAL size |
postgres.conf: replication
archive_mode |
archive_command |
max_wal_senders |
Max concurrent replication threads |
wal_keep_size |
Size of preserved WAL segments on the primary |
max_replication_slots |
Number of replication slots (max concurrent standby connections) |
hot_standby |
When in standby (in recovery), accept connections |
hot_standby_feedback |
When in standby, give status feedback to primary |
postgres.conf: logging
log_filename |
Log filename pattern |
log_min_messages |
Minimum event severity to be logged |
log_min_duration_statement |
Log statements longer than value |
log_checkpoint |
Log checkpoints |
log_line_prefix |
Prefix for all log lines |
log_lock_waits |
Log lock waits longer than value |
log_statement |
Log SQL statements (DML only, DDL only, ...) |
log_temp_files |
Log temp files larger than value |
postgres.conf
Main postgresql config file.
Settings with context = sighup -> send SIGHUP to postgres to apply.
Settings with context = postmaster -> restart instance to apply.
Settings with context = user can be overriden for own user
Can be hot-swapped with SET or ALTER SYSTEM/DATABASE/SESSION.
Per-database/per-session settings are saved in pg_db_role_setting and pg_class.
ALTER SYSTEM applies changes to postgres.auto.conf which is read last/has priority. ALTER SYSTEM is not necessarily applied immediately depending on context of setting. Check pg_settings for the actual running config (and note pending_restart).
See systemctl reload
or pg_reload_conf()
. |
|