algorand / indexer

searchable history and current state
MIT License
114 stars 92 forks source link

writer: deallocate partially prepared statements on error #1592

Closed algorandskiy closed 2 months ago

algorandskiy commented 10 months ago

Summary

Writer does not call Close (and so that DEALLOCATE) if some of statements fail in the middle. Although go's pg driver's Prepare is has map of prepared statements to prevent such errors (see the code excerpt below) it looks more correct to clean up in case of an error.

// Prepare creates a prepared statement with name and sql. sql can contain placeholders
// for bound parameters. These placeholders are referenced positional as $1, $2, etc.
//
// Prepare is idempotent; i.e. it is safe to call Prepare multiple times with the same
// name and sql arguments. This allows a code path to Prepare and Query/Exec without
// concern for if the statement has already been prepared.
func (c *Conn) Prepare(ctx context.Context, name, sql string) (sd *pgconn.StatementDescription, err error) {
    if name != "" {
        var ok bool
        if sd, ok = c.preparedStatements[name]; ok && sd.SQL == sql {
            return sd, nil
        }
    }
...
    sd, err = c.pgConn.Prepare(ctx, name, sql, nil)
...
    if name != "" {
        c.preparedStatements[name] = sd
    }

    return sd, nil
}

Test Plan

No test, it is hard/impossible to test b/c driver's Prepare caches statements.

codecov[bot] commented 10 months ago

Codecov Report

Attention: Patch coverage is 33.33333% with 4 lines in your changes missing coverage. Please review.

Project coverage is 68.43%. Comparing base (4bd144f) to head (10f7c28). Report is 34 commits behind head on main.

Files with missing lines Patch % Lines
idb/postgres/internal/writer/writer.go 33.33% 4 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1592 +/- ## ========================================== - Coverage 68.45% 68.43% -0.02% ========================================== Files 37 37 Lines 7434 7439 +5 ========================================== + Hits 5089 5091 +2 - Misses 1915 1918 +3 Partials 430 430 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

gmalouf commented 10 months ago

@algorandskiy I suggest we ice box/close this for now - the driver for why the work was done may be resolved through configuration