-
Notifications
You must be signed in to change notification settings - Fork 69
AWS Compliance query performance > VPC #915
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
base: main
Are you sure you want to change the base?
Conversation
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.
Pull Request Overview
This PR optimizes AWS VPC compliance query performance by implementing various PostgreSQL optimization techniques. The changes focus on improving query execution times through better use of materialized CTEs, efficient JOIN patterns, and elimination of inefficient subqueries.
Key Changes:
- Replaced
NOT IN
subqueries withLEFT JOIN
patterns for better performance - Added materialized CTEs to cache expensive operations in memory
- Simplified complex OR conditions with array-based port definitions and JOINs
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
File | Description |
---|---|
conformance_pack/vpc.pp | Updated 9 VPC compliance queries with performance optimizations including materialized CTEs, LATERAL JOINs, and improved query patterns |
VPC_query_optimization.md | Added comprehensive documentation of performance improvements with before/after metrics and optimization technique explanations |
conformance_pack/vpc.pp
Outdated
with ingress_ssh_rules as ( | ||
with common_ports as ( | ||
select unnest(array[ | ||
20, 21, 22, 23, 25, 80, 110, 135, 143, 445, 443, 1433, 1434, 3306, 3389, 4333, 445, 5432, 5500, 5601, 8080, 9200, 9300 |
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.
Port 445 is duplicated in the array. The array contains [20, 21, 22, 23, 25, 80, 110, 135, 143, 445, 443, 1433, 1434, 3306, 3389, 4333, 445, 5432, 5500, 5601, 8080, 9200, 9300] where 445 appears twice at positions 10 and 17.
20, 21, 22, 23, 25, 80, 110, 135, 143, 445, 443, 1433, 1434, 3306, 3389, 4333, 445, 5432, 5500, 5601, 8080, 9200, 9300 | |
20, 21, 22, 23, 25, 80, 110, 135, 143, 445, 443, 1433, 1434, 3306, 3389, 4333, 5432, 5500, 5601, 8080, 9200, 9300 |
Copilot uses AI. Check for mistakes.
conformance_pack/vpc.pp
Outdated
from | ||
aws_vpc_route_table | ||
), | ||
subnets_with_explicit_route as materialized( |
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.
There is an extra space before 'materialized'. It should be 'subnets_with_explicit_route as materialized(' to be consistent with other CTEs in the file.
subnets_with_explicit_route as materialized( | |
subnets_with_explicit_route as materialized( |
Copilot uses AI. Check for mistakes.
conformance_pack/vpc.pp
Outdated
), public_subnets_with_implicit_route as ( | ||
select | ||
), | ||
public_subnets_with_implicit_route as materialized( |
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.
There is an extra space before 'materialized'. It should be 'public_subnets_with_implicit_route as materialized(' to be consistent with other CTEs in the file.
public_subnets_with_implicit_route as materialized( | |
public_subnets_with_implicit_route as materialized( |
Copilot uses AI. Check for mistakes.
conformance_pack/vpc.pp
Outdated
group by | ||
route_table_id | ||
from route_tables rt, | ||
lateral jsonb_array_elements(rt.routes) as r |
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.
The LATERAL JOIN syntax is inconsistent with the WHERE clause. The code uses 'r.value' in the WHERE clause but the LATERAL JOIN should alias the elements properly. It should be 'lateral jsonb_array_elements(rt.routes) as r(value)' or the WHERE clause should use 'r' instead of 'r.value'.
lateral jsonb_array_elements(rt.routes) as r | |
lateral jsonb_array_elements(rt.routes) as r(value) |
Copilot uses AI. Check for mistakes.
VPC_query_optimization.md
Outdated
) | ||
or ( | ||
from_port <= 1433 | ||
and to_port >= 3389 |
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.
The documentation shows incorrect port range logic in the old query example. The condition 'from_port <= 1433 and to_port >= 3389' would never match port 1433 since it requires to_port >= 3389. This appears to be a documentation of a bug that was fixed in the optimization.
and to_port >= 3389 | |
and to_port >= 1433 |
Copilot uses AI. Check for mistakes.
VPC Query Optimization
Performance Improvements Summary
1. vpc_configured_to_use_vpc_endpoints
Key Changes:
NOT IN
subqueries withLEFT JOIN
Old Query:
New Query:
2. vpc_security_group_restricted_common_ports
Key Changes:
Old Query:
New Query:
3. vpc_gateway_endpoint_restrict_public_access
Key Changes:
as materialized
to CTEsusing (vpc_endpoint_id)
instead ofon
clauseOld Query:
New Query:
4. vpc_network_acl_remote_administration
Key Changes:
as materialized
to CTEsORDER BY
from CTEs (moved to final query)using (network_acl_id)
instead ofon
clauseOld Query:
New Query:
5. vpc_security_group_associated_to_eni
Key Changes:
as materialized
to CTEson
clause instead ofusing
(different column names)Old Query:
New Query:
6. vpc_route_table_restrict_public_access_to_igw
Key Changes:
as materialized
to CTEsLATERAL JOIN
for JSON processingOld Query:
New Query:
7. vpc_security_group_restrict_ingress_cifs_port_all
Key Changes:
as materialized
to CTEsusing (group_id)
instead ofon
clauseOld Query:
New Query:
8. vpc_peering_connection_route_table_least_privilege
Key Changes:
as materialized
to CTEsOld Query:
New Query:
9. vpc_subnet_public_and_private
Key Changes:
as materialized
to all CTEsLATERAL JOIN
for JSON processingIN
subqueries withEXISTS
bool_or()
aggregation for efficient checksOld Query:
New Query:
Optimization Techniques Summary
1. Materialized CTEs (
as materialized
)2. LATERAL JOIN
3. EXISTS vs IN
4. LEFT JOIN vs NOT IN
5. Array-based Port Definition
6. Using Clause
7. bool_or() Aggregation
Overall Performance Impact
Total Average Improvement: 17% faster queries