Skip to content

Combine project timestamps updates into a single statement

What does this MR do and why?

We currently have these Event callbacks:

  after_create :reset_project_activity
  after_create :set_last_repository_updated_at, if: :push_action?

Each one may produce separate UPDATE statements.

Old statements

-- reset_project_activity
UPDATE
        "projects"
SET
        "updated_at" = $ 1,
        "last_activity_at" = $ 2
WHERE
        "projects"."id" = $ 3
        AND (last_activity_at <= $ 4)
-- set_last_repository_updated_at
UPDATE
        "projects"
SET
        "updated_at" = $ 1,
        "last_repository_updated_at" = $ 2
WHERE
        "projects"."id" = $ 3
        AND (
                last_repository_updated_at < $ 4
                OR last_repository_updated_at IS NULL
        )

These two statements are one of the top contributors to WAL generated for projects table.

This MR combines them into a single statement, so that the write amplification cost is paid only once.

New combined statement

UPDATE
    "projects"
SET
    "updated_at" = '2024-06-25 22:16:40.944515',
        -- In case we are doing an update we can as well update both timestamps
    "last_activity_at" = '2024-06-25 22:16:40.944515',
    "last_repository_updated_at" = '2024-06-25 22:16:40.944515'
WHERE
    "projects"."id" = 333
        -- Execute the update only if one of these conditions is met
    AND (
        last_activity_at <= '2024-06-25 21:16:40.947778'
        OR last_repository_updated_at < '2024-06-25 22:11:40.948814'
        OR last_repository_updated_at IS NULL
    )

https://console.postgres.ai/gitlab/gitlab-production-main/sessions/29492/commands/91625

This query can change and include just one of the timestamps if the other was recently updated, for example

  • update only last_activity_at
  • update only last_repository_updated_at

This change is behind the combined_project_update_on_event_creation feature flag.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Before After

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

Edited by Krasimir Angelov

Merge request reports