Covered:
1. Databases and Schemas
2. Tablespaces
3. Data Type
4. Exploring Databases
5. Locating the database server's message log
6. Locating the database's system identifier
7. Listing databases on this database server
8. How much disk space does a table use?
9. Which are my biggest tables?
10. How many rows are there in a table?
11. Quickly estimating the number of rows in a table
12. Understanding object dependencies
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
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)
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';