pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: armand pirvu <armand.pirvu@gmail.com>
Cc: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: cursors and function question
Date: Tue, 13 Feb 2018 14:31:51 -0800
Message-ID: <b0e739d5-50ce-507c-995b-49f27981b6fa@aklaver.com> (raw)
In-Reply-To: <0B711AA9-3ACA-4A26-BC40-9F971E250AE0@gmail.com>
References: <149EBC11-E33E-4DBA-9130-8C93BE4B1B43@gmail.com>
	<83c954ba-4d14-ac71-f4ea-30ce25b9a310@aklaver.com>
	<CFD427B2-8A62-4F37-821F-AAF67D7E09FC@gmail.com>
	<af847883-367a-6306-8ddd-822c3dcb5003@aklaver.com>
	<0B711AA9-3ACA-4A26-BC40-9F971E250AE0@gmail.com>

On 02/13/2018 01:25 PM, armand pirvu wrote:
> 
>> On Feb 13, 2018, at 1:22 PM, Adrian Klaver <adrian.klaver@aklaver.com 
>> <mailto:adrian.klaver@aklaver.com>> wrote:
>>

> 
> Not a trigger , but the idea is we will do some batch processing from 
> said table let’s name it testtbl
> 
> 1 - we get the records using  select for update with a limit 100 for example
> 2 - update each record using using cursor
> 3 - print the cursor content so that way I have an idea what was updated
> 
> I was thinking that if I can put a unique constraint on the table, I can 
> generate a global table in the function , update main table from global 
> table and return select from global table

Not entirely sure I know what you are trying to accomplish, still:

1) Not sure you need to use cursor, see here for less complicated way:

https://www.postgresql.org/docs/10/static/plpgsql-control-structures.html#PLPGSQL-RECORDS-ITERATING

and

https://www.postgresql.org/docs/10/static/plpgsql-control-structures.html#PLPGSQL-STATEMENTS-RETURNI...

Using RETURN NEXT.

Keeping mind:

https://www.postgresql.org/docs/10/static/plpgsql-cursors.html
"Rather than executing a whole query at once, it is possible to set up a 
cursor that encapsulates the query, and then read the query result a few 
rows at a time. One reason for doing this is to avoid memory overrun 
when the result contains a large number of rows. (However, PL/pgSQL 
users do not normally need to worry about that, since FOR loops 
automatically use a cursor internally to avoid memory problems.) A more 
interesting usage is to return a reference to a cursor that a function 
has created, allowing the caller to read the rows. This provides an 
efficient way to return large row sets from functions."

So if you are keeping the rows to 100 a FOR loop would seem to suffice.

2) By global table do you mean a temporary table? If so not sure that is 
going to work as I am pretty sure it will disappear after the function 
is run. I could see having a permanent table that you INSERT the updated 
rows into with a timestamp. Then you could update the main table from 
that and prune old records using the timestamps.


> 
> I can see the developer desire to use cursors to minimize some effort on 
> his side
> 
> Thanks
> 
> Armand
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (11+ messages)  latest in thread

Message-ID: <b0e739d5-50ce-507c-995b-49f27981b6fa@aklaver.com>
Permalink:  ../b0e739d5-50ce-507c-995b-49f27981b6fa@aklaver.com/
Also on:    postgresql.org/message-id/b0e739d5-50ce-507c-995b-49f27981b6fa@aklaver.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, armand.pirvu@gmail.com
  Subject: Re: cursors and function question
  In-Reply-To: <b0e739d5-50ce-507c-995b-49f27981b6fa@aklaver.com>
  References: <149EBC11-E33E-4DBA-9130-8C93BE4B1B43@gmail.com> <83c954ba-4d14-ac71-f4ea-30ce25b9a310@aklaver.com> <CFD427B2-8A62-4F37-821F-AAF67D7E09FC@gmail.com> <af847883-367a-6306-8ddd-822c3dcb5003@aklaver.com> <0B711AA9-3ACA-4A26-BC40-9F971E250AE0@gmail.com> <b0e739d5-50ce-507c-995b-49f27981b6fa@aklaver.com>

* 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