pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
UUID or auto-increment
13+ messages / 11 participants
[nested] [flat]

* UUID or auto-increment
@ 2020-08-10 15:53 Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Ashkar Dev @ 2020-08-10 15:53 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org; pgsql-general

Hi,
for web application is it needed to use UUID or auto-increment?

1- if two user inserts row at the same time, does it work?
2- dose the database give the same id for both users or execute one of them
first? ( I mean ID conflict not happens?)

Thanks.

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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
@ 2020-08-10 16:38 ` Ravi Krishna <srkrishna@yahoo.com>
  2020-08-10 16:49   ` Re: UUID or auto-increment Michael Lewis <mlewis@entrata.com>
  2020-08-10 16:51   ` Re: UUID or auto-increment Ron <ronljohnsonjr@gmail.com>
  0 siblings, 2 replies; 13+ messages in thread

From: Ravi Krishna @ 2020-08-10 16:38 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org; pgsql-general

Both can handle concurrent writes.  auto-increment is nothing but serial or sequence cols and they can handle unique concurrent request.  That is why sometimes you may have gaps.UUID is not only unique, but is also unique across space. You can have two different databases generate UUID at the same time and it will still be unique. So that will help if you are consolidating different databases into one big data mart and they can all can go to the same table without conflict. With Sequence or Serial that will be a problem.Finally UUID results in write amplication in wal logs.  Keep that in mind if your app does lot of writes.
  

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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
@ 2020-08-10 16:49   ` Michael Lewis <mlewis@entrata.com>
  1 sibling, 0 replies; 13+ messages in thread

From: Michael Lewis @ 2020-08-10 16:49 UTC (permalink / raw)
  To: Ravi Krishna <srkrishna@yahoo.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>; pgsql-general

UUID are also random and not correlated with time typically, so with a very
large table when accessing primarily recent data, hitting an index on a big
table will pull random pages into memory instead of primarily the end of
the index.

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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
@ 2020-08-10 16:51   ` Ron <ronljohnsonjr@gmail.com>
  2020-08-10 16:53     ` Re: UUID or auto-increment Stephen Frost <sfrost@snowman.net>
  2020-08-10 16:58     ` Re: UUID or auto-increment Adrian Klaver <adrian.klaver@aklaver.com>
  1 sibling, 2 replies; 13+ messages in thread

From: Ron @ 2020-08-10 16:51 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 8/10/20 11:38 AM, Ravi Krishna wrote:
[snip]
> Finally UUID results in write amplication in wal logs.  Keep that in mind 
> if your app does lot of writes.

Because UUID is 32 bytes, while SERIAL is 4 bytes?

-- 
Angular momentum makes the world go 'round.

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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
  2020-08-10 16:51   ` Re: UUID or auto-increment Ron <ronljohnsonjr@gmail.com>
@ 2020-08-10 16:53     ` Stephen Frost <sfrost@snowman.net>
  2020-08-10 17:10       ` Re: UUID or auto-increment Israel Brewster <ijbrewster@alaska.edu>
  2020-08-10 20:56       ` Re: UUID or auto-increment Rob Sargent <robjsargent@gmail.com>
  1 sibling, 2 replies; 13+ messages in thread

From: Stephen Frost @ 2020-08-10 16:53 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

Greeitngs,

* Ron (ronljohnsonjr@gmail.com) wrote:
> On 8/10/20 11:38 AM, Ravi Krishna wrote:
> >Finally UUID results in write amplication in wal logs.  Keep that in mind
> >if your app does lot of writes.
> 
> Because UUID is 32 bytes, while SERIAL is 4 bytes?

and because it's random and so will touch a lot more pages when you're
using it...

Avoid UUIDs if you can- map them to something more sensible internally
if you have to deal with them.

Thanks,

Stephen

Attachments:

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

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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
  2020-08-10 16:51   ` Re: UUID or auto-increment Ron <ronljohnsonjr@gmail.com>
  2020-08-10 16:53     ` Re: UUID or auto-increment Stephen Frost <sfrost@snowman.net>
@ 2020-08-10 17:10       ` Israel Brewster <ijbrewster@alaska.edu>
  2020-08-10 17:16         ` Re: UUID or auto-increment Stephen Frost <sfrost@snowman.net>
  2020-08-10 17:38         ` Re: UUID or auto-increment Adam Brusselback <adambrusselback@gmail.com>
  2020-08-10 20:06         ` Re: UUID or auto-increment Peter J. Holzer <hjp-pgsql@hjp.at>
  1 sibling, 3 replies; 13+ messages in thread

From: Israel Brewster @ 2020-08-10 17:10 UTC (permalink / raw)
  To: Stephen Frost <sfrost@snowman.net>; +Cc: Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org


---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
Work: 907-474-5172
cell:  907-328-9145

> On Aug 10, 2020, at 8:53 AM, Stephen Frost <sfrost@snowman.net> wrote:
> 
> Greeitngs,
> 
> * Ron (ronljohnsonjr@gmail.com) wrote:
>> On 8/10/20 11:38 AM, Ravi Krishna wrote:
>>> Finally UUID results in write amplication in wal logs.  Keep that in mind
>>> if your app does lot of writes.
>> 
>> Because UUID is 32 bytes, while SERIAL is 4 bytes?
> 
> and because it's random and so will touch a lot more pages when you're
> using it...

I would point out, however, that using a V1 UUID rather than a V4 can help with this as it is sequential, not random (based on MAC address and timestamp + random). There is a trade off, of course, as with V1 if two writes occur on the same computer at the exact same millisecond, there is a very very small chance of generating conflicting UUID’s (see https://www.sohamkamani.com/blog/2016/10/05/uuid1-vs-uuid4/ <https://www.sohamkamani.com/blog/2016/10/05/uuid1-vs-uuid4/;). As there is still a random component, however, this seems quite unlikely.


> 
> Avoid UUIDs if you can- map them to something more sensible internally
> if you have to deal with them.
> 
> Thanks,
> 
> Stephen

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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
  2020-08-10 16:51   ` Re: UUID or auto-increment Ron <ronljohnsonjr@gmail.com>
  2020-08-10 16:53     ` Re: UUID or auto-increment Stephen Frost <sfrost@snowman.net>
  2020-08-10 17:10       ` Re: UUID or auto-increment Israel Brewster <ijbrewster@alaska.edu>
@ 2020-08-10 17:16         ` Stephen Frost <sfrost@snowman.net>
  2 siblings, 0 replies; 13+ messages in thread

From: Stephen Frost @ 2020-08-10 17:16 UTC (permalink / raw)
  To: Israel Brewster <ijbrewster@alaska.edu>; +Cc: Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org

Greetings,

* Israel Brewster (ijbrewster@alaska.edu) wrote:
> > On Aug 10, 2020, at 8:53 AM, Stephen Frost <sfrost@snowman.net> wrote:
> > * Ron (ronljohnsonjr@gmail.com) wrote:
> >> On 8/10/20 11:38 AM, Ravi Krishna wrote:
> >>> Finally UUID results in write amplication in wal logs.  Keep that in mind
> >>> if your app does lot of writes.
> >> 
> >> Because UUID is 32 bytes, while SERIAL is 4 bytes?
> > 
> > and because it's random and so will touch a lot more pages when you're
> > using it...
> 
> I would point out, however, that using a V1 UUID rather than a V4 can help with this as it is sequential, not random (based on MAC address and timestamp + random). There is a trade off, of course, as with V1 if two writes occur on the same computer at the exact same millisecond, there is a very very small chance of generating conflicting UUID’s (see https://www.sohamkamani.com/blog/2016/10/05/uuid1-vs-uuid4/ <https://www.sohamkamani.com/blog/2016/10/05/uuid1-vs-uuid4/;). As there is still a random component, however, this seems quite unlikely.

Sure, that helps, but it's still not great, and they're still much, much
larger than you'd ever need for an identifier inside of a given system,
so best to map it to something reasonable and avoid them as much as
possible.

Thanks,

Stephen

Attachments:

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

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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
  2020-08-10 16:51   ` Re: UUID or auto-increment Ron <ronljohnsonjr@gmail.com>
  2020-08-10 16:53     ` Re: UUID or auto-increment Stephen Frost <sfrost@snowman.net>
  2020-08-10 17:10       ` Re: UUID or auto-increment Israel Brewster <ijbrewster@alaska.edu>
@ 2020-08-10 17:38         ` Adam Brusselback <adambrusselback@gmail.com>
  2 siblings, 0 replies; 13+ messages in thread

From: Adam Brusselback @ 2020-08-10 17:38 UTC (permalink / raw)
  To: Israel Brewster <ijbrewster@alaska.edu>; +Cc: Stephen Frost <sfrost@snowman.net>; Ron <ronljohnsonjr@gmail.com>; pgsql-general <pgsql-general@lists.postgresql.org>

>  I would point out, however, that using a V1 UUID rather than a V4 can
help with this as it is sequential, not random (based on MAC address and
timestamp + random)

I wanted to make this point, using sequential UUIDs helped me reduce write
amplification quite a bit with my application, I didn't use V1, instead I
used: https://pgxn.org/dist/sequential_uuids/

Reduces the pain caused by UUIDs a ton IMO.
-Adam

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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
  2020-08-10 16:51   ` Re: UUID or auto-increment Ron <ronljohnsonjr@gmail.com>
  2020-08-10 16:53     ` Re: UUID or auto-increment Stephen Frost <sfrost@snowman.net>
  2020-08-10 17:10       ` Re: UUID or auto-increment Israel Brewster <ijbrewster@alaska.edu>
@ 2020-08-10 20:06         ` Peter J. Holzer <hjp-pgsql@hjp.at>
  2020-08-10 20:44           ` Re: UUID or auto-increment Israel Brewster <ijbrewster@alaska.edu>
  2 siblings, 1 reply; 13+ messages in thread

From: Peter J. Holzer @ 2020-08-10 20:06 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 2020-08-10 09:10:00 -0800, Israel Brewster wrote:
> I would point out, however, that using a V1 UUID rather than a V4 can
> help with this as it is sequential, not random (based on MAC address
> and timestamp + random).

If I read the specs correctly, a V1 UUID will roll over every 429
seconds. I think that as far as index locality is concerned, this is
essentially random for most applications.

        hp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

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

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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
  2020-08-10 16:51   ` Re: UUID or auto-increment Ron <ronljohnsonjr@gmail.com>
  2020-08-10 16:53     ` Re: UUID or auto-increment Stephen Frost <sfrost@snowman.net>
  2020-08-10 17:10       ` Re: UUID or auto-increment Israel Brewster <ijbrewster@alaska.edu>
  2020-08-10 20:06         ` Re: UUID or auto-increment Peter J. Holzer <hjp-pgsql@hjp.at>
@ 2020-08-10 20:44           ` Israel Brewster <ijbrewster@alaska.edu>
  2020-08-10 21:30             ` Re: UUID or auto-increment John W Higgins <wishdev@gmail.com>
  0 siblings, 1 reply; 13+ messages in thread

From: Israel Brewster @ 2020-08-10 20:44 UTC (permalink / raw)
  To: Peter J. Holzer <hjp-pgsql@hjp.at>; +Cc: pgsql-general@lists.postgresql.org



> On Aug 10, 2020, at 12:06 PM, Peter J. Holzer <hjp-pgsql@hjp.at> wrote:
> 
> On 2020-08-10 09:10:00 -0800, Israel Brewster wrote:
>> I would point out, however, that using a V1 UUID rather than a V4 can
>> help with this as it is sequential, not random (based on MAC address
>> and timestamp + random).
> 
> If I read the specs correctly, a V1 UUID will roll over every 429
> seconds. I think that as far as index locality is concerned, this is
> essentially random for most applications.

According to wikipedia, the time value in a V1 UUID is a 60-bit number, and will roll over "around 3400AD”, depending on the algorithm used, or 5236AD if the software treats the timestamp as unsigned. This timestamp is extended by a 13 or 14-bit “uniqifying" clock sequence to handle cases of overlap, and then the 48bit MAC address (constant, so no rollover there) is appended. So perhaps that 13 or 14 bit “uniqifying” sequence will roll over every 429 seconds, however the timestamp *as a whole* won’t roll over for quite a while yet, thereby guaranteeing that the UUIDs will be sequential, not random (since, last I checked, time was sequential).

---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
Work: 907-474-5172
cell:  907-328-9145

> 
>        hp
> 
> -- 
>   _  | Peter J. Holzer    | Story must make more sense than reality.
> |_|_) |                    |
> | |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
> __/   | http://www.hjp.at/ |       challenge!"






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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
  2020-08-10 16:51   ` Re: UUID or auto-increment Ron <ronljohnsonjr@gmail.com>
  2020-08-10 16:53     ` Re: UUID or auto-increment Stephen Frost <sfrost@snowman.net>
  2020-08-10 17:10       ` Re: UUID or auto-increment Israel Brewster <ijbrewster@alaska.edu>
  2020-08-10 20:06         ` Re: UUID or auto-increment Peter J. Holzer <hjp-pgsql@hjp.at>
  2020-08-10 20:44           ` Re: UUID or auto-increment Israel Brewster <ijbrewster@alaska.edu>
@ 2020-08-10 21:30             ` John W Higgins <wishdev@gmail.com>
  0 siblings, 0 replies; 13+ messages in thread

From: John W Higgins @ 2020-08-10 21:30 UTC (permalink / raw)
  To: pgsql-general <pgsql-general@lists.postgresql.org>

On Mon, Aug 10, 2020 at 1:45 PM Israel Brewster <ijbrewster@alaska.edu>
wrote:

>
>
> > On Aug 10, 2020, at 12:06 PM, Peter J. Holzer <hjp-pgsql@hjp.at> wrote:
> >
> > On 2020-08-10 09:10:00 -0800, Israel Brewster wrote:
> >> I would point out, however, that using a V1 UUID rather than a V4 can
> >> help with this as it is sequential, not random (based on MAC address
> >> and timestamp + random).
> >
> > If I read the specs correctly, a V1 UUID will roll over every 429
> > seconds. I think that as far as index locality is concerned, this is
> > essentially random for most applications.
>
> According to wikipedia, the time value in a V1 UUID is a 60-bit number,
> and will roll over "around 3400AD”, depending on the algorithm used, or
> 5236AD if the software treats the timestamp as unsigned. This timestamp is
> extended by a 13 or 14-bit “uniqifying" clock sequence to handle cases of
> overlap, and then the 48bit MAC address (constant, so no rollover there) is
> appended. So perhaps that 13 or 14 bit “uniqifying” sequence will roll over
> every 429 seconds, however the timestamp *as a whole* won’t roll over for
> quite a while yet, thereby guaranteeing that the UUIDs will be sequential,
> not random (since, last I checked, time was sequential).
>
>
Except the time portion of a V1 UUID is not written high to low but rather
low then middle then high which means that the time portion is not
expressed in a sequential format and the left 8 chars of a V1 UUID
"rollover" every 429 seconds or so.

For example a V1 UUID right around now looks like

7db3f2ba-db4f-11ea-87d0-0242ac130003

Less than a second later

7db534cc-db4f-11ea-87d0-0242ac130003

So that looks sequential but in roughly 429 seconds it will look like

7db3f2ba-db4f-11ea-87d1-0242ac130003

More importantly in other roughly 300 seconds it would be something like

6ab3f2ba-db4f-11ea-87d2-0242ac130003

Note the move from 87d0 to 87d1 and 87d2 in the middle but the left 8 bytes
"rollover".

That's not quite sequential in terms of indexing.

John

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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
  2020-08-10 16:51   ` Re: UUID or auto-increment Ron <ronljohnsonjr@gmail.com>
  2020-08-10 16:53     ` Re: UUID or auto-increment Stephen Frost <sfrost@snowman.net>
@ 2020-08-10 20:56       ` Rob Sargent <robjsargent@gmail.com>
  1 sibling, 0 replies; 13+ messages in thread

From: Rob Sargent @ 2020-08-10 20:56 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org



On 8/10/20 10:53 AM, Stephen Frost wrote:
> Greeitngs,
> 
> * Ron (ronljohnsonjr@gmail.com) wrote:
>> On 8/10/20 11:38 AM, Ravi Krishna wrote:
>>> Finally UUID results in write amplication in wal logs.  Keep that in mind
>>> if your app does lot of writes.
>>
>> Because UUID is 32 bytes, while SERIAL is 4 bytes?
> 
> and because it's random and so will touch a lot more pages when you're
> using it...
> 
> Avoid UUIDs if you can- map them to something more sensible internally
> if you have to deal with them.
> 
> Thanks,
> 
> Stephen
> 
I suspect the increased storage cost is more related to the size of the 
record than to the ratio of the data types.

What says two consecutively saved records ought to be stored on the same 
page or will likely be sought with the same search criterion.  Serial 
ids put a time order (loosely) on the data which may be completely 
artificial.





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

* Re: UUID or auto-increment
  2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
  2020-08-10 16:38 ` Re: UUID or auto-increment Ravi Krishna <srkrishna@yahoo.com>
  2020-08-10 16:51   ` Re: UUID or auto-increment Ron <ronljohnsonjr@gmail.com>
@ 2020-08-10 16:58     ` Adrian Klaver <adrian.klaver@aklaver.com>
  1 sibling, 0 replies; 13+ messages in thread

From: Adrian Klaver @ 2020-08-10 16:58 UTC (permalink / raw)
  To: Ron <ronljohnsonjr@gmail.com>; pgsql-general@lists.postgresql.org

On 8/10/20 9:51 AM, Ron wrote:
> On 8/10/20 11:38 AM, Ravi Krishna wrote:
> [snip]
>> Finally UUID results in write amplication in wal logs.  Keep that in 
>> mind if your app does lot of writes.
> 
> Because UUID is 32 bytes, while SERIAL is 4 bytes?

You mean 32 digits for 128 bits?:

https://www.postgresql.org/docs/12/datatype-uuid.html

And there is BIGSERIAL which is 8 bytes.

> 
> -- 
> Angular momentum makes the world go 'round.


-- 
Adrian Klaver
adrian.klaver@aklaver.com





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


end of thread, other threads:[~2020-08-10 21:30 UTC | newest]

Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-08-10 15:53 UUID or auto-increment Ashkar Dev <ashkardev@gmail.com>
2020-08-10 16:38 ` Ravi Krishna <srkrishna@yahoo.com>
2020-08-10 16:49   ` Michael Lewis <mlewis@entrata.com>
2020-08-10 16:51   ` Ron <ronljohnsonjr@gmail.com>
2020-08-10 16:53     ` Stephen Frost <sfrost@snowman.net>
2020-08-10 17:10       ` Israel Brewster <ijbrewster@alaska.edu>
2020-08-10 17:16         ` Stephen Frost <sfrost@snowman.net>
2020-08-10 17:38         ` Adam Brusselback <adambrusselback@gmail.com>
2020-08-10 20:06         ` Peter J. Holzer <hjp-pgsql@hjp.at>
2020-08-10 20:44           ` Israel Brewster <ijbrewster@alaska.edu>
2020-08-10 21:30             ` John W Higgins <wishdev@gmail.com>
2020-08-10 20:56       ` Rob Sargent <robjsargent@gmail.com>
2020-08-10 16:58     ` Adrian Klaver <adrian.klaver@aklaver.com>

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