pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Cursors
2+ messages / 2 participants
[nested] [flat]

* Cursors
@ 2018-08-15 21:21 Natalie Wenz <nataliewenz@gmail.com>
  2018-08-16 10:49 ` Re: Cursors Laurenz Albe <laurenz.albe@cybertec.at>
  0 siblings, 1 reply; 2+ messages in thread

From: Natalie Wenz @ 2018-08-15 21:21 UTC (permalink / raw)
  To: pgsql-admin@lists.postgresql.org


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).

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 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)? 

Thanks for your help!
Natalie



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Cursors
  2018-08-15 21:21 Cursors Natalie Wenz <nataliewenz@gmail.com>
@ 2018-08-16 10:49 ` Laurenz Albe <laurenz.albe@cybertec.at>
  0 siblings, 0 replies; 2+ messages in thread

From: Laurenz Albe @ 2018-08-16 10:49 UTC (permalink / raw)
  To: Natalie Wenz <nataliewenz@gmail.com>; pgsql-admin@lists.postgresql.org

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




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2018-08-16 10:49 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-08-15 21:21 Cursors Natalie Wenz <nataliewenz@gmail.com>
2018-08-16 10:49 ` Laurenz Albe <laurenz.albe@cybertec.at>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox