Re: INSERT ... ON CONFLICT IGNORE (and UPDATE) 3.0

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Peter Geoghegan <pg(at)heroku(dot)com>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>, Stephen Frost <sfrost(at)snowman(dot)net>, Bruce Momjian <bruce(at)momjian(dot)us>, Jeff Janes <jeff(dot)janes(at)gmail(dot)com>, Andres Freund <andres(at)anarazel(dot)de>
Subject: Re: INSERT ... ON CONFLICT IGNORE (and UPDATE) 3.0
Date: 2015-04-15 14:58:54
Message-ID: 552E7CAE.6070309@iki.fi
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 04/15/2015 07:51 AM, Peter Geoghegan wrote:
> +heap_finish_speculative(Relation relation, HeapTuple tuple, bool conflict)
> +{
> + if (!conflict)
> + {
> + /*
> + * Update the tuple in-place, in the common case where no conflict was
> + * detected during speculative insertion.
> + *
> + * When heap_insert is called in respect of a speculative tuple, the
> + * page will actually have a tuple inserted. However, during recovery
> + * replay will add an all-zero tuple to the page instead, which is the
> + * same length as the original (but the tuple header is still WAL
> + * logged and will still be restored at that point). If and when the
> + * in-place update record corresponding to releasing a value lock is
> + * replayed, crash recovery takes the final tuple value from there.
> + * Thus, speculative heap records require two WAL records.
> + *
> + * Logical decoding interprets an in-place update associated with a
> + * speculative insertion as a regular insert change. In other words,
> + * the in-place record generated affirms that a speculative insertion
> + * completed successfully.
> + */
> + heap_inplace_update(relation, tuple);
> + }
> + else
> + {

That's a bizarre solution. May I suggest a much simpler one:

Make the heap-insert record the same for normal and speculative
insertions, except for a flag that's set if it's a speculative one.
Replay as usual.

When the speculative insertion is finished, write a new kind of a WAL
record for that. The record only needs to contain the ctid of the tuple.
Replaying that record will clear the flag on the heap tuple that said
that it was a speculative insertion.

In logical decoding, decode speculative insertions like any other
insertion. To decode a super-deletion record, scan the reorder buffer
for the transaction to find the corresponding speculative insertion
record for the tuple, and remove it.

BTW, that'd work just as well without the new WAL record to finish a
speculative insertion. Am I missing something?

> --- a/src/include/storage/off.h
> +++ b/src/include/storage/off.h
> @@ -26,6 +26,7 @@ typedef uint16 OffsetNumber;
> #define InvalidOffsetNumber ((OffsetNumber) 0)
> #define FirstOffsetNumber ((OffsetNumber) 1)
> #define MaxOffsetNumber ((OffsetNumber) (BLCKSZ / sizeof(ItemIdData)))
> +#define MagicOffsetNumber (MaxOffsetNumber + 1)
> #define OffsetNumberMask (0xffff) /* valid uint16 bits */

IMHO it would be nicer if the magic value was more constant, e.g. 0xffff
or 0xfffe, instead of basing it on MaxOffsetNumber which depends on
block size. I would rather not include MaxOffsetNumber of anything
derived from it in the on-disk dormat.

- Heikki

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2015-04-15 15:01:19 Re: INSERT ... ON CONFLICT IGNORE (and UPDATE) 3.0
Previous Message Andrew Dunstan 2015-04-15 14:51:30 Re: [COMMITTERS] pgsql: Move pg_upgrade from contrib/ to src/bin/