-
Notifications
You must be signed in to change notification settings - Fork 84
perf: optimize pg describe tables query #147
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
Open
aokiji
wants to merge
1
commit into
sqls-server:master
Choose a base branch
from
aokiji:perf/optimize_pg_describe_tables_query
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+68
−101
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -221,111 +221,78 @@ func (db *PostgreSQLDBRepository) Tables(ctx context.Context) ([]string, error) | |
| } | ||
|
|
||
| func (db *PostgreSQLDBRepository) DescribeDatabaseTable(ctx context.Context) ([]*ColumnDesc, error) { | ||
| rows, err := db.Conn.QueryContext( | ||
| ctx, | ||
| ` | ||
| SELECT | ||
| c.table_schema, | ||
| c.table_name, | ||
| c.column_name, | ||
| c.data_type, | ||
| c.is_nullable, | ||
| CASE t.constraint_type | ||
| WHEN 'PRIMARY KEY' THEN 'YES' | ||
| ELSE 'NO' | ||
| END, | ||
| c.column_default, | ||
| '' | ||
| FROM | ||
| information_schema.columns c | ||
| LEFT JOIN ( | ||
| SELECT | ||
| ccu.table_schema as table_schema, | ||
| ccu.table_name as table_name, | ||
| ccu.column_name as column_name, | ||
| tc.constraint_type as constraint_type | ||
| FROM information_schema.constraint_column_usage ccu | ||
| LEFT JOIN information_schema.table_constraints tc ON | ||
| tc.table_schema = ccu.table_schema | ||
| AND tc.table_name = ccu.table_name | ||
| AND tc.constraint_name = ccu.constraint_name | ||
| WHERE | ||
| tc.constraint_type = 'PRIMARY KEY' | ||
| ) as t | ||
| ON c.table_schema = t.table_schema | ||
| AND c.table_name = t.table_name | ||
| AND c.column_name = t.column_name | ||
| ORDER BY | ||
| c.table_name, | ||
| c.ordinal_position | ||
| `) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| defer rows.Close() | ||
| tableInfos := []*ColumnDesc{} | ||
| for rows.Next() { | ||
| var tableInfo ColumnDesc | ||
| err := rows.Scan( | ||
| &tableInfo.Schema, | ||
| &tableInfo.Table, | ||
| &tableInfo.Name, | ||
| &tableInfo.Type, | ||
| &tableInfo.Null, | ||
| &tableInfo.Key, | ||
| &tableInfo.Default, | ||
| &tableInfo.Extra, | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| tableInfos = append(tableInfos, &tableInfo) | ||
| } | ||
| return tableInfos, nil | ||
| return db.DescribeDatabaseTableBySchema(ctx, "") | ||
| } | ||
|
|
||
| func (db *PostgreSQLDBRepository) DescribeDatabaseTableBySchema(ctx context.Context, schemaName string) ([]*ColumnDesc, error) { | ||
| rows, err := db.Conn.QueryContext( | ||
| ctx, | ||
| var parameters []interface{} | ||
| var query strings.Builder | ||
|
|
||
| query.WriteString( | ||
| ` | ||
| SELECT | ||
| c.table_schema, | ||
| c.table_name, | ||
| c.column_name, | ||
| c.data_type, | ||
| c.is_nullable, | ||
| CASE t.constraint_type | ||
| WHEN 'PRIMARY KEY' THEN 'YES' | ||
| ELSE 'NO' | ||
| END, | ||
| c.column_default, | ||
| '' | ||
| FROM | ||
| information_schema.columns c | ||
| LEFT JOIN ( | ||
| SELECT | ||
| ccu.table_schema as table_schema, | ||
| ccu.table_name as table_name, | ||
| ccu.column_name as column_name, | ||
| tc.constraint_type as constraint_type | ||
| FROM information_schema.constraint_column_usage ccu | ||
| LEFT JOIN information_schema.table_constraints tc ON | ||
| tc.table_schema = ccu.table_schema | ||
| AND tc.table_name = ccu.table_name | ||
| AND tc.constraint_name = ccu.constraint_name | ||
| WHERE | ||
| ccu.table_schema = $1 | ||
| AND tc.constraint_type = 'PRIMARY KEY' | ||
| ) as t | ||
| ON c.table_schema = t.table_schema | ||
| AND c.table_name = t.table_name | ||
| AND c.column_name = t.column_name | ||
| WHERE | ||
| c.table_schema = $2 | ||
| ORDER BY | ||
| c.table_name, | ||
| c.ordinal_position | ||
| `, schemaName, schemaName) | ||
| SELECT DISTINCT ON (table_schema, table_name, a.attnum) | ||
| c1.relnamespace::regnamespace AS table_schema, c1.relname AS table_name, a.attname AS column_name, CASE WHEN | ||
| t.typtype = 'd'::"char" THEN | ||
| CASE WHEN bt.typelem <> 0::oid | ||
| AND bt.typlen = '-1'::integer THEN | ||
| 'ARRAY'::text | ||
| WHEN nbt.nspname = 'pg_catalog'::name THEN | ||
| format_type(t.typbasetype, NULL::integer) | ||
| ELSE | ||
| 'USER-DEFINED'::text | ||
| END | ||
| ELSE | ||
| CASE WHEN t.typelem <> 0::oid | ||
| AND t.typlen = '-1'::integer THEN | ||
| 'ARRAY'::text | ||
| WHEN nt.nspname = 'pg_catalog'::name THEN | ||
| format_type(a.atttypid, NULL::integer) | ||
| ELSE | ||
| 'USER-DEFINED'::text | ||
| END | ||
| END::information_schema.character_data AS data_type, CASE WHEN a.attnotnull | ||
| OR t.typtype = 'd'::"char" | ||
| AND t.typnotnull THEN | ||
| 'NO'::text | ||
| ELSE | ||
| 'YES'::text | ||
| END::information_schema.yes_or_no AS is_nullable, CASE WHEN conn.contype = 'p' THEN | ||
| 'YES' | ||
| ELSE | ||
| 'NO' | ||
| END AS is_primary_key, CASE WHEN a.attgenerated = ''::"char" THEN | ||
| pg_get_expr(ad.adbin, ad.adrelid) | ||
| ELSE | ||
| NULL::text | ||
| END::information_schema.character_data AS column_default, '' | ||
| FROM pg_catalog.pg_class c1 | ||
| JOIN pg_catalog.pg_attribute a ON a.attrelid = c1.oid | ||
| JOIN (pg_type t | ||
| JOIN pg_namespace nt ON t.typnamespace = nt.oid) ON a.atttypid = t.oid | ||
| LEFT JOIN (pg_type bt | ||
| JOIN pg_namespace nbt ON bt.typnamespace = nbt.oid) ON t.typtype = 'd'::char | ||
| AND t.typbasetype = bt.oid | ||
| LEFT JOIN pg_catalog.pg_constraint conn ON conn.conrelid = c1.oid | ||
| AND a.attnum = ANY (conn.conkey) | ||
| AND conn.contype = 'p' | ||
| LEFT JOIN pg_attrdef ad ON a.attrelid = ad.adrelid | ||
| AND a.attnum = ad.adnum | ||
| WHERE c1.relkind = ANY (ARRAY['p', 'r', 'v']) | ||
| AND a.attnum > 0 | ||
| AND (pg_has_role(c1.relowner, 'USAGE'::text) | ||
| OR has_column_privilege(c1.oid, a.attnum, 'SELECT, INSERT, UPDATE, REFERENCES'::text)) | ||
| `, | ||
| ) | ||
|
|
||
| if (schemaName != "") { | ||
| parameters = append(parameters, schemaName) | ||
| query.WriteString(` | ||
| AND c1.relnamespace = $1::regnamespace::oid | ||
| `) | ||
| } | ||
|
|
||
| query.WriteString("ORDER BY table_name, a.attnum") | ||
| rows, err := db.Conn.QueryContext( ctx, query.String(), parameters...) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not use log.Fatal(err) in this function |
||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not use () in if statement