-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.sql
55 lines (45 loc) · 2.12 KB
/
admin.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- 1. Get the physical size of a specific table
SELECT pg_size_pretty(pg_total_relation_size('schema_name.table_name'));
------------------------------------------------------------------------
-- 2. Get the physical size of a specifc schema
SELECT pg_size_pretty(SUM(pg_relation_size(concat(schemaname,'.',tablename)))) AS alias
FROM pg_tables
WHERE schemaname='public';
------------------------------------------------------------------------
-- 3. Create drop table statements for multiple tables with the same prefix.
-- You need to export the result in CSV to copy it back into PGADMIN for instance
SELECT
CONCAT( 'DROP TABLE IF EXISTS', table_schema ,'.', table_name , ';' ) AS drop_statement
FROM information_schema.tables
WHERE table_schema = 'public' and table_name like 'my_table_%'
order by table_name;
------------------------------------------------------------------------
-- 4. Check if specific index exists in a specific table
SELECT COUNT(*)
FROM pg_indexes
WHERE schemaname = 'public' AND tablename = 'bldgs' AND indexname ILIKE '%geom%';
------------------------------------------------------------------------
-- 5. Check table names on a specific schema
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_schema = 'schema_name' AND table_name ilike 'bldg%';
--OR
SELECT schemaname, tablename
FROM pg_catalog.pg_tables
WHERE schemaname = 'schema_name' and tablename ilike 'bldg%';
------------------------------------------------------------------------
-- 6. get columns names for a specific table
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'public' and table_name = 'county_albers';
------------------------------------------------------------------------
-- 7. get the total number of columns in a table
SELECT COUNT(*)
FROM information_schema.columns
WHERE table_schema = 'XXX' AND table_name='YYY';
------------------------------------------------------------------------
-- 8. generate DROP TABLE statement for all tables with specific prefix
SELECT 'DROP TABLE schema.' || tablename || ';'
FROM pg_tables
WHERE schemaname = 'schema'
AND tablename ILIKE 'bldg_%';