Home  Postgress   Core sql qu ...

core SQL queries you should know as a postgress DBA

As a PostgreSQL DBA, there are a handful of core SQL queries you must know for monitoring, performance tuning, and maintenance — plus a strong understanding of data types, since PostgreSQL supports more than most other RDBMSs (including JSON, arrays, hstore, UUIDs, etc.).

Let’s go through both in a clean, practical way:


🧩 Part 1: Top 10 SQL Queries Every PostgreSQL DBA Should Know

🧠 1️⃣ View Active Queries & Locks

See what’s running right now (who is blocking whom):

SELECT pid, usename, datname, state, query, wait_event_type, wait_event
FROM pg_stat_activity
WHERE state != 'idle'
ORDER BY query_start DESC;

🔍 Use this to find slow queries, long-running transactions, or blocked sessions.


⚙️ 2️⃣ Find Blocking and Blocked Queries

SELECT blocked_locks.pid     AS blocked_pid,
       blocked_activity.usename  AS blocked_user,
       blocking_locks.pid    AS blocking_pid,
       blocking_activity.usename AS blocking_user,
       blocked_activity.query AS blocked_statement,
       blocking_activity.query AS current_statement_in_blocking_process
FROM pg_locks blocked_locks
JOIN pg_stat_activity blocked_activity
  ON blocked_activity.pid = blocked_locks.pid
JOIN pg_locks blocking_locks
  ON blocking_locks.transactionid = blocked_locks.transactionid
  AND blocking_locks.pid != blocked_locks.pid
JOIN pg_stat_activity blocking_activity
  ON blocking_activity.pid = blocking_locks.pid;

Helps identify deadlocks or blocking chains.


📊 3️⃣ Check Table & Index Size

SELECT relname AS object_name,
       pg_size_pretty(pg_total_relation_size(relid)) AS total_size
FROM pg_catalog.pg_statio_user_tables
ORDER BY pg_total_relation_size(relid) DESC;

Shows largest tables/indexes — useful for space management and identifying bloated tables.


🔁 4️⃣ Check Table Bloat (Vacuum Candidates)

SELECT schemaname, relname, n_dead_tup, n_live_tup
FROM pg_stat_all_tables
WHERE n_dead_tup > 1000
ORDER BY n_dead_tup DESC;

High n_dead_tup means VACUUM is needed.


⚡ 5️⃣ Top Slowest Queries (pg_stat_statements)

SELECT query,
       calls,
       total_exec_time,
       mean_exec_time,
       rows
FROM pg_stat_statements
ORDER BY mean_exec_time DESC
LIMIT 10;

🧩 Requires enabling pg_stat_statements in postgresql.conf:

shared_preload_libraries = 'pg_stat_statements'

🔒 6️⃣ Check User Roles & Privileges

SELECT rolname, rolsuper, rolcreaterole, rolcreatedb, rolcanlogin
FROM pg_roles;

Use this to audit privileges or confirm who has superuser access.


📚 7️⃣ Check Database Size

SELECT pg_database.datname,
       pg_size_pretty(pg_database_size(pg_database.datname)) AS size
FROM pg_database
ORDER BY pg_database_size(pg_database.datname) DESC;

Know which databases are growing fastest.


🧹 8️⃣ Find Index Usage (to detect unused ones)

SELECT relname AS table_name,
       indexrelname AS index_name,
       idx_scan AS times_used
FROM pg_stat_user_indexes
WHERE idx_scan = 0;

Helps decide which indexes might be removed to save space.


🧮 9️⃣ Check Autovacuum Stats

SELECT relname, last_vacuum, last_autovacuum, vacuum_count, autovacuum_count
FROM pg_stat_all_tables
ORDER BY autovacuum_count DESC;

Confirms whether autovacuum is actually working.


🧩 🔟 Check Connection & Session Info

SELECT datname, count(*) AS connections
FROM pg_stat_activity
GROUP BY datname;

To avoid exceeding max_connections limit or spotting connection leaks.


📦 Bonus Queries

🧱 Get PostgreSQL version:

SELECT version();

🧱 Show configuration parameter values:

SHOW work_mem;
SHOW shared_buffers;

🧱 Change a parameter at runtime:

SET work_mem = '64MB';

🧠 Part 2: PostgreSQL Data Types — What a DBA Should Know

PostgreSQL supports more data types than almost any other RDBMS. You should know the core categories, their use cases, and storage implications.

CategoryExample TypesNotes
Numericsmallint, integer, bigint, decimal, numeric, real, double precision, serialChoose based on size and precision needs
Characterchar(n), varchar(n), texttext = variable unlimited size, most common
Booleanbooleantrue, false, null
Date/Timedate, time, timestamp, timestamptz, intervalUse timestamptz for timezone-aware data
MonetarymoneyStores currency, locale-dependent
UUIDuuidGreat for distributed systems, ensures unique IDs
Arrayinteger[], text[]Store multiple values in one column (careful with normalization)
JSON / JSONBjson, jsonbFor semi-structured data; jsonb supports indexing and better performance
Networkinet, cidr, macaddrStore IP addresses and networks
Geometricpoint, line, circleUseful for spatial or coordinate data
Full Text Searchtsvector, tsqueryBuilt-in text search indexing
Rangeint4range, numrange, daterangeRepresent ranges of values (e.g., date ranges)
HstorehstoreKey-value storage (simpler than JSON)

🧩 Example Table Using Many Types

CREATE TABLE employees (
    id SERIAL PRIMARY KEY,
    name TEXT,
    salary NUMERIC(10,2),
    joined_on TIMESTAMPTZ DEFAULT now(),
    active BOOLEAN DEFAULT true,
    skills TEXT[],
    address JSONB,
    emp_uuid UUID DEFAULT gen_random_uuid()
);

🧮 Data Type Insights for DBAs

TopicBest Practice
Numeric vs TextUse proper numeric types to avoid slow comparisons/sorts.
TimestampsPrefer timestamptz for global apps.
JSON vs JSONBUse jsonb — it's binary-parsed, indexable, and faster.
Arrays & HstoreUse when schema flexibility is required, but avoid overuse.
UUIDsGreat for distributed systems (no sequence conflicts).
Indexing JSONBUse GIN index for faster lookups: CREATE INDEX idx_jsonb ON table USING gin (jsonb_col);

🧩 In Summary

AreaMust-Know QueriesKey DBA Focus
Monitoringpg_stat_activity, pg_stat_statementsSlow queries & locks
PerformanceEXPLAIN ANALYZE, pg_stat_user_indexesQuery optimization
Space Mgmtpg_total_relation_size()Table/index bloat
Securitypg_roles, pg_hba.confAccess control
Data Typesjsonb, uuid, timestamptz, numericProper design choices
Published on: Oct 07, 2025, 05:17 AM  
 

Comments

Add your comment