Re: Function with limit and offset - PostgreSQL 9.3

From: John R Pierce <pierce(at)hogranch(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Function with limit and offset - PostgreSQL 9.3
Date: 2017-06-09 04:56:05
Message-ID: dc8309c4-08c0-59b1-6d80-0965ba1fe4f0@hogranch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 6/8/2017 6:36 PM, marcinha rocha wrote:
> |UPDATEtablea a SETmigrated =yes WHEREa.id =row.id;|
> On my original select, the row will have migrated = false. Maybe All I
> need to put is a limit 2000 and the query will do the rest?

SELECT does not return data in any determinate order unless you use an
ORDER BY.... so LIMIT 2000 would return some 2000 elements, not
neccessarily the 'first' 2000 elements unless you somehow order them by
however you feel 'first' is defined.

WITH ids AS (INSERT INTO tableb (id) SELECT id FROM tablea WHERE
migrated=FALSE ORDER BY id LIMIT 2000 RETURNING id)
UPDATE tablea a SET a.migrated=TRUE WHERE a.id = ids.id
RETURNING COUNT(a.id);

I'm not 100% sure you can do UPDATE .... RETURNING COUNT(...), worse
case the UPDATE RETURNING would be a subquery of a SELECT COUNT()...

--
john r pierce, recycling bits in santa cruz

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ken Tanzer 2017-06-09 05:37:34 Limiting DB access by role after initial connection?
Previous Message David G. Johnston 2017-06-09 02:47:49 Re: Function with limit and offset - PostgreSQL 9.3