Skip to content

Commit

Permalink
Merge pull request #193 from mdsol/feature/accessible_operations-opti…
Browse files Browse the repository at this point in the history
…mization

[MCC-893601] Performance optimization for `accessible_operations`
  • Loading branch information
ejinotti-mdsol authored Mar 25, 2022
2 parents 4f234bd + bd317e1 commit 34a0e24
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 4.0.3
* Updated PostgreSQL `operations_for_operation_sets` CTE to pre-aggregate `accessible_operations` before joining to
`policy_elements` for performance optimizations.

## 4.0.2
* Updated `accessible_objects_for_operations` code path to use a CTE when using a replica database.

Expand Down
2 changes: 1 addition & 1 deletion lib/policy_machine/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class PolicyMachine
VERSION = "4.0.2"
VERSION = "4.0.3"
end
12 changes: 5 additions & 7 deletions lib/policy_machine_storage_adapters/active_record/postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,23 @@ def self.operations_for_operation_sets(operation_set_ids, operation_names = nil)
assignments.parent_id,
accessible_operations.operation_set_id AS operation_set_id
FROM assignments
INNER JOIN accessible_operations
ON accessible_operations.child_id = assignments.parent_id
JOIN accessible_operations ON accessible_operations.child_id = assignments.parent_id
)
)
SELECT DISTINCT accessible_operations.operation_set_id, ops.unique_identifier
FROM accessible_operations
JOIN policy_elements ops
ON ops.id = accessible_operations.child_id
JOIN policy_elements ops ON ops.id = accessible_operations.child_id
WHERE ops.id IN (SELECT child_id FROM accessible_operations)
SQL

sanitize_arg = [query, operation_set_ids]

if operation_names
query << "WHERE ops.unique_identifier IN (?)"
query << "AND ops.unique_identifier IN (?)"
sanitize_arg << operation_names
else
type = PolicyMachineStorageAdapter::ActiveRecord.class_for_type('operation').name
query << "WHERE ops.type = '#{type}'"
query << "AND ops.type = '#{type}'"
end

sanitized_query = sanitize_sql_for_assignment(sanitize_arg)
Expand Down

0 comments on commit 34a0e24

Please sign in to comment.