0

PostgreSQL work_mem with spring framework

It's possible to increase work_mem for one special query which is implemented with Spring Framework?

My repository class is:

@Repository
public interface AccountSpringRepository extends JpaRepository<Account, Long> {

...

    @Query(value = "SELECT DISTINCT a FROM Account a here_is_the_query> now())")
    List<Account> findAccountByStatus(
            @Param("status") List<EnumStatusType> status,
            Pageable limit);
}

It's possible and how to increase work_mem for the given query?

SET work_mem = '256MB';

Regards, Rafal

1
  • Did you try @Query(value = "set work_mem = '128MB'; SELECT DISTINCT a FROM Account a here_is_the_query> now())") Commented Aug 28, 2019 at 14:53

2 Answers 2

1

I don't know how to do it in the framework specifically, but the way this is done in general is:

  • start a transaction

  • run

    SET LOCAL work_mem = '256MB';  --valid only for the transaction
    
  • run your query

  • commit the transaction

Sign up to request clarification or add additional context in comments.

3 Comments

I know this with LOCAL. But without LOCAL this sould be changed for all followed queries. Why I can't see the new value, when I call "SHOW work_mem" in pgAdmin? I have to evalute the change before we put this on production.
SET changes the parameter only for the current session. Do you really want this for all queries? Do you have low enough max_connections to afford such a high value? If yes, you'd set it in postgresql.conf and reload to activate it.
I changed it only fo the current trancaction.
0

I did this:

@Modifying @Query(value = "SET work_mem = '128MB'", nativeQuery = true) void setWorkMem();

and this:

statement = connection.createStatement(); statement.execute("SET work_mem = '128MB';"); statement.execute("insert into public.test values('trest')");

And when I call "SHOW work_mem" than I get the DEFAULT value back. And in the postgresql log I see:

LOG: Ausführen <unnamed>: SET work_mem = '128MB' -> called from Java code

and not

LOG: Anweisung: -- SET work_mem = '128MB'; -> called from pgAdmin

Why?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.