From 176dd4f82cd32a2780d733b930935e354435921b Mon Sep 17 00:00:00 2001 From: Katharina Przybill <30441792+kathap@users.noreply.github.com> Date: Thu, 26 Nov 2026 09:31:29 +0100 Subject: [PATCH 1/2] Add (type, created_at, id) composite index to events GET /v3/audit_events filtered by type over a time window did a full primary-key scan (~10s, ~19M rows examined) for lack of a (type, created_at) index. Adds events_type_created_at_id_index so the query seeks the type+time window directly; id is included to cover the deferred-join key. --- ...000_add_type_created_at_index_to_events.rb | 42 +++++++++++++++++++ ...dd_type_created_at_index_to_events_spec.rb | 35 ++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 db/migrations/20261124120000_add_type_created_at_index_to_events.rb create mode 100644 spec/migrations/20261124120000_add_type_created_at_index_to_events_spec.rb diff --git a/db/migrations/20261124120000_add_type_created_at_index_to_events.rb b/db/migrations/20261124120000_add_type_created_at_index_to_events.rb new file mode 100644 index 00000000000..f785bf4be4a --- /dev/null +++ b/db/migrations/20261124120000_add_type_created_at_index_to_events.rb @@ -0,0 +1,42 @@ +# rubocop:disable Metrics/BlockLength +Sequel.migration do + no_transaction # required for concurrently option on postgres + + up do + if database_type == :postgres + VCAP::Migration.with_concurrent_timeout(self) do + add_index :events, %i[type created_at id], + name: :events_type_created_at_id_index, + if_not_exists: true, + concurrently: true + end + else + alter_table(:events) do + # rubocop:disable Sequel/ConcurrentIndex + unless @db.indexes(:events).key?(:events_type_created_at_id_index) + add_index %i[type created_at id], + name: :events_type_created_at_id_index + end + # rubocop:enable Sequel/ConcurrentIndex + end + end + end + + down do + if database_type == :postgres + VCAP::Migration.with_concurrent_timeout(self) do + drop_index :events, nil, + name: :events_type_created_at_id_index, + if_exists: true, + concurrently: true + end + else + alter_table(:events) do + # rubocop:disable Sequel/ConcurrentIndex + drop_index nil, name: :events_type_created_at_id_index if @db.indexes(:events).key?(:events_type_created_at_id_index) + # rubocop:enable Sequel/ConcurrentIndex + end + end + end +end +# rubocop:enable Metrics/BlockLength diff --git a/spec/migrations/20261124120000_add_type_created_at_index_to_events_spec.rb b/spec/migrations/20261124120000_add_type_created_at_index_to_events_spec.rb new file mode 100644 index 00000000000..19aedfa6e9e --- /dev/null +++ b/spec/migrations/20261124120000_add_type_created_at_index_to_events_spec.rb @@ -0,0 +1,35 @@ +require 'spec_helper' +require 'migrations/helpers/migration_shared_context' + +RSpec.describe 'migration to add type/created_at composite index to events table', isolation: :truncation, type: :migration do + include_context 'migration' do + let(:migration_filename) { '20261124120000_add_type_created_at_index_to_events.rb' } + end + + describe 'events table' do + it 'adds the composite index and handles idempotency gracefully' do + # Before migration: composite index should not exist + expect(db.indexes(:events)).not_to include(:events_type_created_at_id_index) + + # Test up migration + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error + + expect(db.indexes(:events)).to include(:events_type_created_at_id_index) + + # Verify index column order + expect(db.indexes(:events)[:events_type_created_at_id_index][:columns]).to eq(%i[type created_at id]) + + # Test up migration idempotency: running again should not fail + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error + expect(db.indexes(:events)).to include(:events_type_created_at_id_index) + + # Test down migration + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error + expect(db.indexes(:events)).not_to include(:events_type_created_at_id_index) + + # Test down migration idempotency: running again should not fail + expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1, allow_missing_migration_files: true) }.not_to raise_error + expect(db.indexes(:events)).not_to include(:events_type_created_at_id_index) + end + end +end From 5ae23a820b81590ac80938b2fc7391d88a73c930 Mon Sep 17 00:00:00 2001 From: Katharina Przybill <30441792+kathap@users.noreply.github.com> Date: Thu, 26 Nov 2026 13:32:40 +0100 Subject: [PATCH 2/2] Add test to events spec for new index --- ...000_add_type_created_at_index_to_events.rb | 2 - spec/request/events_spec.rb | 39 +++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/db/migrations/20261124120000_add_type_created_at_index_to_events.rb b/db/migrations/20261124120000_add_type_created_at_index_to_events.rb index f785bf4be4a..e0bda158662 100644 --- a/db/migrations/20261124120000_add_type_created_at_index_to_events.rb +++ b/db/migrations/20261124120000_add_type_created_at_index_to_events.rb @@ -1,4 +1,3 @@ -# rubocop:disable Metrics/BlockLength Sequel.migration do no_transaction # required for concurrently option on postgres @@ -39,4 +38,3 @@ end end end -# rubocop:enable Metrics/BlockLength diff --git a/spec/request/events_spec.rb b/spec/request/events_spec.rb index f74ac41eb5f..6be1d5312fb 100644 --- a/spec/request/events_spec.rb +++ b/spec/request/events_spec.rb @@ -137,6 +137,45 @@ end end + context 'filtering by type and created_at range' do + # Mirrors the GET /v3/audit_events?types=...&created_ats[gt]=...&created_ats[lt]=... + # access pattern that the (type, created_at, id) composite index supports. + let!(:in_window_event) do + create(:event, type: 'audit.app.update', actee: app_model.guid, + space_guid: space.guid, organization_guid: org.guid, + created_at: Time.utc(2026, 11, 4, 12, 0, 0)) + end + let!(:before_window_event) do + create(:event, type: 'audit.app.update', actee: app_model.guid, + space_guid: space.guid, organization_guid: org.guid, + created_at: Time.utc(2026, 11, 1, 12, 0, 0)) + end + let!(:after_window_event) do + create(:event, type: 'audit.app.update', actee: app_model.guid, + space_guid: space.guid, organization_guid: org.guid, + created_at: Time.utc(2026, 11, 10, 12, 0, 0)) + end + # Same window, different type: must be excluded by the type filter. + let!(:other_type_in_window_event) do + create(:event, type: 'audit.app.restart', actee: app_model.guid, + space_guid: space.guid, organization_guid: org.guid, + created_at: Time.utc(2026, 11, 4, 13, 0, 0)) + end + + it 'returns only events of the given type within the created_at window' do + get '/v3/audit_events?types=audit.app.update' \ + '&created_ats[gt]=2026-11-03T00:00:00Z' \ + '&created_ats[lt]=2026-11-05T00:00:00Z', nil, admin_header + + expect(last_response).to have_status_code(200) + returned_guids = parsed_response['resources'].pluck('guid') + expect(returned_guids).to contain_exactly(in_window_event.guid) + expect(returned_guids).not_to include(before_window_event.guid) + expect(returned_guids).not_to include(after_window_event.guid) + expect(returned_guids).not_to include(other_type_in_window_event.guid) + end + end + context 'filtering by target_guid' do it 'returns filtered events' do get "/v3/audit_events?target_guids=#{app_model.guid}", nil, admin_header