Support for 8-byte TOAST values, round two

Поиск
Список
Период
Сортировка
Искать

Support for 8-byte TOAST values, round two

Hi all,

This is a follow-up of the previous thread about $subject, with a
reworked patch set for discussion in v20, as I care about the subject:
https://www.postgresql.org/message-id/aFOnKHG7Wn-Srnpv@paquier.xyz

The main feedback of the previous thread is that the previous
implementation with its callbacks for each vartag was not liked much,
and their were concerns with pointer redirections and performance.
This patch set uses what I am calling the "brutal" approach, relying
on a vartag_external of a varlena or the atttype of the TOAST relation
to decide which external toast pointer we should use.  This uses no
function pointers, and patches the code to deal with Oid or Oid8 TOAST
values where it matters.  So, this time, performance cannot really be
an issue.

The patch set is structured so as all the ground work happens first
(most of it comes from the previous patch set, reorganized a bit), and
the introduction of the varatt pieces are last, based on the following
rules:
- A table can use a 8-byte TOAST value with a new reloption, named
toast_value_type that can be set to "oid" (default) or "oid8",
creating a TOAST table with a value of the assigned type.  This
includes support for dumps as well as binary upgrades, so as the
atttype of the chunk_id of the TOAST table is preserved.  A table with
a TOAST type assigned cannot be changed to a different type through a
VACUUM FULL or a rewrite, as a matter of implementation simplicity.
- Renames and cleanup of various areas related to varatt_external,
renaming things to use OID.
- The 8-byte TOAST values rely on a Oid8, whose value is retrieved
from the control file extended by 4 bytes.  The code supports
wraparound of values so as we don't assign anything between 0 and
FirstNormalObjectId for the lower bytes, same way as before.

The last patch introduces a new vartag_external and the new
varatt_external_oid8, with an Oid8 as value.  Well, not exactly, the
patch uses two uint32 fields so as the structure is packed without
padding, as of:
typedef struct varatt_external_oid8
{
   int32       va_rawsize;
   uint32      va_extinfo;
   uint32      va_valueid_lo;
   uint32      va_valueid_hi;
   Oid         va_toastrelid;
} varatt_external_oid8;

Note that if applying all the patches except the last one, the code
would use an varatt_external_oid with an oid8 TOAST table.  This
works, the split is to make reviews easier.  An oid8 TOAST table
always uses a varatt_external_oid8.

I have done a lot of back-and-forth in the patch to try to find a good
balance between the manipulation of the varlenas in the detoast and
compression paths, as well as reorderbuffer.c and amcheck.  And I have
finished with the attached, which is kind of nice.  The last patch has
a low footprint:
9 files changed, 537 insertions(+), 208 deletions(-)

Happy to discuss about all that at pgconf.dev.  This is of course
intended for v20, added to the next CF.
Thanks,
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Yugo Nagata <nagata@sraoss.co.jp>
Дата:
On Fri, 8 May 2026 15:07:13 +0900
Michael Paquier  wrote:

> Hi all,
> 
> This is a follow-up of the previous thread about $subject, with a
> reworked patch set for discussion in v20, as I care about the subject:
> https://www.postgresql.org/message-id/aFOnKHG7Wn-Srnpv@paquier.xyz
> 
> The main feedback of the previous thread is that the previous
> implementation with its callbacks for each vartag was not liked much,
> and their were concerns with pointer redirections and performance.
> This patch set uses what I am calling the "brutal" approach, relying
> on a vartag_external of a varlena or the atttype of the TOAST relation
> to decide which external toast pointer we should use.  This uses no
> function pointers, and patches the code to deal with Oid or Oid8 TOAST
> values where it matters.  So, this time, performance cannot really be
> an issue.
> 
> The patch set is structured so as all the ground work happens first
> (most of it comes from the previous patch set, reorganized a bit), and
> the introduction of the varatt pieces are last, based on the following
> rules:
> - A table can use a 8-byte TOAST value with a new reloption, named
> toast_value_type that can be set to "oid" (default) or "oid8",
> creating a TOAST table with a value of the assigned type.  This
> includes support for dumps as well as binary upgrades, so as the
> atttype of the chunk_id of the TOAST table is preserved.  A table with
> a TOAST type assigned cannot be changed to a different type through a
> VACUUM FULL or a rewrite, as a matter of implementation simplicity.
> - Renames and cleanup of various areas related to varatt_external,
> renaming things to use OID.
> - The 8-byte TOAST values rely on a Oid8, whose value is retrieved
> from the control file extended by 4 bytes.  The code supports
> wraparound of values so as we don't assign anything between 0 and
> FirstNormalObjectId for the lower bytes, same way as before.
> 
> The last patch introduces a new vartag_external and the new
> varatt_external_oid8, with an Oid8 as value.  Well, not exactly, the
> patch uses two uint32 fields so as the structure is packed without
> padding, as of:
> typedef struct varatt_external_oid8
> {
>    int32       va_rawsize;
>    uint32      va_extinfo;
>    uint32      va_valueid_lo;
>    uint32      va_valueid_hi;
>    Oid         va_toastrelid;
> } varatt_external_oid8;
> 
> Note that if applying all the patches except the last one, the code
> would use an varatt_external_oid with an oid8 TOAST table.  This
> works, the split is to make reviews easier.  An oid8 TOAST table
> always uses a varatt_external_oid8.
> 
> I have done a lot of back-and-forth in the patch to try to find a good
> balance between the manipulation of the varlenas in the detoast and
> compression paths, as well as reorderbuffer.c and amcheck.  And I have
> finished with the attached, which is kind of nice.  The last patch has
> a low footprint:
> 9 files changed, 537 insertions(+), 208 deletions(-)

I applied the patches to the master branch and run a simple test.

After running:
  
 $ pg_resetwal -o 4300000000 -D data

 CREATE TABLE tbl_oid8(t text) with (toast_value_type = 'oid8');
 ALTER TABLE tbl_oid8 ALTER t SET STORAGE EXTERNAL;
 CREATE TABLE tbl_oid(t text) with (toast_value_type = 'oid');
 ALTER TABLE tbl_oid ALTER t SET STORAGE EXTERNAL;

 INSERT INTO tbl_oid8 VALUES (repeat(md5('x'),100));
 INSERT INTO tbl_oid VALUES (repeat(md5('x'),100));

 SELECT tableoid, t = repeat(md5('x'),100) FROM tbl_oid8;
 tableoid | ?column? 
----------+----------
  5032704 | t
(1 row)

 SELECT chunk_id, chunk_seq FROM pg_toast.pg_toast_5032704;
  chunk_id  | chunk_seq 
------------+-----------
 4300000010 |         0
 4300000010 |         1
(2 rows)

 SELECT tableoid, t = repeat(md5('x'),100) FROM tbl_oid;
 tableoid | ?column? 
----------+----------
  5032709 | t
(1 row)

 SELECT chunk_id, chunk_seq FROM pg_toast.pg_toast_5032709;
 chunk_id | chunk_seq 
----------+-----------
  5032715 |         0
  5032715 |         1
(2 rows)

This appears to work as expected.

I noticed one issue, though.
When toast_value_type is specified as something other than oid or oid8, the error
message is:

 postgres=# CREATE TABLE tbl(t text) with (toast_value_type = 'x');
 ERROR:  invalid value for enum option "toast_value_type": x
 DETAIL:  Valid values are "oid".

The detail message should be updated to include oid8.


I also have a few small comments on the patches.

- 0009

(1)
The commit message says:

 TOAST pointers still rely on varatt_external and a single vartag, with
 all the values inserted in the bigint TOAST tables fed from the existing
 OID value generator. 

This made me a bit confused because chunk_ids insereted into TOAST tables using oid8
are generated by GetNewObjectId8().

+		else if (toast_typid == OID8OID)
+			toast_pointer.va_valueid = GetNewObjectId8();

This value is later cast to Oid here, so what is actually stored is a 4-byte chunk_id,
but saying that the values are "fed from the existing OID value generator" seems
a bit inaccurate to me.

(2)
 	/*
-	 * Choose an OID to use as the value ID for this toast value.
+	 * Choose a new value to use as the value ID for this toast value, be it
+	 * for OID or int8-based TOAST relations.

This patch relies on oid8, introduced by b139bd3b6ef, rather than int8, so, I
wonder if this comment sould also be updated.

(3)
 belonging to the owning table.  Every
-TOAST table has the columns chunk_id (an OID
-identifying the particular TOASTed value),
+TOAST table has the columns
+chunk_id (an OID or an 8-byte integer identifying
+the particular TOASTed value),
 chunk_seq (a sequence number for the chunk within its value),

Similarly, I think the documentation should mention oid8 rather than 8-byte
integer. That would also be consistent with the CREATE TABLE documentation for
the toast_value_type parameter.

- 0011

(4)
+					/*
+					 * Check if this value already exists in the new toast
+					 * table (corner case during table rewrite with multiple
+					 * versions of the same row).
+					 */
+					if (toastrel_valueid_exists(toastrel, va_valueid))
+					{
+						/* Match, so short-circuit the data storage loop below */
+						data_todo = 0;
+					}

The same code appears later, together with a more detailed comment. How about
moving the detailed comment here instead, or simply referring to the later comment?

(5)
+/* Is varlena datum a pointer to on-disk toasted data with 8-byte value ID? */
+static inline bool
+VARATT_IS_EXTERNAL_ONDISK_OID8(const void *PTR)
+{
+	return VARATT_IS_EXTERNAL(PTR) && VARTAG_EXTERNAL(PTR) == VARTAG_ONDISK_OID8;
 }

This macro is defined but not used. Is it intended to be used in places that
check VARTAG_ONDISK_OID8 directly, for example:

+	if (VARTAG_EXTERNAL(attr) == VARTAG_ONDISK_OID8)

?

(6)

 any.  Allowing for the varlena header bytes,
-the total size of an on-disk TOAST pointer datum is therefore 18
-bytes regardless of the actual size of the represented value.
+the total size of an on-disk TOAST pointer datum is 18
+bytes when using an OID as chunk_id, or 22 bytes
+when using an 8-byte integer, regardless of the actual size of the represented
+value.
 

Should this also refer to Oid8 rather than 8-byte integner, for consistency?

Regards,
Yugo Nagata

-- 
Yugo Nagata 


Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Wed, Sep 02, 2026 at 01:27:00PM -0700, Bharath Rupireddy wrote:
> I first tried to reproduce the query latency increase problem with
> less time and disk space. Here are the results. I ensured each insert
> steps over K ids using pg_resetwal, which equals the number of
> iterations spent in GetNewOidWithIndex(). There's a clear benefit as
> the 8-byte chunk_id avoids the retries while getting the new OID, and
> since a 64-bit OID is almost never exhausted, this works. Attached the
> test script that I used for reference.
> 
> K                HEAD_INSERT       patched_INSERT
> 1,000,000        1.25 s            1.0 ms
> 10,000,000       12.4 s            1.0 ms
> 50,000,000       1 min             1.0 ms
> 100,000,000      2 min             1.0 ms
> 200,000,000      9 min             0.9 ms

This measures how much time we take to grab a free OID value, hoping
that there is a hole.

> With this context, I started to review the patches. One thing I liked
> is the way the patches were split; they made review a lot easier. Here
> are some comments.

Thanks for the comments.

> Comments on 0001:
> 
> 1/
> +table_relation_fetch_toast_slice(Relation toastrel, Oid8 valueid,
> While changing Oid to Oid8 is fine for internal functions, is it okay
> to change it for the table_relation_fetch_toast_slice table AM? Is
> there any chance that an external table AM forgets to update? I'm not
> arguing against this change, just checking if there's any way out
> here. Of course, the compiler will generate an incompatible function
> parameter type warning, and since the change is going in a major
> release, that seems fine. One idea could be to have a TOAST version
> field elsewhere to detect such changes, but that seems overkill. So,
> having it like 0001 seems fine to me unless others have any thoughts.
> PS: one option is to send the 8-byte OID via a caller-allocated
> varlena result pointer, but this seems a bit ugly.

Yes, assuming that a table AM uses its own fetch_toast_slice(), it
will need to update probably for its own ScanKeyInit() if an external
TOAST table is in use for the OID8 case, assuming that they need to do
so based on the reloption for the toast value type.  I am not really
convinced that we need to be fancy here, telling that after looking at
out-of-code projets that include their own callbacks:
https://github.com/eatonphil/pgtam (no support)
https://github.com/rohankumardubey/pg_mooncake
https://github.com/neurdb/neurdb (no support)
https://github.com/duckdb/pg_duckdb (totally different callback here)
https://github.com/timescale/timescaledb
https://github.com/jeffreydwalter/pg_tde/ (needs refresh)
https://github.com/ewhauser/unitpg/ (just reuses the heap call)

There are also a few more, but I am seeing nothing beyond an extra
ScanKeyInit() update required, in a sea of non-supported cases.

> Widening the per-toast-chunk hash key to an 8-byte OID still handles
> 4-byte chunk_ids correctly during logical decoding, since the smaller
> values zero-extend. The tradeoff is that with an 8-byte key the hash
> no longer uses the uint32_hash fast path and falls back to tag_hash,
> so even existing 4-byte chunk_id tables lose that optimization during
> logical decoding. I haven't measured the effect, but it seems worth
> checking.

Yeah, point taken.  I'm putting a note down on this one.

> I think 0002 and 0003 are purely mechanical; they look good to me and
> can go in first.

Thanks.  0002 and 0003 are kind of independent on the rest, not
requiring any Oid8 idea, so I'll go apply them.

> 1/
> - WHERE o.chunk_id != pg_column_toast_chunk_id(c.value); + WHERE
> o.chunk_id::oid8 != pg_column_toast_chunk_id(c.value);
> 
> Changing the return value of pg_column_toast_chunk_id() to 8-byte OID
> is fine and it can work for existing 4-byte chunk_ids. Do we need the
> above typecasting in tests given the chunk_id is captured from
> pg_column_toast_chunk_id() while creating the table?

I guess we do.  Will double-check.

> Comments on 0005:
> 
> 1/  + return murmurhash64(DatumGetObjectId8(datum));
> 
> I think having catalog cache support for 8-byte OID might be useful on
> its own and it looks good to me except the following: can we typecast
> the Oid8 to murmurhash64((uint64) DatumGetObjectId8(datum))?

Ah, you want to add an extra cast here, with (uint64) for the
murmurhash64.  Hmm why not, that seems more consistent with the rest
of the area.  This one is also a rather independent item for the Oid8,
so I'm also planning to apply it to reduce the stack.

> Comments on 0008:
> 
> 1/ I think adding reloption seems okay to me, so the 0008 patch looks
> good. Just one suggestion: having a GUC to set it once for all new
> tables seems a good idea as it avoids application changes, but
> starting with reloption is good enough.

The round one of the patch set used a GUC.  Andres has argued in favor
of a reloption.

> I will take a look at 0006, 0007, 0009, 0010, 0011 in the coming weeks.

Cool, thanks.

> One more thought: existing tables won't get 8-byte chunk_ids as part
> of pg_upgrade. pg_dump and pg_restore could be used after the upgrade
> for existing customers to get their tables onto this format. At some
> point, providing concurrent repack-like support (or reusing the
> underlying machinery) to do this online would be nice to have.

The infrastructure that could be used to switch the toast value type
and its rewrite is out of scope, the patch being complicated enough..
We could always think about that later, giving the choice in core is
much more important to me as a first step, because without the
OID8/OID choice, there is no discussion about the rewrite part.
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Fri, Sep 04, 2026 at 08:11:00PM -0700, Bharath Rupireddy wrote:
> On Wed, Sep 2, 2026 at 9:58 PM Michael Paquier  wrote:
>> On Wed, Sep 02, 2026 at 01:27:00PM -0700, Bharath Rupireddy wrote:
>>> Widening the per-toast-chunk hash key to an 8-byte OID still handles
>>> 4-byte chunk_ids correctly during logical decoding, since the smaller
>>> values zero-extend. The tradeoff is that with an 8-byte key the hash
>>> no longer uses the uint32_hash fast path and falls back to tag_hash,
>>> so even existing 4-byte chunk_id tables lose that optimization during
>>> logical decoding. I haven't measured the effect, but it seems worth
>>> checking.

While navigating through the code, I am not planning to worry about
it at the end, as the performance sits close to some WAL record
handling and tuple copying, where most of the performance is on the
table there.

> Also, I think there's an issue in ReorderBufferToastAppendChunk() with
> 0001. The chunk_id is still fetched as a 4-byte value, so past 4
> billion chunk_ids the truncated key stored in the TOAST hash won't
> match the 64-bit value used for the lookup later in
> ReorderBufferToastReplace().

Hmm.  This one may be debatable because it would not be reached until
the OID8 business is plugged into the TOAST values.  Adding that in
0001 is less business to deal with after.  For now I've fixed that in
0001 to keep the changes with reorderbuffer.c more isolated.

> That works for me. A quick question, though I haven't tested this.
> Does ALTER TABLE error out if the reloption is changed on an existing
> table?

I was not doing that with only simplicity in mind, so one could change
toast_value_type after the initial TOAST table creation, with one
argument in mind but I agree that it may sounds a bit weird: dumps.

One could change the reloption, do a dump, and re-create the relation
with a different TOAST layer.  Agreed that it's a bit twisted but we
don't have a lot of precedent for this kind of choices with
reloptions.  I recall one similar case in the backend code, with BRIN
indexes and pages_per_range (?) that do not affect post-creation
patterns.  The WITH added by the dump is kind of nice for re-creation
based on raw data.

There is also a second scenario where this can be useful:
CREATE TABLE t (a int);
ALTER TABLE t SET (toast_value_type = oid8);
ALTER TABLE t ADD COLUMN b text; -- creates TOAST relation

I've mentioned that in the docs already, as of the "creating":
+      The toast_value_type specifies the attribute type of
+      chunk_id used when initially creating a toast
+      relation for this table

I have added some tests for ALTER TABLE with some rewrites, though.
That felt like a hole.

> That's fine by me. That said, it would be good to document at least
> one clear way for existing users to move their tables to 8-byte TOAST
> chunk_ids, presumably pg_dump and pg_restore after setting the
> reloption.

Hmm, yeah.  Perhaps.  The dump/restore trick is one, at least.  Not
sure where adding this mention would be best, and if we actually need
to add that.  :)

> I'm also wondering if pg_dump needs an option to generate table
> schemas with the new reloption set, so that users don't have to edit
> the dump file. Editing may not always be possible (for example, with
> the custom and directory formats). This can be a follow-up patch, but
> having at least one supported way to migrate existing tables seems
> important to me.

The best thing that I came up for that was a GUC, because you'd want
a dump/restore option able to say "I don't want the TOAST type" to be
dumped or restored, able to enforce a type across the board for a
database, a namespace or more than N objects at the same time.  The
GUC idea was shut down in round one by Andres.

> Agreed, dropping it seems right at a quick glance. The chunk_id comes
> from the heap row's TOAST pointer and is used as a scan key against
> the TOAST index, so nothing looks up an OID8 value through the catalog
> cache.

Following c5806f2165b5 and 2b3d11aaed89, attached is a rebase of the
rest.

While on it, I have added one StaticAssertDecl() to enforce the
no-padding rule of vartag_external_oid8.  There was also a spot I have
missed in pg_column_toast_chunk_id() to handle oid8 toast pointers, in
the last patch.

On top of that, while doing more review, there was also an issue that
I got on my notes for some time but completely forgot about in this
thread: toast_tuple_find_biggest_attribute() needs some relcaching for
the toast value type to enforce a correct decision, or we may be off
in deciding if something should be compressed or not depending on the
toast table we are dealing with.  This uses the OID's max chunk size
as a safety measure, which should be OK.  This part is added on top of
the rest, as 0009.

Finally, I have asked Claude for a round of reviews for the full patch
set, and it has spotted two stupid mistakes caused by the new vartag
for oid8, where I missed its handling:
- toast_get_compression_id() missed handling for oid8 external
pointers.
- toast_tuple_init() can compare two external TOAST pointers, but we
cannot memcmp() safely when dealing with tuples with different
vartags, the oid8 one being larger than the oid one.  That's a nice
catch, for an egde case when doing cross-updates and TOAST pointer
reuse.

Attached is v14, with all that addressed (should perhaps have moved
the tests in 0008 to 0007, but it's late here and that does not change
the stuff to review).
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Вложения:
Hi,

On Tue, Sep 8, 2026 at 5:58 PM Michael Paquier  wrote:
>
> On Tue, Sep 08, 2026 at 04:55:16PM -0700, Bharath Rupireddy wrote:
> > I don't think pg_control_checkpoint() and pg_controldata emitting an
> > OID extracted from the OID8 value is the right idea. I understand that
> > tools and external modules do need to adapt to the wider value, but
> > that is fine for a new major version, and the control file version
> > bump gives them the signal to do so. Also, the extra 4 bytes from OID8
> > are absorbed by existing padding, so the control file stays well under
> > the safe size limit of 512 bytes anyway.
>
> I don't mean to extend the on-disk file with an extra 4-bytes for the
> Oid.  I mean to *show* an extra field based on a casted value.  These
> are two different things.

I meant changing ControlFile->checkPointCopy->nextOid to Oid8 in 0001
is okay, in addition to displaying it. Both are okay to me from the
external tooling perspective and also the on-disk control file size,
unless I am missing anything here.

> > v15-0001-Enlarge-OID-generation-to-8-bytes.patch: this looks good to
> > me, with one comment. Although the wraparound with an 8-byte OID is
> > hundreds of years away, and 0001 assumes it never happens, do we need
> > to handle the case where someone sets the OID counter closer to the
> > 8-byte OID limit using pg_resetwal and still hits the wraparound? We
> > can either reject this in pg_resetwal, handle the wraparound case in
> > the caller, document it, or mention it in the commit message or
> > somewhere near pg_resetwal. This is not a blocker for getting 0001 in,
> > just to be clear. I think this needs to be fixed as part of
> > v15-0006-Add-support-for-oid8-TOAST-values.patch, which does not check
> > for an existing TOAST OID.
>
> A few things worth noting: we have in xlog_redo() and varsup.h a
> couple of comments that mention wraparound without mentioning the oid8
> bits that can bypass it.  These need to be slightly adjusted..

- * NOTE: if the OID generator wraps around, we skip over OIDs 0-16383
- * and resume with 16384.  This minimizes the odds of OID conflict, by not
+ * NOTE: the OID generator is 8 bytes wide (see GetNewObjectId8())).  For
+ * its lower 4 bytes, we skip over OIDs 0-16383 and resume with 16384.
+ * This minimizes the odds of OID conflict when using GetNewObjectId(), by not

LGTM.

> I can fancy you with one more case, because there is no entry recheck
> in the TOAST insert path when using an 8-byte OID:
> - Insert some TOAST entries with toast_value_type=oid8.
> - Reset to a past value with pg_resetwal.
> - Reinsert, failing hard on INSERT due to a duplicated key.
>
> My best answer is don't set the counter to the past.  Somebody using
> pg_resetwal is looking for trouble already.  It's true that not being
> able to handle the OID8 value conflicts like the normal TOAST path
> could be seen as a defect of the OID8 path, but adding an index check
> (while doable of course), is just wasting resources with 8-bytes,
> because in practice that's never hit.  So "don't set it to the past"
> seems like an answer good enough?  That sounds to me like a
> documentation change.

AFAIK, pg_resetwal is not used to play with OIDs on production systems
(especially setting it backwards or beyond the wraparound limits),
even in the extreme cases when the OID4 wraps around. That said, I may
be missing a legitimate use case for doing so. If one does it on a
production system and the OID8 counter goes back, we do not have
toastid_valueid_exists() checks in the callers for OID8 chunk_ids, so
it would cause duplicate key value violation errors when inserting
into the TOAST index.

I do not know the best way to address this. A doc caveat on
pg_resetwal (like the v16-0001) might be enough. Since there is a
single OID counter (now OID8, already set by --next-oid), another
option is to have pg_resetwal warn or error out when --next-oid would
move the counter backwards, though that would also affect its
legitimate use for OID4. A third option is to keep the uniqueness
probe on the OID8 path too, so assignment stays self-correcting like
the OID4 path. The retry cost that exists with OID4, where values
start overlapping as the OID space fills and the probe loop runs,
would not occur with OID8, since there are far too many unique values
to exhaust before wraparound, so the probe would return on the first
try. It would still add one index lookup per new TOAST value versus
the current no-probe path, so it is not free, and I can re-run the
benchmark I ran previously to measure that overhead if needed.

> > v15-0003-Switch-pg_column_toast_chunk_id-return-value-fro.patch: this
> > looks good to me, with one comment. The chunk_id cast in
> > cluster-toast-value-reuse.spec seems redundant, because that column
> > comes from the output of pg_column_toast_chunk_id(). In
> > misc_functions.sql it is needed. This makes me think about whether we
> > need to support OID to OID8 comparison in more cases: existing queries
> > and monitoring tools that do pg_column_toast_chunk_id(x) = some_oid,
> > or that store the result in an OID column, will now get a
> > type-mismatch error and have to add an explicit cast. Could we ease
> > that by making the OID to OID8 cast implicit, so that an OID compared
> > against an OID8 is promoted automatically? This is not a blocker for
> > getting 0003 in, just to be clear.
>
> My reply here would be to cast the some_oid to oid8, or invent a
> secondary oid8 specific function, leaving pg_column_toast_chunk_id()
> alone and failing if trying to use it with an oid8 TOAST table?
>
> I suspect that there are not that many users of
> pg_column_toast_chunk_id() anyway.  May be wrong of course, but a cast
> to oid8 is fine by me.  I'm just not sure that it is worth the churn
> of having a second function with a different return type.

Changing the existing function's return type to OID8 seems fine to me
instead of a new function here.

> > 5/ Following on from (4). Say I create a table with oid, insert data,
> > change the reloption to oid8, and then run a rewrite (VACUUM FULL,
> > CLUSTER or REPACK). The final table still uses oid, because the
> > rewrite preserves the existing TOAST table by swapping contents rather
> > than recreating it. But the transient table that make_new_heap()
> > builds does get created with the new type, because it goes through
> > create_toast_table(), which reads the reloption without checking
> > whether this is a rewrite. With oid this makes no difference, but once
> > oid8 is added the transient TOAST table would be oid8 while the
> > existing TOAST table still says oid. Is that expected?
>
> I am not completely sure to follow here?  Wouldn't that be the case of
> create_toast_table() defining a OIDOldToast.  In this case, we inherit
> the type ID from the old table when creating what would be the
> transient table.  See the comment named as "break the world" in
> create_toast_table() added in the patch.  Or perhaps I'm missing your
> point entirely.

You are right. The new reloption is not stored in the TOAST table's
pg_class row after ALTER TABLE. Instead, it lives on the heap table's
pg_class row. So, during a TOAST table rewrite, the old TOAST table's
existing chunk_id type (OID4 here) is used for the transient table.
This makes it work for the case of create table with OID4 chunk_id,
insert data, alter to change reloption to OID8, rewrite the table. The
transient table uses OID4 chunk_id, not OID8.

Flip this a bit. Create table with OID8 chunk_id, insert data, alter
to change reloption to OID4, rewrite the table. The transient table
uses OID8 chunk_id because the TOAST table was created with OID8. The
transient table can still store chunk_ids beyond the OID4 limit.

In short, the rewrite works fine with both OID8 and OID4 chunk_ids
across ALTER TABLE and rewrites.

> > 2/ Used oid8 for all tables by default and ran regression tests to see
> > if there are any issues.
>
> Did this one as well at some point.  I'm pretty sure I've caught some
> of the rewrite bugs with that, but I don't recall entirely..

I think it is a good idea to keep running these tests with default
oid8 on one existing or separate BF member. This helps catch any
future changes in these areas by anyone, not necessarily related to
TOAST but to OID generation, pg_resetwal, pg_dump, pg_upgrade, etc.

> Before reworking on a new rebase of the whole patch set, attached is
> an adjusted 0001 for the 8-byte counter, with adjustments for the docs
> of pg_resetwal and fixes for a few comments that worry about OID
> wraparound, that I have noticed after more review.
>
> How does this part look?

The v16-0001 patch looks good to me (except the pg_resetwal comment I
made upthread). make check-world and pgindent are happy with it.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com


Re: Support for 8-byte TOAST values, round two

От:
"Greg Burd" <greg@burd.me>
Дата:
Correction to my last message, since I was clearly under-caffeinated or
rushed to get it sent.  Not AI/LLM-slop this time HUMAN-slop.  ;-P

I wrote:

> I tried, but couldn't reproduce the concern in practice.

That should say the concern does reproduce. Robert's worry was that some
tuples storable today would not be storable at all once the TOAST pointer
gets wider, and that is what the table right below that sentence shows. At
370 text columns set STORAGE EXTERNAL, oid stores the row and oid8 fails
with "row is too big: size 8824, maximum size 8160". The ceiling on
out-of-line attributes per row drops from about 453 to about 366.

Doesn't change where I land, still +1 on the series, and I think this is
the one cost that deserves a sentence in the docs next to the reloption.

best.

-greg


Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Wed, Sep 09, 2026 at 01:14:54PM -0400, Greg Burd wrote:
> That should say the concern does reproduce. Robert's worry was that some
> tuples storable today would not be storable at all once the TOAST pointer
> gets wider, and that is what the table right below that sentence shows. At
> 370 text columns set STORAGE EXTERNAL, oid stores the row and oid8 fails
> with "row is too big: size 8824, maximum size 8160". The ceiling on
> out-of-line attributes per row drops from about 453 to about 366.

Thanks for the exact numbers.  366 toast columns still looks like an
insane thing to do, so I am not much concerned by somebody having a
number of toast attributes in the [366,453[ range that worked with a
TOAST table based on oid, then trying to do a rewrite to a new toast
table based on oid8 to see the load of data fail suddenly.

> Doesn't change where I land, still +1 on the series, and I think this is
> the one cost that deserves a sentence in the docs next to the reloption.

Hmm.  Okay.  I'll think about something here.
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Sat, Sep 12, 2026 at 01:38:06AM +0800, Rui Zhao wrote:
> v17 applies to master (6a1c1102c4), builds warning-free, and make
> check-world passes here.  Three small patches attached: 0001 applies to
> master, 0002 and 0003 on top of 0005.

Thanks for looking and testing.

0001 and 0002 sound like good ideas in terms of coverage, why not, but
I don't really buy the benefit offered by 0003 (see below for reason).

> Rare case: pg_control lost, cluster brought back with pg_resetwal -f as
> documented, the OID counter comes back as the guess, 10000.  On a fresh
> cluster with 1000 values in an oid8 table (chunk_ids 16394 to 17393) and
> 1000 in an oid table, the oid table takes new rows as before and inserts
> into the oid8 table fail with

If you have "lost" a control file, an oid8 value in the past is the
least of your worries.  At least this can be safely be set: just move
it to something like (UINT64_MAX / N) and you should be good enough, N
being a natural integer of your choice.  I still don't buy that adding
a re-check of the value in the TOAST insert path is worth the cost of
this case.
--
Michael

Re: Support for 8-byte TOAST values, round two


On Mon, Sep 14, 2026, at 1:00 AM, Michael Paquier wrote:
> On Fri, Sep 11, 2026 at 08:44:26AM -0700, Bharath Rupireddy wrote:
>> I reviewed v17 patches, overall they look good to me.
>> 
>> v17-0001-Refactor-some-TOAST-value-ID-code-to-use-Oid8-in.patch: It
>> v17-0002-Switch-pg_column_toast_chunk_id-return-value-fro.patch: It
>
> I have discarded the change of reorderbuffer.c that was in v17-0001,
> and added it back to what's now v18-0001.  Then applied these two
> after an extra round of self-review.
>
>> v17-0003-Add-support-for-oid8-TOAST-values.patch: It looks good to me
>> with one nit. Since chunk_id is always at attnum = 1 and all the
>> toast_valueid_scankey_init() callers pass it as 1, and this init
>> function is just to fetch the chunk_id, can we just hard-code it
>> inside and remove the attnum function parameter?
>
> If one wants to control which attribute number to look after, that
> could be extended more later on, yes.  I have removed the argument
> from the function in v18-0001.
>
>> v17-0004-Add-battery-of-tests-related-oid8.patch: It looks good to me.
>
> Okay, thanks.
>
>> pg_dump/pg_restore for demoing OID4 to OID8 migration for
>> existing tables and TOAST table tests during pg_upgrade could be
>> follow-up patches. This makes me think, if someone does an OID8 to
>> OID4 migration and the chunk_ids are beyond the 4-billion limit, the
>> restore should fail rather than silently wrapping the chunk_ids and
>> causing TOAST index insert failures. My point is, we need to test this
>> case as well.
>
> Nah, I don't buy that this is worth the cycles.  If one has the idea
> to create an oid8 TOAST table, inserts more than 4 billion values into
> it, dumps and restores into a table that uses a oid TOAST then the
> result is an infinite loop in the backend on restored INSERTs.
>
> Something that I still see as a hole is that we have the tables from
> the strings test in the main regression test suite and we could
> exploit that a bit more.  Hmm.  How about resetting the OID in the
> control file earlier in the pg_upgrade test and check with a SELECT
> post-upgrade that the counters generated by the main regression test
> suite are more than 2^32?
>
> Added also some tests for REPACK here in what's now 0002, based on the
> feedback of upthread, to check that the a REPACK's rewrite does not
> touch the TOAST table definition.
>
>> v17-0005-Add-support-for-TOAST-pointers-as-oid8.patch: It looks good to me.
>
> And rebased is a v18 with the three remaining pieces, plus two more
> test patches: the tests of Rui with decoding and oid8 values higher
> than 2^32, and my test to have the main regression test suite of
> pg_upgrade run with a default oid8 in the control file higher than
> 2^32, checking that assigned chunk numbers are handled correctly.
> --
> Michael
>
> Attachments:
> * v18-0001-Add-support-for-oid8-TOAST-values.patch
> * v18-0002-Add-battery-of-tests-related-oid8.patch
> * v18-0003-Add-support-for-TOAST-pointers-as-oid8.patch
> * v18-0004-Decode-an-oid8-TOAST-value-with-an-ID-past-2-32.patch
> * v18-0005-Add-check-for-2-32-oid8-chunk-values-across-the-.patch
> * signature.asc

Hello Michael, all,

I tried v18 out, as expected patches/tests/etc. all good on my macOS/arm64
laptop.

I agree with Bharath that dropping the attnum argument from
toast_valueid_scankey_init() was right, and I see it is gone in 0001.

I like what you did in 0005 with the test, moving the pg_resetwal call ahead
of the main regression suite means the whole suite now runs with the counter
already past 2^32, rather than just one case checking a large value.

  ok 1 - set an 8-byte OID counter in the old instance
  ok 7 - oid8 chunk_ids are past 2^32
  ok 25 - 8-byte OID counter is carried over by pg_upgrade

I'd say that closes the gap Bharath identified, where every oid8 test still
produced chunk_ids that fit in four bytes. Rui's decoding test in 0004 covers
the other half, and I checked separately that it fails when it should: with
ReorderBufferToastAppendChunk() put back to the truncating 4-byte read it
reports

  not ok 23 - oid8 TOAST value past 2^32 is decoded in full
  # table public.toasted_oid8: INSERT: id[integer]:1 data[text]:unchanged-toast-datum

and passes again once restored.

The REPACK additions in 0002 match what I measured by hand: with the reloption
flipped, VACUUM FULL, CLUSTER and REPACK all leave the TOAST table's chunk_id
type alone.

A quick check of my own on top of v18, counter preset to 5000000000: chunk_ids
5000000005 through 5000000054, all past 2^32, data intact, verify_heapam()
reports nothing on either the TOAST relation or the main table, and REPACK
leaves chunk_id as oid8 with the data still intact.

Nothing blocking from me on v18.

best.

-greg


Re: Support for 8-byte TOAST values, round two

От:
Rui Zhao <zhaorui126@gmail.com>
Дата:
Вложения:
Hi Michael,

The tests of my last mail (standby promotion, pg_upgrade with the counter
past 2^32, rewrites after ALTER TABLE SET (toast_value_type), logical
replication) give the same results on master at fb60892f40, and
check-world passes there with 0006 and 0007 applied.

> One thing that neither you nor Greg pointed out is a disturbance I
> have found in amcheck.  The code relied on a vartag to decide the max
> chunk size of a TOAST blob:

1. Once 0004 and 0005 are in, amcheck is the one consumer of oid8
pointers left that no test runs with IDs past 2^32. 0006 adds a TAP
test for it.

check_heap.sql runs on the regression instance, whose counter is young,
and the pg_upgrade test, where the counter is moved, does not run
amcheck. 0006 moves the counter past 2^32 before the first start, fills
an oid8 table with uncompressed and compressed out-of-line values,
replaces and deletes some of them, and runs verify_heapam(check_toast
:= true) on the table, verify_heapam() on the TOAST table and
bt_index_parent_check(heapallindexed => true) on the TOAST index,
before and after VACUUM. If I cast the value ID that
check_toasted_attribute() passes to toast_valueid_scankey_init() to
Oid by hand, the test fails, with verify_heapam() reporting every
value:

 toast value 4295067314 not found in toast table

2. The "compressed out-of-line values" of check_heap.sql are not out of
line. 0007 makes them larger.

The rows come from these statements of check_heap.sql:

 ALTER TABLE test_toast_oid8 ALTER COLUMN b SET STORAGE EXTENDED;
 INSERT INTO test_toast_oid8 (a, b)
  (SELECT gs, repeat('xyzzy', 20000) FROM generate_series(6,10) gs);

and repeat('xyzzy', 20000) compresses to 1161 bytes and stays in the
heap:

 SELECT a, length(b), pg_column_size(b), pg_column_compression(b),
        pg_column_toast_chunk_id(b)
   FROM test_toast_oid8 WHERE a = 6;

  a | length | pg_column_size | pg_column_compression | pg_column_toast_chunk_id
  6 | 100000 |           1161 | pglz                  |

so rows 6 to 10 of test_toast_oid and test_toast_oid8 hold no TOAST
pointer, and verify_heapam(check_toast := true) has nothing to look up
for them. With repeat('xyzzy', 100000) the compressed value is 5735
bytes and goes out of line; 0007 also adds a SELECT showing that all
ten values of each table are out of line and five of them compressed.

> To me, applying the value set in the reloption at the moment of the
> dump and not the chunk_id type feels like a natural thing to do.

3. That works with either of two behaviors for ALTER TABLE, and the
current one is neither. I prefer a) and can write it.

Today SET and RESET (toast_value_type) change pg_class.reloptions and
nothing else, and no rewrite applies the new value: VACUUM FULL,
CLUSTER, REPACK and ALTER TABLE SET ACCESS METHOD rebuild the TOAST
table with the type it already has, since create_toast_table() takes
that type from the old TOAST table. A dump and restore is the only
thing that applies the reloption. Other heap reloptions are applied by
a rewrite; a table of 20000 rows with fillfactor:

 default, 167 pages
 SET (fillfactor = 20), 167 pages
 VACUUM FULL, 870 pages
 RESET (fillfactor) and VACUUM FULL, 167 pages

a) Keep SET and RESET as they are and let a rewrite apply the value,
as fillfactor does. A rewrite and a restore would then build the TOAST
table the same way, which is what the reloption already says it does.

Going to oid8 costs nothing there. Going to oid has to assign the
value IDs by probing for free ones, which slows down as the TOAST
table fills, so it is a usable operation for a table that fits with
room to spare and not for one that had a reason to be oid8. A restore
into an oid TOAST table makes the same assignment, so this is not
something a rewrite would introduce; neither route avoids it.

b) Reject RESET and have SET rewrite the table, as SET ACCESS METHOD
does. The reloption then always describes the type in use, whether it
is set or absent, which is why pg_dump can stay as it is. It puts a
rewrite behind an ALTER TABLE ... SET, which no other reloption does
today.

Either one settles what Bharath's 0001 reports: a dump follows the
reloption because that is what the reloption means. If the restore
side should be able to choose the type as well, the way to do that is
the one used for default_table_access_method, but I do not see the
need for it now.

Say which one you prefer and I will send a patch for it.

Regards,
Rui

Re: Support for 8-byte TOAST values, round two

От:
"Greg Burd" <greg@burd.me>
Дата:

On Tue, Sep 15, 2026, at 12:20 PM, Rui Zhao wrote:
> Hi Michael,
>
> The tests of my last mail (standby promotion, pg_upgrade with the counter
> past 2^32, rewrites after ALTER TABLE SET (toast_value_type), logical
> replication) give the same results on master at fb60892f40, and
> check-world passes there with 0006 and 0007 applied.
>
>> One thing that neither you nor Greg pointed out is a disturbance I
>> have found in amcheck.  The code relied on a vartag to decide the max
>> chunk size of a TOAST blob:
>
> 1. Once 0004 and 0005 are in, amcheck is the one consumer of oid8
> pointers left that no test runs with IDs past 2^32. 0006 adds a TAP
> test for it.

Hello Rui,

> If I cast the value ID that check_toasted_attribute() passes to
> toast_valueid_scankey_init() to Oid by hand, the test fails, with
> verify_heapam() reporting every value:
>
> toast value 4295067314 not found in toast table

+1, and thank you for showing it red as well as green. That is the right
follow-up to the thing Michael found in the same file, since the reason
nothing caught that was the same reason nothing would catch this: amcheck
never runs against a counter past 2^32.

> 2. The "compressed out-of-line values" of check_heap.sql are not out of
> line. 0007 makes them larger.

Good catch, and I can confirm the diagnosis. I can also tell you the fix is
not large enough, because the outcome depends on default_toast_compression
and your numbers are the pglz ones. Same two values, same
SET STORAGE EXTENDED, on one build:

  method   input                       stored   placement
  pglz     repeat('xyzzy',20000)         1161   inline
  pglz     repeat('xyzzy',100000)        5735   out-of-line
  lz4      repeat('xyzzy',20000)          415   inline
  lz4      repeat('xyzzy',100000)        1983   inline

So 0007 fixes check_heap.sql on a pglz build and leaves it checking nothing
on an lz4 one, which is the default where lz4 is compiled in. Whatever the
size, a repeat() of a short string compresses far enough that the threshold
is a property of the compression method rather than of the test.

Something that resists compression avoids the question entirely. This goes
out of line under both methods:

  (SELECT string_agg(md5(g::text), '') FROM generate_series(1, 2000) g)

  pglz  64000 chars, stored 64000, out-of-line
  lz4   64000 chars, stored 59738, out-of-line

Pinning default_toast_compression in the test would work too, though it costs
you the coverage of whichever method you did not pin.

> 3. That works with either of two behaviors for ALTER TABLE, and the
> current one is neither. I prefer a) and can write it.

> Today SET and RESET (toast_value_type) change pg_class.reloptions and
> nothing else, and no rewrite applies the new value [...] A dump and
> restore is the only thing that applies the reloption. Other heap
> reloptions are applied by a rewrite

This is a better diagnosis than the one I posted an hour ago. I treated
the surprise as a reporting problem and sent a warning for the reset case.
You have identified that the surprise is a consequence of this reloption
being the only one that no rewrite applies, which is the actual oddity.
The fillfactor page counts make that hard to argue with.

So please consider my patch withdrawn in favor of either of yours. Under b)
it is plainly unnecessary, since a rejected reset has nothing to warn about.
Under a) it is close to unnecessary too, because a user who resets then
rewrites has asked for the change and the two paths agree.

If it helps break the tie, I lean b), for the reason you give: the reloption
then always describes the type in use, whether set or absent. That is the
property that makes Bharath's 0001 unnecessary rather than merely
outvoted, and it is the one that removes the case I was worried about, where
a reset taken for an unrelated reason turns into a restore that will not
finish on a relation with more than four billion out-of-line values.

The cost you name is real, a rewrite behind ALTER TABLE ... SET where no
other reloption does that. I would take it, because SET ACCESS METHOD is
already precedent for exactly that shape, and because an expensive operation
the user asked for beats a cheap one whose consequence shows up later
somewhere else. a) is still a clear improvement on today and I would not
object to it.

Either way this is Michael's call to make, I'm anxious to hear his thoughts
on this.

best.

-greg


Re: Support for 8-byte TOAST values, round two

От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi,

On Tue, Sep 15, 2026 at 12:59 AM Michael Paquier  wrote:
>
> On Mon, Sep 14, 2026 at 04:43:44PM -0700, Bharath Rupireddy wrote:
> > I think I found an issue related to pg_dump/pg_restore where a dump
> > and restore after an ALTER TABLE RESET on the reloption would
> > downgrade the TOAST table from oid8 back to oid. More details in the
> > attached 0001 patch.
>
> I am not convinced by this argument.  To me, applying the value set in
> the reloption at the moment of the dump and not the chunk_id type
> feels like a natural thing to do.  We still have time to decide which
> behavior makes more sense for this release, but choosing what the
> reloption holds at the time of the dump is a property I'd prefer keep.
>
> > I also attached some more patches that I think would help with the
> > overall pg_dump and pg_restore along with the migration path for
> > existing oid TOAST tables.
> >
> > I used the nocfbot prefix for these patches to not come in the way of
> > the main patches, and they can go after them. Please find them
> > attached.
> >
> > 0001 - fixes the pg_dump/pg_restore downgrade issue
> > 0002 - adds a doc section on the migration
> > 0003 - adds tests for the oid to oid8 migration and vice versa
>
> 0002 seems perhaps like something we should do, but it's independent
> of 0001, no?  Not sure that the tests of 0003 are really needed.  That
> feels like bloat compared to the rest already in place.

Firstly, thanks for fixing the thinkos in 0d34736e4c1. I will be more
careful and try to catch these next time.

Secondly, as far as these 3 patches are concerned, feel free to ignore
0003 for sure, as 0002 now pretty much covers what I wanted to say for
pg_dump and pg_restore. 0002 is something I agree we need to have. For
0001, I'm not sure the proposed fix is the best option we have here,
and I haven't read the other patches or thoughts on this problem yet.
But I do think the behavior needs to be addressed somehow, because
resetting the reloption is something one can easily do and later run
into issues or miss them entirely. Maybe just documenting it in 0002's
new section is simple enough, asking users to set the reloption back
correctly before running pg_dump.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com


Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Wed, Sep 16, 2026 at 12:20:28AM +0800, Rui Zhao wrote:
>> One thing that neither you nor Greg pointed out is a disturbance I
>> have found in amcheck.  The code relied on a vartag to decide the max
>> chunk size of a TOAST blob:

(Cut some of the contents, some are clearly AI-generated, which is
slightly annoying to argue with.)

> 1. Once 0004 and 0005 are in, amcheck is the one consumer of oid8
> pointers left that no test runs with IDs past 2^32. 0006 adds a TAP
> test for it.
> 
> If I cast the value ID that
> check_toasted_attribute() passes to toast_valueid_scankey_init() to
> Oid by hand, the test fails, with verify_heapam() reporting every
> value:

I'm wondering if we should think a bit more wildly here.  For example,
we could have a new PG_TEST_EXTRA value that enforces an initial OID
beyong the 2^32 limit.  This is a bit more general than the statement
you are making here.  Just to say that I'm not much a fan of the
cycles spend on a new TAP test for this purpose, where installcheck
could do the same using the SQL queries.  So I don't think that your
0006 buys much by just adding more cycles and spawning a new node for
what's limited and duplicated coverage compared to the SQLs of
amcheck/sql/.  An advantage of dumping the OID up in the pg_upgrade
test is that it's basically free.

> 2. The "compressed out-of-line values" of check_heap.sql are not out of
> line. 0007 makes them larger.
>
> so rows 6 to 10 of test_toast_oid and test_toast_oid8 hold no TOAST
> pointer, and verify_heapam(check_toast := true) has nothing to look up
> for them. With repeat('xyzzy', 100000) the compressed value is 5735
> bytes and goes out of line; 0007 also adds a SELECT showing that all
> ten values of each table are out of line and five of them compressed.

Hmpf, yeah.  These tuples are not chunked into TOAST as they compress
too well.  There are a few ways to make that predictible and cheaper:
- Force default_toast_compression to pglz, like in portals.sql, and to
force a check based on pg_column_compression().
- Use the same trick as test_decoding's toast.sql, for a much faster
query and the same coverage and even less tuples.

There is a similar mistake in test_decoding/sql/toast.sql, for the
same assumptions.  Will adjust both the same way, both tests want the
same data patterns.  Thanks for the report.

>> To me, applying the value set in the reloption at the moment of the
>> dump and not the chunk_id type feels like a natural thing to do.

(Note: this is a point of design I've had in mind for close to 2
years, so it's not like I gave it no thoughts.)

> a) Keep SET and RESET as they are and let a rewrite apply the value,
> as fillfactor does. A rewrite and a restore would then build the TOAST
> table the same way, which is what the reloption already says it does.

And you're missing a very fancy point here.  A user could be trapped
under a oid8->oid switch.  Create an oid8 table, downgrade the
reloption to oid, do a VACUUM FULL and enjoy the infinite loop.
That's no different from somebody currently seeing the infinite 4
billion limitation with an oid table, but it surely hurts when
happening across a rewrite.  toast_tuple_target and fillfactor impact
the heap pages and are per-write targets for future tuples written,
something like a VACUUM feeds on.  Shaping a TOAST table based on a
reloption is an entirely new design space.  It just does not have to
fit in these rules (this does not mean that I don't mind changing some
of the rules of HEAD, depending on how things go in this release
cycle).

> b) Reject RESET and have SET rewrite the table, as SET ACCESS METHOD
> does. The reloption then always describes the type in use, whether it
> is set or absent, which is why pg_dump can stay as it is. It puts a
> rewrite behind an ALTER TABLE ... SET, which no other reloption does
> today.

First.  ALTER TABLE SET (reloption=foo) should never trigger a
rewrite.  It's cheap currently as it just touches catalogs, and it
should stay as such.  SET ACCESS METHOD is a different sub-command in
the ALTER TABLE set of options available.  Whether it triggers a
rewrite is up to it because there is a user intention behind the cost.
A similar argument exists for SET LOGGED.

Anyway, this line of thought discards the fact that toast_value_type
defines the definition of an underlying table.  Having a rewrite
enforced by a plain ALTER TABLE SET can also be surprising, especially
if you are dealing with a 10TB table that needs to be WAL-logged.

FWIW, we've had exactly a very similar kind of discussion in the v14
development when lz4 compression has been introduced for VACUUM
(outremembering a bunch of folks on this thread, I guess).  The
original feature enforced a rewrite of the TOAST values based on the
default_toast_compression if the default was switched to lz4 while
values could be written as pglz, and the conclusion was that it was
not VACUUM's job to enforce new compression policies.

> Either one settles what Bharath's 0001 reports: a dump follows the
> reloption because that is what the reloption means. If the restore
> side should be able to choose the type as well, the way to do that
> is the one used for default_table_access_method, but I do not see
> the need for it now. 

Among the options presented, preventing a RESET of the new reloption
if the reloption has been explicitely set may be OK, but it can be
disruptive with dump and upgrade scenarios in general, which is a
reason why it's originally pretty loose.  Bharath's suggestion of
adding a WITH clause in the dumps based on the type of chunk_id when 
taking the dump sounds like the most tempting semantic to me, because
it sticks the TOAST definition within the CREATE TABLE of the parent.
The devil is in the details, and there's still plenty of time to tweak
the semantics.

A GUC was shut down two years ago by Andres and Tom, see this thread
for a summary (feeding that to an agent would be faster, I guess):
https://www.postgresql.org/message-id/aFOnKHG7Wn-Srnpv@paquier.xyz
So I'm not going back to anything like that.

The rewrite of the TOAST table is something that I think would fit
much better on an option-basis in one of the existing commands, not
enforced upon users, *if and only if* explicitely requested.  REPACK
would be a nice fit, in spirit, to be able to perform the job,
combined with an update of the reloption.  VACUUM and CLUSTER are
there for different purposes.  There would be value for such a thing
for a oid8->oid as much as an oid->oid8.  At the end, it's also up to
users to be very careful.  Not sure there is much a point to outsmart
them with the backend.
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Tue, Sep 15, 2026 at 01:05:09PM -0400, Greg Burd wrote:
> Pinning default_toast_compression in the test would work too, though it costs
> you the coverage of whichever method you did not pin.

Using default_toast_compression is perhaps nicer (same as
portals.sql).  It offers more predictibility in the output and we
don't really care about the compression method in this case.  The md5
computations can be more expensive, which I find annoying, so I'm
eager to just reuse the smart pattern of toast.sql instead.  It's much
cheaper, and enforces the out-of-line behavior we want here.

Note: test_decoding has the same issue.  Oops.  Will fix in a bit
after more checks.

> Either way this is Michael's call to make, I'm anxious to hear his thoughts
> on this.

Replied to that upthread, as of:
https://www.postgresql.org/message-id/aqni9bvWV0UIUEZy@paquier.xyz

I don't think that this new reloption should equal in expectations
with fillfactor and/or tuple_toast_target.
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Mon, Sep 14, 2026 at 02:00:49PM +0900, Michael Paquier wrote:
> And rebased is a v18 with the three remaining pieces, plus two more
> test patches: the tests of Rui with decoding and oid8 values higher
> than 2^32, and my test to have the main regression test suite of
> pg_upgrade run with a default oid8 in the control file higher than
> 2^32, checking that assigned chunk numbers are handled correctly.

While thinking again about v18-0004 and v18-0005 that were the latest
items on my list, I am having second thoughts about v18-0004 that has
the idea to plug in one more pg_resetwal call to satisfy *ONE* case,
while the SQLs of test_decoding are able to do much more than that.

As a whole, it feels like a waste of cycles once we become able to do
an installcheck on a cluster that has an initial OID set to more than
UINT32_MAX.  The pg_resetwal calls in pg_wal and the TLI recovery test
still make sense, as they cover specifics for the WAL records and the
control file.  Just that the SQL tests are harder to justify presented
this way.  So how about this wilder idea of a new PG_TEST_EXTRA
enforcing the initial OID value after an initdb?
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi,

On Tue, Sep 15, 2026 at 6:59 PM Michael Paquier  wrote:
>
> On Mon, Sep 14, 2026 at 02:00:49PM +0900, Michael Paquier wrote:
> > And rebased is a v18 with the three remaining pieces, plus two more
> > test patches: the tests of Rui with decoding and oid8 values higher
> > than 2^32, and my test to have the main regression test suite of
> > pg_upgrade run with a default oid8 in the control file higher than
> > 2^32, checking that assigned chunk numbers are handled correctly.
>
> While thinking again about v18-0004 and v18-0005 that were the latest
> items on my list, I am having second thoughts about v18-0004 that has
> the idea to plug in one more pg_resetwal call to satisfy *ONE* case,
> while the SQLs of test_decoding are able to do much more than that.
>
> As a whole, it feels like a waste of cycles once we become able to do
> an installcheck on a cluster that has an initial OID set to more than
> UINT32_MAX.  The pg_resetwal calls in pg_wal and the TLI recovery test
> still make sense, as they cover specifics for the WAL records and the
> control file.  Just that the SQL tests are harder to justify presented
> this way.  So how about this wilder idea of a new PG_TEST_EXTRA
> enforcing the initial OID value after an initdb?

While I agree with the general idea that having a way to enforce the
initial OID after an initdb is a good one, just curious, why is this
specific to the pg_resetwal being added to the recovery test in
006_logical_decoding.pl? Or is the intention to not exercise this test
always? I'm still not clear on why we need to solve this problem here.

Also, the fact that this test is kept at the end of the test file
means it doesn't impact any other test case in the file, though
keeping it at the end in the future could be hard if that's an issue
at all.

That said, I measured the cost that this additional pg_resetwal brings
to the test timings. On a release build on my dev machine (EC2
r7i.8xlarge), over 10 runs of just 006_logical_decoding.pl, the
testing time goes from 1.101s to 1.162s, of which the pg_resetwal
itself is about 29ms. This isn't huge IMO for bigger machines, however
this could be costlier for most of the CF bot, CI and BF animals out
there though.

In short, I don't think we need to worry much about this as part of
this thread. Maybe separately, yes, if the goal is to remove
pg_resetwal across all TAP tests with what you proposed above, that
seems fine to me.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com


Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Wed, Sep 16, 2026 at 10:48:35AM -0700, Bharath Rupireddy wrote:
> While I agree with the general idea that having a way to enforce the
> initial OID after an initdb is a good one, just curious, why is this
> specific to the pg_resetwal being added to the recovery test in
> 006_logical_decoding.pl? Or is the intention to not exercise this test
> always? I'm still not clear on why we need to solve this problem here.

The duplication at SQL level in the TAP tests that all these
pg_resetwal commands create is what drives me toward a more general
solution as being suited.  We want to check for UNIT32_MAX overflows
in a general way than just something inside one test.  Using a
pg_resetwal command in the pg_upgrade test feels like a good
investment: we run the main regression test suite all the time there.

Instead of a PG_TEST_EXTRA variable another set of ideas one I can
think of out-of-the-box is something that would be based on a compile
flag.  For example a development GUC?

Or maybe just a few feature in itself, like an initdb option that
forces an initial OID after bootstrap in the control file?  Then link
it to a new PG_TEST_EXTRA mode?  Having something that could be
directly used by the buildfarm on an opt-in basis would be kind of
nice.

So, I'd rather have a more general mechanism in place rather than
finishing with 250 pg_resetwal commands sprinkled across the tree that
duplicate SQL tests we have in the script in 10 years.
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi,

On Wed, Sep 16, 2026 at 4:50 PM Michael Paquier  wrote:
>
> On Wed, Sep 16, 2026 at 10:48:35AM -0700, Bharath Rupireddy wrote:
> > While I agree with the general idea that having a way to enforce the
> > initial OID after an initdb is a good one, just curious, why is this
> > specific to the pg_resetwal being added to the recovery test in
> > 006_logical_decoding.pl? Or is the intention to not exercise this test
> > always? I'm still not clear on why we need to solve this problem here.
>
> The duplication at SQL level in the TAP tests that all these
> pg_resetwal commands create is what drives me toward a more general
> solution as being suited.  We want to check for UNIT32_MAX overflows
> in a general way than just something inside one test.  Using a
> pg_resetwal command in the pg_upgrade test feels like a good
> investment: we run the main regression test suite all the time there.
>
> Instead of a PG_TEST_EXTRA variable another set of ideas one I can
> think of out-of-the-box is something that would be based on a compile
> flag.  For example a development GUC?
>
> Or maybe just a few feature in itself, like an initdb option that
> forces an initial OID after bootstrap in the control file?  Then link
> it to a new PG_TEST_EXTRA mode?  Having something that could be
> directly used by the buildfarm on an opt-in basis would be kind of
> nice.
>
> So, I'd rather have a more general mechanism in place rather than
> finishing with 250 pg_resetwal commands sprinkled across the tree that
> duplicate SQL tests we have in the script in 10 years.

IIUC, the SQL duplication referred to is that the 8-byte TOAST
chunk_id tests proposed here repeat the same create table and DMLs
that toast.sql already has. I would like to keep things minimal to the
core where possible and have the 8-byte TOAST tests run every time
across various BF animals (not behind a PG_TEST_EXTRA flag or such).

A developer GUC behind a compile-time macro (similar to clobber cache
for example) would be a nice option. However, this doesn't meet my
requirement of running these tests every time on all BF animals. And I
don't think an initdb option is a good idea here, because this is not
something one would use in production, just for testing, so -1 on
that. Another approach, which I don't recommend, is to have a SQL
function that modifies the OID counter directly. I know this is much
worse because such a function, even behind a superuser check, is
reachable on production systems. No go here, but just saying.

That said, if I relax my requirement of running these tests every time
on every BF animal a bit, the developer GUC behind a compile-time
macro seems like a good idea.

Another approach is a new test module in src/test/modules (similar to
the xid_wraparound module) with a simple SQL function that advances
the OID counter in shared memory, similar to core's SetNextObjectId().
Being a test module, it doesn't impact production systems, and all the
8-byte TOAST chunk_id related tests added for this feature can be
moved here and run across all BF animals.

Others may have a different opinion. Meanwhile, IMHO, the remaining
v18 tests can go in, with this discussed separately.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com


Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Thu, Sep 17, 2026 at 12:03:00PM -0700, Bharath Rupireddy wrote:
> Another approach is a new test module in src/test/modules (similar to
> the xid_wraparound module) with a simple SQL function that advances
> the OID counter in shared memory, similar to core's SetNextObjectId().
> Being a test module, it doesn't impact production systems, and all the
> 8-byte TOAST chunk_id related tests added for this feature can be
> moved here and run across all BF animals.

That could be one thing.  Yes.  Or perhaps not.

> Others may have a different opinion. Meanwhile, IMHO, the remaining
> v18 tests can go in, with this discussed separately.

OK, you're right that it also sucks to do nothing for the logical
decoding.  So I think that I'll just do something for the
test_decoding TAP test.  I am less convinced about the dump/restore
tests you are proposing; these are utterly bloated.
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi,

On Fri, Sep 18, 2026 at 5:03 PM Michael Paquier  wrote:
>
> On Thu, Sep 17, 2026 at 12:03:00PM -0700, Bharath Rupireddy wrote:
> > Another approach is a new test module in src/test/modules (similar to
> > the xid_wraparound module) with a simple SQL function that advances
> > the OID counter in shared memory, similar to core's SetNextObjectId().
> > Being a test module, it doesn't impact production systems, and all the
> > 8-byte TOAST chunk_id related tests added for this feature can be
> > moved here and run across all BF animals.
>
> That could be one thing.  Yes.  Or perhaps not.
>
> > Others may have a different opinion. Meanwhile, IMHO, the remaining
> > v18 tests can go in, with this discussed separately.
>
> OK, you're right that it also sucks to do nothing for the logical
> decoding.  So I think that I'll just do something for the
> test_decoding TAP test.

Works for me. Thanks.

> I am less convinced about the dump/restore
> tests you are proposing; these are utterly bloated.

Out of the 3 patches I posted here:
https://postgr.es/m/CALj2ACUmBhBGb+4d8SRq9JT_ZsObCAvY6-10CKgSWX98zdv2DQ@mail.gmail.com,
I'm fine to drop 0003 (I don't have a strong opinion on that) as
mentioned here:
https://postgr.es/m/CALj2ACWgpn_aWzzagrerFqD0p1kQv_iioWyJB5R8Cu8j3_yqdA%40mail.gmail.com

-- 
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com


Re: Support for 8-byte TOAST values, round two

От:
John Naylor <johncnaylorls@gmail.com>
Дата:
On Tue, Sep 15, 2026 at 2:08 PM Michael Paquier  wrote:
> Thanks.  I have spent the day processing all these free, and finally
> applied the set.

Nice work!

It looks like doc/src/sgml/limits.sgml needs to be updated as well.

-- 
John Naylor
Amazon Web Services


Re: Support for 8-byte TOAST values, round two

От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Вложения:
Hi,

On Mon, Sep 21, 2026 at 4:11 AM John Naylor  wrote:
>
> On Tue, Sep 15, 2026 at 2:08 PM Michael Paquier  wrote:
> > Thanks.  I have spent the day processing all these free, and finally
> > applied the set.
>
> Nice work!
>
> It looks like doc/src/sgml/limits.sgml needs to be updated as well.

Nice catch. Thanks for pointing it out. Please find the attached patch for that.

I also attached the 0001 and 0002 from
https://www.postgresql.org/message-id/CALj2ACUmBhBGb%2B4d8SRq9JT\_ZsObCAvY6-10CKgSWX98zdv2DQ%40mail.gmail.com
here as 0002 and 0003.

In the attached 0003, I fixed an issue with the remote version check
and simplified the tests by reusing an existing table whose reloption
is already reset, and verifying that the dump reports the original
TOAST type. This is good enough to cover the new code without starting
a new instance just to test this. I also used a join instead of a
per-relation lookup in the query to get the TOAST type, which avoids
an extra catalog lookup for every table.

Please have a look.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Mon, Sep 21, 2026 at 03:39:23PM -0700, Bharath Rupireddy wrote:
> On Mon, Sep 21, 2026 at 4:11 AM John Naylor  wrote:
>> It looks like doc/src/sgml/limits.sgml needs to be updated as well.
> 
> Nice catch. Thanks for pointing it out. Please find the attached patch for that.

Yeah, I've missed a spot that required a refresh.

> I also attached the 0001 and 0002 from
> https://www.postgresql.org/message-id/CALj2ACUmBhBGb%2B4d8SRq9JT\_ZsObCAvY6-10CKgSWX98zdv2DQ%40mail.gmail.com
> here as 0002 and 0003.

Not feeling much about 0002 at this stage.  I don't disagree about the
fact of documenting something, but one has a few more ways to do it,
one being a CTAS with a WITH clause.  You could also drop the varlena
attributes from a definition (after having migrated the values),
VACUUM FULL to drop the existing TOAST and add a new text attribute to
force the creation of a new TOAST table based on the new type wanted,
after an ALTER TABLE SET.

> In the attached 0003, I fixed an issue with the remote version check
> and simplified the tests by reusing an existing table whose reloption
> is already reset, and verifying that the dump reports the original
> TOAST type. This is good enough to cover the new code without starting
> a new instance just to test this. I also used a join instead of a
> per-relation lookup in the query to get the TOAST type, which avoids
> an extra catalog lookup for every table.

I still don't think much about this part, FWIW.  That's just switching
some semantics to a different one which shows unclear benefits (aka it
is about what we should do on the dump side if we find a reloption set
or not, vs what's stored on disk).
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Hi,

On Mon, Sep 21, 2026 at 3:53 PM Michael Paquier  wrote:
>
> > I also attached the 0001 and 0002 from
> > https://www.postgresql.org/message-id/CALj2ACUmBhBGb%2B4d8SRq9JT\_ZsObCAvY6-10CKgSWX98zdv2DQ%40mail.gmail.com
> > here as 0002 and 0003.
>
> Not feeling much about 0002 at this stage.  I don't disagree about the
> fact of documenting something, but one has a few more ways to do it,
> one being a CTAS with a WITH clause.  You could also drop the varlena
> attributes from a definition (after having migrated the values),
> VACUUM FULL to drop the existing TOAST and add a new text attribute to
> force the creation of a new TOAST table based on the new type wanted,
> after an ALTER TABLE SET.

Agreed. Since we don't exactly know which option works better for
users, having one thing documented in core tends to be taken as the
recommended approach, which I would like to avoid. So, I'm fine to
withdraw this.

> > In the attached 0003, I fixed an issue with the remote version check
> > and simplified the tests by reusing an existing table whose reloption
> > is already reset, and verifying that the dump reports the original
> > TOAST type. This is good enough to cover the new code without starting
> > a new instance just to test this. I also used a join instead of a
> > per-relation lookup in the query to get the TOAST type, which avoids
> > an extra catalog lookup for every table.
>
> I still don't think much about this part, FWIW.  That's just switching
> some semantics to a different one which shows unclear benefits (aka it
> is about what we should do on the dump side if we find a reloption set
> or not, vs what's stored on disk).

Agreed here as well. I expect one does not frequently reset the TOAST
reloption in production, especially after knowing that it doesn't
affect the table once created and has the data. If anyone raises this
later, at least there is evidence in this thread that it was
discussed. They can set the reloption to match the TOAST table
chunk_id type if they wish to. So, I'm fine to withdraw this as well.

Apologies for the noise. Thanks.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com


Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Mon, Sep 21, 2026 at 03:39:23PM -0700, Bharath Rupireddy wrote:
> On Mon, Sep 21, 2026 at 4:11 AM John Naylor  wrote:
>> It looks like doc/src/sgml/limits.sgml needs to be updated as well.
> 
> Nice catch. Thanks for pointing it out. Please find the attached patch for that.

-  values are large enough to require it.  Only an 18-byte pointer must remain
-  inside the tuple in the table's heap.  For shorter length variable-length
-  fields, either a 4-byte or 1-byte field header is used and the value is
-  stored inside the heap tuple.
+  values are large enough to require it. Only an 18-byte or 22-byte pointer
+  must remain inside the tuple in the table's heap, for a TOAST table using an
+  OID or an OID8 respectively. For shorter length variable-length fields,
+  either a 4-byte or 1-byte field header is used and the value is stored
+  inside the heap tuple.

How about the following instead for this part, slightly tweaked:
"Only an 18-byte or 22-byte TOAST pointer must remain inside the tuple
in the table's heap, depending on whether the TOAST table uses OID or
OID8 values, respectively. For shorter length variable-length fields,
either a 4-byte or 1-byte field header is used and the value is stored
inside the heap tuple."

   Typically, this is only an issue for tables containing many terabytes
   of data; partitioning is a possible workaround.
+  A TOAST table using an OID8 does not have this issue, as it can store up
+  to 2^64 out-of-line values and new identifiers are assigned without
+  searching for a free one; see .

And here, a bit cleaned up:
"A TOAST table using OID8 values does not have this issue as it can
store up to 2^64 out-of-line values.  New identifiers are assigned
without searching for a free one.
See  for details."
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Дата:
Вложения:
Hi,

On Tue, Sep 22, 2026 at 12:58 AM Michael Paquier  wrote:
>
> -  values are large enough to require it.  Only an 18-byte pointer must remain
> -  inside the tuple in the table's heap.  For shorter length variable-length
> -  fields, either a 4-byte or 1-byte field header is used and the value is
> -  stored inside the heap tuple.
> +  values are large enough to require it. Only an 18-byte or 22-byte pointer
> +  must remain inside the tuple in the table's heap, for a TOAST table using an
> +  OID or an OID8 respectively. For shorter length variable-length fields,
> +  either a 4-byte or 1-byte field header is used and the value is stored
> +  inside the heap tuple.
>
> How about the following instead for this part, slightly tweaked:
> "Only an 18-byte or 22-byte TOAST pointer must remain inside the tuple
> in the table's heap, depending on whether the TOAST table uses OID or
> OID8 values, respectively. For shorter length variable-length fields,
> either a 4-byte or 1-byte field header is used and the value is stored
> inside the heap tuple."
>
>    Typically, this is only an issue for tables containing many terabytes
>    of data; partitioning is a possible workaround.
> +  A TOAST table using an OID8 does not have this issue, as it can store up
> +  to 2^64 out-of-line values and new identifiers are assigned without
> +  searching for a free one; see .
>
> And here, a bit cleaned up:
> "A TOAST table using OID8 values does not have this issue as it can
> store up to 2^64 out-of-line values.  New identifiers are assigned
> without searching for a free one.
> See  for details."

Agreed. Using OID/OID8 values and breaking up the sentences reads
better. Please find the attached v2 patch.

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Tue, Sep 22, 2026 at 03:35:00PM -0700, Bharath Rupireddy wrote:
> Agreed. Using OID/OID8 values and breaking up the sentences reads
> better. Please find the attached v2 patch.

Pushed.
--
Michael

Re: Support for 8-byte TOAST values, round two

От:
Hannu Krosing <hannuk@google.com>
Дата:
In my insert tests 8-byte TOAST was much faster that 4-byte toast

During a 30 min run inserting tuples with 8  toasted fields of ~100
bytes oid8 toast kept a steady 18k inserts / second against 14.6k
inserts second for oid4

This resulted in a solid 23% speed-up because it didn't need to verify
new OIDs against the unique index, despite each toast index entry
taking up slightly more space (30.1 bytes vs. 21.4 bytes on average)







On Wed, Sep 23, 2026 at 12:58 PM Michael Paquier  wrote:
>
> On Tue, Sep 22, 2026 at 03:35:00PM -0700, Bharath Rupireddy wrote:
> > Agreed. Using OID/OID8 values and breaking up the sentences reads
> > better. Please find the attached v2 patch.
>
> Pushed.
> --
> Michael


Re: Support for 8-byte TOAST values, round two

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Wed, Sep 30, 2026 at 12:44:28PM +0200, Hannu Krosing wrote:
> In my insert tests 8-byte TOAST was much faster that 4-byte toast
> 
> This resulted in a solid 23% speed-up because it didn't need to verify
> new OIDs against the unique index, despite each toast index entry
> taking up slightly more space (30.1 bytes vs. 21.4 bytes on average)

The speedup is not surprising.  Thanks for posting some numbers.

A note about that.  Postgres hackers had a meetup two weeks ago in
Tokyo where I have reported the activity of this thread, including the
fact that the new values are not rechecked in the INSERT path.

My opinion, same as upthread, is that a recheck is kind of pointless
because we should never go backwards under normal running conditions
and we would run out of LSNs before we reach the OID8 limit.  I did
mention the argument of using pg_resetwal -o to go backwards, leading
to INSERT failures due to duplicates of the TOAST unique index.
Fujii-san had a more biased opinion than mine, mentioning that a
recheck could be a more defensive position, even if it costs in normal
running conditions.

Perhaps my opinion will be overruled in this release cycle, which is
fine, I just want to point out that the API toastid_valueid_exists()
is transparently able to work with a recheck of Oid8 values, so the
value recheck could be plugged in for this case as well, depending on
how the discussion flows.  Perhaps there is a bug I am not aware of
yet, that could justify the recheck, of course.
--
Michael
FAQ