Skip to content

Commit

Permalink
Implement Collection.Compact for PostgreSQL (#3603)
Browse files Browse the repository at this point in the history
Closes #3484.
  • Loading branch information
Elena Grahovac authored Oct 18, 2023
1 parent 0d9e48c commit 84d054b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
2 changes: 0 additions & 2 deletions internal/backends/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ func TestCollectionStats(t *testing.T) {
}

func TestCollectionCompact(t *testing.T) {
t.Skip("https://github.com/FerretDB/FerretDB/issues/3484")

t.Parallel()

ctx := conninfo.Ctx(testutil.Ctx(t), conninfo.New())
Expand Down
37 changes: 36 additions & 1 deletion internal/backends/postgresql/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,42 @@ func (c *collection) Stats(ctx context.Context, params *backends.CollectionStats

// Compact implements backends.Collection interface.
func (c *collection) Compact(ctx context.Context, params *backends.CompactParams) (*backends.CompactResult, error) {
// TODO https://github.com/FerretDB/FerretDB/issues/3484
db, err := c.r.DatabaseGetExisting(ctx, c.dbName)
if err != nil {
return nil, lazyerrors.Error(err)
}

if db == nil {
return nil, backends.NewError(
backends.ErrorCodeDatabaseDoesNotExist,
lazyerrors.Errorf("no ns %s.%s", c.dbName, c.name),
)
}

coll, err := c.r.CollectionGet(ctx, c.dbName, c.name)
if err != nil {
return nil, lazyerrors.Error(err)
}

if coll == nil {
return nil, backends.NewError(
backends.ErrorCodeCollectionDoesNotExist,
lazyerrors.Errorf("no ns %s.%s", c.dbName, c.name),
)
}

var full string

if params != nil && params.Full {
full = " FULL"
}

q := fmt.Sprintf(`VACUUM%s ANALYZE %s`, full, pgx.Identifier{c.dbName, coll.TableName}.Sanitize())

if _, err = db.Exec(ctx, q); err != nil {
return nil, lazyerrors.Error(err)
}

return new(backends.CompactResult), nil
}

Expand Down

0 comments on commit 84d054b

Please sign in to comment.