MEHUL.JAIN

MEHUL.JAIN

Insert into Select from Unnest

I have a use case where I want to insert large number of rows in PG Table at once from Ecto. But ecto has a param limit of 65_535, so the max rows I can insert is 65_535 / tot_number_of_fields_in_schema.

To overcome this, I am trying to see if I can to
Repo.insert_all(Schema, query)
query = from(c in Ecto.Query.fragment("SELECT * FROM unnest(? as field1, ? as field2"), ^field1_array, ^field2_array)

The idea here is, the total params will be 2 in this case

-- before
INSERT INTO users
    (id, name)
    VALUES ($1,$2),($3,$4);

-- after
INSERT INTO users
    (id, name)
    (select * from unnest($1::int[], $2::int[]));

Most Liked

LostKobrakai

LostKobrakai

Take a look at:

Though I also found jsonb_to_recordset quite useful:

Last Post!

MEHUL.JAIN

MEHUL.JAIN

I have done this:

"d" 
|> with_cte("d", as: fragment("SELECT * FROM unnest(ARRAY[1]::int[], ARRAY[2]::int[]) as x(a, b)"))
|> select([d], %{a: d.a})

But how do I remove prefix from cte query? I have a multi tenant/schema application, so I need prefix when I would try to do
DBWrapper.insert_all(SchemaModule, query)

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New