Skip to content

Commit

Permalink
remove redundant db call
Browse files Browse the repository at this point in the history
  • Loading branch information
chilagrow committed Jul 13, 2023
1 parent 5019b40 commit 74d7b68
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
10 changes: 2 additions & 8 deletions internal/handlers/pg/msg_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,21 +402,15 @@ func processStagesStats(ctx context.Context, closer *iterator.MultiCloser, p *st

if hasCount || hasStorage {
if err = p.dbPool.InTransactionRetry(ctx, func(tx pgx.Tx) error {
var exists bool

if exists, err = pgdb.CollectionExists(ctx, tx, p.db, p.collection); err != nil {
return err
}

if !exists {
collStats, err = pgdb.CalculateCollStats(ctx, tx, p.db, p.collection)
if errors.Is(err, pgdb.ErrTableNotExist) {
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrNamespaceNotFound,
fmt.Sprintf("ns not found: %s.%s", p.db, p.collection),
"aggregate",
)
}

collStats, err = pgdb.CalculateCollStats(ctx, tx, p.db, p.collection)
return err
}); err != nil {
return nil, lazyerrors.Error(err)
Expand Down
11 changes: 2 additions & 9 deletions internal/handlers/pg/pgdb/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package pgdb

import (
"context"
"errors"
"fmt"

"github.com/jackc/pgx/v5"
Expand Down Expand Up @@ -144,14 +143,8 @@ func CalculateCollStats(ctx context.Context, tx pgx.Tx, db, collection string) (
var res CollStats

metadata, err := newMetadataStorage(tx, db, collection).get(ctx, false)

switch {
case err == nil:
// do nothing
case errors.Is(err, ErrTableNotExist):
return &res, nil
default:
return nil, lazyerrors.Error(err)
if err != nil {
return nil, err
}

// Call ANALYZE to update statistics, the actual statistics are needed to estimate the number of rows in all tables,
Expand Down

0 comments on commit 74d7b68

Please sign in to comment.