forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
59880: sql: improve pg_table_is_visible performance r=otan a=rafiss This is motivated by a query that Django makes in order to retrieve all the tables visible in the current session. The query uses the pg_table_is_visible function. Previously this was implemented by using the internal executor to inspect the pg_catalog. This was expensive because the internal executor does not share the same descriptor collection as the external context, so it ended up fetching every single table descriptor one-by-one. Now, we avoid the internal executor and lookup the table descriptor directly. There are other builtins that use the internal executor that may face the same problem. Perhaps an approach for the future is to allow builtin functions to be implemented using user-defined scalar functions. I added a benchmark that shows the original Django query performing a constant number of descriptor lookups with this new implementation. fixes cockroachdb#57924 Release note (performance improvement): Improve performance of the pg_table_is_visible builtin function. Co-authored-by: Rafi Shamim <[email protected]>
- Loading branch information
Showing
11 changed files
with
224 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package bench | ||
|
||
import "testing" | ||
|
||
func BenchmarkORMQueries(b *testing.B) { | ||
tests := []RoundTripBenchTestCase{ | ||
{ | ||
name: "django table introspection 1 table", | ||
setup: `CREATE TABLE t1(a int primary key, b int);`, | ||
stmt: `SELECT | ||
a.attname AS column_name, | ||
NOT (a.attnotnull OR ((t.typtype = 'd') AND t.typnotnull)) AS is_nullable, | ||
pg_get_expr(ad.adbin, ad.adrelid) AS column_default | ||
FROM pg_attribute AS a | ||
LEFT JOIN pg_attrdef AS ad ON (a.attrelid = ad.adrelid) AND (a.attnum = ad.adnum) | ||
JOIN pg_type AS t ON a.atttypid = t.oid JOIN pg_class AS c ON a.attrelid = c.oid | ||
JOIN pg_namespace AS n ON c.relnamespace = n.oid | ||
WHERE ( | ||
( | ||
(c.relkind IN ('f', 'm', 'p', 'r', 'v')) AND | ||
(c.relname = '<target table>') | ||
) AND (n.nspname NOT IN ('pg_catalog', 'pg_toast')) | ||
) AND pg_table_is_visible(c.oid)`, | ||
}, | ||
|
||
{ | ||
name: "django table introspection 4 tables", | ||
setup: `CREATE TABLE t1(a int primary key, b int); | ||
CREATE TABLE t2(a int primary key, b int); | ||
CREATE TABLE t3(a int primary key, b int); | ||
CREATE TABLE t4(a int primary key, b int);`, | ||
stmt: `SELECT | ||
a.attname AS column_name, | ||
NOT (a.attnotnull OR ((t.typtype = 'd') AND t.typnotnull)) AS is_nullable, | ||
pg_get_expr(ad.adbin, ad.adrelid) AS column_default | ||
FROM pg_attribute AS a | ||
LEFT JOIN pg_attrdef AS ad ON (a.attrelid = ad.adrelid) AND (a.attnum = ad.adnum) | ||
JOIN pg_type AS t ON a.atttypid = t.oid JOIN pg_class AS c ON a.attrelid = c.oid | ||
JOIN pg_namespace AS n ON c.relnamespace = n.oid | ||
WHERE ( | ||
( | ||
(c.relkind IN ('f', 'm', 'p', 'r', 'v')) AND | ||
(c.relname = '<target table>') | ||
) AND (n.nspname NOT IN ('pg_catalog', 'pg_toast')) | ||
) AND pg_table_is_visible(c.oid)`, | ||
}, | ||
|
||
{ | ||
name: "django table introspection 8 tables", | ||
setup: `CREATE TABLE t1(a int primary key, b int); | ||
CREATE TABLE t2(a int primary key, b int); | ||
CREATE TABLE t3(a int primary key, b int); | ||
CREATE TABLE t4(a int primary key, b int); | ||
CREATE TABLE t5(a int primary key, b int); | ||
CREATE TABLE t6(a int primary key, b int); | ||
CREATE TABLE t7(a int primary key, b int); | ||
CREATE TABLE t8(a int primary key, b int);`, | ||
stmt: `SELECT | ||
a.attname AS column_name, | ||
NOT (a.attnotnull OR ((t.typtype = 'd') AND t.typnotnull)) AS is_nullable, | ||
pg_get_expr(ad.adbin, ad.adrelid) AS column_default | ||
FROM pg_attribute AS a | ||
LEFT JOIN pg_attrdef AS ad ON (a.attrelid = ad.adrelid) AND (a.attnum = ad.adnum) | ||
JOIN pg_type AS t ON a.atttypid = t.oid JOIN pg_class AS c ON a.attrelid = c.oid | ||
JOIN pg_namespace AS n ON c.relnamespace = n.oid | ||
WHERE ( | ||
( | ||
(c.relkind IN ('f', 'm', 'p', 'r', 'v')) AND | ||
(c.relname = '<target table>') | ||
) AND (n.nspname NOT IN ('pg_catalog', 'pg_toast')) | ||
) AND pg_table_is_visible(c.oid)`, | ||
}, | ||
} | ||
|
||
RunRoundTripBenchmark(b, tests) | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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