WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

PostgreSQL · Intermediate · question 23 of 100

What is the difference between the VACUUM and ANALYZE operations in PostgreSQL, and when should you use them?

📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

VACUUM and ANALYZE are two separate operations in PostgreSQL, although they are often performed together.

VACUUM is a maintenance operation in PostgreSQL that reclaims storage space and defragments tables by removing dead rows and optimizing table layouts. Dead rows are rows that are no longer visible or used in any transactions because they have been updated or deleted but still exist in the table. Vacuuming can help reduce table bloat, which can slow down query performance and increase disk usage.

There are several types of VACUUM operations that can be performed:

- FULL VACUUM: Reclaims all expired rows and shrinks the table to its minimum size. This operation requires an exclusive lock on the table and can take a long time to complete, especially on large tables.
- FREEZE: Marks all tuples older than the current transaction ID as frozen, preventing them from being vacuumed until all transaction IDs are older than the frozen ones. This operation can help reduce the need for regular vacuuming and improve read performance.
- ANALYZE: Updates the database statistics for the table, which is used by the query planner to determine the most efficient execution plan for a given query.

ANALYZE is the operation that updates the database statistics for a given table to help the query planner generate more efficient execution plans. The statistics include details like the number of rows, the distribution of values in the columns, and whether an index is used or not. Without up-to-date statistics, the query planner may choose inefficient query plans, resulting in slow performance.

In general, you should use VACUUM regularly to help maintain the health and performance of your PostgreSQL database. By default, PostgreSQL runs vacuuming automatically in the background, but you may need to manually run vacuum when there is significant table bloat or after a large number of updates or deletes. You should also use ANALYZE periodically, especially after significant changes to the schema or data, to ensure the query planner has accurate statistics to generate optimal query plans.

Here’s an example of how to manually run VACUUM and ANALYZE on a table in Java using JDBC:

Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost/mydb", "myuser", "mypassword");
String table = "mytable";

// Run VACUUM
Statement vacuumStmt = conn.createStatement();
vacuumStmt.execute("VACUUM " + table);

// Run ANALYZE
Statement analyzeStmt = conn.createStatement();
analyzeStmt.execute("ANALYZE " + table);

Note that this is just a basic example, and you should always be careful when running vacuuming operations on large tables, as they can take a significant amount of time and block other transactions.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PostgreSQL interview — then scores it.
📞 Practice PostgreSQL — free 15 min
📕 Buy this interview preparation book: 100 PostgreSQL questions & answers — PDF + EPUB for $5

All 100 PostgreSQL questions · All topics