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
  • Loading branch information
vivekmenezes committed Apr 5, 2017
1 parent 5932f65 commit 9c40021
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
15 changes: 7 additions & 8 deletions pkg/sql/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,15 +446,14 @@ 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()
}
txn.Proto().OrigTimestamp = e.cfg.Clock.Now()

planner := session.newPlanner(e, txn)
planner.semaCtx.Placeholders.SetTypes(pinfo)
planner.evalCtx.PrepareOnly = true
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 9c40021

Please sign in to comment.