-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make set_transaction_id use the PK index for its lookup
This patch fixes a longstanding (yet undiscovered) bug in the `set_transaction_id` procedure that caused it to not use the provided index on `id` spanning `xact_id`. Reason was the use of `CURRVAL` in the `WHERE` clause which caused the query to evaluate every row against a new function call, turning the should-be-fast lookup into a seq scan of the `transactions` table. Consequently, `INSERT`s scaled linearly with the size of the `transactions` table, instead of logarithmically. Huge 🤦
- Loading branch information
Showing
4 changed files
with
119 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
defmodule Carbonite.Migrations.V9 do | ||
@moduledoc false | ||
|
||
use Ecto.Migration | ||
use Carbonite.Migrations.Version | ||
alias Carbonite.Migrations.V4 | ||
|
||
@type prefix :: binary() | ||
|
||
@spec create_set_transaction_id_procedure(prefix()) :: :ok | ||
def create_set_transaction_id_procedure(prefix) do | ||
""" | ||
CREATE OR REPLACE FUNCTION #{prefix}.set_transaction_id() RETURNS TRIGGER AS | ||
$body$ | ||
BEGIN | ||
BEGIN | ||
/* verify that no previous INSERT within current transaction (with same id) */ | ||
IF | ||
EXISTS( | ||
WITH constants AS ( | ||
SELECT | ||
COALESCE(NEW.id, CURRVAL('#{prefix}.transactions_id_seq')) AS id, | ||
COALESCE(NEW.xact_id, pg_current_xact_id()) AS xact_id | ||
) | ||
SELECT 1 FROM #{prefix}.transactions | ||
JOIN constants | ||
ON constants.id = transactions.id | ||
AND constants.xact_id = transactions.xact_id | ||
) | ||
THEN | ||
NEW.id = COALESCE(NEW.id, CURRVAL('#{prefix}.transactions_id_seq')); | ||
END IF; | ||
EXCEPTION WHEN object_not_in_prerequisite_state THEN | ||
/* when NEXTVAL has never been called within session, we're good */ | ||
END; | ||
NEW.id = COALESCE(NEW.id, NEXTVAL('#{prefix}.transactions_id_seq')); | ||
NEW.xact_id = COALESCE(NEW.xact_id, pg_current_xact_id()); | ||
RETURN NEW; | ||
END | ||
$body$ | ||
LANGUAGE plpgsql; | ||
""" | ||
|> squish_and_execute() | ||
|
||
:ok | ||
end | ||
|
||
@type up_option :: {:carbonite_prefix, prefix()} | ||
|
||
@impl true | ||
@spec up([up_option()]) :: :ok | ||
def up(opts) do | ||
prefix = Keyword.get(opts, :carbonite_prefix, default_prefix()) | ||
|
||
create_set_transaction_id_procedure(prefix) | ||
|
||
:ok | ||
end | ||
|
||
@type down_option :: {:carbonite_prefix, prefix()} | ||
|
||
@impl true | ||
@spec down([down_option()]) :: :ok | ||
def down(opts) do | ||
prefix = Keyword.get(opts, :carbonite_prefix, default_prefix()) | ||
|
||
V4.create_set_transaction_id_procedure(prefix) | ||
|
||
:ok | ||
end | ||
end |