Re: Large insert and delete batches - Mailing list pgsql-performance

From Marti Raudsepp
Subject Re: Large insert and delete batches
Date
Msg-id CABRT9RAiNkwgbOU2hufF4megQWaZKXEqXmAcyi-+bWK=0y+NTw@mail.gmail.com
Whole thread Raw
In response to Re: Large insert and delete batches  (Kääriäinen Anssi <anssi.kaariainen@thl.fi>)
Responses Re: Large insert and delete batches
List pgsql-performance
On Thu, Mar 1, 2012 at 21:06, Kääriäinen Anssi <anssi.kaariainen@thl.fi> wrote:
> The queries are "select * from the_table where id = ANY(ARRAY[list_of_numbers])"
> and the similar delete, too.

> [...] However, once you go into
> millions of items in the list, the query will OOM my Postgres server.

The problem with IN() and ARRAY[] is that the whole list of numbers
has to be parsed by the SQL syntax parser, which has significant
memory and CPU overhead (it has to accept arbitrary expressions in the
list). But there's a shortcut around the parser: you can pass in the
list as an array literal string, e.g:
select * from the_table where id = ANY('{1,2,3,4,5}')

The SQL parser considers the value one long string and passes it to
the array input function, which is a much simpler routine. This should
scale up much better.

Even better if you could pass in the array as a query parameter, so
the SQL parser doesn't even see the long string -- but I think you
have to jump through some hoops to do that in psycopg2.

Regards,
Marti

pgsql-performance by date:

Previous
From: "Kevin Grittner"
Date:
Subject: Re: efficient data reduction (and deduping)
Next
From: Tom Lane
Date:
Subject: Re: Bad estimation for "where field not in"