View Your Query Buffer In ysqlsh

If you’re working in ysqlsh, YugabyteDB’s interactive SQL shell, you may not know about a small but powerful command: \p.

The \p command prints the current query buffer. Think of it as a peek into the query you’re building interactively before executing it.

This is super helpful when:

  1. You’re composing a long query across multiple lines

  2. You want to double-check before running it

  3. You need to tweak or copy the query for reuse

  4. You are like me and completely forget what query is in the buffer!

Example:

Setup:

				
					CREATE TABLE employee (
    employee_id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    department TEXT
);

CREATE TABLE employee_address (
    address_id SERIAL PRIMARY KEY,
    employee_id INT REFERENCES employee(employee_id),
    street TEXT,
    city TEXT,
    state TEXT
);

INSERT INTO employee (name, department) VALUES
('Julie Brown', 'Engineering'),
('Frank Frong', 'Finance');

INSERT INTO employee_address (employee_id, street, city, state) VALUES
(1, '123 Green St', 'Boston', 'MA'),
(2, '455 Tree Ave', 'Chicago', 'IL');



				
			

Now type this query line by line without ending it in a semicolon (;):

				
					SELECT e.name, e.department, a.street, a.city, a.state
FROM employee e
JOIN employee_address a ON e.employee_id = a.employee_id
WHERE a.state = 'MA'
ORDER BY e.name

				
			

Use \p to view the query in the buffer without executing it, then execute the query with a semicolon (;):

				
					yugabyte=# SELECT e.name, e.department, a.street, a.city, a.state
yugabyte-# FROM employee e
yugabyte-# JOIN employee_address a ON e.employee_id = a.employee_id
yugabyte-# WHERE a.state = 'MA'
yugabyte-# ORDER BY e.name
yugabyte-# \p
SELECT e.name, e.department, a.street, a.city, a.state
FROM employee e
JOIN employee_address a ON e.employee_id = a.employee_id
WHERE a.state = 'MA'
ORDER BY e.name
yugabyte-# ;
    name     | department  |    street    |  city  | state
-------------+-------------+--------------+--------+-------
 Alice Smith | Engineering | 123 Maple St | Boston | MA
(1 row)
				
			

You can use \p to view the query again:

				
					yugabyte=# \p
SELECT e.name, e.department, a.street, a.city, a.state
FROM employee e
JOIN employee_address a ON e.employee_id = a.employee_id
WHERE a.state = 'MA'
ORDER BY e.name
;
				
			

When working interactively, \p is a subtle but mighty tool that makes your ysqlsh sessions safer, cleaner, and more productive.

Try using it the next time you’re mid-query and want to sanity-check before hitting enter!

Have Fun!

While hiking a trail in Acadia National Park, my wife and I came across a group of snowmobiles parked right next to a sign that said, “No Snowmobiling!” The irony gave us a good laugh.