-
-
Notifications
You must be signed in to change notification settings - Fork 23
feat: pgsql query plan analyzer support #479
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
p4veI
wants to merge
1
commit into
staabm:main
Choose a base branch
from
p4veI:pk-psql-anlyzer
base: main
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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace staabm\PHPStanDba\Analyzer; | ||
|
||
interface Analyzer | ||
{ | ||
public function analyze(string $query): QueryPlanResult; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace staabm\PHPStanDba\Analyzer; | ||
|
||
use PDO; | ||
use PHPStan\ShouldNotHappenException; | ||
use staabm\PHPStanDba\QueryReflection\QueryReflection; | ||
|
||
final class QueryPlanAnalyzerPgSql implements Analyzer | ||
{ | ||
/** @var PDO */ | ||
private $connection; | ||
|
||
public function __construct(PDO $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
public function analyze(string $query): QueryPlanResult | ||
{ | ||
$simulatedQuery = 'EXPLAIN (FORMAT JSON) '.$query; | ||
|
||
$stmt = $this->connection->query($simulatedQuery); | ||
|
||
return $this->buildResult($simulatedQuery, $stmt); | ||
} | ||
|
||
/** @param \PDOStatement<array<float|int|string|null>> $stmt */ | ||
private static function buildResult(string $simulatedQuery, $stmt): QueryPlanResult | ||
{ | ||
$allowedUnindexedReads = QueryReflection::getRuntimeConfiguration()->getNumberOfAllowedUnindexedReads(); | ||
if (false === $allowedUnindexedReads) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$allowedRowsNotRequiringIndex = QueryReflection::getRuntimeConfiguration()->getNumberOfRowsNotRequiringIndex(); | ||
if (false === $allowedRowsNotRequiringIndex) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$queryPlanResult = new QueryPlanResult($simulatedQuery); | ||
$result = $stmt->fetch(); | ||
$queryPlans = \is_array($result) && \array_key_exists('QUERY PLAN', $result) | ||
? json_decode($result['QUERY PLAN'], true) | ||
: []; | ||
\assert(\is_array($queryPlans)); | ||
foreach ($queryPlans as $queryPlan) { | ||
$plan = $queryPlan['Plan']; | ||
$table = $plan['Alias'] ?? null; | ||
|
||
if (null === $table) { | ||
continue; | ||
} | ||
|
||
$rowReads = $plan['Plan Rows']; | ||
$nodeType = $plan['Node Type']; | ||
if ('Seq Scan' === $nodeType && $rowReads >= $allowedRowsNotRequiringIndex) { | ||
$queryPlanResult->addRow($table, QueryPlanResult::ROW_SEQ_SCAN); | ||
} | ||
if ('Bitmap Heap Scan' === $nodeType) { | ||
$queryPlanResult->addRow($table, QueryPlanResult::ROW_BITMAP_HEAP_SCAN); | ||
} | ||
if ('Bitmap Index Scan' === $nodeType) { | ||
$queryPlanResult->addRow($table, QueryPlanResult::ROW_BITMAP_INDEX_SCAN); | ||
} | ||
if ('Index Scan' === $nodeType) { | ||
$queryPlanResult->addRow($table, QueryPlanResult::ROW_INDEX_SCAN); | ||
} | ||
if ('Aggregate' === $nodeType) { | ||
$queryPlanResult->addRow($table, QueryPlanResult::ROW_AGGREGATE); | ||
} | ||
if ('Hash Aggregate' === $nodeType) { | ||
$queryPlanResult->addRow($table, QueryPlanResult::ROW_HASH_AGGREGATE); | ||
} | ||
} | ||
|
||
return $queryPlanResult; | ||
} | ||
} |
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
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
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use staabm\PHPStanDba\Analyzer\QueryPlanResult; | ||
use staabm\PHPStanDba\QueryReflection\QueryReflection; | ||
use staabm\PHPStanDba\Rules\QueryPlanAnalyzerRule; | ||
|
||
|
@@ -48,10 +49,6 @@ public static function getAdditionalConfigFiles(): array | |
|
||
public function testNotUsingIndex(): void | ||
{ | ||
if ('pdo-pgsql' === getenv('DBA_REFLECTOR')) { | ||
$this->markTestSkipped('query plan analyzer is not yet implemented for pgsql'); | ||
} | ||
|
||
if ('recording' !== getenv('DBA_MODE')) { | ||
$this->markTestSkipped('query plan analyzer requires a active database connection'); | ||
} | ||
|
@@ -60,7 +57,9 @@ public function testNotUsingIndex(): void | |
$this->numberOfRowsNotRequiringIndex = 2; | ||
|
||
$proposal = "\n\nConsider optimizing the query.\nIn some cases this is not a problem and this error should be ignored."; | ||
$tip = 'see Mysql Docs https://dev.mysql.com/doc/refman/8.0/en/select-optimization.html'; | ||
$tip = 'pdo-pgsql' === getenv('DBA_REFLECTOR') | ||
? QueryPlanResult::PGSQL_TIP | ||
: 'see Mysql Docs https://dev.mysql.com/doc/refman/8.0/en/select-optimization.html'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should also put the mysql tip onto |
||
|
||
$this->analyse([__DIR__.'/data/query-plan-analyzer.php'], [ | ||
[ | ||
|
@@ -93,10 +92,6 @@ public function testNotUsingIndex(): void | |
|
||
public function testNotUsingIndexInDebugMode(): void | ||
{ | ||
if ('pdo-pgsql' === getenv('DBA_REFLECTOR')) { | ||
$this->markTestSkipped('query plan analyzer is not yet implemented for pgsql'); | ||
} | ||
|
||
if ('recording' !== getenv('DBA_MODE')) { | ||
$this->markTestSkipped('query plan analyzer requires a active database connection'); | ||
} | ||
|
@@ -106,16 +101,53 @@ public function testNotUsingIndexInDebugMode(): void | |
$this->numberOfRowsNotRequiringIndex = 2; | ||
|
||
$proposal = "\n\nConsider optimizing the query.\nIn some cases this is not a problem and this error should be ignored."; | ||
if ('pdo-pgsql' === getenv('DBA_REFLECTOR')) { | ||
$this->analyse([__DIR__.'/data/query-plan-analyzer.php'], [ | ||
[ | ||
"Query is not using an index on table 'ada'.".$proposal."\n\nSimulated query: EXPLAIN (FORMAT JSON) SELECT * FROM ada WHERE email = 'test@example.com'", | ||
12, | ||
QueryPlanResult::PGSQL_TIP, | ||
], | ||
[ | ||
"Query is not using an index on table 'ada'.".$proposal."\n\nSimulated query: EXPLAIN (FORMAT JSON) SELECT *,adaid FROM ada WHERE email = 'test@example.com'", | ||
17, | ||
QueryPlanResult::PGSQL_TIP, | ||
], | ||
[ | ||
"Query is not using an index on table 'ada'.".$proposal."\n\nSimulated query: EXPLAIN (FORMAT JSON) SELECT * FROM ada WHERE email = '1970-01-01'", | ||
22, | ||
QueryPlanResult::PGSQL_TIP, | ||
], | ||
[ | ||
"Query is not using an index on table 'ada'.".$proposal."\n\nSimulated query: EXPLAIN (FORMAT JSON) SELECT * FROM ada WHERE email = '1970-01-01'", | ||
23, | ||
QueryPlanResult::PGSQL_TIP, | ||
], | ||
[ | ||
"Query is not using an index on table 'ada'.".$proposal."\n\nSimulated query: EXPLAIN (FORMAT JSON) SELECT * FROM ada WHERE email = '1970-01-01'", | ||
28, | ||
QueryPlanResult::PGSQL_TIP, | ||
], | ||
[ | ||
'Unresolvable Query: Cannot simulate parameter value for type: mixed.', | ||
61, | ||
'Make sure all variables involved have a non-mixed type and array-types are specified.', | ||
], | ||
]); | ||
|
||
return; | ||
} | ||
|
||
$tip = 'see Mysql Docs https://dev.mysql.com/doc/refman/8.0/en/select-optimization.html'; | ||
|
||
$this->analyse([__DIR__.'/data/query-plan-analyzer.php'], [ | ||
[ | ||
"Query is not using an index on table 'ada'.".$proposal."\n\nSimulated query: EXPLAIN SELECT * FROM `ada` WHERE email = 'test@example.com'", | ||
"Query is not using an index on table 'ada'.".$proposal."\n\nSimulated query: EXPLAIN SELECT * FROM ada WHERE email = 'test@example.com'", | ||
12, | ||
$tip, | ||
], | ||
[ | ||
"Query is not using an index on table 'ada'.".$proposal."\n\nSimulated query: EXPLAIN SELECT *,adaid FROM `ada` WHERE email = 'test@example.com'", | ||
"Query is not using an index on table 'ada'.".$proposal."\n\nSimulated query: EXPLAIN SELECT *,adaid FROM ada WHERE email = 'test@example.com'", | ||
17, | ||
$tip, | ||
], | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renaming existing constants is a BC break. Please leave the old ones additionally in and mark them as deprecated