Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql/catalog: avoid making copies of table descriptors #127028

Merged
merged 4 commits into from
Aug 2, 2024

Conversation

fqazi
Copy link
Collaborator

@fqazi fqazi commented Jul 11, 2024

Previously, we would copy table descriptors even if no PostDeserialization changes would apply eagerly. This could lead to extra over head when accessing a large number of tables in the pg_catalog tables. To address this, this patch will make a copy only if we need to modify the original descriptor because of a post deserialization change.

Fixes: #121579

Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@fqazi fqazi force-pushed the eliminateDescCopy branch 3 times, most recently from b38d898 to 54ce8bc Compare July 19, 2024 15:54
@fqazi fqazi force-pushed the eliminateDescCopy branch 3 times, most recently from 92833d1 to 4a1537a Compare July 30, 2024 17:52
@fqazi fqazi changed the title [DNM] sql/catalog: avoid making copies of table descriptors sql/catalog: avoid making copies of table descriptors Jul 30, 2024
@fqazi fqazi marked this pull request as ready for review July 30, 2024 19:36
@fqazi fqazi requested review from a team as code owners July 30, 2024 19:36
@fqazi fqazi requested review from dt and removed request for a team July 30, 2024 19:36
Copy link
Contributor

@spilchen spilchen left a comment

Choose a reason for hiding this comment

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

I had a few suggestions to help with understanding the code, but otherwise the changes makes sense to me.

Reviewed 1 of 1 files at r1, 3 of 4 files at r2, 2 of 2 files at r3, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @dt and @fqazi)


pkg/sql/catalog/catprivilege/fix.go line 29 at r3 (raw file):

// dummyPrivilegeDescriptorBuilder implements PrivilegeDescriptorBuilder with
// the assumption that the privilege descriptor is mutable.
type dummyPrivilegeDescriptorBuilder struct {

nit: The term dummy suggests that it doesn’t actually do anything or is only used for testing purposes, at least that’s my impression. I believe we use it to fix actual descriptors, though it might be suboptimal in terms of memory allocations. Perhaps we could call it SimplePrivilegeDescriptorBuilder, or if we intend to eventually replace all uses of this builder and want to discourage new usage, maybe DeprecatedPrivilegeDescriptorBuilder.


pkg/sql/catalog/catprivilege/fix.go line 34 at r3 (raw file):

// GetLatestPrivilege implements PrivilegeDescriptorBuilder.
func (d dummyPrivilegeDescriptorBuilder) GetLatestPrivilege() *catpb.PrivilegeDescriptor {

nit: I find it odd that GetLatest... may not be the most recent if a change was made to the descriptor returned from GetMutable.... Perhaps a better name is GetReadOnlyPrivilege()?


pkg/sql/catalog/tabledesc/table_desc_builder.go line 946 at r3 (raw file):

		if len(s) < len(ref.ColumnIDs) {
			d = builder.getOrInitModifiedDesc()
			ref.ColumnIDs = s

Do we need to update ref? I think it still points to something in the read-only version of the descriptor


pkg/sql/catalog/tabledesc/table_desc_builder.go line 955 at r3 (raw file):

		if s := cleanedIDs(col.UsesSequenceIds); len(s) < len(col.UsesSequenceIds) {
			d = builder.getOrInitModifiedDesc()
			col.UsesSequenceIds = s

Same comment here but for col.


pkg/sql/catalog/tabledesc/table_desc_builder.go line 960 at r3 (raw file):

		if s := cleanedIDs(col.OwnsSequenceIds); len(s) < len(col.OwnsSequenceIds) {
			d = builder.getOrInitModifiedDesc()
			col.OwnsSequenceIds = s

Same comment here


pkg/sql/catalog/tabledesc/table_desc_builder.go line 988 at r3 (raw file):

		if !mutationIDs.Contains(int(backref.MutationID)) {
			tbl = builder.getOrInitModifiedDesc()
			hasChanged = true

nit: could we break out of the loop once we set tbl?

@fqazi fqazi force-pushed the eliminateDescCopy branch 2 times, most recently from 9faafe1 to 9b2faa1 Compare July 31, 2024 19:03
Copy link
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 (waiting on @dt and @spilchen)


pkg/sql/catalog/catprivilege/fix.go line 29 at r3 (raw file):

Previously, spilchen wrote…

nit: The term dummy suggests that it doesn’t actually do anything or is only used for testing purposes, at least that’s my impression. I believe we use it to fix actual descriptors, though it might be suboptimal in terms of memory allocations. Perhaps we could call it SimplePrivilegeDescriptorBuilder, or if we intend to eventually replace all uses of this builder and want to discourage new usage, maybe DeprecatedPrivilegeDescriptorBuilder.

Done.

Renamed to SimplePrivilegeDescriptorBuilder, and hopefully we will just remove it in future PRs.


pkg/sql/catalog/tabledesc/table_desc_builder.go line 946 at r3 (raw file):

Previously, spilchen wrote…

Do we need to update ref? I think it still points to something in the read-only version of the descriptor

Done.


pkg/sql/catalog/tabledesc/table_desc_builder.go line 955 at r3 (raw file):

Previously, spilchen wrote…

Same comment here but for col.

Done.


pkg/sql/catalog/tabledesc/table_desc_builder.go line 960 at r3 (raw file):

Previously, spilchen wrote…

Same comment here

Done.

@fqazi fqazi requested a review from spilchen July 31, 2024 19:14
Copy link
Contributor

@spilchen spilchen left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 6 of 6 files at r4, 4 of 4 files at r5, 2 of 2 files at r6, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @dt)

@fqazi fqazi force-pushed the eliminateDescCopy branch 3 times, most recently from 23eab25 to f797655 Compare August 1, 2024 20:45
Copy link
Contributor

@spilchen spilchen left a comment

Choose a reason for hiding this comment

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

I took another pass since you found a few more cases. It looks good to me, just had a few minor suggestions.

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


pkg/sql/catalog/catprivilege/fix.go line 114 at r10 (raw file):

			} else {
				privs, _ = reader.GetMutablePrivilege().FindUser(user)
				privs.Privileges = allowedPrivilegesBits

Could you check if oldPrivilegeBits != allowedPrivilegesBits before calling GetMutablePrivilege? Not sure how common that will be to make a difference though.


pkg/sql/catalog/tabledesc/table_desc_builder.go line 120 at r10 (raw file):

	// Set the ModificationTime field before doing anything else.
	// Other changes may depend on it.
	mustSetModTime, err := descpb.MustSetModificationTime(

nit: maybe have a comment that says the error will be handled later via another call to MustSetModificationTime in a code path that can deal with errors.

fqazi added 3 commits August 2, 2024 19:43
Previously, the logic to fix descriptors with missing privileges would
assume that the input descriptor was always modifiable. Since, we are
trying to eliminate making copies of descriptors in general, we need
this repair code to be able to make a copy on demand. To address this,
this patch adds a simple builder interface that can be used to make a
copy of the privilege descriptor on demand.

Release note: None
…uired

Previously, the RunPostDeserializationChanges which are no longer
in use. The following post deserialization changes have been
removed:

1) maybeUpgradeNamespaceName - Upgrade from the old namespace table
   format.
2) maybeRemoveDefaultExprFromComputedColumns - Remove default
   expressions from computed columns.
3) maybeFixPrimaryIndexEncoding - Enforces that primary indexes have
   primary key encoding. This is fully validated now.

Release note: None
Previously, we would copy table descriptors even if no
PostDeserialization changes would apply eagerly. This could lead to
extra over head when accessing a large number of tables in the
pg_catalog tables. To address this, this patch will make a copy only if
we need to modify the original descriptor because of a post
deserialization change.

Fixes: cockroachdb#121579

Release note: None
@fqazi fqazi force-pushed the eliminateDescCopy branch from f797655 to e89a581 Compare August 2, 2024 19:44
Previously, the RunPostDeserializationChanges would always
copy descriptors even with our new copy on write semantics. This would
happen because the original copy never has the modification or creation
timestamps set. To address this, this patch modifies the builder to set
these timestamps within the original copy stored inside.

Release note: None
@fqazi fqazi force-pushed the eliminateDescCopy branch from e89a581 to bf23d86 Compare August 2, 2024 19:45
Copy link
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 @dt and @spilchen)


pkg/sql/catalog/catprivilege/fix.go line 114 at r10 (raw file):

Previously, spilchen wrote…

Could you check if oldPrivilegeBits != allowedPrivilegesBits before calling GetMutablePrivilege? Not sure how common that will be to make a difference though.

Done.

@fqazi
Copy link
Collaborator Author

fqazi commented Aug 2, 2024

@spilchen TFTR!

bors r+

@craig craig bot merged commit 3754d0f into cockroachdb:master Aug 2, 2024
22 checks passed
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.

catalog: only clone descriptors when necessary
3 participants