kenny-evitt
It’s very possible I’m overthinking this! But I also haven’t been able to find anything (via Google) about this specifically.
AFAIK, the only way to create temporary tables via Ecto is using the query/4 function available thru the Ecto SQL adapter (or its alias on the Ecto repo modules). That’s fine.
But how do temporary tables work with Ecto connection pooling and PostgreSQL ‘sessions’? Do I need to run queries that use a temporary table in a transaction that includes the query/4 call that creates the temporary table?
Are there reasons why I might want to create a regular table instead of a (PostgreSQL) temporary table (and then delete the regular table after it’s no longer needed)?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
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
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kip
As you know, temporary tables are deleted at the end of session or transaction. Since a connection (maintained by Postgrex) is a session and connections are reused, you would be best served by ensuring you wrap the temporary table lifecycle in a transaction so the table is deleted at the end of the transaction.
Hard to say the best strategy for you use case on the available data. However using permanent tables transiently means you need to manage unique naming and cleanup in the case of transaction rollback or server crashes.
kenny-evitt
Thanks – I did know the first part, but I hadn’t seen any explicit mention of connections being (equivalent to) sessions. That’s one thing I was worried about.
One potential workaround, to avoid using a transaction, seems to be to use a dynamic repo:
The example has a
:pool_sizesetting of1, which I’m guessing indicates a ‘pool’ with a single connection.But a transaction should be fine too for my case.
Of course – but that’s probably easily handled for my use case, i.e. a daily background job.
kip
One thing to remember is that every statement is always wrapped in a transaction. A single SQL statement is wrapped in an implicit transaction. And of course developers and users can declare explicit transactions.
I don’t think transactions are anything to be avoided or worked around since they underpin the ACID properties of a database.
kenny-evitt
Thanks – those are all good points to make explicit for anyone else that finds this post.
I’ve found it generally hard to reason about ‘transaction semantics’ in systems that weren’t designed with it in mind. After the fact, it’s not usually clear what semantics are ‘correct’ and considerations ‘devolve’ to cost-benefit analysis of the relevant tradeoffs (including development/admin time). Or at least that’s been true of most of my experience!
Interestingly, Microsoft SQL Server seems to explicitly cleanup ‘session data’, e.g. temporary tables, when a database connection in a pool is re-used:
How have you used PostgreSQL temporary tables with Ecto? Strictly in transactions? Or (also) with
DROP ON COMMIT?kip
That’s an interesting read, thanks for the link (about SQL Server). As I read it, I think the the SQLServer connection pool is calling a stored procedure to clear temp tables at either connection checkin or checkout (I didn’t see clearly which).
In Elixir, you can implement very similar (same?) semantics with
Interestingly, this post from @josevalim notes that using
Repo.transaction/1instead ofRepo.checkout/1will likely have better performance.I don’t have a common use case for temp tables but I found this post on stackoverflow that gives good guidance for using
CREATE TEMPORARY TABLE some_table ON COMMIT DROP AS SELECT .....which also appears to suggest a transaction is required with this form to get the expected results.hauleth
I just wonder - what do you need temporary tables for?
kenny-evitt
Thanks! I hadn’t seen (or remembered)
Repo.checkout/1. That looks like what I’d want if I couldn’t use a transaction.kenny-evitt
It turned out that I didn’t need them; or at least not yet!
There are some alternatives, like PostgreSQL materialized views, but I was testing temporary tables and those alternatives because of ‘bad’ performance for a big recurring query.
I was curious about how others were using them with Ecto and PostgreSQL because I hadn’t found any detailed examples, e.g. of ‘checking-out’ a connection, or otherwise isolating a temporary table from other sessions/connections in the same pool.
hauleth
If that was in recurring query then temporary table should not help much, and if that helped anyway (because was probably used more than once in query) then you can achieve exactly the same with CTE.
kenny-evitt
I don’t think CTEs are generally equivalent to temporary tables, even just performance-wise. One difference being that you can’t index the CTE data. Another being that a CTE can only be used with one query (tho I’d expect PostgreSQL would likely re-use its ‘query plan’ if the same CTE was used for multiple queries).
I really like PostgreSQL’s materialized views as an alternative to temporary tables too. Like tables, they can be indexed. And there’s nice concise syntax for ‘refreshing’ one of those views. The only downside is that it persists, tho that seems like a relatively small cost, especially if it’s going to be used regularly anyways.