May 17, 2026, 11:40 PM
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
SELECT
|
||||
o.conname AS constraint_name,
|
||||
(SELECT nspname FROM pg_namespace WHERE oid=m.relnamespace) AS source_schema,
|
||||
m.relname AS source_table,
|
||||
(SELECT a.attname FROM pg_attribute a WHERE a.attrelid = m.oid AND a.attnum = o.conkey[1] AND a.attisdropped = false) AS source_column,
|
||||
(SELECT nspname FROM pg_namespace WHERE oid=f.relnamespace) AS target_schema,
|
||||
f.relname AS target_table,
|
||||
(SELECT a.attname FROM pg_attribute a WHERE a.attrelid = f.oid AND a.attnum = o.confkey[1] AND a.attisdropped = false) AS target_column
|
||||
FROM
|
||||
pg_constraint o LEFT JOIN pg_class f ON f.oid = o.confrelid LEFT JOIN pg_class m ON m.oid = o.conrelid
|
||||
WHERE
|
||||
o.contype = 'f' AND o.conrelid IN (SELECT oid FROM pg_class c WHERE c.relkind = 'r');
|
||||
|
||||
|
||||
|
||||
REFERENCE - https://stackoverflow.com/questions/1152260/postgres-sql-to-list-table-foreign-keys
|
||||
@@ -0,0 +1,47 @@
|
||||
--- It wil find the indexes present on a table 'test'
|
||||
|
||||
postgres=# select * from pg_indexes where tablename='test';
|
||||
schemaname | tablename | indexname | tablespace | indexdef
|
||||
------------+-----------+-----------+-------------+----------------------------------------------------------
|
||||
public. | test | tes_idx1 | ts_postgres | CREATE INDEX tes_idx1 ON public.test USING btree (datid)
|
||||
(1 row)
|
||||
|
||||
-- All indexes present in database:
|
||||
|
||||
postgres#select * from pg_indexes
|
||||
|
||||
-- It will show all index details including size:
|
||||
|
||||
postgres=# \di+
|
||||
List of relations
|
||||
Schema | Name | Type | Owner | Table | Size | Description
|
||||
--------+----------+-------+----------+--------+--------+-------------
|
||||
public | tes_idx | index | postgres | test56 | 64 kB |
|
||||
public | tes_idx1 | index | postgres | test | 472 MB |
|
||||
(2 rows)
|
||||
|
||||
-- Find indexes with respective column name for table( here table name is test)
|
||||
|
||||
REFERENCE - https://stackoverflow.com/questions/2204058/list-columns-with-indexes-in-postgresql
|
||||
select
|
||||
t.relname as table_name,
|
||||
i.relname as index_name,
|
||||
array_to_string(array_agg(a.attname), ', ') as column_names
|
||||
from
|
||||
pg_class t,
|
||||
pg_class i,
|
||||
pg_index ix,
|
||||
pg_attribute a
|
||||
where
|
||||
t.oid = ix.indrelid
|
||||
and i.oid = ix.indexrelid
|
||||
and a.attrelid = t.oid
|
||||
and a.attnum = ANY(ix.indkey)
|
||||
and t.relkind = 'r'
|
||||
and t.relname ='test'
|
||||
group by
|
||||
t.relname,
|
||||
i.relname
|
||||
order by
|
||||
t.relname,
|
||||
i.relname;
|
||||
@@ -0,0 +1,47 @@
|
||||
--- It wil find the indexes present on a table 'test'
|
||||
|
||||
postgres=# select * from pg_indexes where tablename='test';
|
||||
schemaname | tablename | indexname | tablespace | indexdef
|
||||
------------+-----------+-----------+-------------+----------------------------------------------------------
|
||||
public. | test | tes_idx1 | ts_postgres | CREATE INDEX tes_idx1 ON public.test USING btree (datid)
|
||||
(1 row)
|
||||
|
||||
-- All indexes present in database:
|
||||
|
||||
postgres#select * from pg_indexes
|
||||
|
||||
-- It will show all index details including size:
|
||||
|
||||
postgres=# \di+
|
||||
List of relations
|
||||
Schema | Name | Type | Owner | Table | Size | Description
|
||||
--------+----------+-------+----------+--------+--------+-------------
|
||||
public | tes_idx | index | postgres | test56 | 64 kB |
|
||||
public | tes_idx1 | index | postgres | test | 472 MB |
|
||||
(2 rows)
|
||||
|
||||
-- Find indexes with respective column name for table( here table name is test)
|
||||
|
||||
REFERENCE - https://stackoverflow.com/questions/2204058/list-columns-with-indexes-in-postgresql
|
||||
select
|
||||
t.relname as table_name,
|
||||
i.relname as index_name,
|
||||
array_to_string(array_agg(a.attname), ', ') as column_names
|
||||
from
|
||||
pg_class t,
|
||||
pg_class i,
|
||||
pg_index ix,
|
||||
pg_attribute a
|
||||
where
|
||||
t.oid = ix.indrelid
|
||||
and i.oid = ix.indexrelid
|
||||
and a.attrelid = t.oid
|
||||
and a.attnum = ANY(ix.indkey)
|
||||
and t.relkind = 'r'
|
||||
and t.relname ='test'
|
||||
group by
|
||||
t.relname,
|
||||
i.relname
|
||||
order by
|
||||
t.relname,
|
||||
i.relname;
|
||||
@@ -0,0 +1,27 @@
|
||||
-- Below of any commands can be used to find the schema details:
|
||||
|
||||
postgres=# select schema_name,schema_owner from information_schema.schemata;
|
||||
schema_name | schema_owner
|
||||
--------------------+--------------
|
||||
raj | postgres
|
||||
information_schema | postgres
|
||||
public | postgres
|
||||
pg_catalog | postgres
|
||||
pg_toast_temp_1 | postgres
|
||||
pg_temp_1. | postgres
|
||||
pg_toast | postgres
|
||||
(7 rows)
|
||||
|
||||
postgres=# select nspname as schema_name , pg_get_userbyid(nspowner) as schema_owner from pg_catalog.pg_namespace;
|
||||
schema_name | schema_owner
|
||||
--------------------+--------------
|
||||
pg_toast | postgres
|
||||
pg_temp_1 | postgres
|
||||
pg_toast_temp_1 | postgres
|
||||
pg_catalog | postgres
|
||||
public | postgres
|
||||
information_schema | postgres
|
||||
raj | postgres
|
||||
(7 rows)
|
||||
|
||||
postgres=# \dn+
|
||||
@@ -0,0 +1,15 @@
|
||||
-- For getting the physical location of a table:
|
||||
|
||||
postgres=# select pg_relation_filepath('test');
|
||||
pg_relation_filepath
|
||||
----------------------
|
||||
base/13635/17395
|
||||
(1 row)
|
||||
|
||||
-- For getting the physical location of an index:
|
||||
|
||||
postgres=# select pg_relation_filepath('test_idx');
|
||||
pg_relation_filepath
|
||||
----------------------
|
||||
base/13635/17638
|
||||
(1 row)
|
||||
@@ -0,0 +1,27 @@
|
||||
Describe the table:
|
||||
|
||||
postgres=# \d test
|
||||
Table "public.test"
|
||||
Column. | Type | Collation | Nullable | Default
|
||||
------------+---------+-----------+----------+---------
|
||||
sourcefile | text. | | | -- >>>> Will get size for this one
|
||||
sourceline | integer | | |. -- >>>> Will get size for this one also
|
||||
seqno. | integer | |. |
|
||||
name. | text | | |
|
||||
setting. | text | | |
|
||||
applied | boolean | | |
|
||||
error | text. | | |
|
||||
Indexes:
|
||||
"test_idx" btree (sourcefile)
|
||||
"test_idx2" btree (sourceline)
|
||||
|
||||
|
||||
-- Find the column size ( for sourcefile and sourceline)
|
||||
|
||||
postgres=# select pg_size_pretty(sum(pg_column_size(sourcefile))) as total_size from test;
|
||||
total_size
|
||||
------------
|
||||
12 MB
|
||||
(1 row)
|
||||
|
||||
postgres=# select pg_size_pretty(sum(pg_column_size(sourceline))) as total_size from test;
|
||||
@@ -0,0 +1,46 @@
|
||||
postgres=# \d test
|
||||
Table "public.test"
|
||||
Column. | Type | Collation | Nullable | Default
|
||||
------------+---------+-----------+----------+---------
|
||||
sourcefile | text | | |
|
||||
sourceline | integer | | |
|
||||
seqno. | integer | |. |
|
||||
name. | text | | |
|
||||
setting. | text | | |
|
||||
applied | boolean | | |
|
||||
error | text. | | |
|
||||
Indexes:
|
||||
"test_idx" btree (sourcefile)
|
||||
"test_idx2" btree (sourceline)
|
||||
|
||||
-- Find the table_size ( excluding the index_size)
|
||||
|
||||
postgres=# SELECT pg_size_pretty (pg_relation_size('test'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
30 MB
|
||||
(1 row)
|
||||
|
||||
-- Find the total_index size of the table
|
||||
|
||||
postgres=# sELECT pg_size_pretty ( pg_indexes_size('test'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
26 MB
|
||||
(1 row)
|
||||
|
||||
-- Find particular index size:
|
||||
|
||||
postgres=# select pg_size_pretty(pg_total_relation_size('test_idx'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
19 MB
|
||||
|
||||
postgres=# select pg_size_pretty(pg_total_relation_size('test_idx2'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
6496 kB
|
||||
|
||||
Another method:
|
||||
|
||||
postgres=# \di+ "test_idx"
|
||||
@@ -0,0 +1,19 @@
|
||||
-- Find table sizes and its respective index sizes
|
||||
|
||||
SELECT
|
||||
table_name,
|
||||
pg_size_pretty(table_size) AS table_size,
|
||||
pg_size_pretty(indexes_size) AS indexes_size,
|
||||
pg_size_pretty(total_size) AS total_size
|
||||
FROM (
|
||||
SELECT
|
||||
table_name,
|
||||
pg_table_size(table_name) AS table_size,
|
||||
pg_indexes_size(table_name) AS indexes_size,
|
||||
pg_total_relation_size(table_name) AS total_size
|
||||
FROM (
|
||||
SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name
|
||||
FROM information_schema.tables
|
||||
) AS all_tables
|
||||
ORDER BY total_size DESC
|
||||
) AS pretty_sizes limit 10;
|
||||
@@ -0,0 +1,33 @@
|
||||
-- Simple create index:
|
||||
postgres=# create index tab_idx2 on scott.customer(emp_name);
|
||||
CREATE INDEX
|
||||
|
||||
-- Create index with tablespace:
|
||||
|
||||
postgres#CREATE INDEX tab_idx2 on scott.customer(emp_name) TABLESPACE IND_TS;
|
||||
CREATE INDEX
|
||||
|
||||
-- Create index without causing blocking:
|
||||
|
||||
postgres=# create index concurrently tab_idx2 on scott.customer(emp_name) TABLESPACE IND_TS;
|
||||
CREATE INDEX
|
||||
|
||||
-- Create unique index:
|
||||
|
||||
postgres=# create unique index tab_idx2 on scott.customer(emp_name)
|
||||
CREATE INDEX
|
||||
|
||||
-- Create functional index:
|
||||
|
||||
postgres=# create index fun_idx on scott.customer(lower(emp_name));
|
||||
CREATE INDEX
|
||||
|
||||
-- Create multi column index:
|
||||
|
||||
postgres=# create index multi_idx on scott.customer(emp_name,emp_id);
|
||||
CREATE INDEX
|
||||
|
||||
-- drop an index:
|
||||
|
||||
postgres=# drop index fun_idx;
|
||||
DROP INDEX
|
||||
@@ -0,0 +1,19 @@
|
||||
--Below is for finding objects under schema scott: Replace your schema_name with scott
|
||||
|
||||
SELECT n.nspname as "Schema",
|
||||
c.relname as "Name",
|
||||
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
|
||||
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
|
||||
FROM pg_catalog.pg_class c
|
||||
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
||||
where n.nspname ='scott'
|
||||
AND pg_catalog.pg_table_is_visible(c.oid)
|
||||
ORDER BY 1,2;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
NOTE: Make sure that, the schema_name for which you are looking for objects, is present in the search_path of that user. Otherwise it wont return any rows
|
||||
|
||||
postgres=# show search_path;
|
||||
@@ -0,0 +1,5 @@
|
||||
postgres=# select * from pg_views where schemaname not in ('pg_catalog','information_schema','sys');
|
||||
count
|
||||
-------
|
||||
|
||||
postgres#\dv
|
||||
@@ -0,0 +1,47 @@
|
||||
-- Find the sequence details:
|
||||
|
||||
select * from pg_sequences;
|
||||
|
||||
(or)
|
||||
|
||||
\ds+
|
||||
|
||||
List of relations
|
||||
Schema | Name | Type | Owner | Size | Description
|
||||
--------+-----------+----------+--------------+------------+-------------
|
||||
public | class_seq | sequence | enterprisedb | 8192 bytes |
|
||||
(1 row)
|
||||
|
||||
-- Create sequences:
|
||||
|
||||
postgres# CREATE SEQUENCE class_seq INCREMENT 1 MINVALUE 1 MAXVALUE 1000 START 1;
|
||||
CREATE SEQUENCE
|
||||
|
||||
-- Create sequence in descending:
|
||||
|
||||
postgres# CREATE SEQUENCE class_seq INCREMENT -1 MINVALUE 1 MAXVALUE 1000 START 1000;
|
||||
CREATE SEQUENCE
|
||||
|
||||
-- Alter sequence to change maxvalue:
|
||||
|
||||
postgres=# alter sequence class_seq maxvalue 500;
|
||||
ALTER SEQUENCE
|
||||
|
||||
-- Reset a sequence using alter command:
|
||||
|
||||
postgres=# alter sequence class_seq restart with 1;
|
||||
ALTER SEQUENCE
|
||||
|
||||
-- Find next_val and currval of a sequence:
|
||||
|
||||
postgres=# select nextval('class_seq');
|
||||
nextval
|
||||
---------
|
||||
1
|
||||
(1 row)
|
||||
|
||||
postgres=# select currval('class_seq');
|
||||
currval
|
||||
---------
|
||||
1
|
||||
(1 row)
|
||||
@@ -0,0 +1,6 @@
|
||||
Partial index, means index will be created on a specific subset of data of a table.
|
||||
|
||||
edbstore=> create index part_emp_idx on orders(tax) where tax > 400;
|
||||
CREATE INDEX
|
||||
|
||||
edbstore=> \d part_emp_idx
|
||||
@@ -0,0 +1,22 @@
|
||||
Below queries can be used to get schema wise size in postgres db
|
||||
|
||||
postgres=# select schemaname,pg_size_pretty(sum(pg_relation_size(quote_ident(schemaname) || '.' || quote_ident(tablename)))::bigint) as schema_size FROM pg_tables group by schemaname;
|
||||
schemaname | pg_size_pretty
|
||||
--------------------+----------------
|
||||
raj | 8192 bytes
|
||||
public | 3651 MB
|
||||
pg_catalog | 2936 kB
|
||||
information_schema | 96 kB
|
||||
(4 rows)
|
||||
|
||||
postgres=# SELECT schemaname,
|
||||
pg_size_pretty(sum(table_size)::bigint) as schema_size,
|
||||
(sum(table_size) / pg_database_size(current_database())) * 100 as percentage_of_total_db
|
||||
FROM (
|
||||
SELECT pg_catalog.pg_namespace.nspname as schemaname,
|
||||
pg_relation_size(pg_catalog.pg_class.oid) as table_size
|
||||
FROM pg_catalog.pg_class
|
||||
JOIN pg_catalog.pg_namespace ON relnamespace = pg_catalog.pg_namespace.oid
|
||||
) t
|
||||
GROUP BY schemaname
|
||||
ORDER BY schemaname;
|
||||
@@ -0,0 +1,23 @@
|
||||
-- Create a simple table
|
||||
postgres=# Create table member_table ( mem_id integer, member_name varchar(100) , mobile integer not null);
|
||||
CREATE TABLE
|
||||
|
||||
-- Create table with primary key
|
||||
postgres=# Create table member_table ( mem_id integer primary key, member_name varchar(100) , mobile integer not null);
|
||||
CREATE TABLE
|
||||
-- Create table under particular tablespace
|
||||
postgres=# Create table member_table ( mem_id integer primary key, member_name varchar(100) , mobile integer not null) tablespace pg_production_ts;
|
||||
CREATE TABLE
|
||||
|
||||
-- Create table with unique constraint
|
||||
postgres=# Create table member_table ( mem_id integer, member_name varchar(100) , mobile integer not null , constraint mem_id_cons unique(mem_id));
|
||||
CREATE TABLE
|
||||
|
||||
-- Create temporary table:
|
||||
|
||||
postgres=# Create temporary table member_table ( mem_id integer primary key, member_name varchar(100) , mobile integer not null) tablespace pg_default;
|
||||
CREATE TABLE
|
||||
|
||||
-- Drop table
|
||||
|
||||
postgres=# drop table member_table;
|
||||
@@ -0,0 +1,21 @@
|
||||
-- Find table sizes and its respective index sizes
|
||||
|
||||
SELECT
|
||||
table_name,
|
||||
pg_size_pretty(table_size) AS table_size,
|
||||
pg_size_pretty(indexes_size) AS indexes_size,
|
||||
pg_size_pretty(total_size) AS total_size
|
||||
FROM (
|
||||
SELECT
|
||||
table_name,
|
||||
pg_table_size(table_name) AS table_size,
|
||||
pg_indexes_size(table_name) AS indexes_size,
|
||||
pg_total_relation_size(table_name) AS total_size
|
||||
FROM (
|
||||
SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name
|
||||
FROM information_schema.tables
|
||||
) AS all_tables
|
||||
ORDER BY total_size DESC
|
||||
) AS pretty_sizes limit 10;
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
-- Top 10 big tables in postgres
|
||||
|
||||
select schemaname as schema_owner,
|
||||
relname as table_name,
|
||||
pg_size_pretty(pg_total_relation_size(relid)) as total_size,
|
||||
pg_size_pretty(pg_relation_size(relid)) as used_size,
|
||||
pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid))
|
||||
as free_space
|
||||
from pg_catalog.pg_statio_user_tables
|
||||
order by pg_total_relation_size(relid) desc,
|
||||
pg_relation_size(relid) desc
|
||||
limit 10;
|
||||
|
||||
|
||||
|
||||
(or)
|
||||
|
||||
SELECT
|
||||
nspname as schema_name,relname as table_name,pg_size_pretty(pg_relation_size(c.oid)) as "table_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 10;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user