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: create system tables for SQL stats #65374

Merged
merged 1 commit into from
Jul 6, 2021

Conversation

Azhng
Copy link
Contributor

@Azhng Azhng commented May 18, 2021

This PR creates two new system tables: system.sql_statement_stats
and system.sql_transaction_stats per RFC #63752.

This is the initial step that addresses #64743.

Release note: None

@Azhng Azhng requested a review from a team May 18, 2021 01:29
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@Azhng Azhng changed the title sql: create system tables for SQL stats. sql: create system tables for SQL stats May 18, 2021
@Azhng Azhng force-pushed the stats-sys-table branch 2 times, most recently from b9638ca to 285f2e5 Compare May 18, 2021 01:32
Copy link
Contributor

@maryliag maryliag 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 @Azhng)


pkg/sql/catalog/systemschema/system.go, line 370 at r1 (raw file):

	SQLStatementStatsTableSchema = `
CREATE TABLE system.sql_statement_stats (

nit: I think those two tables should use plural, otherwise looks like is several stats for one statement/transaction. So sql_statements_stats and sql_transactions_stats
Also update in all references, e.g. SQLStatementsStatsTableSchema


pkg/sql/catalog/systemschema/system.go, line 386 at r1 (raw file):

    PRIMARY KEY (aggregated_ts, fingerprint_id, plan_hash, app_name, node_id)
      USING HASH WITH BUCKET_COUNT = 8,

You mentioned on the RFC that the size of bucket as 8 was arbitrary and that would require some benchmark testing to choose the value. Was that done? If not, should we add a comment here explaining and that this value could be replaced?


pkg/sql/catalog/systemschema/system.go, line 1852 at r1 (raw file):

	})
	// SQLStatementStatsTable is the descriptor for the SQL statement stats table.
	// It stores statistics for statistics for statement fingerprints.

nit: It stores statistics for statements fingerprints.


pkg/sql/catalog/systemschema/system.go, line 1952 at r1 (raw file):

	// SQLTransactionStatsTable is the descriptor for the SQL transaction stats
	// table. It stores statistics for statistics for transaction fingerprints.

nit: It stores statistics for transactions fingerprints.

@Azhng Azhng force-pushed the stats-sys-table branch from 285f2e5 to fc69990 Compare May 18, 2021 18:56
@Azhng Azhng requested a review from a team May 18, 2021 18:56
@Azhng Azhng requested a review from a team as a code owner May 18, 2021 18:56
@Azhng Azhng requested review from nihalpednekar and removed request for a team May 18, 2021 18:56
Copy link
Contributor Author

@Azhng Azhng 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 @kevin-v-ngo and @maryliag)


pkg/sql/catalog/systemschema/system.go, line 370 at r1 (raw file):

Previously, maryliag (Marylia Gutierrez) wrote…

nit: I think those two tables should use plural, otherwise looks like is several stats for one statement/transaction. So sql_statements_stats and sql_transactions_stats
Also update in all references, e.g. SQLStatementsStatsTableSchema

I think currently our system view is named crdb_internal.node_statement_statistics. So it's slightly breaking the convention here if we named it plural. @kevin-v-ngo What do you think?


pkg/sql/catalog/systemschema/system.go, line 386 at r1 (raw file):

Previously, maryliag (Marylia Gutierrez) wrote…

You mentioned on the RFC that the size of bucket as 8 was arbitrary and that would require some benchmark testing to choose the value. Was that done? If not, should we add a comment here explaining and that this value could be replaced?

Done.

@nihalpednekar nihalpednekar requested review from pbardea and removed request for nihalpednekar May 18, 2021 20:19
@Azhng Azhng force-pushed the stats-sys-table branch from fc69990 to 7b5fb52 Compare May 18, 2021 20:39
@pbardea
Copy link
Contributor

pbardea commented May 18, 2021

(Bulk IO changes look like mainly plumbing which LGTM, so going to untag myself)

@pbardea pbardea removed their request for review May 18, 2021 22:14
Copy link
Contributor Author

@Azhng Azhng left a comment

Choose a reason for hiding this comment

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

Thank you. Who should I tag for changes related to system table schema?

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @kevin-v-ngo and @maryliag)

@nihalpednekar
Copy link

Thank you. Who should I tag for changes related to system table schema?

@rytaft might be a good reviewer to add, as a contributor to the file and pkg/sql/stats owner.

@nihalpednekar nihalpednekar requested a review from rytaft May 18, 2021 23:41
Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

:lgtm: although I haven't touched most of this code in several years. Might be good to also check with someone on the SQL schema team...

Reviewed 8 of 11 files at r1, 29 of 29 files at r2.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @Azhng, @kevin-v-ngo, and @maryliag)


pkg/sql/catalog/bootstrap/metadata.go, line 352 at r2 (raw file):

	// Tables introduced in 21.2.
	target.AddDescriptor(keys.SystemDatabaseID, systemschema.SQLStatementStatsTable)

nit: add a blank line above to be consistent with previous versions


pkg/sql/catalog/systemschema/system.go, line 456 at r2 (raw file):

	singleID1              = []descpb.ColumnID{1}
	sqlStmtHashComputeExpr = "mod(fnv32(COALESCE(CAST(aggregated_ts AS STRING), '':::STRING)) + (fnv32(COALESCE(CAST(app_name AS STRING), '':::STRING)) + (fnv32(COALESCE(CAST(fingerprint_id AS STRING), '':::STRING)) + (fnv32(COALESCE(CAST(node_id AS STRING), '':::STRING)) + fnv32(COALESCE(CAST(plan_hash AS STRING), '':::STRING))))), 8:::INT8)"
	sqlTxnHashComputeExpr  = "mod(fnv32(COALESCE(CAST(aggregated_ts AS STRING), '':::STRING)) + (fnv32(COALESCE(CAST(app_name AS STRING), '':::STRING)) + (fnv32(COALESCE(CAST(fingerprint_id AS STRING), '':::STRING)) + fnv32(COALESCE(CAST(node_id AS STRING), '':::STRING)))), 8:::INT8)"

where did these expressions come from? A comment would help


pkg/sql/tests/system_table_test.go, line 172 at r2 (raw file):

		LocalOnlySessionData: sessiondata.LocalOnlySessionData{
			EnableUniqueWithoutIndexConstraints: true,
			HashShardedIndexesEnabled:           true,

instead of adding the sessionDataOverride, is there a reason you can't just update the session data in the test file and set this to true? Does it break anything?

@Azhng Azhng force-pushed the stats-sys-table branch from 7b5fb52 to 42f3ecb Compare May 19, 2021 22:26
Copy link
Contributor Author

@Azhng Azhng 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! 1 of 0 LGTMs obtained (waiting on @maryliag and @rytaft)


pkg/sql/catalog/systemschema/system.go, line 456 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

where did these expressions come from? A comment would help

Done.


pkg/sql/tests/system_table_test.go, line 172 at r2 (raw file):

Previously, rytaft (Rebecca Taft) wrote…

instead of adding the sessionDataOverride, is there a reason you can't just update the session data in the test file and set this to true? Does it break anything?

Done.

Copy link
Contributor Author

@Azhng Azhng left a comment

Choose a reason for hiding this comment

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

Table names have been renamed to "system.statement_statistics" and "system.transaction_statistics" per feedback on the RFC.

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

@Azhng Azhng requested a review from a team May 19, 2021 22:27
@Azhng Azhng force-pushed the stats-sys-table branch 2 times, most recently from 3d53ca3 to 88026aa Compare May 20, 2021 00:44
Copy link
Collaborator

@rytaft rytaft left a comment

Choose a reason for hiding this comment

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

:lgtm: Might want to ask @ajwerner for a quick glance at the system table stuff.

Reviewed 27 of 27 files at r3.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @maryliag)

@Azhng Azhng force-pushed the stats-sys-table branch from 88026aa to 7c26f95 Compare May 22, 2021 02:32
@Azhng Azhng requested a review from ajwerner May 22, 2021 02:33
Copy link
Contributor

@ajwerner ajwerner 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 @Azhng, @maryliag, and @rytaft)


pkg/sql/catalog/systemschema/system.go, line 422 at r4 (raw file):

    PRIMARY KEY (aggregated_ts, fingerprint_id, app_name, node_id)
      USING HASH WITH BUCKET_COUNT = 8,

I wonder if we're going to be horribly sad about this soon. @mgartner, IIUC, is going to look at switching hash sharded indexes to no longer be physically stored columns. It's a bummer that we need to create this extra hash column and makes the implementation somewhat messy. I worry we'll carry this vestige of the original implementation with us forever if we choose to use it now.

What was the back-of-the-envelope for write throughput of a cluster that convinced us that we needed to hash this index?

Copy link
Contributor Author

@Azhng Azhng 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 @maryliag, @mgartner, and @rytaft)


pkg/sql/catalog/systemschema/system.go, line 422 at r4 (raw file):

Previously, ajwerner wrote…

I wonder if we're going to be horribly sad about this soon. @mgartner, IIUC, is going to look at switching hash sharded indexes to no longer be physically stored columns. It's a bummer that we need to create this extra hash column and makes the implementation somewhat messy. I worry we'll carry this vestige of the original implementation with us forever if we choose to use it now.

What was the back-of-the-envelope for write throughput of a cluster that convinced us that we needed to hash this index?

Is the switch to no longer physically storing hash column going to happen this release? IIUC, we can always change the system table schema without worrying about migrations before the 21.2 happens.

I think the back-of-the-envelope for write throughput based on the data we have in snowflake for CC, the worst case would be 14 GB/hr for a 32 node cluster. Though it is one of the extreme cases. Most of the other clusters would generate tens to hundreds of MB of writes.

@mgartner
Copy link
Collaborator


pkg/sql/catalog/systemschema/system.go, line 422 at r4 (raw file):

Is the switch to no longer physically storing hash column going to happen this release?

We're exploring simplifying the hash-sharded index implementation this release by using virtual computed columns. But there are still some unanswered questions, like, does this even make sense given that the hash-sharded column has to be park of the PK.

But I don't expect the syntax for creating a hash-sharded index to change. What problems would arise if the underlying hash-sharded implementation changes if there's already this existing system table in a cluster?

Copy link
Contributor

@ajwerner ajwerner 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 @Azhng, @maryliag, @mgartner, and @rytaft)


pkg/sql/catalog/systemschema/system.go, line 422 at r4 (raw file):

But there are still some unanswered questions, like, does this even make sense given that the hash-sharded column has to be park of the PK.

In this case, you mean? Generally, the hash-sharded column does not need to be part of the primary key. You are right that if we keep it in the primary key, then it's a total wash.

I think the back-of-the-envelope for write throughput based on the data we have in snowflake for CC, the worst case would be 14 GB/hr for a 32 node cluster. Though it is one of the extreme cases.

That is a lot.


pkg/sql/catalog/systemschema/system.go, line 423 at r4 (raw file):

    PRIMARY KEY (aggregated_ts, fingerprint_id, app_name, node_id)
      USING HASH WITH BUCKET_COUNT = 8,
    INDEX "fingerprint_stats_idx" (fingerprint_id, aggregated_ts, app_name, node_id),

I'm somewhat suspect of this choice of primary and secondary index. It'd be nice to have some data generation and then some benchmarking. Changing this schema may be somewhat painful after it merges, at least to some extent. I worry that, in the long term, we'll wish that the hash-sharded index had small values and that the primary index were more scattered. Did you consider using fingerprint_stats_idx as the primary index and the current primary index as a secondary index? I think that'll allow for fast loading of a page for a given fingerprint and reduce the size of data that needs to be replicated to the hash sharded index by several orders of magnitude.

Copy link
Contributor Author

@Azhng Azhng 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 @ajwerner, @maryliag, @mgartner, and @rytaft)


pkg/sql/catalog/systemschema/system.go, line 422 at r4 (raw file):

You are right that if we keep it in the primary key, then it's a total wash.

I'm not quite following this. Can you elaborate more?

That is a lot.

The write throughput scales based on unique number of fingerprints (~1 KiB per fingerprint) and number of nodes in the cluster. During the RFC discussion, knz pointed out that the performance sensitive clusters tend to tune their workload to have small number of unique number of fingerprints. This means in these clusters, we will be seeing a lot less unique fingerprints.


pkg/sql/catalog/systemschema/system.go, line 423 at r4 (raw file):

Did you consider using fingerprint_stats_idx as the primary index and the current primary index as a secondary index?

We did consider this as the original design. However, this table also will power the loading for the statement page, which displays aggregated execution statistics for all statement fingerprints for a given time interval. Since we considered (grep for 'statement page') loading statement page to be a more important use case compared to loading statistics for a single fingerprint, we chose the design of using hash sharded PK.

Changing this schema may be somewhat painful after it merges, at least to some extent

Even if we change prior to the 21.2 release ?

@mgartner
Copy link
Collaborator


pkg/sql/catalog/systemschema/system.go, line 422 at r4 (raw file):

In this case, you mean? Generally, the hash-sharded column does not need to be part of the primary key. You are right that if we keep it in the primary key, then it's a total wash.

For a hash-sharded primary index, the hash-sharded column is the first column in the PK, no?

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

Can I make a procedural recommendation here that may derail this stuff a tad? I just went through the RFC a bit and I think this project could really benefit from some go interfaces. I'm increasingly on board with this layout etc but think everything will fall into place better with some interface structure that we put in place first rather that last. I don't want to get too in your way here, I know this is way more than you bargained for.

I think we're in a bit of a mess right now with the in-memory implementation and how we're just injecting the entire *sql.Server into the status APIs and calling arbitrary methods.

A pattern that has been working well from my perspective lately is a set of low-dependency interfaces sitting in a package and some implementations sitting in separate subpackages. This way we can trivially do things like compose the retrieval and combination of persisted stats with in-memory stats without changing any clients. We can also test things about the implementation via the interface.

How do you feel about something like a stmtstats package which has interfaces like Reader and Writer that provide access to the various layers? Then we can compose some of these implementations, simplify clients, and dramatically simplify testing of different layers.

Maybe you've got a code structure plan in mind. I'd love to reach some alignment before we just glom more arbitrary functionality onto gigantic objects like *sql.Server.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @Azhng, @maryliag, and @rytaft)


pkg/sql/catalog/systemschema/system.go, line 422 at r4 (raw file):

In this case, you mean? Generally, the hash-sharded column does not need to be part of the primary key. You are right that if we keep it in the primary key, then it's a total wash.

For a hash-sharded primary index, the hash-sharded column is the first column in the PK, no?

Correct


pkg/sql/catalog/systemschema/system.go, line 423 at r4 (raw file):

Changing this schema may be somewhat painful after it merges, at least to some extent

Even if we change prior to the 21.2 release ?

Not too painful, just non-zero.

Copy link
Contributor

@knz knz left a comment

Choose a reason for hiding this comment

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

👍 on Andrew's suggestion.

Reviewed 2 of 27 files at r3, 7 of 7 files at r4.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @Azhng and @maryliag)

Copy link
Contributor Author

@Azhng Azhng left a comment

Choose a reason for hiding this comment

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

Discussed with @ajwerner offline and decided to move forward with the refactoring. Also filed a tracking issue

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

This PR creates two new system tables: system.statement_stats
and system.transaction_stats per RFC cockroachdb#63752.

This is the initial step that addresses cockroachdb#64743.

Release note: None
@Azhng Azhng force-pushed the stats-sys-table branch from 7c26f95 to 0a1e719 Compare July 5, 2021 21:20
@Azhng
Copy link
Contributor Author

Azhng commented Jul 5, 2021

Rebased to resolve the merge conflict. See follow up PR #67090 for the SQL Stats flush mechanism. @ajwerner can you take another look.

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

:lgtm_strong:

Reviewed 3 of 31 files at r5.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (and 1 stale) (waiting on @knz, @maryliag, and @rytaft)

@Azhng
Copy link
Contributor Author

Azhng commented Jul 6, 2021

TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented Jul 6, 2021

🕐 Waiting for PR status (Github check) to be set, probably by CI. Bors will automatically try to run when all required PR statuses are set.

@craig
Copy link
Contributor

craig bot commented Jul 6, 2021

GitHub status checks took too long to complete, so bors is giving up. You can adjust bors configuration to have it wait longer if you like.

@Azhng
Copy link
Contributor Author

Azhng commented Jul 6, 2021

bors r+

@craig
Copy link
Contributor

craig bot commented Jul 6, 2021

Build succeeded:

@craig craig bot merged commit 3fce327 into cockroachdb:master Jul 6, 2021
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.

9 participants