Skip to main content
St Louis

Back to all posts

How to Trace A SQL Query In Postgresql?

Published on
7 min read
How to Trace A SQL Query In Postgresql? image

Best Tools to Trace SQL Queries to Buy in December 2025

1 Nitlak 2 PCS Tracing Wheel,Tracing Wheel Sewing Tool,Wooden Handle Stainless Steel Pounce Wheel,Tracing Wheel for Sewing Patterns, Tracing Wheel Tool for Leathercraft, Professional Marking Spacer

Nitlak 2 PCS Tracing Wheel,Tracing Wheel Sewing Tool,Wooden Handle Stainless Steel Pounce Wheel,Tracing Wheel for Sewing Patterns, Tracing Wheel Tool for Leathercraft, Professional Marking Spacer

  • DUAL SIZES: 7 & 5.79 WHEELS MEET ALL YOUR SEWING NEEDS!
  • DURABLE STEEL: RUST-RESISTANT WHEELS FOR ACCURATE STITCHING!
  • COMFORT DESIGN: ERGONOMIC HANDLES REDUCE FATIGUE DURING USE!
BUY & SAVE
$8.49
Nitlak 2 PCS Tracing Wheel,Tracing Wheel Sewing Tool,Wooden Handle Stainless Steel Pounce Wheel,Tracing Wheel for Sewing Patterns, Tracing Wheel Tool for Leathercraft, Professional Marking Spacer
2 LUNARM 2 Pcs Tracing Wheel Sewing Tool, Plastic Handle Needle Point Tracing Wheel, Random Color Tracing Wheel Sewing Tool, Professional Stitch Marking Spacer for Arts and Leather Crafts

LUNARM 2 Pcs Tracing Wheel Sewing Tool, Plastic Handle Needle Point Tracing Wheel, Random Color Tracing Wheel Sewing Tool, Professional Stitch Marking Spacer for Arts and Leather Crafts

  • DURABLE, CORROSION-RESISTANT DESIGN ENSURES LONG-LASTING PERFORMANCE.

  • ERGONOMIC HANDLE REDUCES HAND FATIGUE FOR GREATER SEWING COMFORT.

  • VERSATILE TOOL FOR TRACING, SEWING, AND CRAFTING ON VARIOUS MATERIALS.

BUY & SAVE
$7.59
LUNARM 2 Pcs Tracing Wheel Sewing Tool, Plastic Handle Needle Point Tracing Wheel, Random Color Tracing Wheel Sewing Tool, Professional Stitch Marking Spacer for Arts and Leather Crafts
3 LUNARM 1 PCS Tracing Wheel Sewing Tool - Professional Stitch Marking Spacer, 4mm Leather Needle Point Tracing Wheel with Wooden Handle for Arts and Leather Crafts

LUNARM 1 PCS Tracing Wheel Sewing Tool - Professional Stitch Marking Spacer, 4mm Leather Needle Point Tracing Wheel with Wooden Handle for Arts and Leather Crafts

  • DURABLE & RUST-RESISTANT: BUILT TO LAST FOR ALL YOUR SEWING NEEDS!
  • ERGONOMIC HANDLE: ENJOY COMFORT AND REDUCE HAND FATIGUE WHILE CRAFTING.
  • VERSATILE & SHARP: EASILY MARKS VARIOUS MATERIALS FOR EFFICIENT SEWING.
BUY & SAVE
$6.49
LUNARM 1 PCS Tracing Wheel Sewing Tool - Professional Stitch Marking Spacer, 4mm Leather Needle Point Tracing Wheel with Wooden Handle for Arts and Leather Crafts
4 4 Pcs Tracing Wheel Sewing Tool with Packaging Bag, 2 Different Size Needle Point Tracing Wheel, Leather Paper Cloth Sewing Overstitch Wheel Tool

4 Pcs Tracing Wheel Sewing Tool with Packaging Bag, 2 Different Size Needle Point Tracing Wheel, Leather Paper Cloth Sewing Overstitch Wheel Tool

  • COMPACT 4-PC SET WITH STORAGE BAG FOR EASY PORTABILITY.
  • VIBRANT COLORS ENHANCE YOUR SEWING EXPERIENCE AND CREATIVITY.
  • DURABLE DESIGN ENSURES PRECISE LINES FOR ALL YOUR CRAFTING NEEDS.
BUY & SAVE
$8.48
4 Pcs Tracing Wheel Sewing Tool with Packaging Bag, 2 Different Size Needle Point Tracing Wheel, Leather Paper Cloth Sewing Overstitch Wheel Tool
5 Honbay 4pcs Tracing Wheel Craft Sewing Tool

Honbay 4pcs Tracing Wheel Craft Sewing Tool

  • EFFORTLESSLY TRANSFER MARKINGS ON MOST FABRICS WITH DOUBLE THICKNESS.
  • IDEAL FOR DARTS, PLEATS, TUCKS, POCKETS, AND BUTTONHOLES.
  • GREAT FOR ALL CRAFT SEWING PROJECTS; PACK OF 4 INCLUDED!
BUY & SAVE
$6.39
Honbay 4pcs Tracing Wheel Craft Sewing Tool
6 "Honbay 3PCS Plastic Handle Tracing Wheel Craft Sewing Tool, Random Color"

"Honbay 3PCS Plastic Handle Tracing Wheel Craft Sewing Tool, Random Color"

  • VERSATILE TOOL FOR LEATHER, PAPER, AND CLOTH CRAFTING.
  • COMES IN A FUN, RANDOM COLOR PACK OF 3!
  • COMPACT SIZE: 15CM LONG, PERFECT FOR DETAILED WORK.
BUY & SAVE
$5.59
"Honbay 3PCS Plastic Handle Tracing Wheel Craft Sewing Tool, Random Color"
+
ONE MORE?

When it comes to tracing a SQL query in PostgreSQL, you can employ various methods and tools to achieve the desired result. Here is a brief explanation of how you can trace a SQL query in PostgreSQL:

  1. Query Logs: PostgreSQL has a built-in logging mechanism that allows you to trace SQL queries. By enabling the logging feature, you can record queries executed on the server along with additional information like timestamps, duration, and other details. The logs can be found in the PostgreSQL log file, which can be configured in the PostgreSQL configuration file.
  2. Log Analysis Tools: You can use log analysis tools or utilities to process the query logs generated by PostgreSQL. These tools allow you to analyze the logs, filter specific queries, and extract relevant information. Some popular log analysis tools include pgBadger, pgFouine, and pgAdmin.
  3. EXPLAIN Command: PostgreSQL provides an EXPLAIN command that helps in analyzing the execution plan of a query. By prefixing your query with EXPLAIN, you can get insights into how PostgreSQL plans to execute the query. This information includes the order of operations, type of join used, indexes used, and estimated costs. It helps to optimize queries for better performance.
  4. Auto-Explain Extension: The Auto-Explain extension in PostgreSQL automatically runs the EXPLAIN command on queries exceeding a specified threshold. It saves the execution plans in a separate log table, allowing you to analyze them at a later stage. This extension provides valuable information about query performance without the need to manually execute EXPLAIN on each query.
  5. pg_stat_statements Extension: The pg_stat_statements extension tracks statistics and performance details for SQL queries executed on the server. It records information such as query execution time, number of times executed, and more. By enabling this extension, you can get insights into query patterns, identify slow-running or frequently executed queries, and optimize them accordingly.

These methods and tools mentioned above can assist you in tracing SQL queries in PostgreSQL. Depending on your specific requirements, you can choose the most suitable approach to gain insights into query execution and performance.

How to enable auto explain in Postgresql?

To enable auto explain in PostgreSQL, you need to follow these steps:

  1. Open the PostgreSQL configuration file postgresql.conf. The location of the file may vary depending on your operating system and PostgreSQL installation.
  2. Search for the shared_preload_libraries configuration parameter and make sure it includes pg_stat_statements. If not, add it to the parameter and save the file. For example: shared_preload_libraries = 'pg_stat_statements'
  3. Restart the PostgreSQL server to load the new configuration. Again, the process to restart might vary depending on your operating system.
  4. Once the server is running again, connect to the PostgreSQL database using a client tool (psql, pgAdmin, etc.) as a superuser or a user with the necessary privileges.
  5. Enable the auto_explain module by running the following command: SET auto_explain.log_min_duration = 0; This sets the minimum execution time for auto explain to 0, meaning it will log every query. You can adjust this value according to your needs.
  6. To view the query plans generated by auto explain, you can execute the following command: SELECT * FROM pg_auto_explain_log; This query will retrieve the query plans and related information logged by auto explain.

Remember to exercise caution when enabling auto explain in a production environment as it may have some impact on performance due to the added logging.

How to interpret the output of the EXPLAIN command in Postgresql?

When you run the EXPLAIN command in Postgresql, it provides information about the execution plan for a given query. The output can be quite detailed and contains several columns. Here's how you can interpret the most crucial parts:

  1. Query Plan: The query plan is displayed as a tree-like structure with multiple levels. Each level represents an operation or step in the execution plan. The plan starts from the top and proceeds towards the bottom.
  2. Operation: The "Operation" column represents the specific operation being performed at each level. It can include operations like Seq Scan (sequential scan), Index Scan, Bitmap Heap Scan, Hash Join, Nested Loop, etc. This column tells you the primary operation being used to access the data.
  3. Order: The numbers in the leftmost column of each level indicate the sequential order of the operations. The lower the number, the earlier that operation will be executed.
  4. Cost: The "Cost" column shows the estimated cost of each operation. The cost is a unitless value that represents the estimated amount of work required to execute that operation. The lower the cost, the better.
  5. Rows: The "Rows" column represents the estimated number of rows that will be returned by each operation. It gives you an idea of the size of the data set being processed at each step.
  6. Join Type: If the execution plan involves joins, the "Join Type" column specifies the type of join being performed, such as Inner Join, Left Join, Right Join, or Full Join.
  7. Filter: The "Filter" column displays any filters or conditions being applied to filter the rows at each stage.
  8. Indexes: If an index is being used, it will be mentioned in the output, showing which index is being utilized.

By analyzing these aspects, you can understand how the query is executed, which operations are involved, and whether any optimizations like index usage are employed. It helps in identifying potential performance bottlenecks and optimizing the query or table design if needed.

How to utilize the pg_query_state contrib module in Postgresql?

To utilize the pg_query_state contrib module in PostgreSQL, follow these steps:

  1. Install the pg_query_state module if it is not already installed. This module is not included with a standard PostgreSQL installation and needs to be manually added. You can download the module from the PostgreSQL extensions website or compile it from source.
  2. Once the module is installed, load it into your database. You can do this by executing the following command in psql or any other PostgreSQL client: CREATE EXTENSION pg_query_state; This command should be run by a superuser or a user with the necessary privileges.
  3. After the extension is loaded, you can use the pg_query_state function in your queries. This function is used to track the internal state of the PostgreSQL query executor. It takes an SQL query as a parameter and returns the internal state information as a JSON object. For example, you can run a simple query like this: SELECT pg_query_state('SELECT * FROM your_table'); This will return a JSON object that contains information about the query execution, such as the execution plan, number of rows returned, execution time, etc. You can also use the pg_query_state function in combination with other PostgreSQL functions or views to get more detailed information about query execution.
  4. Analyze the returned JSON object to understand the internal query execution state. You can extract specific fields or values from the JSON object using the JSON functions provided by PostgreSQL. For example, you can extract the execution time of a query like this: SELECT (pg_query_state('SELECT * FROM your_table'))->'Execution time'->>'Actual Total Time' AS execution_time; This will extract the execution time as a text value. Note that the structure and content of the returned JSON object may vary depending on the PostgreSQL version and extension version you are using. Refer to the documentation of the pg_query_state module for the specific details.

Remember to use the pg_query_state module with caution and only in non-production environments. It is primarily meant for debugging and performance analysis purposes.