How to execute raw sql for each session?

93 views
Skip to first unread message

l.ri...@gmail.com

unread,
Jan 31, 2018, 11:34:28 AM1/31/18
to elixir-ecto
Hi,

I found a problem with executing raw sql.

In my query I want to use `SET work_mem = '32 MB'` to increase the Postgres work_mem for this particular session, leaving the system default for all others.

I have not found a way to do this, whenever I prepend this statement before my query, I run into an error with prepared statements.

** (Postgrex.Error) ERROR 42601 (syntax_error): cannot insert multiple commands into a prepared statement

I have sifted through all READMEs to see how I can disable prepared statements for a particular query, there seems to be no way.

What can I do about this?
How would I run some SQL for each connection before I use it?

Any help highly appreciated.

Kind regards,


Lukas Rieder 

José Valim

unread,
Jan 31, 2018, 2:32:33 PM1/31/18
to elixi...@googlegroups.com
You can use the after connect callback. For example, in your config files:

    after_connect: {Postgrex, :query!, ["SET search_path TO global_prefix", []]}

You can also specify your own module that will receive the Postgrex connection as argument.

I will add some documentation to the adapters.

José Valim
Founder and 
Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/ad5d923e-996d-4997-9d60-91b3f8539353%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

l.ri...@gmail.com

unread,
Feb 1, 2018, 5:11:09 AM2/1/18
to elixir-ecto
Thank you so much, that worked really well!
If you point me to the right files, I am happy to submit a PR for documentation.

:)


On Wednesday, January 31, 2018 at 8:32:33 PM UTC+1, José Valim wrote:
You can use the after connect callback. For example, in your config files:

    after_connect: {Postgrex, :query!, ["SET search_path TO global_prefix", []]}

You can also specify your own module that will receive the Postgrex connection as argument.

I will add some documentation to the adapters.

José Valim
Founder and 
Director of R&D

On Wed, Jan 31, 2018 at 5:34 PM, <l.ri...@gmail.com> wrote:
Hi,

I found a problem with executing raw sql.

In my query I want to use `SET work_mem = '32 MB'` to increase the Postgres work_mem for this particular session, leaving the system default for all others.

I have not found a way to do this, whenever I prepend this statement before my query, I run into an error with prepared statements.

** (Postgrex.Error) ERROR 42601 (syntax_error): cannot insert multiple commands into a prepared statement

I have sifted through all READMEs to see how I can disable prepared statements for a particular query, there seems to be no way.

What can I do about this?
How would I run some SQL for each connection before I use it?

Any help highly appreciated.

Kind regards,


Lukas Rieder 

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.

José Valim

unread,
Feb 1, 2018, 6:05:47 AM2/1/18
to elixi...@googlegroups.com
I have already updated the docs after asked :) thanks for the offer though!

--
Reply all
Reply to author
Forward
0 new messages