StatementErrored does not need context nor return an error

query-exec-mode
Jack Christensen 2020-11-11 15:52:59 -06:00
parent 426124b32f
commit cba610c245
3 changed files with 11 additions and 20 deletions

View File

@ -88,24 +88,18 @@ func (c *LRU) Clear(ctx context.Context) error {
return nil
}
func (c *LRU) StatementErrored(ctx context.Context, sql string, err error) error {
func (c *LRU) StatementErrored(sql string, err error) {
pgErr, ok := err.(*pgconn.PgError)
if !ok {
// we don't know how to handle this error
return nil
return
}
isInvalidCachedPlanError := pgErr.Severity == "ERROR" &&
pgErr.Code == "0A000" &&
pgErr.Message == "cached plan must not change result type"
if !isInvalidCachedPlanError {
// only flush if a plan has been changed out from under us
return nil
if isInvalidCachedPlanError {
c.stmtsToClear = append(c.stmtsToClear, sql)
}
c.stmtsToClear = append(c.stmtsToClear, sql)
return nil
}
func (c *LRU) clearStmt(ctx context.Context, sql string) error {

View File

@ -89,8 +89,7 @@ func TestLRUStmtInvalidation(t *testing.T) {
require.EqualValues(t, 1, cache.Len())
require.ElementsMatch(t, []string{"select 1"}, fetchServerStatements(t, ctx, conn))
err = cache.StatementErrored(ctx, "select 1", fakeInvalidCachePlanError)
require.NoError(t, err)
cache.StatementErrored("select 1", fakeInvalidCachePlanError)
_, err = cache.Get(ctx, "select 2")
require.NoError(t, err)
require.EqualValues(t, 1, cache.Len())
@ -117,7 +116,7 @@ func TestLRUStmtInvalidation(t *testing.T) {
require.Error(t, res.Close())
require.Equal(t, byte('E'), conn.TxStatus())
err = cache.StatementErrored(ctx, "select 1", fakeInvalidCachePlanError)
cache.StatementErrored("select 1", fakeInvalidCachePlanError)
require.EqualValues(t, 1, cache.Len())
res = conn.Exec(ctx, "rollback")
@ -156,7 +155,7 @@ func TestLRUStmtInvalidationIntegration(t *testing.T) {
result = conn.ExecPrepared(ctx, sd1.Name, nil, nil, nil).Read()
require.EqualError(t, result.Err, "ERROR: cached plan must not change result type (SQLSTATE 0A000)")
cache.StatementErrored(ctx, sql, result.Err)
cache.StatementErrored(sql, result.Err)
sd2, err := cache.Get(ctx, sql)
require.NoError(t, err)

View File

@ -21,12 +21,10 @@ type Cache interface {
Clear(ctx context.Context) error
// StatementErrored informs the cache that the given statement resulted in an error when it
// was last used against the database. In some cases, this will cause the cache to flush
// the statement from the cache. It will only do so when the underlying `*pgconn.PgConn`
// is not currently in a transaction. If the connection is in the middle of a transaction,
// the bad statement will instead be flushed during the next call to Get that occurrs outside
// of a transaction.
StatementErrored(ctx context.Context, sql string, err error) error
// was last used against the database. In some cases, this will cause the cache to maer that
// statement as bad. The bad statement will instead be flushed during the next call to Get
// that occurs outside of a failed transaction.
StatementErrored(sql string, err error)
// Len returns the number of cached prepared statement descriptions.
Len() int