Skip to content

Commit

Permalink
sql: ensure a Prepare referencing a table created in the same txn works
Browse files Browse the repository at this point in the history
fixes #14548
  • Loading branch information
vivekmenezes committed Apr 4, 2017
1 parent 9110251 commit 6da7e28
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pkg/sql/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,12 @@ func (e *Executor) Prepare(

// Prepare needs a transaction because it needs to retrieve db/table
// descriptors for type checking.
// TODO(andrei): is this OK? If we're preparing as part of a SQL txn, how do
// we check that they're reading descriptors consistent with the txn in which
// they'll be used?
txn := client.NewTxn(e.cfg.DB)
if err := txn.SetIsolation(session.DefaultIsolationLevel); err != nil {
panic(err)
txn := session.TxnState.txn
if txn == nil {
txn = client.NewTxn(e.cfg.DB)
if err := txn.SetIsolation(session.DefaultIsolationLevel); err != nil {
panic(err)
}
}
txn.Proto().OrigTimestamp = e.cfg.Clock.Now()

Expand Down
36 changes: 36 additions & 0 deletions pkg/sql/pgwire/pgwire_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,42 @@ func TestPGPrepareFail(t *testing.T) {
}
}

// Run a Prepare referencing a table created in the same transaction.
func TestPGPrepareAfterCreateInTxn(t *testing.T) {
defer leaktest.AfterTest(t)()
s, _, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop()

pgURL, cleanupFn := sqlutils.PGUrl(t, s.ServingAddr(), "TestPGPrepareAfterCreateInTxn", url.User(security.RootUser))
defer cleanupFn()

db, err := gosql.Open("postgres", pgURL.String())
if err != nil {
t.Fatal(err)
}
defer db.Close()

tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}

if _, err := tx.Exec(`
CREATE DATABASE d;
CREATE TABLE d.kv (k CHAR PRIMARY KEY, v CHAR);
`); err != nil {
t.Fatal(err)
}

if _, err := tx.Prepare(`INSERT INTO d.kv (k,v) VALUES ($1, $2);`); err != nil {
t.Fatal(err)
}

if err := tx.Commit(); err != nil {
t.Fatal(err)
}
}

type preparedQueryTest struct {
qargs []interface{}
results [][]interface{}
Expand Down

0 comments on commit 6da7e28

Please sign in to comment.