Skip to content

Commit

Permalink
pgwire: fix for specific cases of unnamed portal execution
Browse files Browse the repository at this point in the history
The Postgres docs say that an unnamed portal is automatically closed
when a Bind command for another unnamed portal or a simple query is
sent. We already implemented that for "normal" portals, but not for
portals that were partially executed.

This does not address the general case, but it does handle the common
case that we see from tools like asyncpg.

Release note (bug fix): Previously, executing an unnamed portal with a
row count limit could encounter errors if another unnamed portal was
opened. Now, CockroachDB correctly handles the common case of preparing
and binding an unnamed portal by closing the existing unnamed portal.
This matches the Postgres wire protocol.
  • Loading branch information
rafiss committed Jun 22, 2022
1 parent a8f7ed7 commit bc502ed
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 13 deletions.
83 changes: 70 additions & 13 deletions pkg/sql/pgwire/command_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package pgwire

import (
"context"
"fmt"
"time"

"github.com/cockroachdb/cockroach/pkg/col/coldata"
Expand Down Expand Up @@ -462,6 +461,18 @@ func (r *limitedCommandResult) moreResultsNeeded(ctx context.Context) error {
// Keep track of the previous CmdPos so we can rewind if needed.
prevPos := r.conn.stmtBuf.AdvanceOne()
for {
// If the portal is immediately followed by a COMMIT, we can proceed and
// let the portal be destroyed at the end of the transaction.
if isCommit, err := r.isCommit(ctx); err != nil {
return err
} else if isCommit {
return r.rewindAndClosePortal(ctx, prevPos)
}
if shouldClose, err := r.shouldCloseUnnamedPortal(ctx); err != nil {
return err
} else if shouldClose {
return r.rewindAndClosePortal(ctx, prevPos)
}
cmd, curPos, err := r.conn.stmtBuf.CurCmd()
if err != nil {
return err
Expand Down Expand Up @@ -508,27 +519,22 @@ func (r *limitedCommandResult) moreResultsNeeded(ctx context.Context) error {
return err
}
default:
// If the portal is immediately followed by a COMMIT, we can proceed and
// let the portal be destroyed at the end of the transaction.
if isCommit, err := r.isCommit(); err != nil {
return err
} else if isCommit {
return r.rewindAndClosePortal(ctx, prevPos)
}
// We got some other message, but we only support executing to completion.
telemetry.Inc(sqltelemetry.InterleavedPortalRequestCounter)
return errors.WithDetail(sql.ErrLimitedResultNotSupported,
fmt.Sprintf("cannot perform operation %T while a different portal is open", c))
return errors.WithDetailf(sql.ErrLimitedResultNotSupported,
"cannot perform operation %T while a different portal is open", c)
}
prevPos = curPos
}
}

// isCommit checks if the statement buffer has a COMMIT at the current
// position. It may either be (1) a COMMIT in the simple protocol, or (2) a
// Parse/Bind/Execute sequence for a COMMIT query.
func (r *limitedCommandResult) isCommit() (bool, error) {
cmd, _, err := r.conn.stmtBuf.CurCmd()
// Parse/Bind/Execute sequence for a COMMIT query. This peeks ahead in the
// statement buffer, but does not advance the position.
func (r *limitedCommandResult) isCommit(ctx context.Context) (bool, error) {
cmd, curPos, err := r.conn.stmtBuf.CurCmd()
defer r.conn.stmtBuf.Rewind(ctx, curPos)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -584,6 +590,57 @@ func (r *limitedCommandResult) isCommit() (bool, error) {
return false, nil
}

// shouldCloseUnnamedPortal checks if the current portal is the unnamed portal
// and if it should be closed. The Postgres docs specify that:
// "An unnamed portal is destroyed at the end of the transaction, or as soon as
// the next Bind statement specifying the unnamed portal as destination is
// issued. (Note that a simple Query message also destroys the unnamed portal.)"
// This function peeks ahead in the statement buffer, but does not advance the
// position.
func (r *limitedCommandResult) shouldCloseUnnamedPortal(ctx context.Context) (bool, error) {
if r.portalName != "" {
return false, nil
}
cmd, curPos, err := r.conn.stmtBuf.CurCmd()
defer r.conn.stmtBuf.Rewind(ctx, curPos)
if err != nil {
return false, err
}
// Case 1: Check if cmd is a simple query.
if _, ok := cmd.(sql.ExecStmt); ok {
return true, nil
}
// Case 2: Check if cmd is a Bind for an unnamed portal.
if bindStmt, ok := cmd.(sql.BindStmt); ok {
if bindStmt.PortalName == "" {
return true, nil
}
}
// Case 3: Check if cmd is a Prepare followed by a Bind for an unnamed portal.
if prepareStmt, ok := cmd.(sql.PrepareStmt); ok {
r.conn.stmtBuf.AdvanceOne()
cmd, _, err = r.conn.stmtBuf.CurCmd()
if err != nil {
return false, err
}
// Allow Describe, but just skip over it.
if _, ok := cmd.(sql.DescribeStmt); ok {
r.conn.stmtBuf.AdvanceOne()
cmd, _, err = r.conn.stmtBuf.CurCmd()
if err != nil {
return false, err
}
}
// The next cmd must be a bind command for the prepared statement.
if bindStmt, ok := cmd.(sql.BindStmt); ok {
if bindStmt.PreparedStatementName == prepareStmt.Name && bindStmt.PortalName == "" {
return true, nil
}
}
}
return false, nil
}

// rewindAndClosePortal closes the portal in the same way implicit transactions
// do, but also rewinds the stmtBuf to still point to the portal close so that
// the state machine can do its part of the cleanup.
Expand Down
78 changes: 78 additions & 0 deletions pkg/sql/pgwire/testdata/pgtest/portals
Original file line number Diff line number Diff line change
Expand Up @@ -1394,3 +1394,81 @@ ReadyForQuery
{"Type":"DataRow","Values":[{"text":"2"}]}
{"Type":"CommandComplete","CommandTag":"FETCH 2"}
{"Type":"ReadyForQuery","TxStatus":"T"}

# Test for executing/re-preparing unnamed portal. PG docs say:
# "An unnamed portal is destroyed at the end of the transaction, or as soon as
# the next Bind statement specifying the unnamed portal as destination is
# issued. (Note that a simple Query message also destroys the unnamed portal.)"

send
Parse {"Query": "SELECT * FROM generate_series(1, 4)"}
Bind
Execute {"MaxRows": 1}
Parse {"Name": "s17", "Query": "SELECT * FROM generate_series(10, 14)"}
Bind {"PreparedStatement": "s17"}
Execute {"MaxRows": 1}
Sync
----

until
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"10"}]}
{"Type":"PortalSuspended"}
{"Type":"ReadyForQuery","TxStatus":"I"}

# A Describe between Parse and Bind is also allowed.

send
Parse {"Query": "SELECT * FROM generate_series(1, 4)"}
Bind
Execute {"MaxRows": 1}
Parse {"Query": "SELECT n::INT4 FROM generate_series(10, 14) n"}
Describe {"ObjectType": "S"}
Bind
Execute {"MaxRows": 1}
Sync
----

until
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"ParseComplete"}
{"Type":"ParameterDescription","ParameterOIDs":null}
{"Type":"RowDescription","Fields":[{"Name":"n","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0}]}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"10"}]}
{"Type":"PortalSuspended"}
{"Type":"ReadyForQuery","TxStatus":"I"}

send
Parse {"Query": "SELECT * FROM generate_series(1, 4)"}
Bind
Execute {"MaxRows": 1}
Query {"String": "SELECT n::INT4 FROM generate_series(10, 12) n"}
Sync
----

until
ReadyForQuery
----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"1"}]}
{"Type":"PortalSuspended"}
{"Type":"RowDescription","Fields":[{"Name":"n","TableOID":0,"TableAttributeNumber":0,"DataTypeOID":23,"DataTypeSize":4,"TypeModifier":-1,"Format":0}]}
{"Type":"DataRow","Values":[{"text":"10"}]}
{"Type":"DataRow","Values":[{"text":"11"}]}
{"Type":"DataRow","Values":[{"text":"12"}]}
{"Type":"CommandComplete","CommandTag":"SELECT 3"}
{"Type":"ReadyForQuery","TxStatus":"I"}

0 comments on commit bc502ed

Please sign in to comment.