PGSQL SQLs
PGSQL SQLs
PGSQL SQLs
#$PGDATA/postgresql.auto.con
CREATE CLUSTER:
initdb -D edbstore
mkdir -p /home/postgres/PostgreSQL/9.6/edbdata
./initdb -D /home/postgres/PostgreSQL/9.6/edbdata -W
view /home/postgres/PostgreSQL/9.6/edbdata/postgresql.conf
pg_ctl -D /home/postgres/PostgreSQL/9.6/edbdata start
psql -D /home/postgres/PostgreSQL/9.6/edbdata
#create user
create user user1 password 'abc123' [superuser];
#create database
creeate database db1;
https://www.postgresql.org/docs/9.2/static/app-psql.h
#session level
#user level
#database level
#cluster level
min_wal_size (80mb)
max_wal_size (1gb)
checkpoint_timeout (5min)
wal_compression (off)
#where to log
log_destination
##valid values are combination of stderr, cs
vlog, syslog, and eventlog
logging_collector
log_directory
log_filename
##postgresql-%Y-%M-%d.log
log_file_mode
log_rotation_age
log_rotation_size
#when to log
client_min_messages (NOTICE)
log_min_messages (WARNING)
log_min_error_stagement (ERROR)
log_min_duration_statement (-1)
##500ms meaning any sql taking more than 500
milli sec is written to the server log
#what to log
log_connections (off)
log_disconnections (off)
log_error_verbosity (default)
##terse or verbose
log_duration (off)
log_line_prefix
log_statement
log_temp_files (-1)
log_checkpoints (off)
#background writer settings (for tuning purpose, down the sleeping time and inc
rease the lru_messages)
bgwriter_default (200ms)
bgwriter_lru_maxpages (100)
bgwriter_lru_multiplier (2.0)
#statement behavior
search_path ("$user",public)
default_tablespace
temp_tablespaces
statement_timeout
#vacuum cost settings
vaccum_cost_delay - 0ms
vaccum_cost_page_hit - 1
vaccum_cost_page_miss - 10
vaccum_cost_page_dirty - 20
vaccum_cost_limit - 200
#autovacuum settings
autovacuum - on
log_autovacuum_min_duration - -1
autovacuum_max_workers - 3
autovacuum_work_mem - -1
Configuration File Includes:
The "include" directive Allows config file to be deivided in separate files.
Put all common settings of dozens of clusters that may be maintained and include
that config file in postgresql.conf file.
config file is : $PGDATA/postgresql.conf
include 'filename'
include_dir 'directory_name'
Exercise-1
==========
Keep a backup of postgresql.sql
Make necessary changes in the server parameter file for the following settings:
- server allows 200 connected users
max_connections=200
- server should reserve 10 connection slots for DBA
superuser_reserved_connections=10
- max time to complete client authentication will be 10sec
authentication_timeout=10
cd $PGDATA
cp -p postgresql.conf postgresql.conf.201610081143
vi postgresql.conf
max_connections=200
superuser_reserved_connections=10
authentication_timeout=10
Exercise-2
==========
Track down certain activities on the database server logging has to be
ed. Go through the server parameters that control loggin and implement
wing:
- save all the error message in a file inside the pg_log folder in the
ata directory ($PGDATA)
cd $PGDATA
mkdir pg_log
psql
alter system set logging_collector=on;
../bin/pg_ctl restart
psql
show logging_collector
ls -l $PGDATA/pg_log
- log all queries which are taking more than 5 seconds to execute, and
e
log_min_duration_statement=500
log_connections=on
implement
the follo
cluster d
their tim
Exercise-3
==========
Perform the recormmended changes and verify.
- Shared buffer to 256MB
- Effective cache for indexes to 512MB
- Maintenance memory to 64MB
- Temporary memory to 8MB
shared_buffers=256MB
effective_cache_size=512MB
maintenance_work_mem=8MB
temp_buffers=8096
postgres=# select name, setting,short_desc,unit from pg_settings where name in (
'shared_buffers','effective_cache_size','maintenance_work_mem','temp_buffers');
name
| setting |
short_desc
| unit
----------------------+---------+-------------------------------------------------------------------+-----effective_cache_size | 524288 | Sets the planner's assumption about the size o
f the disk cache.
| 8kB
maintenance_work_mem | 65536
ance operations.
| kB
shared_buffers
| 16384
by the server.
| 8kB
temp_buffers
| 1024
sed by each session. | 8kB
(4 rows)
postgres=#
(make the above changes in $PGDATA/postgresql.conf and restart as shared_buffers
need db bounce)
$ pg_ctl -D /home/postgres/PostgreSQL/9.6/edbdata/ restart
Exercise-4
==========
autovacuum_max_workers=6 #needs db bounce
autovacuum_vacuum_threshold=100
autovacuum_vacuum_scale_factor=0.3
autovacuum_analyze_threshold=100
autovacuum_vacuum_cost_limit=100
#long running queries
SELECT pid, now() - pg_stat_activity.query_start AS duration, query, state F
ROM pg_stat_activity WHERE now() - pg_stat_activity.query_start > interval '5 mi
nutes';
echo "select procpid,datname,current_query,now() - pg_stat_activity.query_start
as duration from pg_stat_activity where pg_stat_activity.current_query <> ''::t
ext and now() - pg_stat_activity.query_start > interval '5 minutes'" | sudo -u p
ostgres psql
#cancel the query
SELECT pg_cancel_backend(__pid__);
#kill the session
SELECT pg_terminate_backend(__pid__);
#OBJECT HIERARCHY:
DATABASE CLUSTER -> USERS/GROUP (ROLE) : DATABASE : TABLESPACE -> CATALOGS : SCH