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: use global defaults for internal session data rather than zero-values #101486

Merged
merged 7 commits into from
Apr 29, 2023

Conversation

rafiss
Copy link
Collaborator

@rafiss rafiss commented Apr 13, 2023

fixes #70888

Release note: None

@rafiss rafiss requested a review from ecwall April 13, 2023 19:14
@rafiss rafiss requested a review from a team as a code owner April 13, 2023 19:14
@rafiss rafiss requested a review from a team April 13, 2023 19:14
@rafiss rafiss requested a review from a team as a code owner April 13, 2023 19:14
@rafiss rafiss requested a review from mgartner April 13, 2023 19:14
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss rafiss requested review from a team as code owners April 13, 2023 20:31
@rafiss rafiss requested a review from a team April 13, 2023 20:31
@rafiss rafiss requested review from a team as code owners April 13, 2023 20:31
@rafiss rafiss requested review from a team, benbardin and shermanCRL and removed request for a team April 13, 2023 20:31
Copy link
Contributor

@ecwall ecwall 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 @benbardin, @mgartner, @rafiss, and @shermanCRL)


pkg/sql/internal.go line 56 at r4 (raw file):

// steps of background jobs and schema changes. Each session variable is
// initialized using the correct default value.
func NewInternalSessionData(

What makes this "internal"? Why is it instantiated differently from the user SessionData?

Copy link
Collaborator Author

@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 (waiting on @benbardin, @ecwall, @mgartner, and @shermanCRL)


pkg/sql/internal.go line 56 at r4 (raw file):

Previously, ecwall (Evan Wall) wrote…

What makes this "internal"? Why is it instantiated differently from the user SessionData?

there are two things that make it internal:

  • the opName is passed in to indicate which internal operation is being executed
  • a real user session has SessionArgs which come from the connection string parameters, and also includes defaults that are configured by ALTER ROLE username SET varname = .... we don't have this here.

@rafiss rafiss force-pushed the fix-zero-values branch 7 times, most recently from b03249d to 236414b Compare April 15, 2023 06:06
Copy link
Contributor

@ecwall ecwall left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 10 files at r1, 2 of 53 files at r5, 25 of 60 files at r6, 18 of 22 files at r7, 4 of 6 files at r8, 1 of 1 files at r9, 2 of 3 files at r10, 1 of 52 files at r11, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @benbardin, @mgartner, @rafiss, and @shermanCRL)


pkg/sql/internal.go line 57 at r11 (raw file):

// steps of background jobs and schema changes. Each session variable is
// initialized using the correct default value.
func NewInternalSessionData(

It looks like a few other params might be useful here like the setting database and the code I mentioned here https://reviewable.io/reviews/cockroachdb/cockroach/101486#-NTEXaA_8kM71fqLJX-O (feel free to do this in a separate ticket(s) since this PR already cleans up a lot).


pkg/sql/planner.go line 374 at r11 (raw file):

	sd.SessionData.UserProto = user.EncodeProto()
	sd.SessionData.Internal = true
	sd.SearchPath = sessiondata.DefaultSearchPathForUser(user)

newInternalPlanner looks like a constructor function so I would not expect it to also modify the input sd.

Can you extract the code modifying sd and call it before calling newInternalPlanner (maybe in a separate ticket)?

var sessionSerialized []byte
tDB.QueryRow(t, "SELECT crdb_internal.serialize_session()").Scan(&sessionSerialized)
require.NoError(t, protoutil.Unmarshal(sessionSerialized, &sessionData))
require.NoError(t, protoutil.Unmarshal(sessionSerialized, &m))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will this work on a mixed-version cluster?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, this will be fine, since adding new fields to a proto is backwards compatibile

Copy link
Contributor

@chengxiong-ruan chengxiong-ruan left a comment

Choose a reason for hiding this comment

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

very nice!

Copy link
Collaborator Author

@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 (waiting on @benbardin, @ecwall, @mgartner, and @shermanCRL)


pkg/sql/planner.go line 374 at r11 (raw file):

Previously, ecwall (Evan Wall) wrote…

newInternalPlanner looks like a constructor function so I would not expect it to also modify the input sd.

Can you extract the code modifying sd and call it before calling newInternalPlanner (maybe in a separate ticket)?

i added a call to clone so the input isn't modified

rafiss added 6 commits April 29, 2023 09:01
After changing the sessiondata to stop using zero-value defaults, a test
started failing. It works when using a zero sessiondata, which deserves
more investigation.

Release note: None
Prevent zero-values from being used for session vars when creating an InternalDB.

Release note: None
Now that the internal executor is using a non-zero session data, that
means it uses the declarative schema changer by default. The upgrade
manager had a few assumptions in it that were specific to the old schema
changer:

- The PGAttributeNum field should only be set if it differs from the
  column descriptor ID.
- Check the DeclarativeSchemaChangerState when waiting for migrations to
  finish.

Release note: None
@rafiss
Copy link
Collaborator Author

rafiss commented Apr 29, 2023

tftr!

bors r=chengxiong-ruan

@craig
Copy link
Contributor

craig bot commented Apr 29, 2023

Build succeeded:

@craig craig bot merged commit 1ee30a1 into cockroachdb:master Apr 29, 2023
@rafiss rafiss deleted the fix-zero-values branch May 16, 2023 16:40
mgartner added a commit to mgartner/cockroach that referenced this pull request Jun 6, 2023
Since cockroachdb#101486, the internal executor has used global defaults for
session settings. This effectively enabled `optimizer_use_histograms`
for the internal executor, which was disabled before. This caused a
huge performance regression. The root cause of the regression is not yet
understood. This commit disables `optimizer_use_histograms` as a
temporary solution for the performance regression.

Informs cockroachdb#102954

Release note: None
craig bot pushed a commit that referenced this pull request Jun 6, 2023
104443: sql: disable histogram usage for internal executor r=mgartner a=mgartner

Since #101486, the internal executor has used global defaults for
session settings. This effectively enabled `optimizer_use_histograms`
for the internal executor, which was disabled before. This caused a
huge performance regression. The root cause of the regression is not yet
understood. This commit disables `optimizer_use_histograms` as a
temporary solution for the performance regression.

Informs #102954

Epic: None

Release note: None

Co-authored-by: Marcus Gartner <[email protected]>
craig bot pushed a commit that referenced this pull request Aug 18, 2023
108947: sql: fix sql compaction job full scan r=j82w a=j82w

The sql-stats-compaction is failing with TransactionRetryError. This is caused by the internal executor uses the zero-values for the settings, rather than the cluster defaults. This causes `SET reorder_joins_limit = 0;` which then causes the `sql-stats-compaction` delete statement to do a full scan. The full scan is causing the query to take a long time causing other queries to conflict with it.

Error:
`TransactionRetryWithProtoRefreshError: TransactionRetryError: retry txn (RETRY_SERIALIZABLE - failed preemptive refresh due to a conflict: committed value on key`

The fix to use the correct default value instead of 0 is made in #101486.

Solution is to change the query to avoid the join and thus the full scan.

Fixes: #108936

Release note (sql change): Optimized the sql-stats-compaction job delete query to avoid full scan. This helps avoid the TransactionRetryError which can cause the job to fail.

Co-authored-by: j82w <[email protected]>
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.

sql: internal executor doesn't have session init'd properly and is just using go defaults
5 participants