Обсуждение: Cursors

Поиск
Список
Период
Сортировка

Cursors

От
Natalie Wenz
Дата:
I have a couple of general questions regarding cursors.

First, does something like this hold open one transaction, or is each fetch a separate transaction? As a dba I'm
wonderingif this would interfere with maintenance operations (xid rolling via autovacuum, attaching/detaching
partitions,etc). 

declare cursname no scroll cursor without hold for ...
fetch forward 50000 from cursname


Secondly, on this particular thing (it's a three-table join, with one of those tables being very "wide", so there's
definitelya pg_toast table that's involved besides), it seems to perform pretty terribly. What are the advantages of
gatheringrecords this way compared to doing a series of queries with a range (record number >= x and record number <
y)? 

Thanks for your help!
Natalie

Re: Cursors

От
Laurenz Albe
Дата:
Natalie Wenz wrote:
> I have a couple of general questions regarding cursors. 
> 
> First, does something like this hold open one transaction, or is each fetch a separate transaction?
> As a dba I'm wondering if this would interfere with maintenance operations
> (xid rolling via autovacuum, attaching/detaching partitions, etc).

Unless a cursor is declared WITH HOLD, its life time is limited
to one database transaction.

Using a cursor does not keep a transaction open: if you close the
transaction, the cursor is implicitly closed, and subsequent attempts
to fetch from it will fail.

> Secondly, on this particular thing (it's a three-table join, with one of those tables being
> very "wide", so there's definitely a pg_toast table that's involved besides), it seems to
> perform pretty terribly. What are the advantages of gathering records this way compared to
> doing a series of queries with a range (record number >= x and record number < y)? 

You should compare the execution plans.
By default, "cursor_tuple_fraction" is set to 0.1, meaning that PostgreSQL
assumes that only 10% of all rows will be fetched.  In this case, a plan
that delivers the first rows will be preferred, while the execution time
if you fetch all rows may suffer.

Set "cursor_tuple_fraction" to 1.0 if you plan to fetch all rows.

The advantage of fetching a large result set with a cursor over fetching
parts of it with several queries is that the query has to be planned
and executed only once.

Moreover, if you use LIMIT and OFFSET for retrieving parts of the query in chunks,
you will suffer because processing for large OFFSET values is inherently inefficient:
it has to fetch and descard all rows until the OFFSET is reached.
So you may end up selecting the same first rows over and over, only to discard them.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com