pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #13846: INSERT ON CONFLICT consumes sequencers on conflicts
13+ messages / 7 participants
[nested] [flat]

* BUG #13846: INSERT ON CONFLICT consumes sequencers on conflicts
@ 2016-01-05 15:02  paul@salesintel.com
  0 siblings, 1 reply; 13+ messages in thread

From: paul@salesintel.com @ 2016-01-05 15:02 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      13846
Logged by:          Paul Hester
Email address:      paul@salesintel.com
PostgreSQL version: 9.5rc1
Operating system:   Windows 10 Pro
Description:        

When using an INSERT statement with an ON CONFLICT clause, if there is a
conflict, and the table being inserted into has a column defaulted to
nextval('seq'), the sequencer 'seq' is always incremented. This can quickly
and artificially consume all values of the sequencer; it behaves as if
conflict detection happens after inserts are attempted, rather than before.




-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencers on conflicts
@ 2016-01-05 16:13  Andres Freund <andres@anarazel.de>
  parent: paul@salesintel.com
  0 siblings, 2 replies; 13+ messages in thread

From: Andres Freund @ 2016-01-05 16:13 UTC (permalink / raw)
  To: paul@salesintel.com; +Cc: pgsql-bugs

On 2016-01-05 15:02:27 +0000, paul@salesintel.com wrote:
> The following bug has been logged on the website:
> 
> Bug reference:      13846
> Logged by:          Paul Hester
> Email address:      paul@salesintel.com
> PostgreSQL version: 9.5rc1
> Operating system:   Windows 10 Pro
> Description:        
> 
> When using an INSERT statement with an ON CONFLICT clause, if there is a
> conflict, and the table being inserted into has a column defaulted to
> nextval('seq'), the sequencer 'seq' is always incremented. This can quickly
> and artificially consume all values of the sequencer; it behaves as if
> conflict detection happens after inserts are attempted, rather than before.

Yes. That's by design. You can't reliably do conflict detection before
evaluating column default values.

Andres Freund


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencers onconflicts
@ 2016-01-05 17:27  Paul <paul@salesintel.com>
  parent: Andres Freund <andres@anarazel.de>
  1 sibling, 0 replies; 13+ messages in thread

From: Paul @ 2016-01-05 17:27 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs

If none of the columns with defaults are used to detect conflicts, why must they be evaluated before conflict detection in order to reliably detect a conflict?

Lots of systems categorize information into ‘organizational’ data that may be hierarchical in nature and fairly static, in relation to the other ‘operational’ kind of data, which is usually generated from events; those events being organized by the former for batching and reporting purposes, etc. It is often the case UPSERTS are most useful when deriving the organizational data from the operational data, meaning a very typical usage of an UPSERT would have many more updates than inserts, in direct correlation to the number of events being processed. Its not unreasonable over a relatively short period of time (within the overall lifetime of a particular system) to process billions of events, meaning SERIAL typed columns used as surrogate keys to the natural keys of organizational data could exhaust their sequencers for no good reason, and SMALLSERIAL just wouldn’t be usable, BIGSERIAL could work, but they’re, uhm.. BIG.

There should be a way to not evaluate defaulted columns not used in the conflict_target, so that SERIAL typed columns not used for conflict detection don’t unnecessary exhaust their sequencers…. (I think that was like a quadruple-negative sentence there…, sorry about that)

Regards,
Paul

From: Andres Freund
Sent: Tuesday, January 5, 2016 9:13 AM
To: paul@salesintel.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: [BUGS] BUG #13846: INSERT ON CONFLICT consumes sequencers onconflicts

On 2016-01-05 15:02:27 +0000, paul@salesintel.com wrote:
> The following bug has been logged on the website:
> 
> Bug reference:      13846
> Logged by:          Paul Hester
> Email address:      paul@salesintel.com
> PostgreSQL version: 9.5rc1
> Operating system:   Windows 10 Pro
> Description:        
> 
> When using an INSERT statement with an ON CONFLICT clause, if there is a
> conflict, and the table being inserted into has a column defaulted to
> nextval('seq'), the sequencer 'seq' is always incremented. This can quickly
> and artificially consume all values of the sequencer; it behaves as if
> conflict detection happens after inserts are attempted, rather than before.

Yes. That's by design. You can't reliably do conflict detection before
evaluating column default values.

Andres Freund

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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencers on conflicts
@ 2016-01-05 19:27  Peter Geoghegan <pg@heroku.com>
  parent: Andres Freund <andres@anarazel.de>
  1 sibling, 1 reply; 13+ messages in thread

From: Peter Geoghegan @ 2016-01-05 19:27 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-bugs

On Tue, Jan 5, 2016 at 8:13 AM, Andres Freund <andres@anarazel.de> wrote:
>> When using an INSERT statement with an ON CONFLICT clause, if there is a
>> conflict, and the table being inserted into has a column defaulted to
>> nextval('seq'), the sequencer 'seq' is always incremented. This can quickly
>> and artificially consume all values of the sequencer; it behaves as if
>> conflict detection happens after inserts are attempted, rather than before.
>
> Yes. That's by design. You can't reliably do conflict detection before
> evaluating column default values.

Right. If you didn't consume a sequence value, but just did a
"peek-ahead", then several concurrently inserting sessions would all
"peek-ahead" and see the same value. There'd then be a race condition
that broke the useful guarantees that ON CONFLICT DO UPDATE makes.

-- 
Peter Geoghegan


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencers on conflicts
@ 2016-01-06 05:04  Paul Hester <paul@salesintel.com>
  parent: Peter Geoghegan <pg@heroku.com>
  0 siblings, 2 replies; 13+ messages in thread

From: Paul Hester @ 2016-01-06 05:04 UTC (permalink / raw)
  To: Peter Geoghegan <pg@heroku.com>; +Cc: Andres Freund <andres@anarazel.de>; pgsql-bugs

No peek-ahead necessary, when the sequenced column would not be used in the
conflict_target. UPSERTS to dimension tables in a star schema as part of an
OLAP system are very handy, but typically the records in the dimension
tables use a surrogate key based on sequences for maintenance & performance
reasons, and the 'natural' key/value is the only column that would be used
to detect a conflict. In this case, the 'normal' path would be the
conflict_action nearly all the time, and not the insert. If the typical
path was the INSERT, I'd bet the data would be from some event source, and
I guess in that case if there were a key column defaulted with a sequence,
like an order#, it would have to be used to resolve conflicts. But it's
hard to imagine a case in a process flow where you wouldn't already know if
the event existed or not, limiting the need for an UPSERT on event data.

This limitation, of consuming sequencers used for defaults on columns not
necessary to resolve conflict, diminishes the viability for using Postges
UPSERTS for large data warehouses, is all I'm saying (or requires surrogate
keys being 64 bit). Just caught me by surprise in comparison to other
RDBMSs offering some form of UPSERTs that would not consume a sequencer if
its values weren't required to resolve conflicts.

On Tue, Jan 5, 2016 at 12:27 PM, Peter Geoghegan <pg@heroku.com> wrote:

> On Tue, Jan 5, 2016 at 8:13 AM, Andres Freund <andres@anarazel.de> wrote:
> >> When using an INSERT statement with an ON CONFLICT clause, if there is a
> >> conflict, and the table being inserted into has a column defaulted to
> >> nextval('seq'), the sequencer 'seq' is always incremented. This can
> quickly
> >> and artificially consume all values of the sequencer; it behaves as if
> >> conflict detection happens after inserts are attempted, rather than
> before.
> >
> > Yes. That's by design. You can't reliably do conflict detection before
> > evaluating column default values.
>
> Right. If you didn't consume a sequence value, but just did a
> "peek-ahead", then several concurrently inserting sessions would all
> "peek-ahead" and see the same value. There'd then be a race condition
> that broke the useful guarantees that ON CONFLICT DO UPDATE makes.
>
> --
> Peter Geoghegan
>



-- 
*Paul Hester*
Co-founder & Chief Architect
www.salesintel.com

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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencers on conflicts
@ 2016-01-06 18:19  Andres Freund <andres@anarazel.de>
  parent: Paul Hester <paul@salesintel.com>
  1 sibling, 1 reply; 13+ messages in thread

From: Andres Freund @ 2016-01-06 18:19 UTC (permalink / raw)
  To: Paul Hester <paul@salesintel.com>; +Cc: Peter Geoghegan <pg@heroku.com>; pgsql-bugs

On 2016-01-05 22:04:41 -0700, Paul Hester wrote:
> No peek-ahead necessary, when the sequenced column would not be used in the
> conflict_target. UPSERTS to dimension tables in a star schema as part of an
> OLAP system are very handy, but typically the records in the dimension
> tables use a surrogate key based on sequences for maintenance & performance
> reasons, and the 'natural' key/value is the only column that would be used
> to detect a conflict. In this case, the 'normal' path would be the
> conflict_action nearly all the time, and not the insert. If the typical
> path was the INSERT, I'd bet the data would be from some event source, and
> I guess in that case if there were a key column defaulted with a sequence,
> like an order#, it would have to be used to resolve conflicts. But it's
> hard to imagine a case in a process flow where you wouldn't already know if
> the event existed or not, limiting the need for an UPSERT on event data.

It'd be very fragile. You could only do that if the value isn't involved
in any unique indexes, if there are no BEFORE triggers (since they need
to see the value).

> This limitation, of consuming sequencers used for defaults on columns not
> necessary to resolve conflict, diminishes the viability for using Postges
> UPSERTS for large data warehouses, is all I'm saying (or requires surrogate
> keys being 64 bit). Just caught me by surprise in comparison to other
> RDBMSs offering some form of UPSERTs that would not consume a sequencer if
> its values weren't required to resolve conflicts.

If you use normal sequences you already need to cope with gaps in
sequences. And ids needing to be 64bits if you're a longer lived
business and it's for a a halfway "popular" table, isn't something new.


So I don't think addressing this is worth adding code for, especially
given the fragility of the situations where it'd be usable.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencers on conflicts
@ 2016-01-06 19:26  Thomas Kellerer <spam_eater@gmx.net>
  parent: Paul Hester <paul@salesintel.com>
  1 sibling, 0 replies; 13+ messages in thread

From: Thomas Kellerer @ 2016-01-06 19:26 UTC (permalink / raw)
  To: pgsql-bugs

Paul Hester schrieb am 06.01.2016 um 06:04:
> This limitation, of consuming sequencers used for defaults on columns
> not necessary to resolve conflict, diminishes the viability for using
> Postges UPSERTS for large data warehouses, is all I'm saying (or
> requires surrogate keys being 64 bit). Just caught me by surprise in
> comparison to other RDBMSs offering some form of UPSERTs that would
> not consume a sequencer if its values weren't required to resolve
> conflicts.

Why does this "diminish" the viability of a sequence?






-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencers onconflicts
@ 2016-01-06 21:00  Paul <paul@salesintel.com>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 2 replies; 13+ messages in thread

From: Paul @ 2016-01-06 21:00 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: Peter Geoghegan <pg@heroku.com>; pgsql-bugs

To be clear, the column defaulted to a sequencer is not at all involved in determining conflicts, and therefore it should not matter if it is used in a unique index, nor would there be any problems with a BEFORE trigger.

Gaps aren’t any kind of issue, and it’s not about how popular a related table is to the table upon which the UPSERT would be applied.

For example, I might be receiving event data from a shop floor where everything is instrumented; tank levels, step counters, etc. There could be millions of events over a relatively short period of time. I might have a field in the event data which indicates the ‘class’ of device that generated the event; ex: ‘pump’, ‘actuator’, ‘regulator’, ‘thermometer’. There may only be less than a few hundred classes of devices that rarely change. So I have a table, with a surrogate SMALLINT key defaulted to a sequence, and a ‘name’ column which is a device class name.

As the raw event data is processed, I want to do an UPSERT into the device class table, using only the class name to resolve conflicts, to get it’s surrogate key, which is then used while inserting the related event record linking that event record to the particular device class.  Since device classes are rarely added, it will be the common case that an actual INSERT is almost never performed.

Using INSERT ON CONFLICT in this case, however, would mean the device class table, which would only ever hold a few hundred records and would only require a SMALLINT for its surrogate key, would have to have a BIGINT for no good reason, which really isn’t a big deal. What IS the big deal is that the event table would also require a BIGINT for the device class column to relate the event to the device class. In most cases, there are many more ‘organizing’ columns, similar to the device class in this example, meaning BIGINTs would have to be used for no good reason within event tables, which can hold vast volumes of data. This can have a real and measurable impact on memory, network, and storage requirements, which eventually map to dollars.

This basic scenario is very common, and one that greatly benefits from UPSERT capability; i.e. linking event data to organizational data. It’s much less common to have to UPSERT into some event table; i.e. the INSERT part of UPSERTs is usually the path less taken. I used the example of a shop floor with device classes, but its the same issue for looking at orders by product hierarchies, commissions by territories, stars by classification, web-page-hits by marketing campaigns…. I could list hundreds of processes within many problem domains that are of this same basic pattern.

My only point is, there’s another great capability in Postgres, for doing efficient concurrent UPSERTS, but that one of the most common and prime use cases for it is going to cost more money from having to use 64bit numbers everywhere for no good reason.




From: Andres Freund
Sent: Wednesday, January 6, 2016 11:19 AM
To: Paul Hester
Cc: Peter Geoghegan; pgsql-bugs
Subject: Re: [BUGS] BUG #13846: INSERT ON CONFLICT consumes sequencers onconflicts

On 2016-01-05 22:04:41 -0700, Paul Hester wrote:
> No peek-ahead necessary, when the sequenced column would not be used in the
> conflict_target. UPSERTS to dimension tables in a star schema as part of an
> OLAP system are very handy, but typically the records in the dimension
> tables use a surrogate key based on sequences for maintenance & performance
> reasons, and the 'natural' key/value is the only column that would be used
> to detect a conflict. In this case, the 'normal' path would be the
> conflict_action nearly all the time, and not the insert. If the typical
> path was the INSERT, I'd bet the data would be from some event source, and
> I guess in that case if there were a key column defaulted with a sequence,
> like an order#, it would have to be used to resolve conflicts. But it's
> hard to imagine a case in a process flow where you wouldn't already know if
> the event existed or not, limiting the need for an UPSERT on event data.

It'd be very fragile. You could only do that if the value isn't involved
in any unique indexes, if there are no BEFORE triggers (since they need
to see the value).

> This limitation, of consuming sequencers used for defaults on columns not
> necessary to resolve conflict, diminishes the viability for using Postges
> UPSERTS for large data warehouses, is all I'm saying (or requires surrogate
> keys being 64 bit). Just caught me by surprise in comparison to other
> RDBMSs offering some form of UPSERTs that would not consume a sequencer if
> its values weren't required to resolve conflicts.

If you use normal sequences you already need to cope with gaps in
sequences. And ids needing to be 64bits if you're a longer lived
business and it's for a a halfway "popular" table, isn't something new.


So I don't think addressing this is worth adding code for, especially
given the fragility of the situations where it'd be usable.

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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencers onconflicts
@ 2016-01-06 21:13  Peter Geoghegan <pg@heroku.com>
  parent: Paul <paul@salesintel.com>
  1 sibling, 2 replies; 13+ messages in thread

From: Peter Geoghegan @ 2016-01-06 21:13 UTC (permalink / raw)
  To: Paul <paul@salesintel.com>; +Cc: Andres Freund <andres@anarazel.de>; pgsql-bugs

On Wed, Jan 6, 2016 at 1:00 PM, Paul <paul@salesintel.com> wrote:
> My only point is, there’s another great capability in Postgres, for doing
> efficient concurrent UPSERTS, but that one of the most common and prime use
> cases for it is going to cost more money from having to use 64bit numbers
> everywhere for no good reason.

Not really; at least, it won't increase the size of a plain BIGSERIAL
primary key index (which will generally not be composite):

postgres=# create table foo as select (random() * 100000000)::int4
pkey from generate_series(1, 100000);
SELECT 100000
postgres=# create index on foo (pkey);
CREATE INDEX

postgres=# \dt+ foo
                   List of relations
 Schema │ Name │ Type  │ Owner │  Size   │ Description
────────┼──────┼───────┼───────┼─────────┼─────────────
 public │ foo  │ table │ pg    │ 3544 kB │
(1 row)

postgres=# \di+ foo_pkey_idx
                           List of relations
 Schema │     Name     │ Type  │ Owner │ Table │  Size   │ Description
────────┼──────────────┼───────┼───────┼───────┼─────────┼─────────────
 public │ foo_pkey_idx │ index │ pg    │ foo   │ 2208 kB │
(1 row)

postgres=# alter table foo alter column pkey type int8;
ALTER TABLE

postgres=# \di+ foo_pkey_idx
                           List of relations
 Schema │     Name     │ Type  │ Owner │ Table │  Size   │ Description
────────┼──────────────┼───────┼───────┼───────┼─────────┼─────────────
 public │ foo_pkey_idx │ index │ pg    │ foo   │ 2208 kB │
(1 row)

The unchanged size of the index foo_pkey_idx seen here due to
alignment considerations. Granted, the heap might still be a bit
larger than it would otherwise be, because it will usually be
"composite", but I think the "cost" of your using int8 will not break
the bank.

-- 
Peter Geoghegan

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs


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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencersonconflicts
@ 2016-01-06 21:21  Paul <paul@salesintel.com>
  parent: Peter Geoghegan <pg@heroku.com>
  1 sibling, 0 replies; 13+ messages in thread

From: Paul @ 2016-01-06 21:21 UTC (permalink / raw)
  To: Peter Geoghegan <pg@heroku.com>; +Cc: Andres Freund <andres@anarazel.de>; pgsql-bugs

All things are relative, to how much data, and how big the bank. And having built gigantic (billions of billions of fact records, hundreds of dimensions, millions of members) data warehouse and olap solutions, you really do try to keep stuff small because it does have an impact on cost, size, and performance.
 
On a more conceptual note, if a sequence-defaulted column is used to resolve a conflict, i.e. it’s in the conflict_target, than by definition there will never be a conflict!

So anything about ‘fragility’ and ‘peek-ahead’ and ‘BEFORE triggers’ not working just doesn’t make any sense. If a sequence is used to resolve a conflict, there will never be a conflict, and if it isn’t, there’s no need to get the next sequenced value before being able to determine conflicting records meaning no concurrency issues; you would still only need to get the next sequential value only for an actual insert.




From: Peter Geoghegan
Sent: Wednesday, January 6, 2016 2:13 PM
To: Paul
Cc: Andres Freund; pgsql-bugs
Subject: Re: [BUGS] BUG #13846: INSERT ON CONFLICT consumes sequencersonconflicts

On Wed, Jan 6, 2016 at 1:00 PM, Paul <paul@salesintel.com> wrote:
> My only point is, there’s another great capability in Postgres, for doing
> efficient concurrent UPSERTS, but that one of the most common and prime use
> cases for it is going to cost more money from having to use 64bit numbers
> everywhere for no good reason.

Not really; at least, it won't increase the size of a plain BIGSERIAL
primary key index (which will generally not be composite):

postgres=# create table foo as select (random() * 100000000)::int4
pkey from generate_series(1, 100000);
SELECT 100000
postgres=# create index on foo (pkey);
CREATE INDEX

postgres=# \dt+ foo
                   List of relations
 Schema │ Name │ Type  │ Owner │  Size   │ Description
────────┼──────┼───────┼───────┼─────────┼─────────────
 public │ foo  │ table │ pg    │ 3544 kB │
(1 row)

postgres=# \di+ foo_pkey_idx
                           List of relations
 Schema │     Name     │ Type  │ Owner │ Table │  Size   │ Description
────────┼──────────────┼───────┼───────┼───────┼─────────┼─────────────
 public │ foo_pkey_idx │ index │ pg    │ foo   │ 2208 kB │
(1 row)

postgres=# alter table foo alter column pkey type int8;
ALTER TABLE

postgres=# \di+ foo_pkey_idx
                           List of relations
 Schema │     Name     │ Type  │ Owner │ Table │  Size   │ Description
────────┼──────────────┼───────┼───────┼───────┼─────────┼─────────────
 public │ foo_pkey_idx │ index │ pg    │ foo   │ 2208 kB │
(1 row)

The unchanged size of the index foo_pkey_idx seen here due to
alignment considerations. Granted, the heap might still be a bit
larger than it would otherwise be, because it will usually be
"composite", but I think the "cost" of your using int8 will not break
the bank.

-- 
Peter Geoghegan



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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencersonconflicts
@ 2016-01-06 22:00  Paul <paul@salesintel.com>
  parent: Peter Geoghegan <pg@heroku.com>
  1 sibling, 1 reply; 13+ messages in thread

From: Paul @ 2016-01-06 22:00 UTC (permalink / raw)
  To: Peter Geoghegan <pg@heroku.com>; +Cc: Andres Freund <andres@anarazel.de>; pgsql-bugs

I’m looking at math more like a single Fact table having  500 million records, with 10 dimension columns. If INTs were used for the dimension columns, that’s 20GB. If I had to change those dimension columns to BIGINTs, that’s 40GB. This can impact how much you can fit into server memory, where olaps like to live, and just slow down moving stuff around between memory and disk and over the network and backups, etc.

On a technical note, why is the following flow considered ‘fragile’?
1) Evaluate only columns used in conflict_target
a. Conflict-resolving Columns with default nextval() increment the corresponding sequencer
i. And in this case, there were never be conflicts by definition, so ON CONFLICT can always be ignored
2) If conflict, DO UPDATE
a. If nextval()-defaulted column used in conflict_target, we never get here
b. Defaults never evaluated
3) Else Evaluate remaining columns not used in conflict_target and INSERT
a. Columns with nextval() increment their corresponding sequencer





From: Peter Geoghegan
Sent: Wednesday, January 6, 2016 2:13 PM
To: Paul
Cc: Andres Freund; pgsql-bugs
Subject: Re: [BUGS] BUG #13846: INSERT ON CONFLICT consumes sequencersonconflicts

On Wed, Jan 6, 2016 at 1:00 PM, Paul <paul@salesintel.com> wrote:
> My only point is, there’s another great capability in Postgres, for doing
> efficient concurrent UPSERTS, but that one of the most common and prime use
> cases for it is going to cost more money from having to use 64bit numbers
> everywhere for no good reason.

Not really; at least, it won't increase the size of a plain BIGSERIAL
primary key index (which will generally not be composite):

postgres=# create table foo as select (random() * 100000000)::int4
pkey from generate_series(1, 100000);
SELECT 100000
postgres=# create index on foo (pkey);
CREATE INDEX

postgres=# \dt+ foo
                   List of relations
 Schema │ Name │ Type  │ Owner │  Size   │ Description
────────┼──────┼───────┼───────┼─────────┼─────────────
 public │ foo  │ table │ pg    │ 3544 kB │
(1 row)

postgres=# \di+ foo_pkey_idx
                           List of relations
 Schema │     Name     │ Type  │ Owner │ Table │  Size   │ Description
────────┼──────────────┼───────┼───────┼───────┼─────────┼─────────────
 public │ foo_pkey_idx │ index │ pg    │ foo   │ 2208 kB │
(1 row)

postgres=# alter table foo alter column pkey type int8;
ALTER TABLE

postgres=# \di+ foo_pkey_idx
                           List of relations
 Schema │     Name     │ Type  │ Owner │ Table │  Size   │ Description
────────┼──────────────┼───────┼───────┼───────┼─────────┼─────────────
 public │ foo_pkey_idx │ index │ pg    │ foo   │ 2208 kB │
(1 row)

The unchanged size of the index foo_pkey_idx seen here due to
alignment considerations. Granted, the heap might still be a bit
larger than it would otherwise be, because it will usually be
"composite", but I think the "cost" of your using int8 will not break
the bank.

-- 
Peter Geoghegan



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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencersonconflicts
@ 2016-01-06 22:11  Andres Freund <andres@anarazel.de>
  parent: Paul <paul@salesintel.com>
  0 siblings, 0 replies; 13+ messages in thread

From: Andres Freund @ 2016-01-06 22:11 UTC (permalink / raw)
  To: Paul <paul@salesintel.com>; +Cc: Peter Geoghegan <pg@heroku.com>; pgsql-bugs

Hi,

On 2016-01-06 15:00:17 -0700, Paul wrote:
> I’m looking at math more like a single Fact table having  500 million
> records, with 10 dimension columns. If INTs were used for the
> dimension columns, that’s 20GB.

> If I had to change those dimension columns to BIGINTs, that’s
> 40GB. This can impact how much you can fit into server memory, where
> olaps like to live, and just slow down moving stuff around between
> memory and disk and over the network and backups, etc.

Row headers, padding & alignment makes that absolutely different in
reality. It's like a sub 20% difference.

And in pretty much all the cases with sufficient insertion rates you're
going to want bigints anyway. If there's few rows it doesn't matter
anyway.


> On a technical note, why is the following flow considered ‘fragile’?

The fragility comes from the fact that it'd only be used in a subset of
cases. E.g. if a BEFORE trigger is present the to-be-inserted rows needs
to be "materialized" to be presented to the trigger.


> 1) Evaluate only columns used in conflict_target
> a. Conflict-resolving Columns with default nextval() increment the corresponding sequencer
> i. And in this case, there were never be conflicts by definition, so
> ON CONFLICT can always be ignored

Wrong. Rows with sequences can very well conflict, there's nothing
forcing sequences to always be used.

Also note that sequence default values aren't in any way different from other
default values, and that relevant pieces of code currently don't know
whether a default value is a nextval or not.

> 2) If conflict, DO UPDATE
> a. If nextval()-defaulted column used in conflict_target, we never get here
> b. Defaults never evaluated
> 3) Else Evaluate remaining columns not used in conflict_target and INSERT
> a. Columns with nextval() increment their corresponding sequencer

Perhaps read the code, and prototype it? I can tell you that it'd be a
significant amount of work, and that I'm personally absolutely not
interested in investing significant amounts time into it. But that
doesn't have to stop *you*.


Anyway, EOD for me.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* Re: BUG #13846: INSERT ON CONFLICT consumes sequencers onconflicts
@ 2016-01-07 00:13  Greg Stark <stark@mit.edu>
  parent: Paul <paul@salesintel.com>
  1 sibling, 0 replies; 13+ messages in thread

From: Greg Stark @ 2016-01-07 00:13 UTC (permalink / raw)
  To: Paul <paul@salesintel.com>; +Cc: Andres Freund <andres@anarazel.de>; Peter Geoghegan <pg@heroku.com>; pgsql-bugs

On Wed, Jan 6, 2016 at 9:00 PM, Paul <paul@salesintel.com> wrote:
> As the raw event data is processed, I want to do an UPSERT into the device
> class table, using only the class name to resolve conflicts, to get it’s
> surrogate key, which is then used while inserting the related event record
> linking that event record to the particular device class.  Since device
> classes are rarely added, it will be the common case that an actual INSERT
> is almost never performed.


Fwiw while the implementation of UPSERT is fairly efficient it's
probably still too high an overhead to handle this kind of case.
Ingesting high volume of sensor data like this you want to be very
fast with little extra work. It's perfectly sensible to use UPSERT to
insert sensor ids but you wouldn't want to do so on every single
sensor datum or you'll end up spending more time and I/O doing that
than just ingesting your raw data.

Luckily in a case like that you don't expect the sensor ids to be
updated or deleted so you aren't really concerned about concurrency
except for a race between multiple insertions. So simply keeping a
list of known sensors and using upsert whenever a new sensor id is
seen (and periodically refreshing the list) would work perfectly well.
And independently of how sequences work you would want to be doing
that anyways for performance.

I'm more sympathetic to your concern about storage density than Peter
and Andres seem to be but unfortunately the reality is that it's much
harder to implement what you describe than it might appear. That often
happens in Postgres because it's a general purpose flexible platform
and has to handle whatever setup users create. So often solutions that
make perfect sense for a typical setup actually don't work in general.
Implementing UPSERT correctly for general case was actually really
really hard already so avoiding extra complications for features like
this is an important strategy for getting things done. I'm sure you'll
agree that we're better off with UPSERT with this limitation than no
UPSERT at all...

Fwiw this is the kind of feedback for which there are beta releases.
We need users to try to develop applications using new features before
they're released to find exactly these types of mismatches with user
expectations. Even if someone wanted to work on this now it wouldn't
happen until 9.6 which means any application that it would have helped
would probably already have run into the problem and had to adjust
already.

-- 
greg


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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


end of thread, other threads:[~2016-01-07 00:13 UTC | newest]

Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-01-05 15:02 BUG #13846: INSERT ON CONFLICT consumes sequencers on conflicts paul@salesintel.com
2016-01-05 16:13 ` Andres Freund <andres@anarazel.de>
2016-01-05 17:27   ` Re: BUG #13846: INSERT ON CONFLICT consumes sequencers onconflicts Paul <paul@salesintel.com>
2016-01-05 19:27   ` Peter Geoghegan <pg@heroku.com>
2016-01-06 05:04     ` Paul Hester <paul@salesintel.com>
2016-01-06 18:19       ` Andres Freund <andres@anarazel.de>
2016-01-06 21:00         ` Re: BUG #13846: INSERT ON CONFLICT consumes sequencers onconflicts Paul <paul@salesintel.com>
2016-01-06 21:13           ` Re: BUG #13846: INSERT ON CONFLICT consumes sequencers onconflicts Peter Geoghegan <pg@heroku.com>
2016-01-06 21:21             ` Re: BUG #13846: INSERT ON CONFLICT consumes sequencersonconflicts Paul <paul@salesintel.com>
2016-01-06 22:00             ` Re: BUG #13846: INSERT ON CONFLICT consumes sequencersonconflicts Paul <paul@salesintel.com>
2016-01-06 22:11               ` Re: BUG #13846: INSERT ON CONFLICT consumes sequencersonconflicts Andres Freund <andres@anarazel.de>
2016-01-07 00:13           ` Re: BUG #13846: INSERT ON CONFLICT consumes sequencers onconflicts Greg Stark <stark@mit.edu>
2016-01-06 19:26       ` Thomas Kellerer <spam_eater@gmx.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