Maxximiliann

Maxximiliann

Function:

def delete_single_record(table_name, key, identifier) do
Repo.transaction(fn ->
  selected_record =
    from(record in table_name,
      where: field(record, ^key) == ^identifier
    )

  {num_of_deleted_records, _} = Repo.delete_all(selected_record)

  case num_of_deleted_records do
    0 -> Repo.delete_all(selected_record)
    _ -> {:ok, num_of_deleted_records}
  end
end)
end

Exception:
2021-08-27T13:10:16.977283+00:00 error: ** Task 'Elixir.Foo' terminating, ** Started from <0.2203.1>, ** When function == fun erlang:apply/2, ** arguments == [#Fun<Elixir.SomeLambda.0.42097265>,[#{error => some_message}]], ** Reason for termination == , ** {#{'__exception__' => true,'__struct__' => 'Elixir.Postgrex.Error',connection_id => nil,message => nil,postgres => #{code => serialization_failure,detail => <<"Reason code: Canceled on identification as a pivot, during commit attempt.">>,file => <<"predicate.c">>,hint => <<"The transaction might succeed if retried.">>,line => <<"4853">>,message => <<"could not serialize access due to read/write dependencies among transactions">>,pg_code => <<"40001">>,routine => <<"PreCommit_CheckForSerializationFailure">>,severity => <<"ERROR">>,unknown => <<"ERROR">>},query => nil},[{'Elixir.DBConnection',run_transaction,4,[{file,"lib/db_connection.ex"},{line,1547}]},{'Elixir.Task.Supervised',invoke_mfa,2,[{file,"lib/task/supervised.ex"},{line,90}]},{'Elixir.Task.Supervised',reply,5,[{file,"lib/task/supervised.ex"},{line,35}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,226}]}]}

How can this exception be handled in such a way that the deletion is simply retried rather than the system crashing?

Showing Posts 1 to 6

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@maxximiliann you can catch the exception and try again, although you’ll likely want to place a limit on how many times you do this.

Can you elaborate a bit on what this code is supposed to do? Why does it try to do the delete again if no records were deleted?

Maxximiliann

Maxximiliann OP

That was my poor attempt at handling the exception, he h hehe he :slight_smile:

New attempt:

def delete_single_record(table_name, key, identifier) do
	Repo.transaction(fn ->
	  selected_record =
	    from(record in table_name,
	      where: field(record, ^key) == ^identifier
	    )

	  try do
	    Repo.delete_all(selected_record)
	  catch
	    Postgrex.Error -> Repo.delete_all(selected_record)
	  end
	end)
end

Result:

** (Mix) Could not start application MyApp: MyApp.Application.start(:normal, []) returned an error: shutdown: failed to start child: Supervisor.Main
    ** (EXIT) shutdown: failed to start child: MyGenServer
        ** (EXIT) an exception was raised:
            ** (Postgrex.Error) ERROR 40001 (serialization_failure) could not serialize access due to read/write dependencies among transactions

    hint: The transaction might succeed if retried.

Reason code: Canceled on identification as a pivot, during commit attempt.
                (db_connection 2.4.0) lib/db_connection.ex:1547: DBConnection.run_transaction/4
                (elixir 1.12.2) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
                (elixir 1.12.2) lib/task/supervised.ex:35: Task.Supervised.reply/5
                (stdlib 3.15.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

What am I missing?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I believe you need to wrap the whole Repo.transaction in a try, since the exception is occurring when the transaction commits.

I am sort of surprised you are getting this error though, since this transaction isn’t (as far as I can see) using the serializable isolation level, and it’s also just a single query. Are you eliding any parts of this function?

Maxximiliann

Maxximiliann OP

I ran ALTER DATABASE db_name SET DEFAULT_TRANSACTION_ISOLATION TO SERIALIZABLE; from Postgres to serialize all transactions. I wasn’t aware this could also be done straight from Elixir. Where can I read up on this?

Thanks for all your help! :slight_smile:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Ah. You can opt into serialized transactions on a transaction by transaction basis by doing:

Repo.transaction(fn ->
  Repo.query!("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE")
  # other stuff here
end)

For serializing all transactions setting the default as you’ve done is probably easiest.

Back to your original issue, calling delete_all twice in the same transaction isn’t going to help, you need to retry the whole transaction. Merely attempting it one more time may not be enough either, it may take several tries. I would reconsider whether you want this default.

Maxximiliann

Maxximiliann OP

Gotcha. This did the trick:


  def delete_single_record(table_name, key, identifier) do
    try do
      Repo.transaction(fn ->
        selected_record =
          from(record in table_name,
            where: field(record, ^key) == ^identifier
          )

        Repo.delete_all(selected_record)
      end)
    catch
      Postgrex.Error -> delete_single_record(table_name, key, identifier)
    end
  end

Thanks again! :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews