Skip to content

catalog: schema descriptor validation reads all functions under a schema#138739

Merged
craig[bot] merged 2 commits intocockroachdb:masterfrom
fqazi:funcDescScanBug
Jan 13, 2025
Merged

catalog: schema descriptor validation reads all functions under a schema#138739
craig[bot] merged 2 commits intocockroachdb:masterfrom
fqazi:funcDescScanBug

Conversation

@fqazi
Copy link
Copy Markdown
Collaborator

@fqazi fqazi commented Jan 9, 2025

Presently, schema descriptor validation read all function descriptors under schema. This is problematic when concurrent CREATE TABLE statements referring to any function under a schema are executed will run into frequent transaction retry errors. To address this this patch will do the following:

  1. Extend the Descriptor.GetReferencedDescIDs interface to support a validation level flag, indicating which references should be returned (i.e. forward references, back references, etc..).
  2. Update the schema descriptor to only return back references if they are requested
  3. Update the validation logic so that mutable descriptors only get mutable validation (with back references). i.e. Not just KV read descriptors
  4. Update CREATE TABLE name resolution logic to avoid mutable descriptors, and only use non-leased ones.
  5. Add a new test case that validates that concurrent create statements do not run into retry errors. Note: These tests are currently disabled with multi-tenant because of: sql: TestCreateObjectConcurrency does not work with secondary tenants #138733

Fixes: #138384

@fqazi fqazi requested a review from a team as a code owner January 9, 2025 15:22
@fqazi fqazi added backport-24.1.x Flags PRs that need to be backported to 24.1. backport-24.2.x backport-24.3.x Flags PRs that need to be backported to 24.3 labels Jan 9, 2025
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Copy link
Copy Markdown
Contributor

@annrpom annrpom left a comment

Choose a reason for hiding this comment

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

:lgtm:

@fqazi fqazi force-pushed the funcDescScanBug branch 3 times, most recently from f5e981d to f95e45e Compare January 10, 2025 14:31
Copy link
Copy Markdown
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

nice tests! just had a nit and question

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @fqazi)


pkg/sql/catalog/descs/descriptor.go line 651 at r1 (raw file):

	}
	requiredLevel := validate.MutableRead
	if !flags.isMutable {

nit: let's consolidate the logic by adding and else to the if flags.isMutable above.


pkg/sql/catalog/typedesc/type_desc.go line 554 at r1 (raw file):

// GetReferencedDescIDs returns the IDs of all descriptors referenced by
// this descriptor, including itself.
func (desc *immutable) GetReferencedDescIDs(

do you think we can tackle some of the issues described in #77140 ? (could be a separate PR)

Copy link
Copy Markdown
Collaborator Author

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @rafiss)


pkg/sql/catalog/typedesc/type_desc.go line 554 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

do you think we can tackle some of the issues described in #77140 ? (could be a separate PR)

Let me do that as a follow on and apply the constrained look up in more places. I'll pick up the issue.

@fqazi fqazi requested a review from rafiss January 10, 2025 17:32
Copy link
Copy Markdown
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @fqazi)


pkg/sql/catalog/descs/descriptor.go line 637 at r7 (raw file):

) error {
	// Add the descriptors to the uncommitted layer if we want them to be mutable.
	if flags.isMutable {

nit: we already have an if flags.isMutable check here; let's reuse this rather than duplication an if

Copy link
Copy Markdown
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewed 10 of 11 files at r1, 3 of 3 files at r6.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @fqazi)

Copy link
Copy Markdown
Collaborator Author

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

Reviewed all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @rafiss)

@fqazi fqazi requested a review from rafiss January 13, 2025 13:16
Copy link
Copy Markdown
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 3 files at r8, 1 of 3 files at r10, 2 of 2 files at r11, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @fqazi)

fqazi added 2 commits January 13, 2025 13:44
Previously, when fetching immutable non-leased descriptors we still did
back reference validation. This could be problematic for certain
CREATE statements, since they fetch non-leased schema descriptors for
name resolutions, which in turns fetches every function descriptor. To
avoid this, this patch adds a level parameter to GetReferencedDescIDs,
which allows the caller to filter out back references. It also update
the validation code to only use mutable validation on mutable
descriptors.

Fixes: cockroachdb#138384
Release note: None
Previously, the logic detect schema changes on schema descriptors during
CREATE statements incorrectly fetched a mutable descriptor. To address
this, this patch only bypasses leasing for this read operation, so that
we don't run the full back reference validation during create operations
on schema. This patch also adds tests to make sure concurrent CREATE
statements never hit unexpected contention.

Release note: None
@fqazi
Copy link
Copy Markdown
Collaborator Author

fqazi commented Jan 13, 2025

bors r+

@fqazi
Copy link
Copy Markdown
Collaborator Author

fqazi commented Jan 13, 2025

@rafiss TFTR!

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Jan 13, 2025

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

Labels

backport-24.1.x Flags PRs that need to be backported to 24.1. backport-24.3.x Flags PRs that need to be backported to 24.3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

catalog: schema descriptor validation reads all functions under a schema

4 participants