Skip to content

[Backport] Column-level metadata loading and serialization - #22768

Merged
Mytherin merged 5 commits into
duckdb:v1.5-variegatafrom
ywelsch:yw/column-mem-update-15
May 26, 2026
Merged

Mytherin merged 5 commits into
duckdb:v1.5-variegatafrom
ywelsch:yw/column-mem-update-15

Conversation

@ywelsch

@ywelsch ywelsch commented May 19, 2026

Copy link
Copy Markdown
Contributor

This is a backport of #22333 to the v1.5 branch. The branch-specific adaptations are in 85e1d5f and the effective change is gated by an experimental setting (force_column_metadata_reuse).

Mark and others added 3 commits May 19, 2026 12:26
When making changes to only a subset of columns, DuckDB currently
requires loading and serializing the full column metadata for all other
columns in the touched rowgroups too. For large tables, this can create
a significant IO and memory overhead (as this memory is not tracked by
the buffer pool).

This PR makes it so that operations such as `ALTER TABLE ... ADD COLUMN
| REMOVE COLUMN | ALTER TYPE` as well as `UPDATE` operations only need
to load and write out the metadata for the changed columns.

It further extends the `pragma_storage_info` table function with a new
named parameter called `only_loaded_segments` which, when explicitly set
to true, will only return the segment information for those segments
that are actually loaded in memory. This helps test that the change has
the desired effect (i.e. only relevant columns are loaded).

@Mytherin Mytherin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks - I still think this is very large for a bug-fix patch that also touches on some critical sections of the code (storage / checkpointing). Even though most of it is gated behind a flag many of the refactors are not. I'm OK with merging it but it would help if you could do extra testing on your end to ensure this works correctly and there's no other hidden issues that pop up.

Comment thread src/storage/table/row_group.cpp Outdated

if (has_per_column_metadata_blocks) {
row_group->per_column_metadata_blocks = per_column_metadata_blocks;
row_group->per_column_metadata_blocks.RemoveColumn(target_idx);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should this not be removed_column? Isn't target_idx here equal to columns.size() - 1?

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.

Good catch. This was effectively trying to remove the extra metadata at target_idx = columns.size(), which does not exist, i.e. keeping the metadata as is. It's not a big deal though, as the metadata was fixed during the following checkpoint.

I've addressed this in 394bb32

if (debug_verify_blocks) {
if (write_action == RowGroupWriteAction::REUSE_EXISTING_ROW_GROUP_METADATA) {
auto existing_column_count = entry->ReferenceNode()->GetColumnCount();
reuse_column.reserve(existing_column_count);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Either this needs to be resize or we need to use push_back to add elements to this vector

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.

++, fixed in 394bb32

auto write_action = row_group_write_data.write_action;
auto debug_verify_blocks = Settings::Get<DebugVerifyBlocksSetting>(GetAttached().GetDatabase()) &&
dynamic_cast<SingleFileTableDataWriter *>(&checkpoint_state.writer) != nullptr;
std::vector<bool> reuse_column;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: we prefer to just use vector, not std::vector

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.

muscle memory, sorry (fixed in 394bb32)

vector<ColumnSegmentInfo> GetColumnSegmentInfo(const QueryContext &context);
vector<ColumnSegmentInfo>
GetColumnSegmentInfo(const QueryContext &context,
ColumnSegmentInfoScanType scan_type = ColumnSegmentInfoScanType::ALL) const;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is a feature / API change that should be left out on this branch

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.

addressed in fb82839 (reduces testing coverage though).

@ywelsch
ywelsch marked this pull request as draft May 20, 2026 21:44
@ywelsch
ywelsch marked this pull request as ready for review May 20, 2026 21:44
@ywelsch

ywelsch commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks - I still think this is very large for a bug-fix patch that also touches on some critical sections of the code (storage / checkpointing). Even though most of it is gated behind a flag many of the refactors are not. I'm OK with merging it but it would help if you could do extra testing on your end to ensure this works correctly and there's no other hidden issues that pop up.

Agreed. If you're fine with the change (i.e. LGTM) I will merge it into our staging environments today to get some extra testing cycles (first with the flag off) to make sure there are no regressions.

Note that the fixes in 394bb32 should also make it back to the main branch (would you like me to create a PR, or can you fix this while merging 1.5 changes back to main?)

@ywelsch
ywelsch requested a review from Mytherin May 21, 2026 06:22
@Mytherin
Mytherin merged commit cd3b2ad into duckdb:v1.5-variegata May 26, 2026
111 checks passed
@Mytherin

Copy link
Copy Markdown
Collaborator

Thanks!

kaka11chen pushed a commit to kaka11chen/astrovela_vane that referenced this pull request Aug 14, 2026
…ckdb#22768)

This is a backport of #22333 to the v1.5 branch. The branch-specific
adaptations are in
duckdb/duckdb@85e1d5f
and the effective change is gated by an experimental setting
(force_column_metadata_reuse).

Upstream-Commit: duckdb/duckdb@cd3b2ad
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.

2 participants