Skip to content

Fix MPP tuple attribute versioning - #2284

Open
reshke wants to merge 5 commits into
apache:mainfrom
reshke:fix-mpp-attribute-versioning-20261110
Open

reshke wants to merge 5 commits into
apache:mainfrom
reshke:fix-mpp-attribute-versioning-20261110

Conversation

@reshke

@reshke reshke commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

deliberately separate from #2279

@reshke
reshke force-pushed the fix-mpp-attribute-versioning-20261110 branch 2 times, most recently from 75ecf55 to eea1203 Compare November 12, 2026 07:05
@reshke
reshke requested review from my-ship-it and tuhaihe November 12, 2026 08:14
Comment thread src/backend/catalog/heap.c Resolved
@reshke reshke mentioned this pull request Nov 13, 2026
13 tasks
@reshke
reshke force-pushed the fix-mpp-attribute-versioning-20261110 branch 2 times, most recently from 177c8dc to 62eb913 Compare November 14, 2026 02:30

@leborchuk leborchuk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@reshke
reshke force-pushed the fix-mpp-attribute-versioning-20261110 branch 3 times, most recently from 9dd53d3 to f6d0bca Compare November 23, 2026 13:15
anarazel and others added 5 commits November 24, 2026 16:11
Using a tuple descriptor that does not match the "shape" of a composite
datum can lead to crashes, spurious errors and can be used for exploits.
While postgres has code that prevents such mismatches, there are a
number of weaknesses in those protections that are nontrivial to fix,
particularly in the back-branches.

This commit introduces a new safety layer to prevent the worst of the
negative consequences. From now on, composite datums are marked as such
and named composite datums are versioned, allowing detection of
mismatches between a tuple descriptor and the composite during
deforming. In case of mismatches, errors are raised.

For details about how this works, see
src/backend/catalog/README.attver.md.

Note that, over time, we should prevent most of the things that the
safety layer detects at a higher level.

This fixes a number of security relevant composite type confusions.

This is a significant piece of infrastructure grafted onto the existing
code, in a back-patchable way.  There unfortunately still are some
compatibility issues due to this commit:

- To get the full protection, extensions need to be rebuilt, due to
  DatumGetHeapTupleHeader() not being a macro anymore (but an
  out-of-line function). That also means that some extensions built
  against a minor version with this change will fail with a linker
  failure when loaded in an earlier minor version.

  Extensions that have not been recompiled can observe
  HeapTupleHeaderGetTypMod() returning parts of the version instead of
  -1.

- Upgrading to a minor version with this fix, performing DDL,
  downgrading and then upgrading again can cause errors for composite
  datums formed while downgraded; DDL while downgraded can additionally
  break the version invariants.

- Code looking at pg_attribute.attoptions will see the new pg_attver
  key, which could lead to external applications setting this new key
  via DDL.

  To reduce the impact of that, ALTER COLUMN ... SET (pg_attver = ...)
  is ignored.

- Dumps of a server with this change, made by a pg_dump without it,
  contain ALTER COLUMN ... SET (pg_attver = ...), which servers without
  this change reject.  As pg_upgrade uses the target's pg_dump,
  upgrading from a minor version with this change to a minor version of
  a later major version without it fails.

- Use of TupleDescCopy() is forbidden with tupledescs for named
  composites. The new TupleDescCopySize()/TupleDescCopySized() can copy
  any descriptor, and verify that the destination is large enough.

- Code that forms datums and then modifies fields in t_choice.t_datum
  without removing HEAP_IS_DATUM can trigger spurious errors. To reduce
  the impact of that, the version check skips some known cases of such
  modifications (see list below for the known effects on extensions).

- DatumGetHeapTupleHeader() will be slower when pre-upgrade tuples are
  passed, due to copying those datums before modifying them.

- Tuples returned by heap_form_tuple() are now marked as datum tuples
  and, for named composites, version-checked when deformed. Code that
  writes such tuples to its own storage needs to clear the marking (via
  HeapTupleHeaderClearDatum()).

- heap_modify_tuple() used to copy t_ctid, which is not possible
  anymore, due to that containing the datum version. No callers could be
  found that need the copy.

At least some of the above should be mentioned in the release notes.

Investigation via github codesearch, a dump of all the extensions on
pgxn, extensions from various distributions, etc, suggests the following
effects on extensions:

- PostGIS does some quite broken things on the upgrade from < 3.6 to
  3.6, including corrupting the catalog. This is more noticeable with
  these changes. In particular WARNINGs will be emitted and pg_upgrade
  will fail, until PostGIS is fixed.

  See https://trac.osgeo.org/postgis/ticket/6142

- PL/R (trigger arguments), pg_variables (stored records) and pljs copy
  tuples and set the composite datum header fields by hand, which leads
  to spurious errors when the result is read.  They should use
  heap_copy_tuple_as_datum() instead.

- ddlx fails for columns with an explicitly set attribute option, as it
  expects at most one option per column.  pgcontext refuses to convert
  versioned columns, as it rejects columns with any attribute options.

- firebird_fdw spuriously errors out in some cases, due to overwriting
  the xmax (but not xmin). Other FDWs clear out xmin, which this commit
  uses as a signal that the version information is not usable, but
  there's no easy way to detect this in the firebird_fdw case. The fix
  would be a one-liner.

- storage_engine's rowcompress table AM stores composite datum-style
  tuples, which will raise errors on read if the table schema is
  subsequently changed. The fix is trivial.

- pgrx-based extensions will not be protected, even after recompiling,
  due to pgrx copying postgres code like heap_getattr(). pgrx will need
  to update that copied code.

- pg_strom's arrow FDW bypasses core functionality for more than 100
  columns and inserts attributes itself. That does not set the attribute
  versions correctly and thus will trigger spurious errors if composites
  of that FDW are used.

- timescaledb has a bug causing crashes during chunk creation as part of
  DDL commands if attribute options are present. With this change every
  new / altered attribute has an attribute option.

  A later commit will work around these crashes.

- Citus' distributed aggregation path theoretically can hit the new
  error in TupleDescCopy(). However, it looks like Citus' planner does
  not ever generate such plans - but the ERROR can be reached by a user
  issuing a query with calls to the relevant internal Citus functions.

  The fix again is trivial, should it be needed (use
  CreateTupleDescCopy() instead of open-coding it).

The new composite_type_mismatch developer option allows superusers to
turn the version mismatch error into a warning, or to disable the check.
Both settings are unsafe.

With this infrastructure in place, additional checks can be
performed (e.g. in heap_datum_check(), heap_datum_update_type()), at the
cost of some complexity and runtime overhead.  A subsequent commit adds
those.

Reported-by: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Reported-by: Andres Freund <andres@anarazel.de>
Reported-by: Andrew Gierth <andrew@tao11.riddles.org.uk>
Reported-by: Alexander Lakhin <exclusion@gmail.com>
Reported-by: Francesco Verardi <frevadiscor89@gmail.com>
Reported-by: Hcamael <baiyjrh@gmail.com>
Reported-by: Tomer Fichman <tomer@irregular.com>
Reported-by: OpenAI Security Research Team
Reported-by: Peter Geoghegan <pg@bowt.ie>
Reported-by: Mehmet Ince @mdisec <mehmet@mehmetince.net>
Reported-by: Bryan Green <dbryan.green@gmail.com>
Reported-by: Sumedh Pathak <sumedhpathak@microsoft.com>
Reported-by: Nicholas Carlini <npc@anthropic.com>
Reported-by: Sergey Shinderuk <s.shinderuk@postgrespro.ru>
Reported-by: Yuxiao Guo <dllggyx@outlook.com>
Reported-by: Nikhil Sontakke <nikhil@planetscale.com>
Reported-by: Stephan Lipp <Stephan@Adalogics.com>
Reported-by: Daniel Bakker <daniel@jackds.nl>
Reviewed-by: Melanie Plageman <melanieplageman@gmail.com>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Reviewed-by: Noah Misch <noah@leadboat.com>
Reviewed-by: Robert Haas <robertmhaas@gmail.com> (parts)
Security: CVE-2026-6460
Backpatch-through: 14
(cherry picked from commit 0c8ffa1ad2f44be4b058a3ec96d9cb0c71f2593f)
(cherry picked from commit ba32c14)
Some of these were originally written with AI contributions, but they
have been heavily rewritten since.

The most important thing this tests is pg_upgrade's handling of
attribute versions, including verification of composite columns staying
properly readable across upgrades.

Security: CVE-2026-6460
Backpatch-through: 14
(cherry picked from commit 788915adfeb7b79bf85340d070443df0057e48a6)
(cherry picked from commit 2fdeb96)
The previously added attribute versioning checks prevent cases of
deforming a datum tuple with an incompatible tuple descriptor for the
same type.  However, we have a number of cases where we deform a datum
tuple with a descriptor for a different relation. The additional checks
introduces here add verification for such cases, by checking that the
tuple descriptors are compatible.

The following cases are now checked during deforming:

- Deforming a versioned tuple with a RECORDOID tupledesc

  This can happen with triggers and EvalPlanQual rechecks of function
  scans.

- Deforming a versioned tuple with a tupledesc for another relation

  This happens a bunch for partitioned tables.

Additionally, when changing the type of a tuple, the old and new types
are compared for compatibility. This currently happens in:

- heap_copy_tuple_as_datum() and via that in places like
  ExecEvalConvertRowtype(), ExecFetchSlotHeapTupleDatum(), ...

- plpgsql's coerce_function_result_tuple()

In a perfect world these extra checks would not be needed. But...

It is perhaps worth noting that this knowingly emits error messages in
an untranslatable way. That was considered acceptable for now, these
error checks should not normally be hit.

Note that these checks do add nontrivial overhead. This is ameliorated
by the fact that the common ways to reach the checks are part of already
expensive operations (like trigger invocations during DML into a
partitioned table), which are expensive enough to make the added
overhead comparatively small.

Reviewed-by: Melanie Plageman <melanieplageman@gmail.com>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Reviewed-by: Noah Misch <noah@leadboat.com>
Security: CVE-2026-6460
Backpatch-through: 14
(cherry picked from commit 0453c553952116c6b8ac5f155f616a13eef0980e)
(cherry picked from commit c2461cc)
TimescaleDB crashes if an attribute option is present during chunk
creation as part of a DDL command, due to calling AlterTableInternal()
without having set up event trigger state.  With the introduction of
attribute versioning, which stores the version in an internal attribute
option in the back-branches, attribute options are much more common.

The breakage is easy enough to work around, so do that.

This will only be applied to release branches, as this is clearly a bug
in TimescaleDB.

Security: CVE-2026-6460
Backpatch-through: 14-19
(cherry picked from commit 5968dfa6733081e126d094f3e878eafca0e8a8c2)
(cherry picked from commit 40ae164)
Synchronize composite type versions across coordinator and segments, handle Cloudberry rowtype cases, and keep regression coverage reliable for distributed execution.

Use a random distribution for the zero-attribute composite fixture so ALTER TYPE tests datum compatibility without changing a distribution key whose hash depends on the composite shape. Check OID wraparound behavior without assuming the shared counter retains an exact value.
@reshke
reshke force-pushed the fix-mpp-attribute-versioning-20261110 branch from f6d0bca to 1167f3a Compare November 24, 2026 11:11
@reshke
reshke enabled auto-merge (rebase) November 24, 2026 11:11
WRITE_OID_FIELD(keyOid1);
WRITE_OID_FIELD(keyOid2);
WRITE_OID_FIELD(oid);
WRITE_UINT64_FIELD(attributeVersion);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad this about this fix is that this form is not back-patchable. Back-branch should consider alternative approach
on-main fix should however be as close to upstream as possible, IMO

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm however for OID assignment this should not be a problem

@reshke reshke changed the title Fix mpp attribute versioning Fix MPP tuple attribute versioning Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants