Showing posts with label pg_namespace. Show all posts
Showing posts with label pg_namespace. Show all posts

Monday, October 31, 2022

Postgresql: Command runbook for DBA

Postgresql runbook for a DBA:

Steps to perform "cluster" command in postgres.... after brief data collection


Ensure to enable putty log...

1) Connect to postgres user:
command:
su - postgres

2) Find out the postgres process running:
command:
ps -ef|grep -i postgres

3) echo $PATH and $PGDATA variable
command:
echo $PATH
echo $PGDATA

4) Find out the location of psql
command:
which psql

5) Collect the cron job details
command:
crontab -l

6) Collect df -h output
command:
df -h

7) Connect to psql
command:
psql

8) Collect the db list
command:
\l
or 

select a.oid
,a.datname
,b.rolname owner
,a.encoding
,a.dattablespace
,a.datcollate
,a.datctype
,a.datacl
from pg_database a
,pg_authid b
where b.oid=a.datdba;

9) Connect to the db where the table is located
command:
\c <dbname>

10) View the schema list
command:
select * from pg_catalog.pg_namespace;
or
\dn+

11) show your search path first
command:
show search_path;

12) Set the search path the candidate schema
command:
SET search_path TO <candidate_schema>;

13) show your search path again [remember not to disconnect or exit this connection]
command:
show search_path;

14) List the tables along with their sizes
command:
\d+

15) Collect more details on the table
command:
select schemaname,tablename,tableowner,hasindexes from pg_tables where schemaname='<candidate_schema>' order by 1;

select relname table_name
,relnamespace schema_name
,reltype relation_type
,relowner owner
,relpages pages_or_blocks
,reltuples numrows
,relhasindex has_index
from pg_class
where relnamespace=(select oid from pg_namespace where nspname=<candidate_schema>')
and relname='<candidate_table>' order by 2,1;

16) Take description details
command:
\d <candidate table_name>;

17) Let us examine the index status
command:
select c.schemaname
,c.tablename
,c.indexname
,b.indisunique
,b.indisprimary
,b.indisclustered
,b.indisvalid
,b.indisready
,b.indislive
from pg_class a
,pg_index b
,pg_indexes c
,pg_namespace d
where d.nspname='<candidate_schema>'
and c.schemaname=d.nspname
and c.tablename='<candidate_table>'
and c.indexname=a.relname
and a.oid=b.indexrelid
and a.relnamespace=d.oid
;

18) Now secure backup of the candidate table [in case needed, we can revert both table structure and data]
command:
pg_dump -U <candidate_username> -d <candidate_database> -t <candidate_schema>.<candidate_table> > <candidate_mountspace>/pgtst_schema_pgtst_tbl2.sql

19) Let us run cluster command: << outage begins here
command:
Connect back to the same db;
SET search_path TO <candidate_schema>;
cluster verbose <candidate_schemaname>.<candidate_tablename> using <candidate_index>;

20) Let us run analyze
command:
Connect back to the same db;
SET search_path TO <candidate_schema>;
analyze verbose <candidate_schemaname>.<candidate_tablename>;

20) Let us examine the index status again
command:
Connect back to the same db;
SET search_path TO <candidate_schema>;

select c.schemaname
,c.tablename
,c.indexname
,b.indisunique
,b.indisprimary
,b.indisclustered
,b.indisvalid
,b.indisready
,b.indislive
from pg_class a
,pg_index b
,pg_indexes c
,pg_namespace d
where d.nspname='<candidate_schema>'
and c.schemaname=d.nspname
and c.tablename='<candidate_table>'
and c.indexname=a.relname
and a.oid=b.indexrelid
and a.relnamespace=d.oid
;

21) Let us examine the table status again
command:
Connect back to the same db;
SET search_path TO <candidate_schema>;
select schemaname,tablename,tableowner,hasindexes from pg_tables where schemaname='<candidate_schema>' order by 1;

select relname table_name
,relnamespace schema_name
,reltype relation_type
,relowner owner
,relpages pages_or_blocks
,reltuples numrows
,relhasindex has_index
from pg_class
where relnamespace=(select oid from pg_namespace where nspname='pgtst_schema')
and relname='<candidate_table>' order by 2,1;

\dn+

This closes the command summary for clustering operation.

Sunday, October 23, 2022

Postgresql: Interacting with schemas

To get a list of schemas in a db, we can run the below query..

select * from pg_catalog.pg_namespace;

Actual output:

postgres=# \c
You are now connected to database "postgres" as user "postgres".
postgres=#
postgres=# select * from pg_catalog.pg_namespace;
  oid  |      nspname       | nspowner |                            nspacl
-------+--------------------+----------+---------------------------------------------------------------
    99 | pg_toast           |       10 |
    11 | pg_catalog         |       10 | {postgres=UC/postgres,=U/postgres}
  2200 | public             |     6171 | {pg_database_owner=UC/pg_database_owner,=U/pg_database_owner}
 13915 | information_schema |       10 | {postgres=UC/postgres,=U/postgres}
(4 rows)
postgres=#

postgres=# \dn
      List of schemas
  Name  |       Owner
--------+-------------------
 public | pg_database_owner
(1 row)

postgres=#

This result is coming from postgres database (default).
If we run the same command in another custom build db/schema...

pgtst_db=> \c
You are now connected to database "pgtst_db" as user "pgtst_usr".
pgtst_db=>

pgtst_db=> select * from pg_catalog.pg_namespace;
  oid  |      nspname       | nspowner |                            nspacl
-------+--------------------+----------+---------------------------------------------------------------
    99 | pg_toast           |       10 |
    11 | pg_catalog         |       10 | {postgres=UC/postgres,=U/postgres}
  2200 | public             |     6171 | {pg_database_owner=UC/pg_database_owner,=U/pg_database_owner}
 13915 | information_schema |       10 | {postgres=UC/postgres,=U/postgres}
 24582 | pgtst_schema       |    16388 |
(5 rows)
pgtst_db=>

pgtst_db=> \dn
         List of schemas
     Name     |       Owner
--------------+-------------------
 pgtst_schema | pgtst_usr
 public       | pg_database_owner
(2 rows)

pgtst_db=>


So we learn, the custom build DB is a copy of template 1 and hence it is reporting those default 4 schemas which forms postgresql dictionary.
Also the \dn command hides the postgresql dictionary to avoid hopefully the end user interaction.

Thanks