4

As the title states, I can run the following query:

EXPLAIN ANALYZE SELECT * FROM accounts WHERE screen_name='realDonaldTrump';

and get the output:

Gather  (cost=1000.00..87090.59 rows=1 width=122) (actual time=369.168..383.632 rows=1 loops=1)
   Workers Planned: 2
   Workers Launched: 2   
->  Parallel Seq Scan on accounts  (cost=0.00..86090.49 rows=1 width=122) (actual time=312.741..326.849 rows=0 loops=3)
        Filter: ((screen_name)::text = 'realDonaldTrump'::text)
        Rows Removed by Filter: 1156140 
Planning Time: 0.065 ms 
Execution Time: 383.649 ms

Now my question is how can I transform this query to use more or use less workers? Online I found some parameters like max_parallel_workers, but I am unable to incorporate them into the query. I am running this query from pycharm -> postgresql console.

Edit: After running the query from the comment, this is the output:

Gather  (cost=1000.00..87090.59 rows=1 width=122) (actual time=348.717..403.839 rows=1 loops=1)
  Workers Planned: 2
  Workers Launched: 2
  ->  Parallel Seq Scan on accounts  (cost=0.00..86090.49 rows=1 width=122) (actual time=311.207..324.566 rows=0 loops=3)
        Filter: ((screen_name)::text = 'realDonaldTrump'::text)
        Rows Removed by Filter: 1156140
Planning Time: 0.563 ms
Execution Time: 403.856 ms

Edit 2: After messing around with the postgresql.conf file and changing values of max_parallel_workers_per_gather and max_parallel_workers and then restarting postgres, nothing changed. Only later after I ran this query:

SET max_parallel_workers_per_gather = 4;

Did the initial sql query run with the requested 4 workers.

5
  • 1
    Why don't you create an index on screen_name? Commented Oct 6, 2020 at 12:09
  • Not inside the query. You either set it as a setting in the session, or a setting of the database/whole cluster. Commented Oct 6, 2020 at 12:12
  • I'm doing school work and we are learning about indexes. Commented Oct 6, 2020 at 12:26
  • Can I get a hint as to where I can change these settings? I changed the value for paralel_workers in my accounts table using pgAdmin but the query still executes with 2 workers instead of the specified number. Commented Oct 6, 2020 at 12:36
  • If you want to learn about indexes, then create the index and observe the results rather than taking the "brute force attack" with more workers Commented Oct 7, 2020 at 8:52

1 Answer 1

8

You can increase max_parallel_workers_per_gather, or you can

ALTER TABLE accounts SET (parallel_workers = 6);
Sign up to request clarification or add additional context in comments.

3 Comments

I understand the sql query as it is simple, but when I run it, the workers used (in the query from the question) are still only 2, not 6. And I can't for the life of me find max_parallel_workers_per_gather anywhere in pgAdmin, or am I supposed to look elsewhere?
That's a configuration patameter in postgresql.conf. Can you edit the question and add the EXPLAIN (ANALYZE) output for the query after you ran the ALTER TABLE?
Edited the question with the output.

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.