Re: Getting an index scan to be a parallel index scan

2023-02-01 Thread Ranier Vilela
Em qua., 1 de fev. de 2023 às 02:39, Alex Kaiser 
escreveu:

> Hello,
>
> I'm trying to get the following query to use a plan with parallelism, but
> I haven't been successful and would like some advice.
>
> The schema and table that I'm using is this:
>
> CREATE TABLE testing(
>id INT,
>info INT,
>data_one TEXT,
>data_two TEXT,
>primary key(id, info)
> );
>
> INSERT INTO testing(id, info, data_one, data_two)
> SELECT idx, idx, md5(random()::text), md5(random()::text)
> FROM generate_series(1,1000) idx;
>
> Then the query that I'm trying to run is this (I'll include the full query
> at the very end of the email because it is long:
>
> select * from testing where id in (1608377,5449811, ... <1000 random ids>
> ,4654284,3558460);
>
> Essentially I have a list of 1000 ids and I would like the rows for all of
> those ids.
>
> This seems like it would be pretty easy to parallelize, if you have X
> threads then you would split the list of IDs into 1000/X sub lists and give
> one to each thread to go find the rows for ids in the given list.  Even
> when I use the following configs I don't get a query plan that actually
> uses any parallelism:
>
> psql (15.1 (Debian 15.1-1.pgdg110+1))
> Type "help" for help.
>
> postgres=# show max_parallel_workers;
>  max_parallel_workers
> --
>  8
> (1 row)
>
> postgres=# set max_parallel_workers_per_gather = 8;
> SET
> postgres=# set parallel_setup_cost = 0;
> SET
> postgres=# set parallel_tuple_cost = 0;
> SET
> postgres=# set force_parallel_mode = on;
> SET
> postgres=# explain select * from testing where id in (1608377,5449811, ...
>  ... ,4654284,3558460);
>
Can you try:
select * from testing where id any = (values(1608377),(5449811),(5334677)
...  ... ,(4654284),(3558460));

Or alternately you can use EXTEND STATISTICS to improve Postgres planner
choice.

regards,
Ranier Vilela


Re: Getting an index scan to be a parallel index scan

2023-02-01 Thread David Rowley
On Wed, 1 Feb 2023 at 18:39, Alex Kaiser  wrote:
> postgres=# set force_parallel_mode = on;

There's been a bit of debate about that GUC and I'm wondering how you
came to the conclusion that it might help you. Can you share details
of how you found out about it and what made you choose to set it to
"on"?

David




Re: Getting an index scan to be a parallel index scan

2023-02-01 Thread Alex Kaiser
Rainier,

I tried using the any syntax (had to modify your query slightly) and it
didn't result in any change in the query plan.

postgres=# explain select * from testing where id =
ANY(array[1608377,5449811, ...  ...
,4654284,3558460]::integer[]);
QUERY PLAN
--
 Gather  (cost=0.43..6138.81 rows=1000 width=74)
   Workers Planned: 1
   Single Copy: true
   ->  Index Scan using testing_pkey on testing  (cost=0.43..6138.81
rows=1000 width=74)
 Index Cond: (id = ANY ('{1608377,5449811, ...  ... ,4654284,3558460}'::integer[]))

I've never messed around with extended statistics, but I'm not sure how
they would help here. From what I've read they seem to help when your query
is restricting over multiple columns. Since this query is only on one
column I'm not sure what a good "CREATE STATISTICS ..." command to run
would be to improve the query plan. Any suggestions?


David,

As for how I found 'force_parallel_mode', I think I found it first here:
https://postgrespro.com/list/thread-id/2574997 and then I also saw it when
I was searching for 'parallel' on https://postgresqlco.nf .

It's not that I think the parameter would help my query, it was really as a
last resort to try and force the query to be parallel. Without that
parameter, it just does a normal index scan (see the result below). My
thinking with using that parameter was to see if I could force a parallel
query plan just to see if maybe the planner just thought the parallel plan
would be more expensive. So I was surprised to see that even with that
parameter turned on it doesn't actually do anything in parallel.  Here is
the plan with that parameter turned off:

postgres=# set force_parallel_mode = off;
SET
postgres=# explain select * from testing where id =
ANY(array[1608377,5449811, ...  ...
,4654284,3558460]::integer[]);
QUERY PLAN

 Index Scan using testing_pkey on testing  (cost=0.43..6138.81 rows=1000
width=74)
   Index Cond: (id = ANY ('{1608377,5449811, ... < removed for brevity >
... 4654284,3558460}'::integer[]))
(2 rows)


Thanks,
Alex Kaiser

On Wed, Feb 1, 2023 at 3:30 AM David Rowley  wrote:

> On Wed, 1 Feb 2023 at 18:39, Alex Kaiser  wrote:
> > postgres=# set force_parallel_mode = on;
>
> There's been a bit of debate about that GUC and I'm wondering how you
> came to the conclusion that it might help you. Can you share details
> of how you found out about it and what made you choose to set it to
> "on"?
>
> David
>


Re: Getting an index scan to be a parallel index scan

2023-02-01 Thread Justin Pryzby
On Wed, Feb 01, 2023 at 11:22:47AM -0800, Alex Kaiser wrote:
> I've never messed around with extended statistics, but I'm not sure how
> they would help here. From what I've read they seem to help when your query
> is restricting over multiple columns. Since this query is only on one
> column I'm not sure what a good "CREATE STATISTICS ..." command to run
> would be to improve the query plan. Any suggestions?

They wouldn't help.  It seems like that was a guess.

> As for how I found 'force_parallel_mode', I think I found it first here:
> https://postgrespro.com/list/thread-id/2574997 and then I also saw it when
> I was searching for 'parallel' on https://postgresqlco.nf .

Yeah.  force_parallel_mode is meant for debugging, only, and we're
wondering how people end up trying to use it for other purposes.

http://rhaas.blogspot.com/2018/06/using-forceparallelmode-correctly.html

Did you try adjusting min_parallel_index_scan_size /
min_parallel_table_scan_size ?

-- 
Justin




Re: Getting an index scan to be a parallel index scan

2023-02-01 Thread Thomas Munro
On Wed, Feb 1, 2023 at 6:39 PM Alex Kaiser  wrote:
> select * from testing where id in (1608377,5449811, ... <1000 random ids> 
> ,4654284,3558460);
>
> Essentially I have a list of 1000 ids and I would like the rows for all of 
> those ids.
>
> This seems like it would be pretty easy to parallelize, if you have X threads 
> then you would split the list of IDs into 1000/X sub lists and give one to 
> each thread to go find the rows for ids in the given list.  Even when I use 
> the following configs I don't get a query plan that actually uses any 
> parallelism:

It sounds like the plan you are imagining is something like:

Gather
  Nested Loop Join
Outer side: 
Inner side: Index scan of your big table

Such a plan would only give the right answer if each process has a
non-overlapping subset of the constant values to probe the index with,
and together they have the whole set.  Hypothetically, a planner could
chop that set up beforehand and and give a different subset to each
process (just as you could do that yourself using N connections and
separate queries), but that might be unfair: one process might find
lots of matches, and the others might find none, because of the
distribution of data.  So you'd ideally want some kind of "work
stealing" scheme, where each worker can take more values to probe from
whenever it needs more, so that they all keep working until the values
run out.  We don't have a thing that can do that.  You might imagine
that a CTE could do it, so WITH keys_to_look_up AS (VALUES (1), (2),
...) SELECT ... JOIN ON ..., but that also doesn't work because we
don't have a way to do "partial" scans of CTEs either (though someone
could invent that).  Likewise for temporary tables: they are invisible
to parallel workers, so they can't help us.  I have contemplated
"partial function scans" for set-returning functions, where a function
could be given a bit of shared memory and various other infrastructure
to be able to be "parallel aware" (= able to coordinate across
processes so that each process gets a subset of the data), and one
could imagine that that would allow various solutions to the problem,
but that's vapourware.

But you can get a plan like that if you insert all those values into a
regular table, depending on various settings, stats and
min_parallel_table_scan_size (try 0, I guess that'll definitely do
it).  Which probably isn't the answer you wanted to hear.




Re: Getting an index scan to be a parallel index scan

2023-02-01 Thread Alex Kaiser
Justin,

I did try changing min_parallel_index_scan_size /
min_parallel_table_scan_size and didn't see any change (the below is with
force_parallel_mode = off):

postgres=# set min_parallel_index_scan_size = 0;
SET
postgres=# set min_parallel_table_scan_size = 0;
SET
postgres=# explain select * from testing where id =
ANY(array[1608377,5449811, ...  ...
,4654284,3558460]::integer[]);
QUERY PLAN

 Index Scan using testing_pkey on testing  (cost=0.43..6138.81 rows=1000
width=74)
   Index Cond: (id = ANY ('{1608377,5449811, ... < removed for brevity >
... 4654284,3558460}'::integer[]))
(2 rows)


As for 'force_parallel_mode', while this isn't "debugging PG", it isn't
something that I would actually turn on production, just something I was
playing with to see the cost of parallel queries when the planner might not
think they are the most efficient.


Thomas,

Thanks for the explanation. Yes, that is the query plan I was imagining. I
do see how chopping it up could result in an unfair distribution. But my
counter to that would be that wouldn't chopping it up still be better than
not. If things do happen to work out to be fair, now it's X times as fast,
if things are very unfair, then you haven't really lost much (besides the
parallel overhead) compared to the non-parallel query. Or maybe it should
be possible to do the parallel query if there were some statistics (either
normal ones or extended ones) that told the planner that the result would
probably be fair?

Though I do agree that the "work stealing" option would be the most
efficient, but would be a lot more complicated to code up.

I tried out inserting into a separate table, and as you guessed that
worked. For my production scenario that isn't really feasible, but still
cool to see it work.


postgres=# create table ids(
  probe_id int PRIMARY KEY
);

insert into ids(probe_id) values (774494);
insert into ids(probe_id) values (9141914);
...

postgres=# select count(*) from ids;
 count
---
  1000
(1 row)

postgres=# explain select * from testing where id in (select * from ids);
   QUERY PLAN
-
 Gather  (cost=0.43..3504.67 rows=1000 width=74)
   Workers Planned: 2
   ->  Nested Loop  (cost=0.43..3504.67 rows=417 width=74)
 ->  Parallel Seq Scan on ids  (cost=0.00..9.17 rows=417 width=4)
 ->  Index Scan using testing_pkey on testing  (cost=0.43..8.37
rows=1 width=74)
   Index Cond: (id = ids.probe_id)
(6 rows)

Thanks,
Alex Kaiser

On Wed, Feb 1, 2023 at 1:52 PM Thomas Munro  wrote:

> On Wed, Feb 1, 2023 at 6:39 PM Alex Kaiser  wrote:
> > select * from testing where id in (1608377,5449811, ... <1000 random
> ids> ,4654284,3558460);
> >
> > Essentially I have a list of 1000 ids and I would like the rows for all
> of those ids.
> >
> > This seems like it would be pretty easy to parallelize, if you have X
> threads then you would split the list of IDs into 1000/X sub lists and give
> one to each thread to go find the rows for ids in the given list.  Even
> when I use the following configs I don't get a query plan that actually
> uses any parallelism:
>
> It sounds like the plan you are imagining is something like:
>
> Gather
>   Nested Loop Join
> Outer side: 
> Inner side: Index scan of your big table
>
> Such a plan would only give the right answer if each process has a
> non-overlapping subset of the constant values to probe the index with,
> and together they have the whole set.  Hypothetically, a planner could
> chop that set up beforehand and and give a different subset to each
> process (just as you could do that yourself using N connections and
> separate queries), but that might be unfair: one process might find
> lots of matches, and the others might find none, because of the
> distribution of data.  So you'd ideally want some kind of "work
> stealing" scheme, where each worker can take more values to probe from
> whenever it needs more, so that they all keep working until the values
> run out.  We don't have a thing that can do that.  You might imagine
> that a CTE could do it, so WITH keys_to_look_up AS (VALUES (1), (2),
> ...) SELECT ... JOIN ON ..., but that also doesn't work because we
> don't have a way to do "partial" scans of CTEs either (though someone
> could invent that).  Likewise for temporary tables: they are invisible
> to parallel workers, so they can't help us.  I have contemplated
> "partial function scans" for set-returning functions, where a function
> could be given a bit of shared memory and various other infrastructure
> to be able to be "parallel aware" (= able to coordinate across
> processes so that each process gets a subset of the data), and one
> could imagine that that would allow various solutions to the prob

Re: Getting an index scan to be a parallel index scan

2023-02-01 Thread Thomas Munro
On Thu, Feb 2, 2023 at 1:54 PM Alex Kaiser  wrote:
> Thanks for the explanation. Yes, that is the query plan I was imagining. I do 
> see how chopping it up could result in an unfair distribution. But my counter 
> to that would be that wouldn't chopping it up still be better than not. If 
> things do happen to work out to be fair, now it's X times as fast, if things 
> are very unfair, then you haven't really lost much (besides the parallel 
> overhead) compared to the non-parallel query. Or maybe it should be possible 
> to do the parallel query if there were some statistics (either normal ones or 
> extended ones) that told the planner that the result would probably be fair?

Maybe, but unfairness multiplies if it's part of a larger plan; what
if the output of those nodes is the input to much more work, but now
THAT work is being done by one process?  But yeah, statistics could
help with that.  I'm vaguely aware that other systems that do more
partition-based parallelism spend a lot of effort on that sort of
thinking.

> Though I do agree that the "work stealing" option would be the most 
> efficient, but would be a lot more complicated to code up.

Yeah.  I probably used the wrong word; what I was describing is
(something like) page-based parallelism, where input gets chopped up
into arbitrary chunks and handed out to consumers on demand, but we
don't know anything about the values in those chunks; that allows for
many interesting kind of plans, and it's nice because it's fair.

Another kind of parallelism is partition-based, which PostgreSQL can
do in a limited sense: we can send workers into different partitions
of a table (what we can't do is partition the table on-the-fly, which
is central to most parallelism in some other systems).  Let's see:

CREATE TABLE testing(
   id INT,
   info INT,
   data_one TEXT,
   data_two TEXT,
   primary key(id, info)
) partition by hash (id);
create table testing_p0 partition of testing for values with (modulus
2, remainder 0);
create table testing_p1 partition of testing for values with (modulus
2, remainder 1);
INSERT INTO testing(id, info, data_one, data_two)
SELECT idx, idx, md5(random()::text), md5(random()::text)
FROM generate_series(1,1000) idx;
analyze;

explain select count(*) from testing where id in
(1608377,5449811,5334677,5458230,2053195,3572313,1949724,3559988,5061560,8479775,
...);

 Aggregate
   ->  Append
 ->  Index Only Scan using testing_p0_pkey on testing_p0 testing_1
 ->  Index Only Scan using testing_p1_pkey on testing_p1 testing_2

Hmph.  I can't seem to convince it to use Parallel Append.  I think it
might be because the planner is not smart enough to chop down the =ANY
lists to match the partitions.  One sec...

Ok I hacked my copy of PostgreSQL to let me set parallel_setup_costs
to negative numbers, and then I told it that parallelism is so awesome
that it makes your queries cost -100 timerons before they even
start.  Now I see a plan like:

 Gather  (cost=-99.57..-987689.45 rows=2000 width=74)
   Workers Planned: 2
   ->  Parallel Append  (cost=0.43..12110.55 rows=832 width=74)
 ->  Parallel Index Scan using testing_p0_pkey on testing_p0 testing_1
 ->  Parallel Index Scan using testing_p1_pkey on testing_p1 testing_2

But it's probing every index for every one of the values in the big
list, not just the ones that have a non-zero chance of finding a
match, which is a waste of cycles.  I think if the planner were
smarter about THAT (as it is for plain old "="), then the costing
would have chosen parallelism naturally by cost.

But it's probably not as cool as page-based parallelism, because
parallelism is limited by your partitioning scheme.

If I had more timerons myself, I'd like to try to make parallel
function scans, or parallel CTE scans, work...




Re: Getting an index scan to be a parallel index scan

2023-02-01 Thread David Rowley
On Thu, 2 Feb 2023 at 14:49, Thomas Munro  wrote:
> If I had more timerons myself, I'd like to try to make parallel
> function scans, or parallel CTE scans, work...

I've not really looked in detail but I thought parallel VALUES scan
might be easier than those two.

David




Re: Getting an index scan to be a parallel index scan

2023-02-01 Thread Alex Kaiser
Okay after reading
http://rhaas.blogspot.com/2018/06/using-forceparallelmode-correctly.html I
do see that I was using force_parallel_mode incorectly and wouldn't have
gotten what I wanted even if the original query was possible to parallelize.

> Maybe, but unfairness multiplies if it's part of a larger plan

Ah, I didn't think of that, and it's a good point.

> Ok I hacked my copy of PostgreSQL to let me set parallel_setup_costs
> to negative numbers ...

Thanks for taking the time to do that and look into that.  I don't actually
think it's worth the confusion to allow this in general, but I was thinking
that setting "force_parallel_mode = on" would essentially be doing
something equivalent to this (though I now see that is wrong).

> But it's probing every index for every one of the values in the big
> list, not just the ones that have a non-zero chance of finding a
> match, which is a waste of cycles.

In my case, this would actually be quite helpful because the real
bottleneck when I run this in production is time spent waiting for IO.  I
was hoping to spread that IO wait time over multiple threads, and wouldn't
really care about the few extra wasted CPU cycles.  But I can't actually do
this as I can't set parallel_setup_costs to be negative, so I wouldn't be
able to get PG to choose the parallel plan even if I did partition the
table.

> If I had more timerons myself ...

If only we all had more timerons ... :)

Thanks,
Alex Kaiser

On Wed, Feb 1, 2023 at 6:12 PM David Rowley  wrote:

> On Thu, 2 Feb 2023 at 14:49, Thomas Munro  wrote:
> > If I had more timerons myself, I'd like to try to make parallel
> > function scans, or parallel CTE scans, work...
>
> I've not really looked in detail but I thought parallel VALUES scan
> might be easier than those two.
>
> David
>