Re: ALTER TABLE uses a bistate but not for toast tables - Mailing list pgsql-hackers

From Drouvot, Bertrand
Subject Re: ALTER TABLE uses a bistate but not for toast tables
Date
Msg-id fa936b64-f228-dd58-c0bd-44f19d43d5f6@amazon.com
Whole thread Raw
In response to ALTER TABLE uses a bistate but not for toast tables  (Justin Pryzby <pryzby@telsasoft.com>)
Responses Re: ALTER TABLE uses a bistate but not for toast tables
Re: ALTER TABLE uses a bistate but not for toast tables
List pgsql-hackers

Hi,

On 6/22/22 4:38 PM, Justin Pryzby wrote:
ATRewriteTable() calls table_tuple_insert() with a bistate, to avoid clobbering
and polluting the buffers.

But heap_insert() then calls
heap_prepare_insert() >
heap_toast_insert_or_update >
toast_tuple_externalize >
toast_save_datum >
heap_insert(toastrel, toasttup, mycid, options, NULL /* without bistate:( */);

Good catch!

I came up with this patch.  

+       /* Release pin after main table, before switching to write to toast table */
+       if (bistate)
+               ReleaseBulkInsertStatePin(bistate);

I'm not sure we should release and reuse here the bistate of the main table: it looks like that with the patch ReadBufferBI() on the main relation wont have the desired block already pinned (then would need to perform a read).

What do you think about creating earlier a new dedicated bistate for the toast table?

+       if (bistate)
+       {
+               table_finish_bulk_insert(toastrel, options); // XXX

I think it's too early, as it looks to me that at this stage we may have not finished the whole bulk insert yet.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

pgsql-hackers by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: [RFC] building postgres with meson - v12
Next
From: Michael Paquier
Date:
Subject: Re: SYSTEM_USER reserved word implementation