SlideShare una empresa de Scribd logo
1 de 14
Creating and
Managing
Databases
Part-2
• Databases and Schemas
• Tablespaces
• Data Type
• Exploring Databases
• Locating the database server's message log
• Locating the database's system identifier
• Listing databases on this database server
• How much disk space does a table use?
• Which are my biggest tables?
• How many rows are there in a table?
• Quickly estimating the number of rows in a table
• Understanding object dependencies
Database Structure
Here are some things that are important to know when attempting to understand the database structure of PostgreSQL.
Items related to the database
 PostgreSQL consists of several databases. This is called a database cluster.
 When initdb () is executed, template0 , template1 , and postgres databases are created.
 The template0 and template1 databases are template databases for user database creation and contain the system catalog tables.
 The list of tables in the template0 and template1 databases is the same immediately after initdb (). However, the template1 database can create objects that the
user needs.
 The user database is created by cloning the template1 database.
Items related to the tablespace
 The pg_default and pg_global tablespaces are created immediately after initdb().
 If you do not specify a tablespace at the time of table creation, it is stored in the pg_dafault tablespace.
 Tables managed at the database cluster level are stored in the pg_global tablespace.
 The physical location of the pg_default tablespace is $PGDATAbase.
 The physical location of the pg_global tablespace is $PGDATAglobal.
 One tablespace can be used by multiple databases. At this time, a database-specific subdirectory is created in the table space directory.
Database Structure
Items related to the table
 There are three files per table.
 One is a file for storing table data. The file name is the OID of the table.
 One is a file to manage table free space. The file name is OID_fsm .
 One is a file for managing the visibility of the table block. The file name is OID_vm .
 The index does not have a _vm file. That is, OID and OID_fsm are composed of two files.
Other Things to Remember...
 The file name at the time of table and index creation is OID, and OID and pg_class.relfilenode are the same at this point. However, when a
rewrite operation ( Truncate , CLUSTER , Vacuum Full , REINDEX , etc.) is performed, the relfilenode value of the affected object is changed,
and the file name is also changed to the relfilenode value. You can easily check the file location and name by using pg_relation_filepath ('<
object name >'). template0, template1, postgres database
PostgreSQL Databases Creation
Create database Using PostgreSQL utility
Connect To the database :
• postgres@r1 ~]$ cd /opt/PostgreSQL/11/bin/
• [postgres@r1 bin]$./psql -p 5432 -d postgres -U postgres
• Password for user postgres:
• postgres=#
postgres=# CREATE DATABASE teachlax;
CREATE DATABASE
postgres=# create user u1 with password 'u1’;
CREATE ROLE
postgres=# CREATE DATABASE sales OWNER u1 TABLESPACE tbs1;
CREATE DATABASE
postgres=# l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
sales | u1 | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
teachlax | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/postgres
(5 rows)
postgres=# drop database teachlax;
DROP DATABASE
postgres=# l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
sales | u1 | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/postgres
(4 rows)
PostgreSQL Databases
change database owner from “U1” to “U2”:
postgres=# create user u2 with password 'u2';
CREATE ROLE
postgres=# alter database sales owner to u2;
ALTER DATABASE
Set the Default tablespace :
postgres=# alter database account set
default_tablespace=tbs1;
ALTER DATABASE
postgres=# db
List of tablespaces
Name | Owner | Location
------------+----------+----------------
pg_default | postgres |
pg_global | postgres |
tbs1 | postgres | /home/postgres
(3 rows)
Listing database using sql query:
postgres=# select datname from pg_database;
datname
-----------
template1
template0
postgres
sales
musicdb
account
(6 rows)
PostgreSQL Databases Creation
create a database coping template0 in sql:
postgres=# create database temp0copy template template0;
CREATE DATABASE
create a database coping template0 in shell:
[postgres@r1 ~]$ createdb -T template0 temp0copy2
Password:
[postgres@r1 ~]$
Listing database using command line:
[postgres@r1 ~]$ psql -l
Password:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+----------+----------+-------------+-------------+-----------------------
account | u1 | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
musicdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
sales | u2 | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
temp0copy | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
temp0copy2 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
……
(8 rows)
PostgreSQL Tables
Find Out Essential Settings in a Postgres DB
• show config_file;
• show effective_cache_size;
• show shared_buffers;
• show all;
How to calculate postgreSQL database size in disk ?
• SELECT pg_database_size('postgres');
• SELECT pg_size_pretty(pg_database_size('postgres'));
How to calculate postgreSQL table size in disk ?
• SELECT pg_size_pretty(pg_total_relation_size('k1'));
• SELECT pg_size_pretty(pg_relation_size('k1'));
• select pg_relation_size('audit_hist');
How to view the indexes of an existing postgreSQL table ?
• Syntax: # d k1
• d pg_attribute
Show Biggest PostgreSQL Tables/Indexes And Their Size
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_relation_size(C.oid)) AS "size"
FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_relation_size(C.oid) DESC LIMIT 20;
How to find the largest table in the postgreSQL database?
SELECT relname, relpages FROM pg_class ORDER BY relpages DESC;
Tables rows count
SELECT nspname AS schemaname,relname as Tablename ,reltuples as numRows FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname IN ('WDB_SCHEMA') AND relkind='r’ ORDER BY relname;
Size of the Objects
SELECT relname AS objectname, relkind AS objecttype, reltuples AS "#entries",
pg_size_pretty(relpages::bigint*8*1024) AS size
FROM pg_class WHERE relpages >= 8 ORDER BY relpages DESC;
Quickly estimating the number of rows in a table
SELECT (CASE WHEN reltuples > 0 THEN pg_relation_size(oid)*reltuples /(8192*relpages)
ELSE 0 END)::bigint AS estimated_row_count FROM pg_class WHERE oid = 'mytable'::regclass;
Current activities
SELECT S.pid, age(clock_timestamp(), query_start), usename, query, L.mode, L.locktype, L.granted
FROM pg_stat_activity S inner join pg_locks L on S.pid = L.pid order by L.granted, L.pid DESC
PostgreSQL table access statistics, index io statistics
#1 table access statistics
#select schemaname,relname,seq_scan,idx_scan,cast(idx_scan as numeric) /
(idx_scan + seq_scan)
as idx_scan_pct
from pg_stat_user_tables where (idx_scan +seq_scan) >0 order by idx_scan_pct;
Higher pct means more likely your postgreSQL is using index scan, which is good.
#2 table io statistics
#select relname,cast(heap_blks_hit as numeric) /(heap_blks_hit +heap_blks_read)
as hit_pct,heap_blks_hit,heap_blks_read from pg_statio_user_tables
where (heap_blks_hit + heap_blks_read) >0 order by hit_pct; Higher hit_pct means
more likely the data required is cached.
#3 index access statistics
this shows all of the disk i/o for every index on each table
#select relname,cast(idx_blks_hit as numeric) /(idx_blks_hit + idx_blks_read )
as hit_pct,idx_blks_hit,idx_blks_read from pg_statio_user_tables
where (idx_blks_hit +idx_blks_read) >0 order by hit_pct;
#4 index io statistics
#select indexrelname,cast(idx_blks_hit as numeric) /( idx_blks_hit + idx_blks_read)
as hit_pct,idx_blks_hit,idx_blks_read from pg_statio_user_indexes
where (idx_blks_hit +idx_blks_read)>0 order by hit_pct ;
#5 Less used indexes(from top to bottom)
#select
schemaname,relname,indexrelname,idx_scan,pg_size_pretty(pg_relation_size(i.in
dexrelid))
as index_size from pg_stat_user_indexes i join pg_index using (indexrelid)
where indisunique is false order by idx_scan,relname;
Note: The main thing that the counts in pg_stat_user_indexes are useful for is to
determining which indexes are actually being used by your application. Since
indexes add overhead to the system, but drop them with care.
To show current server configuration setting.
SHOW ALL;
SELECT name, setting, unit, context FROM pg_settings;
Locating the Configuration File
If you are unsure where the postgresql.conf
config file is located, the simplest method for
finding the location is to connect to the
postgres client (psql) and issue the SHOW
config_file; command:
postgres=# SHOW config_file;
config_file
------------------------------------------
/etc/postgresql/11.2/main/postgresql.conf
Locate the Data Directory Path
It’s also a good idea to confirm the path of the
data directory for your postgres installation.
This will be useful later on, and retrieving the
path is a matter of another simple SHOW
statement:
postgres=# SHOW data_directory;
data_directory
------------------------------
/var/lib/postgresql/11.2/main
Locate Database directory
# ls -l /var/lib/pgsql/11.2/data/base
total 96
drwx------ 2 postgres postgres 12288 Jun 12 14:15 1
drwx------ 2 postgres postgres 4096 Jan 22 2015 12891
drwx------ 2 postgres postgres 4096 Jun 12 14:16 12896
drwx------ 2 postgres postgres 12288 Jun 12 14:16 16389
drwx------ 2 postgres postgres 12288 Jun 12 14:16 16390
drwx------ 2 postgres postgres 12288 Jul 2 23:23 16391
drwx------ 2 postgres postgres 20480 Jul 17 16:41 16392
drwx------ 2 postgres postgres 4096 Jun 12 14:15 16393
drwx------ 2 postgres postgres 12288 Jun 12 14:15 16394
drwx------ 2 postgres postgres 4096 Jan 22 2015 pgsql_tmp
The physical file are all named as so called OID
# select datname,oid from pg_database where
datname='postgres';
datname | oid
----------+-------
postgres | 12896
Together with the list of files under base directory, you see the
database directory for database postgres is
ERROR REPORTING AND LOGGING
Configuring PostgreSQL to Generate Log Output
# - Where to Log -
log_destination = 'csvlog' # 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 = on # 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
Here we’re telling postgres to generate logs in the CSV format and to output them to the pg_log directory (within the data
directory). We’ve also uncommented the log_filename setting to produce some proper name including timestamps for the log
files.
PostgreSQL Tablespace-1
1.creating tbs1 directory:
cd /tab1/
mkdir tbs1
2.changing tbs1 permission to postgres user:
chown -R postgres:postgres tbs1
3.creating tablespace with name of tbs1 ,tbs1 is a logical name you can change
whatever you want:
CREATE TABLESPACE tbs1 LOCATION '/tab1/tbs1';
4.Listing postgresql tablespace:
postgres=# db+
List of tablespaces
Name | Owner | Location | Access privileges | Options | Size |
Description
------------+----------+------------+-------------------+---------+---------+-------------
pg_default | postgres | | | | 23 MB |
pg_global | postgres | | | | 573 kB |
tbs1 | postgres | /tab1/tbs1 | | | 0 bytes |
(3 rows)
Creating Tables With/Without Tablespace:
postgres=# create database dbname2 tablespace tbs1;
CREATE DATABASE
postgres=# c dbname2
You are now connected to database "dbname2" as user "nijam".
dbname2=# create table t1 (a int);
CREATE TABLE
dbname2=# create table t2 (a int) tablespace tbs1;
CREATE TABLE
dbname2=# create table t3 (a int) tablespace tbs2;
CREATE TABLE
Basic Commands of Tablespace:
To determine the set of existing tablespaces:
select oid,spcname from pg_tablespace;
Following meta-command is also useful for listing the existing tablespaces:
db+
Tablespace Rename:
alter tablespace tbs1 rename to tbs3;
Changing Tablespace ownership:
alter tablespace tbs1 owner to scott;
Tablespace reset:
alter tablepace tbs1 reset default_tablespace;
Tablespace Drop:
drop tablespace tbs1;
Note: A tablespace cannot be dropped until all objects in all databases using the tablespace have been
removed.
Assign default tablespace to particular user:
ALTER ROLE someuser SET default_tablespace = tbs1;
Disk space occupied by a tablespace:
select pg_size_pretty(pg_tablespace_size('tbs1'));
(or)
/u02/tbs1/du -c -h
Temporarily for current session while you are creating a batch of tables using
SET default_tablespace = tbs2;
Changing the default tablespace for the whole instance:
all newly created objects go into a new tablespace.
postgres=# alter system set default_tablespace='tbs3';
ALTER SYSTEM
postgres=# select pg_reload_conf();
pg_reload_conf
----------------
t
(1 row)
postgres=# show default_tablespace ;
default_tablespace
--------------------
tbs3
(1 row)
Tablespace creation with I/O cost:
CREATE TABLESPACE tbs3 LOCATION '/some_disk_mount'
WITH (seq_page_cost=0.5, random_page_cost=0.75, effective_io_concurrency=10);
How to find what tablespace a table/index is in on PostgreSQL?
For table:
SELECT tablespace FROM pg_tables WHERE tablename = 't1' AND schemaname = 'schema1';
For index:
SELECT tablespace FROM pg_indexes WHERE indexname = 't1_index' AND schemaname =
'schema1';
Data Types
Structured Query Language

Más contenido relacionado

La actualidad más candente

Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL AdministrationEDB
 
Oracle: Binding versus caging
Oracle: Binding versus cagingOracle: Binding versus caging
Oracle: Binding versus cagingBertrandDrouvot
 
Reduce Resource Consumption & Clone in Seconds your Oracle Virtual Environmen...
Reduce Resource Consumption & Clone in Seconds your Oracle Virtual Environmen...Reduce Resource Consumption & Clone in Seconds your Oracle Virtual Environmen...
Reduce Resource Consumption & Clone in Seconds your Oracle Virtual Environmen...BertrandDrouvot
 
Replication and replica sets
Replication and replica setsReplication and replica sets
Replication and replica setsRandall Hunt
 
MongoDB: Advance concepts - Replication and Sharding
MongoDB: Advance concepts - Replication and ShardingMongoDB: Advance concepts - Replication and Sharding
MongoDB: Advance concepts - Replication and ShardingKnoldus Inc.
 
Example R usage for oracle DBA UKOUG 2013
Example R usage for oracle DBA UKOUG 2013Example R usage for oracle DBA UKOUG 2013
Example R usage for oracle DBA UKOUG 2013BertrandDrouvot
 
Postgres 12 Cluster Database operations.
Postgres 12 Cluster Database operations.Postgres 12 Cluster Database operations.
Postgres 12 Cluster Database operations.Vijay Kumar N
 
Introduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerIntroduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerMydbops
 
Cassandra 2.1 boot camp, Protocol, Queries, CQL
Cassandra 2.1 boot camp, Protocol, Queries, CQLCassandra 2.1 boot camp, Protocol, Queries, CQL
Cassandra 2.1 boot camp, Protocol, Queries, CQLJoshua McKenzie
 
MongoDB Database Replication
MongoDB Database ReplicationMongoDB Database Replication
MongoDB Database ReplicationMehdi Valikhani
 
Apache Con NA 2013 - Cassandra Internals
Apache Con NA 2013 - Cassandra InternalsApache Con NA 2013 - Cassandra Internals
Apache Con NA 2013 - Cassandra Internalsaaronmorton
 
MySQL shell and It's utilities - Praveen GR (Mydbops Team)
MySQL shell and It's utilities - Praveen GR (Mydbops Team)MySQL shell and It's utilities - Praveen GR (Mydbops Team)
MySQL shell and It's utilities - Praveen GR (Mydbops Team)Mydbops
 
Backup and Recovery
Backup and RecoveryBackup and Recovery
Backup and RecoveryAnar Godjaev
 
Cassandra 2.1 boot camp, Read/Write path
Cassandra 2.1 boot camp, Read/Write pathCassandra 2.1 boot camp, Read/Write path
Cassandra 2.1 boot camp, Read/Write pathJoshua McKenzie
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performanceguest9912e5
 
PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12Andrew Dunstan
 

La actualidad más candente (20)

Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
 
Postgre sql unleashed
Postgre sql unleashedPostgre sql unleashed
Postgre sql unleashed
 
Gg steps
Gg stepsGg steps
Gg steps
 
Oracle: Binding versus caging
Oracle: Binding versus cagingOracle: Binding versus caging
Oracle: Binding versus caging
 
Reduce Resource Consumption & Clone in Seconds your Oracle Virtual Environmen...
Reduce Resource Consumption & Clone in Seconds your Oracle Virtual Environmen...Reduce Resource Consumption & Clone in Seconds your Oracle Virtual Environmen...
Reduce Resource Consumption & Clone in Seconds your Oracle Virtual Environmen...
 
Replication and replica sets
Replication and replica setsReplication and replica sets
Replication and replica sets
 
MongoDB: Advance concepts - Replication and Sharding
MongoDB: Advance concepts - Replication and ShardingMongoDB: Advance concepts - Replication and Sharding
MongoDB: Advance concepts - Replication and Sharding
 
Postgresql Federation
Postgresql FederationPostgresql Federation
Postgresql Federation
 
Example R usage for oracle DBA UKOUG 2013
Example R usage for oracle DBA UKOUG 2013Example R usage for oracle DBA UKOUG 2013
Example R usage for oracle DBA UKOUG 2013
 
Postgres 12 Cluster Database operations.
Postgres 12 Cluster Database operations.Postgres 12 Cluster Database operations.
Postgres 12 Cluster Database operations.
 
Introduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizerIntroduction to Mongodb execution plan and optimizer
Introduction to Mongodb execution plan and optimizer
 
Cassandra 2.1 boot camp, Protocol, Queries, CQL
Cassandra 2.1 boot camp, Protocol, Queries, CQLCassandra 2.1 boot camp, Protocol, Queries, CQL
Cassandra 2.1 boot camp, Protocol, Queries, CQL
 
MongoDB Database Replication
MongoDB Database ReplicationMongoDB Database Replication
MongoDB Database Replication
 
Oracle Tracing
Oracle TracingOracle Tracing
Oracle Tracing
 
Apache Con NA 2013 - Cassandra Internals
Apache Con NA 2013 - Cassandra InternalsApache Con NA 2013 - Cassandra Internals
Apache Con NA 2013 - Cassandra Internals
 
MySQL shell and It's utilities - Praveen GR (Mydbops Team)
MySQL shell and It's utilities - Praveen GR (Mydbops Team)MySQL shell and It's utilities - Praveen GR (Mydbops Team)
MySQL shell and It's utilities - Praveen GR (Mydbops Team)
 
Backup and Recovery
Backup and RecoveryBackup and Recovery
Backup and Recovery
 
Cassandra 2.1 boot camp, Read/Write path
Cassandra 2.1 boot camp, Read/Write pathCassandra 2.1 boot camp, Read/Write path
Cassandra 2.1 boot camp, Read/Write path
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
 
PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12PostgreSQL 8.4 TriLUG 2009-11-12
PostgreSQL 8.4 TriLUG 2009-11-12
 

Similar a Postgresql Database Administration Basic - Day2

Sql introduction
Sql introductionSql introduction
Sql introductionvimal_guru
 
10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQLSatoshi Nagayasu
 
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...InfluxData
 
Discard inport exchange table & tablespace
Discard inport exchange table & tablespaceDiscard inport exchange table & tablespace
Discard inport exchange table & tablespaceMarco Tusa
 
Unit I - introduction to r language 2.pptx
Unit I - introduction to r language 2.pptxUnit I - introduction to r language 2.pptx
Unit I - introduction to r language 2.pptxSreeLaya9
 
Making Postgres Central in Your Data Center
Making Postgres Central in Your Data CenterMaking Postgres Central in Your Data Center
Making Postgres Central in Your Data CenterEDB
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce AlgorithmsAmund Tveit
 
Postgres в основе вашего дата-центра, Bruce Momjian (EnterpriseDB)
Postgres в основе вашего дата-центра, Bruce Momjian (EnterpriseDB)Postgres в основе вашего дата-центра, Bruce Momjian (EnterpriseDB)
Postgres в основе вашего дата-центра, Bruce Momjian (EnterpriseDB)Ontico
 
PostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and AlertingPostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and AlertingGrant Fritchey
 
Information Retrieval - Data Science Bootcamp
Information Retrieval - Data Science BootcampInformation Retrieval - Data Science Bootcamp
Information Retrieval - Data Science BootcampKais Hassan, PhD
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLCommand Prompt., Inc
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLMark Wong
 
Tutorial On Database Management System
Tutorial On Database Management SystemTutorial On Database Management System
Tutorial On Database Management Systempsathishcs
 
Oracle forensics 101
Oracle forensics 101Oracle forensics 101
Oracle forensics 101fangjiafu
 
SequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational DatabaseSequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational Databasewangzhonnew
 
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...NoSQLmatters
 

Similar a Postgresql Database Administration Basic - Day2 (20)

Sql introduction
Sql introductionSql introduction
Sql introduction
 
10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL
 
Perl Programming - 04 Programming Database
Perl Programming - 04 Programming DatabasePerl Programming - 04 Programming Database
Perl Programming - 04 Programming Database
 
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
 
Discard inport exchange table & tablespace
Discard inport exchange table & tablespaceDiscard inport exchange table & tablespace
Discard inport exchange table & tablespace
 
Unit I - introduction to r language 2.pptx
Unit I - introduction to r language 2.pptxUnit I - introduction to r language 2.pptx
Unit I - introduction to r language 2.pptx
 
Data Exploration in R.pptx
Data Exploration in R.pptxData Exploration in R.pptx
Data Exploration in R.pptx
 
Making Postgres Central in Your Data Center
Making Postgres Central in Your Data CenterMaking Postgres Central in Your Data Center
Making Postgres Central in Your Data Center
 
Mapreduce Algorithms
Mapreduce AlgorithmsMapreduce Algorithms
Mapreduce Algorithms
 
Postgres в основе вашего дата-центра, Bruce Momjian (EnterpriseDB)
Postgres в основе вашего дата-центра, Bruce Momjian (EnterpriseDB)Postgres в основе вашего дата-центра, Bruce Momjian (EnterpriseDB)
Postgres в основе вашего дата-центра, Bruce Momjian (EnterpriseDB)
 
PostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and AlertingPostgreSQL Performance Problems: Monitoring and Alerting
PostgreSQL Performance Problems: Monitoring and Alerting
 
Information Retrieval - Data Science Bootcamp
Information Retrieval - Data Science BootcampInformation Retrieval - Data Science Bootcamp
Information Retrieval - Data Science Bootcamp
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
 
pg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQLpg_proctab: Accessing System Stats in PostgreSQL
pg_proctab: Accessing System Stats in PostgreSQL
 
Aggregate.pptx
Aggregate.pptxAggregate.pptx
Aggregate.pptx
 
An Introduction to Postgresql
An Introduction to PostgresqlAn Introduction to Postgresql
An Introduction to Postgresql
 
Tutorial On Database Management System
Tutorial On Database Management SystemTutorial On Database Management System
Tutorial On Database Management System
 
Oracle forensics 101
Oracle forensics 101Oracle forensics 101
Oracle forensics 101
 
SequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational DatabaseSequoiaDB Distributed Relational Database
SequoiaDB Distributed Relational Database
 
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...
Simon Elliston Ball – When to NoSQL and When to Know SQL - NoSQL matters Barc...
 

Último

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Último (20)

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Postgresql Database Administration Basic - Day2

  • 1. Creating and Managing Databases Part-2 • Databases and Schemas • Tablespaces • Data Type • Exploring Databases • Locating the database server's message log • Locating the database's system identifier • Listing databases on this database server • How much disk space does a table use? • Which are my biggest tables? • How many rows are there in a table? • Quickly estimating the number of rows in a table • Understanding object dependencies
  • 2. Database Structure Here are some things that are important to know when attempting to understand the database structure of PostgreSQL. Items related to the database  PostgreSQL consists of several databases. This is called a database cluster.  When initdb () is executed, template0 , template1 , and postgres databases are created.  The template0 and template1 databases are template databases for user database creation and contain the system catalog tables.  The list of tables in the template0 and template1 databases is the same immediately after initdb (). However, the template1 database can create objects that the user needs.  The user database is created by cloning the template1 database. Items related to the tablespace  The pg_default and pg_global tablespaces are created immediately after initdb().  If you do not specify a tablespace at the time of table creation, it is stored in the pg_dafault tablespace.  Tables managed at the database cluster level are stored in the pg_global tablespace.  The physical location of the pg_default tablespace is $PGDATAbase.  The physical location of the pg_global tablespace is $PGDATAglobal.  One tablespace can be used by multiple databases. At this time, a database-specific subdirectory is created in the table space directory.
  • 3. Database Structure Items related to the table  There are three files per table.  One is a file for storing table data. The file name is the OID of the table.  One is a file to manage table free space. The file name is OID_fsm .  One is a file for managing the visibility of the table block. The file name is OID_vm .  The index does not have a _vm file. That is, OID and OID_fsm are composed of two files. Other Things to Remember...  The file name at the time of table and index creation is OID, and OID and pg_class.relfilenode are the same at this point. However, when a rewrite operation ( Truncate , CLUSTER , Vacuum Full , REINDEX , etc.) is performed, the relfilenode value of the affected object is changed, and the file name is also changed to the relfilenode value. You can easily check the file location and name by using pg_relation_filepath ('< object name >'). template0, template1, postgres database
  • 4. PostgreSQL Databases Creation Create database Using PostgreSQL utility Connect To the database : • postgres@r1 ~]$ cd /opt/PostgreSQL/11/bin/ • [postgres@r1 bin]$./psql -p 5432 -d postgres -U postgres • Password for user postgres: • postgres=# postgres=# CREATE DATABASE teachlax; CREATE DATABASE postgres=# create user u1 with password 'u1’; CREATE ROLE postgres=# CREATE DATABASE sales OWNER u1 TABLESPACE tbs1; CREATE DATABASE postgres=# l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | sales | u1 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | teachlax | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (5 rows) postgres=# drop database teachlax; DROP DATABASE postgres=# l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | sales | u1 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (4 rows)
  • 5. PostgreSQL Databases change database owner from “U1” to “U2”: postgres=# create user u2 with password 'u2'; CREATE ROLE postgres=# alter database sales owner to u2; ALTER DATABASE Set the Default tablespace : postgres=# alter database account set default_tablespace=tbs1; ALTER DATABASE postgres=# db List of tablespaces Name | Owner | Location ------------+----------+---------------- pg_default | postgres | pg_global | postgres | tbs1 | postgres | /home/postgres (3 rows) Listing database using sql query: postgres=# select datname from pg_database; datname ----------- template1 template0 postgres sales musicdb account (6 rows)
  • 6. PostgreSQL Databases Creation create a database coping template0 in sql: postgres=# create database temp0copy template template0; CREATE DATABASE create a database coping template0 in shell: [postgres@r1 ~]$ createdb -T template0 temp0copy2 Password: [postgres@r1 ~]$ Listing database using command line: [postgres@r1 ~]$ psql -l Password: List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges ------------+----------+----------+-------------+-------------+----------------------- account | u1 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | musicdb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | sales | u2 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | temp0copy | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | temp0copy2 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres …… (8 rows)
  • 7. PostgreSQL Tables Find Out Essential Settings in a Postgres DB • show config_file; • show effective_cache_size; • show shared_buffers; • show all; How to calculate postgreSQL database size in disk ? • SELECT pg_database_size('postgres'); • SELECT pg_size_pretty(pg_database_size('postgres')); How to calculate postgreSQL table size in disk ? • SELECT pg_size_pretty(pg_total_relation_size('k1')); • SELECT pg_size_pretty(pg_relation_size('k1')); • select pg_relation_size('audit_hist'); How to view the indexes of an existing postgreSQL table ? • Syntax: # d k1 • d pg_attribute Show Biggest PostgreSQL Tables/Indexes And Their Size SELECT nspname || '.' || relname AS "relation", pg_size_pretty(pg_relation_size(C.oid)) AS "size" FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE nspname NOT IN ('pg_catalog', 'information_schema') ORDER BY pg_relation_size(C.oid) DESC LIMIT 20; How to find the largest table in the postgreSQL database? SELECT relname, relpages FROM pg_class ORDER BY relpages DESC; Tables rows count SELECT nspname AS schemaname,relname as Tablename ,reltuples as numRows FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) WHERE nspname IN ('WDB_SCHEMA') AND relkind='r’ ORDER BY relname; Size of the Objects SELECT relname AS objectname, relkind AS objecttype, reltuples AS "#entries", pg_size_pretty(relpages::bigint*8*1024) AS size FROM pg_class WHERE relpages >= 8 ORDER BY relpages DESC; Quickly estimating the number of rows in a table SELECT (CASE WHEN reltuples > 0 THEN pg_relation_size(oid)*reltuples /(8192*relpages) ELSE 0 END)::bigint AS estimated_row_count FROM pg_class WHERE oid = 'mytable'::regclass; Current activities SELECT S.pid, age(clock_timestamp(), query_start), usename, query, L.mode, L.locktype, L.granted FROM pg_stat_activity S inner join pg_locks L on S.pid = L.pid order by L.granted, L.pid DESC
  • 8. PostgreSQL table access statistics, index io statistics #1 table access statistics #select schemaname,relname,seq_scan,idx_scan,cast(idx_scan as numeric) / (idx_scan + seq_scan) as idx_scan_pct from pg_stat_user_tables where (idx_scan +seq_scan) >0 order by idx_scan_pct; Higher pct means more likely your postgreSQL is using index scan, which is good. #2 table io statistics #select relname,cast(heap_blks_hit as numeric) /(heap_blks_hit +heap_blks_read) as hit_pct,heap_blks_hit,heap_blks_read from pg_statio_user_tables where (heap_blks_hit + heap_blks_read) >0 order by hit_pct; Higher hit_pct means more likely the data required is cached. #3 index access statistics this shows all of the disk i/o for every index on each table #select relname,cast(idx_blks_hit as numeric) /(idx_blks_hit + idx_blks_read ) as hit_pct,idx_blks_hit,idx_blks_read from pg_statio_user_tables where (idx_blks_hit +idx_blks_read) >0 order by hit_pct; #4 index io statistics #select indexrelname,cast(idx_blks_hit as numeric) /( idx_blks_hit + idx_blks_read) as hit_pct,idx_blks_hit,idx_blks_read from pg_statio_user_indexes where (idx_blks_hit +idx_blks_read)>0 order by hit_pct ; #5 Less used indexes(from top to bottom) #select schemaname,relname,indexrelname,idx_scan,pg_size_pretty(pg_relation_size(i.in dexrelid)) as index_size from pg_stat_user_indexes i join pg_index using (indexrelid) where indisunique is false order by idx_scan,relname; Note: The main thing that the counts in pg_stat_user_indexes are useful for is to determining which indexes are actually being used by your application. Since indexes add overhead to the system, but drop them with care. To show current server configuration setting. SHOW ALL; SELECT name, setting, unit, context FROM pg_settings;
  • 9. Locating the Configuration File If you are unsure where the postgresql.conf config file is located, the simplest method for finding the location is to connect to the postgres client (psql) and issue the SHOW config_file; command: postgres=# SHOW config_file; config_file ------------------------------------------ /etc/postgresql/11.2/main/postgresql.conf Locate the Data Directory Path It’s also a good idea to confirm the path of the data directory for your postgres installation. This will be useful later on, and retrieving the path is a matter of another simple SHOW statement: postgres=# SHOW data_directory; data_directory ------------------------------ /var/lib/postgresql/11.2/main Locate Database directory # ls -l /var/lib/pgsql/11.2/data/base total 96 drwx------ 2 postgres postgres 12288 Jun 12 14:15 1 drwx------ 2 postgres postgres 4096 Jan 22 2015 12891 drwx------ 2 postgres postgres 4096 Jun 12 14:16 12896 drwx------ 2 postgres postgres 12288 Jun 12 14:16 16389 drwx------ 2 postgres postgres 12288 Jun 12 14:16 16390 drwx------ 2 postgres postgres 12288 Jul 2 23:23 16391 drwx------ 2 postgres postgres 20480 Jul 17 16:41 16392 drwx------ 2 postgres postgres 4096 Jun 12 14:15 16393 drwx------ 2 postgres postgres 12288 Jun 12 14:15 16394 drwx------ 2 postgres postgres 4096 Jan 22 2015 pgsql_tmp The physical file are all named as so called OID # select datname,oid from pg_database where datname='postgres'; datname | oid ----------+------- postgres | 12896 Together with the list of files under base directory, you see the database directory for database postgres is
  • 10. ERROR REPORTING AND LOGGING Configuring PostgreSQL to Generate Log Output # - Where to Log - log_destination = 'csvlog' # 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 = on # 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 Here we’re telling postgres to generate logs in the CSV format and to output them to the pg_log directory (within the data directory). We’ve also uncommented the log_filename setting to produce some proper name including timestamps for the log files.
  • 11. PostgreSQL Tablespace-1 1.creating tbs1 directory: cd /tab1/ mkdir tbs1 2.changing tbs1 permission to postgres user: chown -R postgres:postgres tbs1 3.creating tablespace with name of tbs1 ,tbs1 is a logical name you can change whatever you want: CREATE TABLESPACE tbs1 LOCATION '/tab1/tbs1'; 4.Listing postgresql tablespace: postgres=# db+ List of tablespaces Name | Owner | Location | Access privileges | Options | Size | Description ------------+----------+------------+-------------------+---------+---------+------------- pg_default | postgres | | | | 23 MB | pg_global | postgres | | | | 573 kB | tbs1 | postgres | /tab1/tbs1 | | | 0 bytes | (3 rows) Creating Tables With/Without Tablespace: postgres=# create database dbname2 tablespace tbs1; CREATE DATABASE postgres=# c dbname2 You are now connected to database "dbname2" as user "nijam". dbname2=# create table t1 (a int); CREATE TABLE dbname2=# create table t2 (a int) tablespace tbs1; CREATE TABLE dbname2=# create table t3 (a int) tablespace tbs2; CREATE TABLE
  • 12. Basic Commands of Tablespace: To determine the set of existing tablespaces: select oid,spcname from pg_tablespace; Following meta-command is also useful for listing the existing tablespaces: db+ Tablespace Rename: alter tablespace tbs1 rename to tbs3; Changing Tablespace ownership: alter tablespace tbs1 owner to scott; Tablespace reset: alter tablepace tbs1 reset default_tablespace; Tablespace Drop: drop tablespace tbs1; Note: A tablespace cannot be dropped until all objects in all databases using the tablespace have been removed. Assign default tablespace to particular user: ALTER ROLE someuser SET default_tablespace = tbs1; Disk space occupied by a tablespace: select pg_size_pretty(pg_tablespace_size('tbs1')); (or) /u02/tbs1/du -c -h Temporarily for current session while you are creating a batch of tables using SET default_tablespace = tbs2; Changing the default tablespace for the whole instance: all newly created objects go into a new tablespace. postgres=# alter system set default_tablespace='tbs3'; ALTER SYSTEM postgres=# select pg_reload_conf(); pg_reload_conf ---------------- t (1 row) postgres=# show default_tablespace ; default_tablespace -------------------- tbs3 (1 row) Tablespace creation with I/O cost: CREATE TABLESPACE tbs3 LOCATION '/some_disk_mount' WITH (seq_page_cost=0.5, random_page_cost=0.75, effective_io_concurrency=10); How to find what tablespace a table/index is in on PostgreSQL? For table: SELECT tablespace FROM pg_tables WHERE tablename = 't1' AND schemaname = 'schema1'; For index: SELECT tablespace FROM pg_indexes WHERE indexname = 't1_index' AND schemaname = 'schema1';