Skip to content

Commit 60d3347

Browse files
amihajlovicamihajlovicpashagolub
authored
[-] reset session before returning connection to the pool (#589)
Co-authored-by: amihajlovic <[email protected]> Co-authored-by: Pavlo Golub <[email protected]>
1 parent 5338d10 commit 60d3347

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

internal/pgengine/bootstrap.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ func (pge *PgEngine) getPgxConnConfig() *pgxpool.Config {
179179
pgconn.TypeMap().RegisterType(&pgtype.Type{Name: "timetable.log_type", OID: pge.logTypeOID, Codec: &pgtype.EnumCodec{}})
180180
return err
181181
}
182+
// reset session before returning connection to the pool
183+
// after task completion, if the task is not properly finalized (especially when running in autonomous mode),
184+
// some objects and/or setting changes will still exist in the session
185+
connConfig.AfterRelease = func(pgconn *pgx.Conn) bool {
186+
var err error
187+
if _, err = pgconn.Exec(context.Background(), "DISCARD ALL"); err == nil {
188+
_, err = pgconn.Exec(context.Background(), "LISTEN "+quoteIdent(pge.ClientName))
189+
}
190+
return err != nil
191+
}
192+
182193
if !pge.Start.Debug { //will handle notification in HandleNotifications directly
183194
connConfig.ConnConfig.OnNotification = pge.NotificationHandler
184195
}

internal/pgengine/copy_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ func TestCopyFromFile(t *testing.T) {
1515
ctx := context.Background()
1616
_, err := pge.CopyFromFile(ctx, "fake.csv", "COPY location FROM STDIN")
1717
assert.Error(t, err, "Should fail for missing file")
18-
_, err = pge.ConfigDb.Exec(ctx, "CREATE TEMP TABLE csv_test(id integer, val text)")
18+
_, err = pge.ConfigDb.Exec(ctx, "CREATE UNLOGGED TABLE csv_test(id integer, val text)")
1919
assert.NoError(t, err, "Should create temporary table")
20+
defer func() {
21+
_, err = pge.ConfigDb.Exec(ctx, "DROP TABLE csv_test")
22+
assert.NoError(t, err, "Should drop temporary table")
23+
}()
2024
assert.NoError(t, os.WriteFile("test.csv", []byte("1,foo\n2,bar"), 0666), "Should create source CSV file")
2125
cnt, err := pge.CopyFromFile(ctx, "test.csv", "COPY csv_test FROM STDIN (FORMAT csv)")
2226
assert.NoError(t, err, "Should copy from file")

0 commit comments

Comments
 (0)