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

Fix SchemaStats to return empty stats #1359

Merged
merged 31 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
4b29a73
WIP
Oct 12, 2022
92e821f
WIP
Oct 12, 2022
83a68fe
Merge branch 'main' of https://github.com/w84thesun/FerretDB
Oct 18, 2022
9b1e885
merge conflicts
Oct 20, 2022
ce440cb
Merge branch 'main' of https://github.com/FerretDB/FerretDB
Oct 21, 2022
31ecf5c
Merge branch 'main' of https://github.com/FerretDB/FerretDB
Oct 24, 2022
5b29d95
Merge branch 'main' of https://github.com/FerretDB/FerretDB
Oct 25, 2022
7c9d053
Merge branch 'main' of https://github.com/FerretDB/FerretDB
Oct 27, 2022
2e8d978
Merge remote-tracking branch 'upstream/main'
Oct 28, 2022
fc972ac
Merge branch 'main' of https://github.com/FerretDB/FerretDB
Oct 31, 2022
ad2dddf
Merge branch 'main' of https://github.com/FerretDB/FerretDB
Nov 2, 2022
93425cb
WIP
Nov 2, 2022
1b998b6
Merge branch 'main' into issue-1317-fix-schema-stats
Nov 3, 2022
3ede7d8
WIP
Nov 4, 2022
d36af6e
Merge remote-tracking branch 'origin/issue-1317-fix-schema-stats' int…
Nov 4, 2022
d51d29d
Merge branch 'main' into issue-1317-fix-schema-stats
Nov 4, 2022
2fbea99
WIP
Nov 4, 2022
6c39c3e
Merge remote-tracking branch 'origin/issue-1317-fix-schema-stats' int…
Nov 4, 2022
06123e3
Merge branch 'main' into issue-1317-fix-schema-stats
Nov 4, 2022
61cab15
Merge branch 'main' into issue-1317-fix-schema-stats
Nov 4, 2022
1ed16b0
WIP
Nov 4, 2022
8c9895e
wip
rumyantseva Nov 8, 2022
888c3c5
no docusaurus
rumyantseva Nov 8, 2022
7c98377
revert the change that wasn't needed
rumyantseva Nov 8, 2022
138fddb
cover both cases
rumyantseva Nov 8, 2022
f5ac179
wip
rumyantseva Nov 8, 2022
f7bdbd0
Merge branch 'main' into issue-1317-fix-schema-stats
Nov 8, 2022
17b6e7a
fix gomod
rumyantseva Nov 8, 2022
b117bd4
Merge branch 'main' into issue-1317-fix-schema-stats
mergify[bot] Nov 8, 2022
fca25fe
godoc
rumyantseva Nov 9, 2022
7366726
Merge branch 'main' into issue-1317-fix-schema-stats
mergify[bot] Nov 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions integration/commands_administration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package integration

import (
"errors"
"fmt"
"math"
"net"
Expand All @@ -27,8 +26,6 @@ import (
"testing"
"time"

"github.com/jackc/pgconn"
"github.com/jackc/pgerrcode"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -890,17 +887,6 @@ func TestCommandsAdministrationServerStatusMetrics(t *testing.T) {

var actual bson.D
err := collection.Database().RunCommand(ctx, command).Decode(&actual)
// TODO Don't ignore this error after https://github.com/FerretDB/FerretDB/issues/1317
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
switch pgErr.Code {
case pgerrcode.InvalidSchemaName, pgerrcode.UndefinedTable:
err = nil
}
}
}

require.NoError(t, err)

actualMetric, err := ConvertDocument(t, actual).GetByPath(tc.metricsPath)
Expand Down Expand Up @@ -930,8 +916,6 @@ func TestCommandsAdministrationServerStatusMetrics(t *testing.T) {
}

func TestCommandsAdministrationServerStatusStress(t *testing.T) {
setup.SkipForPostgresWithReason(t, "https://github.com/FerretDB/FerretDB/issues/1317")

t.Parallel()

ctx, collection := setup.Setup(t) // no providers there, we will create collections concurrently
Expand Down Expand Up @@ -974,11 +958,13 @@ func TestCommandsAdministrationServerStatusStress(t *testing.T) {
}

// Attempt to create a collection for Tigris with a schema.
// If we get an error, that's MongoDB (FerretDB ignores that argument for non-Tigris handlers),
// so we create collection without schema.
// If we get an error that support for "validator" is not implemented, that's Postgres.
// If we get an error that "$tigrisSchemaString" is unknown, that's MongoDB.
// In both cases, we create a collection without a schema.
err := db.CreateCollection(ctx, collName, &opts)
if err != nil {
if strings.Contains(err.Error(), `support for field "validator" is not implemented yet`) {
if strings.Contains(err.Error(), `support for field "validator" is not implemented yet`) ||
strings.Contains(err.Error(), `unknown top level operator: $tigrisSchemaString`) {
err = db.CreateCollection(ctx, collName)
}
}
Expand Down
4 changes: 2 additions & 2 deletions integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ replace github.com/FerretDB/FerretDB => ../
require (
github.com/AlekSi/pointer v1.2.0
github.com/FerretDB/FerretDB v0.0.0-00010101000000-000000000000
github.com/jackc/pgconn v1.13.0
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
github.com/prometheus/client_golang v1.13.1
github.com/stretchr/testify v1.8.1
go.mongodb.org/mongo-driver v1.10.4
Expand All @@ -34,6 +32,8 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.10.2 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
Expand Down
41 changes: 3 additions & 38 deletions internal/handlers/pg/pgdb/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,44 +180,9 @@ func (pgPool *Pool) checkConnection(ctx context.Context) error {
// If schema is empty, it calculates statistics across the whole PostgreSQL database.
// If collection is empty it calculates statistics across the whole PostgreSQL schema.
func (pgPool *Pool) SchemaStats(ctx context.Context, schema, collection string) (*DBStats, error) {
var res DBStats

sql := `
SELECT COUNT(distinct t.table_name) AS CountTables,
COALESCE(SUM(s.n_live_tup), 0) AS CountRows,
COALESCE(SUM(pg_total_relation_size('"'||t.table_schema||'"."'||t.table_name||'"')), 0) AS SizeTotal,
COALESCE(SUM(pg_indexes_size('"'||t.table_schema||'"."'||t.table_name||'"')), 0) AS SizeIndexes,
COALESCE(SUM(pg_relation_size('"'||t.table_schema||'"."'||t.table_name||'"')), 0) AS SizeRelation,
COUNT(distinct i.indexname) AS CountIndexes
FROM information_schema.tables AS t
LEFT OUTER
JOIN pg_stat_user_tables AS s ON s.schemaname = t.table_schema
AND s.relname = t.table_name
LEFT OUTER
JOIN pg_indexes AS i ON i.schemaname = t.table_schema
AND i.tablename = t.table_name`

// TODO Exclude service schemas from the query above https://github.com/FerretDB/FerretDB/issues/1068

args := []any{}

if schema != "" {
sql += " WHERE t.table_schema = $1"
args = append(args, schema)

if collection != "" {
sql += " AND t.table_name = $2"
args = append(args, collection)
}
}

res.Name = schema
err := pgPool.QueryRow(ctx, sql, args...).
Scan(&res.CountTables, &res.CountRows, &res.SizeTotal, &res.SizeIndexes, &res.SizeRelation, &res.CountIndexes)
if err != nil {
return nil, lazyerrors.Error(err)
}
return &res, nil
return &DBStats{
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
Name: schema,
}, nil
}

// InTransaction wraps the given function f in a transaction.
Expand Down