Sub topic 13
Running queries in the Query Tool
This chapter assumes you can write basic SQL (SELECT, WHERE, JOIN, ORDER BY) and have already registered a server in pgAdmin 4 and browsed its object tree. It covers how to open the Query Tool, run SQL, read the output tabs, control transactions, and avoid mistakes. Menu names and shortcuts below match recent pgAdmin 4 releases. Older versions may differ slightly.
Opening the Query Tool
The Query Tool is a SQL editor tied to one database connection. To open it:
- In the browser tree, select the database you want to work in.
- Choose Tools → Query Tool, or right-click the database and choose Query Tool.
The tab title shows the database, the role you connected as, and the server. Check this before you run anything. Opening the tool on the wrong database is the most common mistake. In psql you would use \conninfo to confirm the same thing.
Each Query Tool tab has its own session. Two tabs on the same database are two separate connections. They have separate transactions, separate temporary tables, and separate SET values.
Running statements
Type SQL in the editor and press F5, or click the Execute/Refresh (play) button.
SELECT order_id, customer_id, total_amount
FROM public.orders
ORDER BY order_id DESC
LIMIT 20;The Query Tool has these behaviors:
- If you select text, only the selection runs. With no selection, the whole editor contents run.
- If the editor holds several statements, they all run in order, but the Data Output tab shows only the result of the last one. Earlier results are not shown.
- Statements are separated by semicolons. Pressing F5 never runs "the statement under the cursor", unlike some other SQL editors. Select the statement first if you want one.
- A query that is still running can be stopped with the Stop button. This sends a cancel request to the server.
The editor also offers autocomplete (Ctrl+Space), comment toggling (Ctrl+/), and SQL formatting (Ctrl+Shift+F). Use the file buttons to open and save .sql files.
Reading the output panel
The panel below the editor has several tabs.
| Tab | What it shows |
|---|---|
| Data Output | The result grid of the last statement that returned rows. |
| Messages | Command status such as `UPDATE 3`, execution time, errors, and `NOTICE` messages. |
| Explain | The query plan, after you use an explain command. |
| Notifications | Messages from `LISTEN`/`NOTIFY`. |
| Query History | Past queries for this session, with timing and status. |
The grid has some features worth knowing:
- Column headers show the name and the data type. Click a header to select a column.
- NULL values appear as
[null], so you can tell them from empty strings. This is the same idea as\pset nullin psql. - Download as CSV (F8) exports the full result of the query, not just the visible rows.
- Copy copies selected cells. Use the copy-with-headers option when you paste into a spreadsheet.
Row limits
The Query Tool toolbar has a row limit setting. Recent versions default to 1000 rows. If your query returns more than that, the grid shows only the first rows, even though the server may have produced more. The limit helps keep the browser from freezing on large results. Set it to No limit only when you know the result is small, or when you export it.
Transactions and auto-commit
By default the Query Tool is in auto-commit mode. Each statement is committed as soon as it finishes, as in psql. You can switch auto-commit off with the toolbar button. After that, the first statement starts a transaction. Nothing is saved until you press Commit or run COMMIT;. The status bar shows the current transaction state, and the connection can show an "in transaction" or "failed" state.
Two risks come from this:
- With auto-commit on, a mistaken
DELETEwithout aWHEREclause is final immediately. - With auto-commit off, an open transaction can hold locks. Other sessions that need the same rows will wait. Close the tab only after you commit or roll back. pgAdmin warns you if a transaction is still open, but do not rely on that.
If an error occurs inside a transaction, PostgreSQL marks the transaction as failed. Every later statement fails with current transaction is aborted until you roll back. pgAdmin has an auto-rollback on error preference that rolls back the failed statement for you. The psql equivalent is \set ON_ERROR_ROLLBACK, shown in the psqlrc chapter.
Running EXPLAIN from the toolbar
You can type EXPLAIN yourself, or use the toolbar. F7 runs Explain and Shift+F7 runs Explain Analyze on the editor contents. The result appears in the Explain tab, and the tab can show a graphical tree or the plan as text. Options such as Verbose, Costs, Buffers, Timing, and Summary are in the explain dropdown.
EXPLAIN ANALYZE actually runs the statement. For a SELECT, that is harmless. For INSERT, UPDATE, or DELETE, the changes are real. Wrap them in a transaction and roll back:
BEGIN;
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM orders WHERE customer_id = 42;
ROLLBACK;The next chapter explains how to read the plan. For now, remember the rule: plain EXPLAIN only estimates, and ANALYZE runs the query.
Editing data in the grid
If your query selects from a single table and includes its primary key, the grid is often editable. You can change a cell, then use Save Data Changes (F6). pgAdmin generates an UPDATE using the primary key. Results from joins, aggregates, or tables without a primary key are read-only. When auto-commit is on, saving takes effect immediately.
Cell editing is convenient for small fixes, but it hides the SQL being run. For anything that matters, write the UPDATE yourself so it is visible and can be saved in a script.
Query History and reuse
The Query History tab records each query that ran in the current session, with its time and duration. Select an entry to see the full SQL, then copy it back into the editor. History is limited, and pgAdmin may store it per server and database. Do not treat it as an audit log. Save important scripts to files.
Common problems
- Only one result appears. You ran several
SELECTstatements and see only the last. Run them one at a time by selecting each, or open more tabs. - Results look incomplete. The row limit is cutting them off. Check the toolbar setting.
- Objects "disappear" or settings reset. You switched tabs and are in a different session. Temporary tables and
SETvalues belong to one tab only. - Everything fails after one error. A transaction is in a failed state. Roll back.
- Changes are not visible to other users. Auto-commit is off and the transaction is still open.
- A query hangs. Another session may hold a lock. Look at the Dashboard's session list, covered in a later chapter.
Exercise
You switch off auto-commit in a Query Tool tab, run UPDATE orders SET status = 'shipped' WHERE order_id = 7;, and the Messages tab shows UPDATE 1. A colleague in psql runs SELECT status FROM orders WHERE order_id = 7; and still sees the old status. Why, and what do you do to make the change visible?
Show answer
With auto-commit off, the UPDATE runs inside an open transaction, and other sessions cannot see uncommitted changes. Press Commit (or run COMMIT;) in the Query Tool tab. Then the colleague's next query sees the new status. To discard the change instead, roll back.
Exercise
You want to see the real execution time of DELETE FROM orders WHERE customer_id = 42; without deleting any rows permanently. Write the statements to run in the Query Tool.
Show answer
BEGIN; EXPLAIN (ANALYZE, BUFFERS) DELETE FROM orders WHERE customer_id = 42; ROLLBACK; EXPLAIN ANALYZE executes the DELETE, so the surrounding transaction and the final ROLLBACK undo it.