pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Natalie Wenz <nataliewenz@gmail.com>
To: pgsql-admin@lists.postgresql.org
Subject: Re: Cursors
Date: Thu, 16 Aug 2018 12:49:38 +0200
Message-ID: <1534416578.3084.23.camel@cybertec.at> (raw)
In-Reply-To: <0E01B4A9-D944-4300-8A56-FC14DFFAB999@gmail.com>
References: <0E01B4A9-D944-4300-8A56-FC14DFFAB999@gmail.com>

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




view thread (2+ messages)

Message-ID: <1534416578.3084.23.camel@cybertec.at>
Permalink:  ../1534416578.3084.23.camel@cybertec.at/
Also on:    postgresql.org/message-id/1534416578.3084.23.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-admin@postgresql.org
  Cc: laurenz.albe@cybertec.at, nataliewenz@gmail.com, pgsql-admin@lists.postgresql.org
  Subject: Re: Cursors
  In-Reply-To: <1534416578.3084.23.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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