Skip to content

Add field skip_indexes to system.query_log - #79635

Open
pheepa wants to merge 14 commits into
ClickHouse:masterfrom
pheepa:master
Open

pheepa wants to merge 14 commits into
ClickHouse:masterfrom
pheepa:master

Conversation

@pheepa

@pheepa pheepa commented Apr 26, 2025 •

Copy link
Copy Markdown
Contributor

Resolves #78676

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes to

Added a field skip_indexes to system table query_log which shows which skip indexes were used during query execution.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Workflow [PR]
Sync PR [sync-upstream/pr/79635]

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Apr 26, 2025
@clickhouse-gh

clickhouse-gh Bot commented Apr 26, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a0f7f93]

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Apr 26, 2025
@pheepa

pheepa commented Apr 28, 2025

Copy link
Copy Markdown
Contributor Author

@rschu1ze Hi
Can you please check the failed test logs? Not sure what to look for

@rschu1ze rschu1ze self-assigned this Apr 28, 2025
@rschu1ze

Copy link
Copy Markdown
Member

@pheepa I am on a business trip right now but I'll check once I am back, thanks!

@rschu1ze rschu1ze left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@pheepa Would you like to merge from master, there are some merge conflicts - thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does field projection in system.query_log also include the database name (default)?

@pheepa pheepa May 7, 2025 •

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.

yes, some example

Comment thread src/Interpreters/executeQuery.cpp Resolved
Comment thread src/Interpreters/Context.h Resolved
Comment thread src/Interpreters/Context.h Resolved
Comment thread src/Interpreters/Context.h Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is not clear what this enum is good for. Column, partition and skip_index seem unrelated concepts.

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.

I wanted to encapsulate the logic for setting different fields of QueryAccessInfo
Since these attributes have same type, we cant override twice addQueryAccessInfo(String &, String &)
But I would appreciate if you share better solution

Comment thread src/Interpreters/QueryLog.cpp Resolved
@rschu1ze rschu1ze changed the title Add skip_indexes in system.query_log Add field skip_indexes to system.query_log May 4, 2025
@clickhouse-gh

clickhouse-gh Bot commented Jun 10, 2025

Copy link
Copy Markdown
Contributor

Dear @rschu1ze, this PR hasn't been updated for a while. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@grantholly-clickhouse

grantholly-clickhouse commented Oct 17, 2025 •

Copy link
Copy Markdown
Contributor

We appreciate the contribution, but due to our mistake, this got abandoned and then I implemented it in another pull request: #87862.
What should have happened is this PR should have been assigned to someone else, like myself, to continue working on it. Sorry for the mixup. Please feel free take a look at the new PR if you're still interested, and let us know what you think.
In the future, if your PR gets stale for any reason, and you want to resume work on it, please feel free to mention the earlier assignee. We're happy to continue working on it with you, because community contributions like this one are so important to us, and we make it a priority to collaborate on them. Again, sorry for the mistake on my part.

@pheepa

pheepa commented Oct 18, 2025

Copy link
Copy Markdown
Contributor Author

Hey @grantholly-clickhouse, thank you for noticing!
I got stuck with tests that always fail.
For now, my PR looks more comprehensive and closer to the ending, so it would be great if you could take a look if you want to colaborate. Or, how would you like to work together? Please feel free to share your thoughts.

@grantholly-clickhouse

Copy link
Copy Markdown
Contributor

Let's start with bringing in the upstream master branch and fixing conflicts. After that, I want to bring over the query test that I was using in my branch and see if it works here.

@grantholly-clickhouse

Copy link
Copy Markdown
Contributor

@pheepa could you bring in the latest changes and fix conflicts?

@clickhouse-gh

clickhouse-gh Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Dear @grantholly-clickhouse, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@abashkeev abashkeev added the comp-system-table system.* tables (introspection, system metadata, performance visibility). label Sep 3, 2026
@maxknv maxknv added the external Author is not a member of the ClickHouse organization label Nov 2, 2026
Keep the `addQueryAccessInfo` overloads that master added and uses (`StorageID`, columns, `removeQueryAccessInfoTable`); `buildPushingToViewsChain.cpp` and the old `docs/en` page were removed in master (the code moved to `InsertDependenciesBuilder`, the docs are autogenerated now).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [be0a7c4]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_asan_ubsan, db disk, distributed plan, targeted) FAIL
03443_log_skip_indexes FAIL cidb
03443_log_skip_indexes FAIL cidb
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/pr_body_check.py FAIL
python3 ./ci/jobs/scripts/workflow_hooks/feature_docs.py FAIL
python3 ./ci/jobs/scripts/workflow_hooks/can_be_merged.py --review-threads FAIL

AI Review

Summary

Adds skip_indexes to system.query_log and generalizes QueryAccessInfo collection. Reviewed head be0a7c4dd295910b50e1ad1f8a69bd173cd1a493 and all prior discussion. The supplied regression test passes on the PR's CI binary, but focused probes confirm a partition-format compatibility regression and incorrect index attribution. One blocker and two majors remain.

PR Metadata

New Feature fits the addition once the existing partition format is preserved. A changelog entry is required; its prose names the feature and user impact, but the truncated heading prevents ci/tools/changelog.py from recognizing it. See the replacement below.

Findings

❌ Blockers

  • ReadFromMergeTree.cpp:5760: Existing partition IDs change format without a compatibility path. Routing partitions through backQuoteIfNeed changes db.partitioned.3 to db.partitioned.`3` and can also quote database/table names. A reproduced numeric-ID parser now returns NULL, breaking existing analysis and matching against system.parts.partition_id. Preserve the previous representation; otherwise provide a compatibility setting and migration documentation. Consumers traced: QueryAccessInfo copy/swap, logQueryStart, addStatusInfoToQueryLogElement, and QueryLogElement::appendToBlock all preserve these strings; StorageSystemUserQueryLog forwards the values after removing LowCardinality, without undoing quoting.

⚠️ Majors

  • ReadFromMergeTree.cpp:5763–5766: Diagnostic statistics produce false index-usage records. An index added after insertion, without materialization, is logged despite its missing file being skipped. Disjunction processing also logs the nonexistent index db.disjunction.`<Combined skip indexes>` . Both were reproduced. Collect concrete index usage and exclude synthetic statistics.
  • ReadFromMergeTree.cpp:5768–5769: Runtime join indexes are omitted. Later selection and evaluation of runtime_skip_indexes never update QueryAccessInfo. A join using a bloom filter on a non-primary-key probe column logs [] despite dropping 25 granules through that index. Record executed index usage from both analysis and reading, then merge it into QueryFinish.

💡 Nits

  • PR description: Replace the truncated changelog-entry block with:

    ### Changelog entry:
    Added `skip_indexes` to `system.query_log`, listing the data skipping indexes used by each query.
Tests
  • ⚠️ Extend 03443_log_skip_indexes or add focused stateless coverage for an unmaterialized index, synthetic disjunction statistics, and a join that applies an index through a runtime filter. The current five cases pass while all three attribution defects remain.
  • ⚠️ Preserve the existing expected output of 02751_query_log_test_partitions to protect partition-format compatibility.
Missing context / blind spots
  • ⚠️ Parallel-replica and distributed-index-analysis attribution was traced in source but not exercised locally. Focused runs in those modes would close the remaining execution-coverage gap.
Final Verdict

❌ Block. Preserve existing partition formatting, collect actual index usage across analysis and runtime execution, and cover the demonstrated failures. Restore the changelog heading.

}
context->getQueryContext()->addQueryAccessInfo(partition_names);
context->getQueryContext()->addQueryAccessInfo(
data.getStorageID().getFullTableName(), Context::TableAccessInfoType::PARTITION, partition_names);

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.

[Blocker] Preserve the existing partitions representation when adding skip_indexes.

This now passes partition IDs through backQuoteIfNeed, changing an ordinary numeric partition from db.partitioned.3 to db.partitioned.`3` . Database and table names can acquire quoting too. I reproduced this with PARTITION BY a and a = 3: extracting the third dot-separated component and applying toUInt64OrNull now returns NULL. Existing queries that parse partition IDs or match them against system.parts.partition_id therefore break. The changed reference for 02751_query_log_test_partitions also demonstrates the compatibility change.

Keep the previous partition formatting and apply the new quoting helper only to columns and indexes. If changing the existing format is intentional, it needs an explicit compatibility path and migration documentation.

Comment on lines +5763 to +5766
for (const auto & index_stat : result.index_stats)
{
if (index_stat.type == IndexType::Skip)
skip_index_names.emplace_back(index_stat.name);

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.

[Major] index_stats contains diagnostic entries that are not indexes actually used by the query.

filterPartsByPrimaryKeyAndSkipIndexes emits a Skip statistic for each useful index even when its file is absent. On the PR's CI binary, creating a part, adding a minmax index with ALTER TABLE ... ADD INDEX without materializing it, then filtering its column with use_skip_indexes_on_data_read = 0 logs that index here, while the server explicitly reports that the file does not exist and skips it.

The same loop also copies the synthetic <Combined skip indexes> statistic: an a < 16 OR b = 42 query over two indexed columns logs db.disjunction.`<Combined skip indexes>` alongside the real names. That index does not exist.

Track concrete index usage rather than treating the EXPLAIN statistics list as a usage set, and add coverage for both an unmaterialized index and disjunction processing.

Comment on lines +5768 to +5769
context->getQueryContext()->addQueryAccessInfo(
data.getStorageID().getFullTableName(), Context::TableAccessInfoType::SKIP_INDEX, skip_index_names);

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.

[Major] Record indexes applied during execution as well as those represented in the initial analysis.

This is the only producer of skip_indexes, but runtime_skip_indexes is selected later in this method and consumed by MergeTreeSkipIndexReader::read without updating QueryAccessInfo. A join whose probe key has no constant predicate therefore omits an index that it actually uses.

I reproduced this on the PR's CI binary with fact(id, k, v, INDEX idx_k k TYPE bloom_filter GRANULARITY 1) ORDER BY id, 2,000 rows, and a join on k to a dimension filtered to 64 keys. With enable_join_runtime_filters_index_analysis = 1 and use_skip_indexes_on_data_read = 1, QueryFinish.skip_indexes is [], although RuntimeFilterGranulesDropped is 25 and the server confirms pruning through the skip index (primary_key=false, skip_index=true).

Record usage where the runtime index is evaluated, merge it into the final query log entry, and add a stateless join regression case. Initial analysis statistics alone cannot cover this path.

@clickhouse-gh clickhouse-gh Bot added comp-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). and removed comp-system-table system.* tables (introspection, system metadata, performance visibility). labels Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). external Author is not a member of the ClickHouse organization pr-feature Pull request with new product feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

system.query_log should record if the query used skip indexes

7 participants