Repository navigation
Perf | Reduce SqlBulkCopy destination metadata query cost - #4988
Merged
Merged
Conversation
- Skip the #Column_Aliases temp table and result set unless mappings use SQL Graph aliases. - Add Switch.Microsoft.Data.SqlClient.UseLegacyBulkCopyMetadataDiscovery (default false) to restore the pre-6.0 SET FMTONLY ON / SELECT * query. Graph alias mappings still use the catalog-based query. - Add unit tests for query generation and manual tests for the legacy path. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…ery (#4985) Default false uses the pre-6.0 FMTONLY query. Setting it to true enables the sys.all_columns query for hidden columns and SQL Graph aliases. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
cheenamalhotra
requested review from
a team
and
a balanced review from Copilot
November 4, 2026 01:53
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The new constant is declared inside #if NET but referenced unconditionally, causing the net462 target to fail compilation with CS0103.
1 open issue
What changed in this PR
Restores the pre-6.0 SqlBulkCopy destination-metadata query to address the server CPU regression reported in #4985. Catalog discovery remains available through an opt-in switch for hidden-column and SQL Graph alias mappings.
Changes:
- Uses
SET FMTONLY ON; SELECT *by default. - Creates the alias temporary table only when catalog discovery requires alias resolution.
- Documents the compatibility change and adds query-generation and integration tests.
| File | Description |
|---|---|
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlBulkCopyTests.cs |
Tests query selection, alias handling, and destination-name escaping. |
src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/LocalAppContextSwitchesTest.cs |
Checks the switch defaults to false. |
src/Microsoft.Data.SqlClient/tests/ManualTests/BulkCopy/UnprivilegedLogin.cs |
Enables catalog discovery for permission tests. |
src/Microsoft.Data.SqlClient/tests/ManualTests/BulkCopy/SqlGraphTables.cs |
Enables catalog discovery for graph tests. |
src/Microsoft.Data.SqlClient/tests/ManualTests/BulkCopy/LegacyMetadataDiscovery.cs |
Adds integration coverage for legacy discovery and mapping behavior. |
src/Microsoft.Data.SqlClient/tests/ManualTests/BulkCopy/HiddenTargetColumn.cs |
Enables catalog discovery for hidden-column tests. |
src/Microsoft.Data.SqlClient/tests/Common/LocalAppContextSwitchesHelper.cs |
Saves, sets, and restores the new switch in tests. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs |
Selects the metadata query and handles optional alias results. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/LocalAppContextSwitches.cs |
Defines the catalog-discovery switch and cached value. |
.github/instructions/features.instructions.md |
Documents the switch, default, and mapping implications. |
Review effort: Balanced
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
🔵 Needs a closer look
SQL Server metadata-shape and hidden-column/graph compatibility changes remain unverified, with current-head CI pending and trusted copies of the review policies missing.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
cheenamalhotra
marked this pull request as draft
November 4, 2026 02:10
Use a tri-state switch to preserve hidden-column and graph-alias support while keeping ordinary copies on the fast metadata path. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This was referenced Dec 1, 2026
Closed
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Description
Reduces the destination-metadata query cost reported in #4985 by restoring the pre-6.0
SET FMTONLY ON; SELECT *query for Bulk Copy operations mapped by destination name.Adds
Switch.Microsoft.Data.SqlClient.UseBulkCopyCatalogMetadataDiscoveryas a tri-state switch:sys.all_columnsif a name is missing. Destination-ordinal mappings, implicit/default mappings, and SQL Graph aliases use catalog discovery directly.truefalseCompatibility: Automatic mode preserves current destination positions and supports hidden-column names from #1854 and SQL Graph aliases. Explicit
trueavoids the extra metadata round trip for hidden-name workloads. Explicitfalserestores fast-only discovery and its limitations.Fallback happens before any rows are sent; it does not retry the bulk write. The alias temp table is only created when mappings require it. Catalog metadata and alias capability are retained when the cache is reused. SQL Server restrictions on writing generated columns still apply. No public API changes. The switch is documented in the feature reference and API remarks.
Options considered
Why this design
Automatic mode chooses the query from the destination mappings:
"Name"to"Name"0to1$from_idSELECT *omits hidden columns. For a table ordered asId, ValidFrom (hidden), ValidTo (hidden), OtherDate, destination position1would becomeOtherDateinstead ofValidFrom. Choosing catalog discovery upfront for ordinal/default mappings prevents values from going into the wrong column. A fast cache created by earlier named mappings is replaced before using ordinals. This choice adds no extra metadata round trip.Who benefits when upgrading from 5.2?
Customers using explicit destination-name mappings avoid the catalog query for ordinary columns. The customer in #4985 uses name mappings, so this targets their reported regression.
Customers using destination ordinals or no explicit mappings retain catalog discovery, even for ordinary tables without hidden columns. They may therefore still see higher metadata-discovery CPU than 5.2; this does not restore 5.2 performance for every Bulk Copy workload. The cost matters most for frequent copies, many tables/databases, and small batches. Metadata caching helps when the same
SqlBulkCopyinstance is reused. These copies still avoid alias temp-table work when no graph aliases are mapped.Only automatic name fallback adds one metadata batch round trip, including an invalid name that still fails after catalog discovery. Without metadata caching, the retry repeats on each copy. With caching, successful catalog discovery is reused. Each discovery query is one SQL batch; nested server-side
EXECcalls do not add network round trips. Server CPU improvement still needs measurement against the customer workload.Self-review notes
Validation
Issues
Fixes #4985
Testing
Added query-generation coverage for all three modes, automatic fallback decisions, alias resolution, destination-name escaping, and cache compatibility. Added sync/async integration coverage for ordinary copies, writable hidden columns, graph aliases, temporary tables, and metadata caching. Existing catalog-dependent tests explicitly enable the switch.
Added sync/async ordinal and default-mapping cases with a visible date column after writable hidden columns. Ordinal cases also cover replacing a fast cache after switching from names to ordinals. Added unit coverage for retaining cached alias capability.
dotnet test src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft.Data.SqlClient.UnitTests.csproj -f net10.0 --filter "FullyQualifiedName~SqlBulkCopyTests|FullyQualifiedName~LocalAppContextSwitchesTest|FullyQualifiedName~SqlBulkCopyCacheMetadataTest" -v q- passed, 70 tests, none skipped.dotnet build src/Microsoft.Data.SqlClient/tests/ManualTests/Microsoft.Data.SqlClient.ManualTests.csproj -f net10.0 -v q- passed, no warnings.dotnet build src/Microsoft.Data.SqlClient/src/Microsoft.Data.SqlClient.csproj -f net462 -v q- passed, no warnings.Integration tests and customer CPU measurements were not run because no SQL Server is available. .NET Framework runtime tests were not run on macOS.
Guidelines
Please review the contribution guidelines before submitting a pull request: