Skip to content

Record issue metrics without subtransaction

What does this MR do?

Use UPSERT to create or update issue metrics so we do not need a subtransaction and multiple queries

I tested this with:

Issue.first.metrics.delete
Issue.first.save

Before:

  ...
  TRANSACTION (0.2ms)  SAVEPOINT active_record_1 /*application:console,line:/data/cache/bundle-2.7.2/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/savepoints.rb:11:in `create_savepoint'*/
  Issue::Metrics Load (0.3ms)  SELECT "issue_metrics".* FROM "issue_metrics" WHERE "issue_metrics"."issue_id" = $1 LIMIT $2 /*application:console,line:/data/cache/bundle-2.7.2/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:53:in `exec_query'*/  [["issue_id", 1], ["LIMIT", 1]]
  Issue::Metrics Create (1.0ms)  INSERT INTO "issue_metrics" ("issue_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" /*application:console,line:/data/cache/bundle-2.7.2/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:53:in `exec_query'*/  [["issue_id", 1], ["created_at", "2021-08-19 02:48:46.629584"], ["updated_at", "2021-08-19 02:48:46.629584"]]
  TRANSACTION (0.2ms)  RELEASE SAVEPOINT active_record_1 /*application:console,line:/data/cache/bundle-2.7.2/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/abstract/savepoints.rb:19:in `release_savepoint'*/
  ...
  Issue::Metrics Update (0.5ms)  UPDATE "issue_metrics" SET "first_associated_with_milestone_at" = $1, "updated_at" = $2 WHERE "issue_metrics"."id" = $3 /*application:console,line:/data/cache/bundle-2.7.2/ruby/2.7.0/gems/activerecord-6.1.3.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:70:in `exec_delete'*/  [["first_associated_with_milestone_at", "2021-08-19 02:48:46.632308"], ["updated_at", "2021-08-19 02:48:46.673098"], ["id", 451]]
  ...

After:

   ...
   (1.3ms)  INSERT INTO issue_metrics (issue_id, first_associated_with_milestone_at, first_added_to_board_at, created_at, updated_at)
VALUES (1, NOW(), NULL, NOW(), NOW())
ON CONFLICT (issue_id)
DO UPDATE SET
  first_associated_with_milestone_at = LEAST(issue_metrics.first_associated_with_milestone_at, EXCLUDED.first_associated_with_milestone_at),
  first_added_to_board_at = LEAST(issue_metrics.first_added_to_board_at, EXCLUDED.first_added_to_board_at),
  updated_at = NOW()
RETURNING id
 /*application:console,line:/app/models/issue/metrics.rb:28:in `record!'*/
 ...

So this also reduces the number of SQL queries in the worst case because previously we safe_find_or_create and then update the record. Now it would be a single upsert all the time.

In the best and most common case where the record exists, the old code would do a single select query while the new code will do a no-op upsert.

Related to #338815 (closed)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

Does this MR contain changes to processing or storing of credentials or tokens, authorization and authentication methods or other items described in the security review guidelines? If not, then delete this Security section.

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team
Edited by Heinrich Lee Yu

Merge request reports

Loading