indexer icon indicating copy to clipboard operation
indexer copied to clipboard

writer: deallocate partially prepared statements on error

Open algorandskiy opened this issue 1 year ago • 2 comments

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.

algorandskiy avatar Jan 12 '24 15:01 algorandskiy

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (4bd144f) 68.45% compared to head (10f7c28) 68.43%.

Files Patch % Lines
idb/postgres/internal/writer/writer.go 33.33% 4 Missing :warning:
Additional details and impacted files
@@            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.

codecov[bot] avatar Jan 12 '24 15:01 codecov[bot]

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

gmalouf avatar Jan 12 '24 21:01 gmalouf