Skip to content

pgwire: fix for specific cases of unnamed portal execution #83164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"}