-
Notifications
You must be signed in to change notification settings - Fork 852
Add SQL formatting and query plan analysis module via explain.tensor.ru API #9808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MGorkov
wants to merge
14
commits into
pgadmin-org:master
Choose a base branch
from
MGorkov:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2c0e741
Add integration with external Explain PostgreSQL API for query plans …
MGorkov 28fe939
fix coderabbit issues
MGorkov 4653a9f
fix coderabbit issues again
MGorkov f9e341b
fix
MGorkov 117e25c
fix possible BadRequest raising
MGorkov f956529
fix url opener
MGorkov afa5e4c
new module name expl_pgsql
MGorkov 8577c6d
add tests
MGorkov d018fa6
add docs
MGorkov cd6775f
Merge branch 'master' into expl_pgsql
MGorkov 18a9e43
update translations
MGorkov e4f886a
fix coderabbit issuse
MGorkov 63a062c
change Explain PostgreSQL to Explain Tensor
MGorkov bf817c4
Explain Tensor is disabled by default
MGorkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,4 @@ PL/SQL code. | |
| erd_tool | ||
| psql_tool | ||
| ai_tools | ||
| expl_tensor | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| .. _expl_tensor: | ||
|
|
||
| *************************** | ||
| `Explain Tensor`:index: | ||
| *************************** | ||
|
|
||
| **Explain Tensor** is a powerful module integrated into pgAdmin 4 that enables advanced analysis of query execution plans and beautification of SQL code. This tool helps developers and DBAs understand how PostgreSQL executes queries, identify performance bottlenecks, and optimize database workloads effectively. | ||
|
|
||
| Key Features: | ||
|
|
||
| * Visual representation of query execution plans with detailed node information. | ||
| * SQL formatting and beautification capabilities for improved readability. | ||
| * Integration with pgAdmin’s browser interface for seamless workflow. | ||
|
|
||
| **Requirements:** | ||
|
|
||
| Before using the Explain Tensor module, ensure the following: | ||
|
|
||
| 1. You are connected to a PostgreSQL server with sufficient privileges to execute commands. | ||
|
|
||
| 2. Ensure Explain Tensor are enabled in the server configuration (set ``EXPLAIN_TENSOR_ENABLED`` to ``True`` in ``config.py``). | ||
|
|
||
| 3. Configure Explain Tensor in :ref:`Preferences → Explain Tensor <the-explain-tensor-node>`. | ||
|
|
||
| **Note:** | ||
|
|
||
| * Using Explain Tensor requires an active internet connection. | ||
|
|
||
| * When analyzing query plans via Explain Tensor, the plan and query are sent to a third-party service | ||
| (by default: https://explain.tensor.ru). | ||
|
|
||
|
|
||
| Configuring Explain Tensor | ||
| ****************************** | ||
|
|
||
| To configure Explain Tensor, navigate to *File → Preferences → Explain Tensor*. | ||
|
|
||
| .. image:: images/preferences_expl_tensor.png | ||
| :alt: Explain Tensor preferences | ||
| :align: center | ||
|
|
||
| 1. Set the **Explain Plan** switch to *True*. | ||
|
|
||
| 2. Enter the *Explain Tensor API* URL (default: https://explain.tensor.ru). | ||
|
|
||
| 3. Set the **Format SQL** switch to *True* if you want to use the SQL formatting capability. | ||
|
|
||
| 4. Set the **Private Plans** switch to *True* if you want to store plans in your personal archive. | ||
|
|
||
| After configuring, click *Save* to apply the changes. | ||
|
|
||
|
|
||
| Using Explain Tensor | ||
| ************************* | ||
|
|
||
| To analyze a query plan: | ||
|
|
||
| 1. Open the **Query Tool** in pgAdmin. | ||
|
|
||
| 2. Enter your SQL query (e.g., ``SELECT * FROM pg_stat_activity``). | ||
|
|
||
| 3. Select the ``Buffers`` and ``Timing`` options from the dropdown menu next to the **Explain Analyze** button in the toolbar. | ||
|
|
||
| 4. Click the **Explain Analyze** button in the toolbar (or press ``Shift+F7``) to generate the execution plan. | ||
|
|
||
| Upon successful generation, the *Explain Tensor* panel will appear. | ||
|
|
||
| .. image:: images/expl_tensor_analyze.png | ||
| :alt: Example of Explain Tensor output | ||
| :align: center | ||
|
|
||
|
|
||
| Understanding Execution Plans | ||
| ***************************** | ||
|
|
||
| Each node in the execution plan represents a step in query processing. The Explain Tensor module displays: | ||
|
|
||
| * **Plan Tree** – A simplified view of the execution algorithm. Numeric indicators are displayed separately and highlighted with colors indicating load intensity. | ||
|
|
||
| Hover over nodes for tooltips with extended information. Nodes are color-coded based on performance impact: | ||
|
|
||
| * Green – Low cost | ||
| * Yellow – Medium cost | ||
| * Red – High cost (potential bottleneck) | ||
|
|
||
| .. image:: images/expl_tensor_plantree.png | ||
| :alt: Example of Explain Tensor plan tree | ||
| :align: center | ||
|
|
||
| * **Diagram** – Shows real dependencies between nodes and resource flows. | ||
|
|
||
| .. image:: images/expl_tensor_diagram.png | ||
| :alt: Example of Explain Tensor diagram | ||
| :align: center | ||
|
|
||
| * **Schema** – Visualizes database tables and their relationships. | ||
|
|
||
| .. image:: images/expl_tensor_schema.png | ||
| :alt: Example of Explain Tensor schema | ||
| :align: center | ||
|
|
||
| * **Statistics** – Summary statistics allow you to analyze large plans in aggregated form, sorted by any metric such as execution time, disk reads, cache usage, or filtered rows. | ||
|
|
||
| .. image:: images/expl_tensor_stats.png | ||
| :alt: Example of Explain Tensor statistics | ||
| :align: center | ||
|
|
||
| * **Pie Chart** – Helps quickly identify dominant nodes and their approximate share of resource consumption. | ||
|
|
||
| .. image:: images/expl_tensor_piechart.png | ||
| :alt: Example of Explain Tensor pie chart | ||
| :align: center | ||
|
|
||
| * **Tiled Visualization** – Allows compact evaluation of node connections in large plans and highlights problematic sections. | ||
|
|
||
| .. image:: images/expl_tensor_tilemap.png | ||
| :alt: Example of Explain Tensor tiled visualization | ||
| :align: center | ||
|
|
||
| * **Smart Recommendations** – Automatically generated based on structural and resource metrics, these provide precise guidance on resolving performance issues. | ||
|
|
||
| .. image:: images/expl_tensor_recs.png | ||
| :alt: Example of Explain Tensor recommendations | ||
| :align: center | ||
|
|
||
| * **Personal Archive** – Contains all the plans you've analyzed, giving you instant access regardless of whether they were published publicly. | ||
|
|
||
| .. image:: images/expl_tensor_personal.png | ||
| :alt: Example of Explain Tensor personal archive | ||
| :align: center | ||
|
|
||
|
|
||
| Formatting SQL Code | ||
| ******************* | ||
|
|
||
| The **Format SQL** feature automatically indents and aligns SQL statements for better clarity. | ||
|
|
||
| To format the SQL query, use the *Edit* → *Format SQL* button (or press ``Ctrl+K``). | ||
|
|
||
| Example input: | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| SELECT u.name, p.title FROM users u JOIN posts p ON u.id=p.user_id WHERE u.active=true ORDER BY p.created_at DESC; | ||
|
|
||
| Formatted output: | ||
|
|
||
| .. code-block:: sql | ||
|
|
||
| SELECT | ||
| u.name | ||
| , p.title | ||
| FROM | ||
| users u | ||
| JOIN | ||
| posts p | ||
| ON u.id = p.user_id | ||
| WHERE | ||
| u.active = TRUE | ||
| ORDER BY | ||
| p.created_at DESC; | ||
|
|
||
|
|
||
| This makes complex queries easier to read and debug. | ||
|
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.