Sub topic 3
Navigating the pgAdmin Object Browser
This chapter assumes you know basic SQL (SELECT, WHERE, JOIN, ORDER BY), can use a terminal, and can reach a PostgreSQL server. It covers pgAdmin 4, the graphical client for PostgreSQL.
Navigating the pgAdmin object browser
Register a server by right-clicking Servers in the left-hand tree and choosing Register, then Server. Enter the host, port, username and database. The browser then shows the server's objects as a tree: Databases, then each database's Schemas, then Tables, Views, Functions and Indexes under each schema.
Running queries in the Query Tool
Select a database, then open the Query Tool from the Tools menu. Type SQL and press F5 to run it. Results appear in the Data Output pane, and errors and row counts appear in the Messages pane.
SELECT customer_id, COUNT(*) AS order_count
FROM orders
GROUP BY customer_id
ORDER BY order_count DESC
LIMIT 5;Reading query plans with EXPLAIN
Put EXPLAIN (ANALYZE, BUFFERS) in front of a query and press F5 to see its execution plan. ANALYZE actually runs the statement, so wrap any UPDATE, DELETE or INSERT in a transaction that you roll back.
BEGIN;
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM orders WHERE customer_id = 42;
ROLLBACK;Read the plan from the innermost node outward. Compare estimated rows with actual rows. A large gap usually means outdated statistics, so run ANALYZE on the table.
Dashboard and Statistics tabs
The Dashboard shows live server activity, including sessions, transactions and locks. The Statistics tab under a table or database shows row counts, scan counts and dead tuples.
| Tab | Shows | Use it to |
|---|---|---|
| Dashboard | Sessions, transactions, I/O | Spot a busy or stalled server |
| Statistics | Row counts, scans, dead tuples | Find tables that need VACUUM |
Visualizing tables with the ERD tool
The ERD tool draws tables as boxes and foreign keys as lines between them. Open it from a schema's context menu. Use it to check how tables relate before you write joins.
When to use psql and when to use pgAdmin
Use psql for scripts, remote sessions over SSH, and quick catalog queries. Use pgAdmin to browse the object tree, read plans visually, monitor the server, and draw ERDs.
Exercise
Exercise
Which pgAdmin tab would you open to find tables with many dead tuples?
Show answer
The Statistics tab under the table or database. It shows dead tuple counts for each table.