pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
READ UNCOMMITTED in postgres
5+ messages / 5 participants
[nested] [flat]

* READ UNCOMMITTED in postgres
@ 2019-12-18 23:12 Matthew Phillips <mphillips34@gmail.com>
  2019-12-18 23:20 ` Re: READ UNCOMMITTED in postgres Stephen Frost <sfrost@snowman.net>
  2019-12-19 06:54 ` Re: READ UNCOMMITTED in postgres Thomas Kellerer <spam_eater@gmx.net>
  2019-12-19 08:19 ` Re: READ UNCOMMITTED in postgres Simon Riggs <simon@2ndquadrant.com>
  0 siblings, 3 replies; 5+ messages in thread

From: Matthew Phillips @ 2019-12-18 23:12 UTC (permalink / raw)
  To: pgsql-general

Hi,
With the current READ UNCOMMITTED discussion happening on pgsql-hackers
[1], It did raise a question/use-case I recently encountered and could not
find a satisfactory solution for. If someone is attempting to poll for new
records on a high insert volume table that has a monotonically increasing
id, what is the best way to do it? As is, with a nave implementation, rows
are not guaranteed to appear in monotonic order; so if you were to keep a
$MAX_ID, and SELECT WHERE p_id > $MAX_ID, you would hit gaps. Is there a
clean way to do this? I've seen READ UNCOMMITTED used for this with DB2.

Thanks
Matt

[1]
https://www.postgresql.org/message-id/CANP8%2Bj%2BmgWfcX9cTPsk7t%2B1kQCxgyGqHTR5R7suht7mCm_x_hA%40ma...

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

* Re: READ UNCOMMITTED in postgres
  2019-12-18 23:12 READ UNCOMMITTED in postgres Matthew Phillips <mphillips34@gmail.com>
@ 2019-12-18 23:20 ` Stephen Frost <sfrost@snowman.net>
  2 siblings, 0 replies; 5+ messages in thread

From: Stephen Frost @ 2019-12-18 23:20 UTC (permalink / raw)
  To: Matthew Phillips <mphillips34@gmail.com>; +Cc: pgsql-general

Greetings,

* Matthew Phillips (mphillips34@gmail.com) wrote:
> With the current READ UNCOMMITTED discussion happening on pgsql-hackers
> [1], It did raise a question/use-case I recently encountered and could not
> find a satisfactory solution for. If someone is attempting to poll for new
> records on a high insert volume table that has a monotonically increasing
> id, what is the best way to do it? As is, with a nave implementation, rows
> are not guaranteed to appear in monotonic order; so if you were to keep a
> $MAX_ID, and SELECT WHERE p_id > $MAX_ID, you would hit gaps. Is there a
> clean way to do this? I've seen READ UNCOMMITTED used for this with DB2.

There's the LISTEN/NOTIFY system, which at a high level is a better
approach than using a polling system.

Thanks,

Stephen

Attachments:

  signature.asc (application/pgp-signature, 818B) download

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

* Re: READ UNCOMMITTED in postgres
  2019-12-18 23:12 READ UNCOMMITTED in postgres Matthew Phillips <mphillips34@gmail.com>
@ 2019-12-19 06:54 ` Thomas Kellerer <spam_eater@gmx.net>
  2 siblings, 0 replies; 5+ messages in thread

From: Thomas Kellerer @ 2019-12-19 06:54 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Matthew Phillips schrieb am 19.12.2019 um 00:12:
> Hi, With the current READ UNCOMMITTED discussion happening on
> pgsql-hackers [1], It did raise a question/use-case I recently
> encountered and could not find a satisfactory solution for. If
> someone is attempting to poll for new records on a high insert volume
> table that has a monotonically increasing id, what is the best way to
> do it? As is, with a nave implementation, rows are not guaranteed to
> appear in monotonic order; so if you were to keep a $MAX_ID, and
> SELECT WHERE p_id > $MAX_ID, you would hit gaps. Is there a clean way
> to do this? I've seen READ UNCOMMITTED used for this with DB2.

In my understanding READ UNCOMMITTED in other databases is typically used to avoid read-locks which Postgres doesn't have. 
So I wonder what benefits READ UNCOMMITTED would have to begin with.

But, if you want to poll for new rows, then why don't you use a timestamp column?

  select *
  from the_table
  where created_at >= <last check time>

  





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

* Re: READ UNCOMMITTED in postgres
  2019-12-18 23:12 READ UNCOMMITTED in postgres Matthew Phillips <mphillips34@gmail.com>
@ 2019-12-19 08:19 ` Simon Riggs <simon@2ndquadrant.com>
  2019-12-19 09:47   ` Re: READ UNCOMMITTED in postgres Olivier Gautherot <ogautherot@gautherot.net>
  2 siblings, 1 reply; 5+ messages in thread

From: Simon Riggs @ 2019-12-19 08:19 UTC (permalink / raw)
  To: Matthew Phillips <mphillips34@gmail.com>; +Cc: pgsql-general

On Wed, 18 Dec 2019 at 23:13, Matthew Phillips <mphillips34@gmail.com>
wrote:


> With the current READ UNCOMMITTED discussion happening on pgsql-hackers
> [1], It did raise a question/use-case I recently encountered and could not
> find a satisfactory solution for. If someone is attempting to poll for new
> records on a high insert volume table that has a monotonically increasing
> id, what is the best way to do it? As is, with a nave implementation, rows
> are not guaranteed to appear in monotonic order; so if you were to keep a
> $MAX_ID, and SELECT WHERE p_id > $MAX_ID, you would hit gaps. Is there a
> clean way to do this? I've seen READ UNCOMMITTED used for this with DB2.
>

Not sure it helps much. The new records aren't truly there until commit.

Using max_id alone is not an effective technique. It's just an optimization.

Just be careful to not advance max_id too quickly, and remember which ones
you've already checked. Or wait for the next monontonic value each time,
accepting the lag.

-- 
Simon Riggs                http://www.2ndQuadrant.com/
<http://www.2ndquadrant.com/;
PostgreSQL Solutions for the Enterprise

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

* Re: READ UNCOMMITTED in postgres
  2019-12-18 23:12 READ UNCOMMITTED in postgres Matthew Phillips <mphillips34@gmail.com>
  2019-12-19 08:19 ` Re: READ UNCOMMITTED in postgres Simon Riggs <simon@2ndquadrant.com>
@ 2019-12-19 09:47   ` Olivier Gautherot <ogautherot@gautherot.net>
  0 siblings, 0 replies; 5+ messages in thread

From: Olivier Gautherot @ 2019-12-19 09:47 UTC (permalink / raw)
  To: Simon Riggs <simon@2ndquadrant.com>; +Cc: Matthew Phillips <mphillips34@gmail.com>; pgsql-general

On Thu, Dec 19, 2019 at 9:20 AM Simon Riggs <simon@2ndquadrant.com> wrote:

> On Wed, 18 Dec 2019 at 23:13, Matthew Phillips <mphillips34@gmail.com>
> wrote:
>
>
>> With the current READ UNCOMMITTED discussion happening on pgsql-hackers
>> [1], It did raise a question/use-case I recently encountered and could not
>> find a satisfactory solution for. If someone is attempting to poll for new
>> records on a high insert volume table that has a monotonically increasing
>> id, what is the best way to do it? As is, with a nave implementation, rows
>> are not guaranteed to appear in monotonic order; so if you were to keep a
>> $MAX_ID, and SELECT WHERE p_id > $MAX_ID, you would hit gaps. Is there a
>> clean way to do this? I've seen READ UNCOMMITTED used for this with DB2.
>>
>
If READ UNCOMMITTED returns data belonging to transactions in process,
there is a risk that you consider data that will end up in a ROLLBACK.


>
> Not sure it helps much. The new records aren't truly there until commit.
>

True. And to make things worse, the timestamp (probably invocation of now()
) will record the beginning of the transaction. So if your transaction
takes a few seconds, or does not always take the same time, you will face a
challenge.


> Using max_id alone is not an effective technique. It's just an
> optimization.
>

I would recommend to manage p_id with a sequence... as long as you're not
in multi-master (you will find out that each master handles its own set of
values and you could end up with some surprises). Doing it with MAX(p_id) +
1 is looking for concurrency problems.


> Just be careful to not advance max_id too quickly, and remember which ones
> you've already checked. Or wait for the next monontonic value each time,
> accepting the lag.
>

Again, as long as you can ensure that there won't be any ROLLBACK.
Otherwise you could end up waiting for ever...


>
> --
> Simon Riggs                http://www.2ndQuadrant.com/
> <http://www.2ndquadrant.com/;
> PostgreSQL Solutions for the Enterprise
>


--
Olivier Gautherot
Tel: +33 6 02 71 92 23
https://www.linkedin.com/in/ogautherot/

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


end of thread, other threads:[~2019-12-19 09:47 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-12-18 23:12 READ UNCOMMITTED in postgres Matthew Phillips <mphillips34@gmail.com>
2019-12-18 23:20 ` Stephen Frost <sfrost@snowman.net>
2019-12-19 06:54 ` Thomas Kellerer <spam_eater@gmx.net>
2019-12-19 08:19 ` Simon Riggs <simon@2ndquadrant.com>
2019-12-19 09:47   ` Olivier Gautherot <ogautherot@gautherot.net>

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