pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts
4+ messages / 3 participants
[nested] [flat]

* Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts
@ 2016-01-07 00:14 Paul <paul@salesintel.com>
  2016-05-06 19:02 ` Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

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

Hi Andres, appreciate your replies. I hope you will still read this when you can, as I believe I can make the financial case, and that the solution is relatively simple. I also believe there’s still some misunderstanding to the problem. Maybe you could point me to the right place in the code.




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

Regarding the space increase going from 10 INTs to 10 BIGINTS per row, using my example of 500 million fact records, and referring to http://www.postgresql.org/docs/9.5/static/storage-page-layout.html

Using 10 INT columns per record, with row-index, row-header, and 64 bit alignment overhead, requires 4B (row Idx) + 24B (row hdr) + 40B (row data) = 68B per row.  With a page size of 8192B, minus 24B for the page header, gives 8168 / 68 = ~120 rows per page.

Using 20 BIGINT columns per record, with row-index, row-header, and 64 bit alignment overhead, requires 4B (row Idx) + 24B (row hdr) + 80B (row data) = 108B per row.  With a page size of 8192B, minus 24B for the page header, gives 8168 / 108 = ~75 rows per page.

1 – 75 / 120 = ~38%

So its much nearer 40%, and not sub 20%. When dealing in 10s to 100’s of GBs (there are terabyte warehouses), 38% is significant, and can have a real financial impact with respect to memory, network, primary and backup storage costs. 




>> 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.

This is true for the surrogate key (generated by a sequencer default) of a Fact table (which comes from operational business transactions, like taking orders, or web-page visits). As I stated, there are warehouses with billions of fact records, that are just about always sequentially inserted and have a timestamp holding when they occurred. I have never come across a practical need to do UPSERTs on Fact tables (i.e. transaction tables, event tables, data generated by things occurring in the real world etc.).

But the table where the UPSERT is being applied is not the Fact table! It’s a related Dimension table, and in most cases, the INSERT path will not be taken!! A Fact table will have columns representing Members along Dimensions. For example, an Order Fact table holding records for individual Order Line Items, would most likely have a Dimension column to represent the Status of the Line Item; i.e. Open, Shipped, Invoiced, Closed. There would be a separate Status dimension table, with a SMALLINT column, that would be used in the Fact table to relate an Order Line Item to a particular Status. It is the Status Dimension table upon which an UPSERT would be performed for every Order Line Item ETL’d from the OLTP system into the warehouse.

So, I’m not clear how your above statement applies exactly.



>> 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.
(I’m assuming you mean the BEFORE INSERT trigger, and I’m noting you state ‘to-be-inserted’ records.)

I would then argue that the BEFORE trigger is being applied inappropriately in the case of an INSERT with an ON CONFLICT clause. First, I think the purpose of the INSERT ON CONFLICT is to implement UPSERTs; would you agree? We were always able to implement UPSERT logic before, it just was a pain in the butt because of concurrency. Before, I would have had a separate INSERT statement and a separate UPDATE statement. If I had a BEFORE INSERT trigger, it would only execute for records that were actually going to be inserted. However, you’re now stating that the INSERT ON CONFLICT will execute a BEFORE INSERT trigger even for records that are not inserted?? That doesn’t seem quite logical, and contrary to ‘old fashioned’ upsert logic. Does this also mean that the BEFORE UPDATE trigger is always called as well, or is it never called?



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

My oversight, you are correct here, should an explicit value be provided for a column normally defaulted from a sequencer. But that doesn’t invalidate the basic premise of what I’ve been trying to get across.



>> 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.

The relevant pieces of code wouldn’t need to know anything about how the default value is computed for any column. The relevant code merely needs to only evaluate default values for only the columns used to resolve conflicts, and not all of the columns. Then, for the records that don’t conflict, evaluate any remaining default expressions, and then call the BEFORE INSERT trigger with only the records that are actually going to be inserted!!


I greatly welcome your thoughts, and please point to the right place in the code.

Regards,
paul


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

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.

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

* Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts
  2016-01-07 00:14 Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts Paul <paul@salesintel.com>
@ 2016-05-06 19:02 ` David G. Johnston <david.g.johnston@gmail.com>
  2016-05-06 19:10   ` Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts Peter Geoghegan <pg@heroku.com>
  0 siblings, 1 reply; 4+ messages in thread

From: David G. Johnston @ 2016-05-06 19:02 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 5:14 PM, Paul <paul@salesintel.com> wrote:

> If I had a BEFORE INSERT trigger, it would only execute for records that
> were actually going to be inserted. However, you’re now stating that the
> INSERT ON CONFLICT will execute a BEFORE INSERT trigger even for records
> that are not inserted?? That doesn’t seem quite logical, and contrary to
> ‘old fashioned’ upsert logic. Does this also mean that the BEFORE UPDATE
> trigger is always called as well, or is it never called?
>

​This seems to boil down to the two possible ways of manually implementing
UPSERT:

UPDATE, if not present, INSERT
INSERT, if failing, UPDATE

In the later the before insert trigger fires and influences whether the
insert​

​fails.  In the former you are already pretty certain the insert will
​succeed because the UPDATE found no records.

We've implemented INSERT, if failing UPDATE.  The insert has to be
attempted and right now there is no concept of targeted partial
deferrability when constructing the record to be inserted.

To solve this situation it is likely that some form of "UPDATE ON MISSING
INSERT" would need to be designed.  The insert portion would specify
"DEFAULT" for sequence columns and would execute nextval() only if the ON
MISSING portion is executed.

Fundamentally, the difference is that ON MISSING is considerably less
complicated than ON CONFLICT.  What is wanted here is an ON MISSING
interpretation but what we've implemented is ON CONFLICT.  It seems that
the hackers are in agreement that our implementation of ON CONFLICT is
consistent with its definition.  That it doesn't efficiently solve problems
better handled by ON MISSING - while unfortunate - doesn't constitute a
bug: only an opportunity for future enhancement.

David J.


​

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

* Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts
  2016-01-07 00:14 Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts Paul <paul@salesintel.com>
  2016-05-06 19:02 ` Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts David G. Johnston <david.g.johnston@gmail.com>
@ 2016-05-06 19:10   ` Peter Geoghegan <pg@heroku.com>
  2016-05-06 19:15     ` Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Peter Geoghegan @ 2016-05-06 19:10 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: Paul <paul@salesintel.com>; Andres Freund <andres@anarazel.de>; pgsql-bugs

On Fri, May 6, 2016 at 12:02 PM, David G. Johnston
<david.g.johnston@gmail.com> wrote:
> To solve this situation it is likely that some form of "UPDATE ON MISSING
> INSERT" would need to be designed.  The insert portion would specify
> "DEFAULT" for sequence columns and would execute nextval() only if the ON
> MISSING portion is executed.

That's unworkable, at least without accepting a bunch of new
edge-cases, like having the insert then have a duplicate violation
involving a value that was determined to not exist in the first phase.
IOW, it's unworkable to do an insert on the basis of an *absence* of
something in an index or in a table (and not get those edge-cases).
Doing so on the basis of the *presence* of a value (i.e. INSERT ... ON
CONFLICT DO UPDATE as implemented) lets the implementation clamp down
on race conditions enough to provide those useful user-visible
guarantees about getting 1 of 2 possible outcomes.

There are multiple definitions of a value "existing" here that are in
tension here. It's rather complicated.

-- 
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] 4+ messages in thread

* Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts
  2016-01-07 00:14 Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts Paul <paul@salesintel.com>
  2016-05-06 19:02 ` Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts David G. Johnston <david.g.johnston@gmail.com>
  2016-05-06 19:10   ` Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts Peter Geoghegan <pg@heroku.com>
@ 2016-05-06 19:15     ` David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: David G. Johnston @ 2016-05-06 19:15 UTC (permalink / raw)
  To: Peter Geoghegan <pg@heroku.com>; +Cc: Paul <paul@salesintel.com>; Andres Freund <andres@anarazel.de>; pgsql-bugs

On Fri, May 6, 2016 at 12:10 PM, Peter Geoghegan <pg@heroku.com> wrote:

> On Fri, May 6, 2016 at 12:02 PM, David G. Johnston
> <david.g.johnston@gmail.com> wrote:
> > To solve this situation it is likely that some form of "UPDATE ON MISSING
> > INSERT" would need to be designed.  The insert portion would specify
> > "DEFAULT" for sequence columns and would execute nextval() only if the ON
> > MISSING portion is executed.
>
> That's unworkable, at least without accepting a bunch of new
> edge-cases, like having the insert then have a duplicate violation
> involving a value that was determined to not exist in the first phase.
> IOW, it's unworkable to do an insert on the basis of an *absence* of
> something in an index or in a table (and not get those edge-cases).
> Doing so on the basis of the *presence* of a value (i.e. INSERT ... ON
> CONFLICT DO UPDATE as implemented) lets the implementation clamp down
> on race conditions enough to provide those useful user-visible
> guarantees about getting 1 of 2 possible outcomes.
>
> There are multiple definitions of a value "existing" here that are in
> tension here. It's rather complicated.
>

​UPDATE ON MISSING INSERT ON CONFLICT DO THE ORIGINAL UPDATE ...

​I'm nowhere near close enough to this to contribute deeply - the most I
hope for is to spark an idea in someone else.

​David J.
​

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


end of thread, other threads:[~2016-05-06 19:15 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-01-07 00:14 Re: BUG #13846: INSERT ON CONFLICT consumessequencersonconflicts Paul <paul@salesintel.com>
2016-05-06 19:02 ` David G. Johnston <david.g.johnston@gmail.com>
2016-05-06 19:10   ` Peter Geoghegan <pg@heroku.com>
2016-05-06 19:15     ` David G. Johnston <david.g.johnston@gmail.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