package pgconn

import (
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
)

const (
	connStatusUninitialized = iota
	connStatusConnecting
	connStatusClosed
	connStatusIdle
	connStatusBusy
)

const wbufLen = 1024
Notice represents a notice response message reported by the PostgreSQL server. Be aware that this is distinct from LISTEN/NOTIFY notification.
Notification is a message received from the PostgreSQL LISTEN/NOTIFY system
type Notification struct {
	PID     uint32 // backend pid that sent the notification
	Channel string // channel from which notification was received
	Payload string
}
DialFunc is a function that can be used to connect to a PostgreSQL server.
type DialFunc func(ctx context.Context, network, addr string) (net.Conn, error)
LookupFunc is a function that can be used to lookup IPs addrs from host.
type LookupFunc func(ctx context.Context, host string) (addrs []string, err error)
BuildFrontendFunc is a function that can be used to create Frontend implementation for connection.
NoticeHandler is a function that can handle notices received from the PostgreSQL server. Notices can be received at any time, usually during handling of a query response. The *PgConn is provided so the handler is aware of the origin of the notice, but it must not invoke any query method. Be aware that this is distinct from LISTEN/NOTIFY notification.
type NoticeHandler func(*PgConn, *Notice)
NotificationHandler is a function that can handle notifications received from the PostgreSQL server. Notifications can be received at any time, usually during handling of a query response. The *PgConn is provided so the handler is aware of the origin of the notice, but it must not invoke any query method. Be aware that this is distinct from a notice event.
Frontend used to receive messages from backend.
type Frontend interface {
	Receive() (pgproto3.BackendMessage, error)
}
PgConn is a low-level PostgreSQL connection handle. It is not safe for concurrent usage.
type PgConn struct {
	conn              net.Conn          // the underlying TCP or unix domain socket connection
	pid               uint32            // backend pid
	secretKey         uint32            // key to use to send a cancel query message to the server
	parameterStatuses map[string]string // parameters that have been reported by the server
	txStatus          byte
	frontend          Frontend

	config *Config

	status byte // One of connStatus* constants

	bufferingReceive    bool
	bufferingReceiveMux sync.Mutex
	bufferingReceiveMsg pgproto3.BackendMessage
	bufferingReceiveErr error

	peekedMsg pgproto3.BackendMessage
Reusable / preallocated resources
Connect establishes a connection to a PostgreSQL server using the environment and connString (in URL or DSN format) to provide configuration. See documention for ParseConfig for details. ctx can be used to cancel a connect attempt.
func ( context.Context,  string) (*PgConn, error) {
	,  := ParseConfig()
	if  != nil {
		return nil, 
	}

	return ConnectConfig(, )
}
Connect establishes a connection to a PostgreSQL server using config. config must have been constructed with ParseConfig. ctx can be used to cancel a connect attempt. If config.Fallbacks are present they will sequentially be tried in case of error establishing network connection. An authentication error will terminate the chain of attempts (like libpq: https://www.postgresql.org/docs/11/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS) and be returned as the error. Otherwise, if all attempts fail the last error is returned.
Default values are set in ParseConfig. Enforce initial creation by ParseConfig rather than setting defaults from zero values.
	if !.createdByParseConfig {
		panic("config must be created by ParseConfig")
	}
ConnectTimeout restricts the whole connection process.
	if .ConnectTimeout != 0 {
		var  context.CancelFunc
		,  = context.WithTimeout(, .ConnectTimeout)
		defer ()
Simplify usage by treating primary config and fallbacks the same.
	 := []*FallbackConfig{
		{
			Host:      .Host,
			Port:      .Port,
			TLSConfig: .TLSConfig,
		},
	}
	 = append(, .Fallbacks...)

	,  = expandWithIPs(, .LookupFunc, )
	if  != nil {
		return nil, &connectError{config: , msg: "hostname resolving error", err: }
	}

	if len() == 0 {
		return nil, &connectError{config: , msg: "hostname resolving error", err: errors.New("ip addr wasn't found")}
	}

	for ,  := range  {
		,  = connect(, , )
		if  == nil {
			break
		} else if ,  := .(*PgError);  {
			 = &connectError{config: , msg: "server error", err: }
			 := "28P01"                    // worng password
			 := "28000" // db does not exist
			if .Code ==  || .Code ==  {
				break
			}
		}
	}

	if  != nil {
		return nil,  // no need to wrap in connectError because it will already be wrapped in all cases except PgError
	}

	if .AfterConnect != nil {
		 := .AfterConnect(, )
		if  != nil {
			.conn.Close()
			return nil, &connectError{config: , msg: "AfterConnect error", err: }
		}
	}

	return , nil
}

func ( context.Context,  LookupFunc,  []*FallbackConfig) ([]*FallbackConfig, error) {
	var  []*FallbackConfig

skip resolve for unix sockets
		if strings.HasPrefix(.Host, "/") {
			 = append(, &FallbackConfig{
				Host:      .Host,
				Port:      .Port,
				TLSConfig: .TLSConfig,
			})

			continue
		}

		,  := (, .Host)
		if  != nil {
			return nil, 
		}

		for ,  := range  {
			 = append(, &FallbackConfig{
				Host:      ,
				Port:      .Port,
				TLSConfig: .TLSConfig,
			})
		}
	}

	return , nil
}

func ( context.Context,  *Config,  *FallbackConfig) (*PgConn, error) {
	 := new(PgConn)
	.config = 
	.wbuf = make([]byte, 0, wbufLen)
	.cleanupDone = make(chan struct{})

	var  error
	,  := NetworkAddress(.Host, .Port)
	.conn,  = .DialFunc(, , )
	if  != nil {
		return nil, &connectError{config: , msg: "dial error", err: }
	}

	.parameterStatuses = make(map[string]string)

	if .TLSConfig != nil {
		if  := .startTLS(.TLSConfig);  != nil {
			.conn.Close()
			return nil, &connectError{config: , msg: "tls error", err: }
		}
	}

	.status = connStatusConnecting
	.contextWatcher = ctxwatch.NewContextWatcher(
		func() { .conn.SetDeadline(time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC)) },
		func() { .conn.SetDeadline(time.Time{}) },
	)

	.contextWatcher.Watch()
	defer .contextWatcher.Unwatch()

	.frontend = .BuildFrontend(.conn, .conn)

	 := pgproto3.StartupMessage{
		ProtocolVersion: pgproto3.ProtocolVersionNumber,
		Parameters:      make(map[string]string),
	}
Copy default run-time params
	for ,  := range .RuntimeParams {
		.Parameters[] = 
	}

	.Parameters["user"] = .User
	if .Database != "" {
		.Parameters["database"] = .Database
	}

	if ,  := .conn.Write(.Encode(.wbuf));  != nil {
		.conn.Close()
		return nil, &connectError{config: , msg: "failed to write startup message", err: }
	}

	for {
		,  := .receiveMessage()
		if  != nil {
			.conn.Close()
			if ,  := .(*PgError);  {
				return nil, 
			}
			return nil, &connectError{config: , msg: "failed to receive message", err: }
		}

		switch msg := .(type) {
		case *pgproto3.BackendKeyData:
			.pid = .ProcessID
			.secretKey = .SecretKey

		case *pgproto3.AuthenticationOk:
		case *pgproto3.AuthenticationCleartextPassword:
			 = .txPasswordMessage(.config.Password)
			if  != nil {
				.conn.Close()
				return nil, &connectError{config: , msg: "failed to write password message", err: }
			}
		case *pgproto3.AuthenticationMD5Password:
			 := "md5" + hexMD5(hexMD5(.config.Password+.config.User)+string(.Salt[:]))
			 = .txPasswordMessage()
			if  != nil {
				.conn.Close()
				return nil, &connectError{config: , msg: "failed to write password message", err: }
			}
		case *pgproto3.AuthenticationSASL:
			 = .scramAuth(.AuthMechanisms)
			if  != nil {
				.conn.Close()
				return nil, &connectError{config: , msg: "failed SASL auth", err: }
			}

		case *pgproto3.ReadyForQuery:
			.status = connStatusIdle
ValidateConnect may execute commands that cause the context to be watched again. Unwatch first to avoid the watch already in progress panic. This is that last thing done by this method so there is no need to restart the watch after ValidateConnect returns. See https://github.com/jackc/pgconn/issues/40.
				.contextWatcher.Unwatch()

				 := .ValidateConnect(, )
				if  != nil {
					.conn.Close()
					return nil, &connectError{config: , msg: "ValidateConnect failed", err: }
				}
			}
			return , nil
handled by ReceiveMessage
		case *pgproto3.ErrorResponse:
			.conn.Close()
			return nil, ErrorResponseToPgError()
		default:
			.conn.Close()
			return nil, &connectError{config: , msg: "received unexpected message", err: }
		}
	}
}

func ( *PgConn) ( *tls.Config) ( error) {
	 = binary.Write(.conn, binary.BigEndian, []int32{8, 80877103})
	if  != nil {
		return
	}

	 := make([]byte, 1)
	if _,  = io.ReadFull(.conn, );  != nil {
		return
	}

	if [0] != 'S' {
		return errors.New("server refused TLS connection")
	}

	.conn = tls.Client(.conn, )

	return nil
}

func ( *PgConn) ( string) ( error) {
	 := &pgproto3.PasswordMessage{Password: }
	_,  = .conn.Write(.Encode(.wbuf))
	return 
}

func ( string) string {
	 := md5.New()
	io.WriteString(, )
	return hex.EncodeToString(.Sum(nil))
}

func ( *PgConn) () chan struct{} {
	if .bufferingReceive {
		panic("BUG: signalMessage when already in progress")
	}

	.bufferingReceive = true
	.bufferingReceiveMux.Lock()

	 := make(chan struct{})
	go func() {
		.bufferingReceiveMsg, .bufferingReceiveErr = .frontend.Receive()
		.bufferingReceiveMux.Unlock()
		close()
	}()

	return 
}
SendBytes sends buf to the PostgreSQL server. It must only be used when the connection is not busy. e.g. It is as error to call SendBytes while reading the result of a query. This is a very low level method that requires deep understanding of the PostgreSQL wire protocol to use correctly. See https://www.postgresql.org/docs/current/protocol.html.
func ( *PgConn) ( context.Context,  []byte) error {
	if  := .lock();  != nil {
		return 
	}
	defer .unlock()

	if  != context.Background() {
		select {
		case <-.Done():
			return &contextAlreadyDoneError{err: .Err()}
		default:
		}
		.contextWatcher.Watch()
		defer .contextWatcher.Unwatch()
	}

	,  := .conn.Write()
	if  != nil {
		.asyncClose()
		return &writeError{err: , safeToRetry:  == 0}
	}

	return nil
}
ReceiveMessage receives one wire protocol message from the PostgreSQL server. It must only be used when the connection is not busy. e.g. It is an error to call ReceiveMessage while reading the result of a query. The messages are still handled by the core pgconn message handling system so receiving a NotificationResponse will still trigger the OnNotification callback. This is a very low level method that requires deep understanding of the PostgreSQL wire protocol to use correctly. See https://www.postgresql.org/docs/current/protocol.html.
func ( *PgConn) ( context.Context) (pgproto3.BackendMessage, error) {
	if  := .lock();  != nil {
		return nil, 
	}
	defer .unlock()

	if  != context.Background() {
		select {
		case <-.Done():
			return nil, &contextAlreadyDoneError{err: .Err()}
		default:
		}
		.contextWatcher.Watch()
		defer .contextWatcher.Unwatch()
	}

	,  := .receiveMessage()
	if  != nil {
		 = &pgconnError{msg: "receive message failed", err: , safeToRetry: true}
	}
	return , 
}
peekMessage peeks at the next message without setting up context cancellation.
func ( *PgConn) () (pgproto3.BackendMessage, error) {
	if .peekedMsg != nil {
		return .peekedMsg, nil
	}

	var  pgproto3.BackendMessage
	var  error
	if .bufferingReceive {
		.bufferingReceiveMux.Lock()
		 = .bufferingReceiveMsg
		 = .bufferingReceiveErr
		.bufferingReceiveMux.Unlock()
		.bufferingReceive = false
If a timeout error happened in the background try the read again.
		if ,  := .(net.Error);  && .Timeout() {
			,  = .frontend.Receive()
		}
	} else {
		,  = .frontend.Receive()
	}

Close on anything other than timeout error - everything else is fatal
		if ,  := .(net.Error); !( && .Timeout()) {
			.asyncClose()
		}

		return nil, 
	}

	.peekedMsg = 
	return , nil
}
receiveMessage receives a message without setting up context cancellation
func ( *PgConn) () (pgproto3.BackendMessage, error) {
	,  := .peekMessage()
Close on anything other than timeout error - everything else is fatal
		if ,  := .(net.Error); !( && .Timeout()) {
			.asyncClose()
		}

		return nil, 
	}
	.peekedMsg = nil

	switch msg := .(type) {
	case *pgproto3.ReadyForQuery:
		.txStatus = .TxStatus
	case *pgproto3.ParameterStatus:
		.parameterStatuses[.Name] = .Value
	case *pgproto3.ErrorResponse:
		if .Severity == "FATAL" {
			.status = connStatusClosed
			.conn.Close() // Ignore error as the connection is already broken and there is already an error to return.
			close(.cleanupDone)
			return nil, ErrorResponseToPgError()
		}
	case *pgproto3.NoticeResponse:
		if .config.OnNotice != nil {
			.config.OnNotice(, noticeResponseToNotice())
		}
	case *pgproto3.NotificationResponse:
		if .config.OnNotification != nil {
			.config.OnNotification(, &Notification{PID: .PID, Channel: .Channel, Payload: .Payload})
		}
	}

	return , nil
}
Conn returns the underlying net.Conn.
func ( *PgConn) () net.Conn {
	return .conn
}
PID returns the backend PID.
func ( *PgConn) () uint32 {
	return .pid
}
TxStatus returns the current TxStatus as reported by the server in the ReadyForQuery message. Possible return values: 'I' - idle / not in transaction 'T' - in a transaction 'E' - in a failed transaction See https://www.postgresql.org/docs/current/protocol-message-formats.html.
func ( *PgConn) () byte {
	return .txStatus
}
SecretKey returns the backend secret key used to send a cancel query message to the server.
func ( *PgConn) () uint32 {
	return .secretKey
}
Close closes a connection. It is safe to call Close on a already closed connection. Close attempts a clean close by sending the exit message to PostgreSQL. However, this could block so ctx is available to limit the time to wait. The underlying net.Conn.Close() will always be called regardless of any other errors.
func ( *PgConn) ( context.Context) error {
	if .status == connStatusClosed {
		return nil
	}
	.status = connStatusClosed

	defer close(.cleanupDone)
	defer .conn.Close()

Close may be called while a cancellable query is in progress. This will most often be triggered by panic when a defer closes the connection (possibly indirectly via a transaction or a connection pool). Unwatch to end any previous watch. It is safe to Unwatch regardless of whether a watch is already is progress. See https://github.com/jackc/pgconn/issues/29
		.contextWatcher.Unwatch()

		.contextWatcher.Watch()
		defer .contextWatcher.Unwatch()
	}
Ignore any errors sending Terminate message and waiting for server to close connection. This mimics the behavior of libpq PQfinish. It calls closePGconn which calls sendTerminateConn which purposefully ignores errors. See https://github.com/jackc/pgx/issues/637
	.conn.Write([]byte{'X', 0, 0, 0, 4})
	.conn.Read(make([]byte, 1))

	return .conn.Close()
}
asyncClose marks the connection as closed and asynchronously sends a cancel query message and closes the underlying connection.
func ( *PgConn) () {
	if .status == connStatusClosed {
		return
	}
	.status = connStatusClosed

	go func() {
		defer close(.cleanupDone)
		defer .conn.Close()

		 := time.Now().Add(time.Second * 15)

		,  := context.WithDeadline(context.Background(), )
		defer ()

		.CancelRequest()

		.conn.SetDeadline()

		.conn.Write([]byte{'X', 0, 0, 0, 4})
		.conn.Read(make([]byte, 1))
	}()
}
CleanupDone returns a channel that will be closed after all underlying resources have been cleaned up. A closed connection is no longer usable, but underlying resources, in particular the net.Conn, may not have finished closing yet. This is because certain errors such as a context cancellation require that the interrupted function call return immediately, but the error may also cause the connection to be closed. In these cases the underlying resources are closed asynchronously. This is only likely to be useful to connection pools. It gives them a way avoid establishing a new connection while an old connection is still being cleaned up and thereby exceeding the maximum pool size.
func ( *PgConn) () chan (struct{}) {
	return .cleanupDone
}
IsClosed reports if the connection has been closed. CleanupDone() can be used to determine if all cleanup has been completed.
func ( *PgConn) () bool {
	return .status < connStatusIdle
}
IsBusy reports if the connection is busy.
func ( *PgConn) () bool {
	return .status == connStatusBusy
}
lock locks the connection.
func ( *PgConn) () error {
	switch .status {
	case connStatusBusy:
		return &connLockError{status: "conn busy"} // This only should be possible in case of an application bug.
	case connStatusClosed:
		return &connLockError{status: "conn closed"}
	case connStatusUninitialized:
		return &connLockError{status: "conn uninitialized"}
	}
	.status = connStatusBusy
	return nil
}

func ( *PgConn) () {
	switch .status {
	case connStatusBusy:
		.status = connStatusIdle
	case connStatusClosed:
	default:
		panic("BUG: cannot unlock unlocked connection") // This should only be possible if there is a bug in this package.
	}
}
ParameterStatus returns the value of a parameter reported by the server (e.g. server_version). Returns an empty string for unknown parameters.
func ( *PgConn) ( string) string {
	return .parameterStatuses[]
}
CommandTag is the result of an Exec function
RowsAffected returns the number of rows affected. If the CommandTag was not for a row affecting command (e.g. "CREATE TABLE") then it returns 0.
Find last non-digit
	 := -1
	for  := len() - 1;  >= 0; -- {
		if [] >= '0' && [] <= '9' {
			 = 
		} else {
			break
		}
	}

	if  == -1 {
		return 0
	}

	var  int64
	for ,  := range [:] {
		 = *10 + int64(-'0')
	}

	return 
}

func ( CommandTag) () string {
	return string()
}
Insert is true if the command tag starts with "INSERT".
func ( CommandTag) () bool {
	return len() >= 6 &&
		[0] == 'I' &&
		[1] == 'N' &&
		[2] == 'S' &&
		[3] == 'E' &&
		[4] == 'R' &&
		[5] == 'T'
}
Update is true if the command tag starts with "UPDATE".
func ( CommandTag) () bool {
	return len() >= 6 &&
		[0] == 'U' &&
		[1] == 'P' &&
		[2] == 'D' &&
		[3] == 'A' &&
		[4] == 'T' &&
		[5] == 'E'
}
Delete is true if the command tag starts with "DELETE".
func ( CommandTag) () bool {
	return len() >= 6 &&
		[0] == 'D' &&
		[1] == 'E' &&
		[2] == 'L' &&
		[3] == 'E' &&
		[4] == 'T' &&
		[5] == 'E'
}
Select is true if the command tag starts with "SELECT".
func ( CommandTag) () bool {
	return len() >= 6 &&
		[0] == 'S' &&
		[1] == 'E' &&
		[2] == 'L' &&
		[3] == 'E' &&
		[4] == 'C' &&
		[5] == 'T'
}

type StatementDescription struct {
	Name      string
	SQL       string
	ParamOIDs []uint32
	Fields    []pgproto3.FieldDescription
}
Prepare creates a prepared statement. If the name is empty, the anonymous prepared statement will be used. This allows Prepare to also to describe statements without creating a server-side prepared statement.
func ( *PgConn) ( context.Context, ,  string,  []uint32) (*StatementDescription, error) {
	if  := .lock();  != nil {
		return nil, 
	}
	defer .unlock()

	if  != context.Background() {
		select {
		case <-.Done():
			return nil, &contextAlreadyDoneError{err: .Err()}
		default:
		}
		.contextWatcher.Watch()
		defer .contextWatcher.Unwatch()
	}

	 := .wbuf
	 = (&pgproto3.Parse{Name: , Query: , ParameterOIDs: }).Encode()
	 = (&pgproto3.Describe{ObjectType: 'S', Name: }).Encode()
	 = (&pgproto3.Sync{}).Encode()

	,  := .conn.Write()
	if  != nil {
		.asyncClose()
		return nil, &writeError{err: , safeToRetry:  == 0}
	}

	 := &StatementDescription{Name: , SQL: }

	var  error

:
	for {
		,  := .receiveMessage()
		if  != nil {
			.asyncClose()
			return nil, 
		}

		switch msg := .(type) {
		case *pgproto3.ParameterDescription:
			.ParamOIDs = make([]uint32, len(.ParameterOIDs))
			copy(.ParamOIDs, .ParameterOIDs)
		case *pgproto3.RowDescription:
			.Fields = make([]pgproto3.FieldDescription, len(.Fields))
			copy(.Fields, .Fields)
		case *pgproto3.ErrorResponse:
			 = ErrorResponseToPgError()
		case *pgproto3.ReadyForQuery:
			break 
		}
	}

	if  != nil {
		return nil, 
	}
	return , nil
}
CancelRequest sends a cancel request to the PostgreSQL server. It returns an error if unable to deliver the cancel request, but lack of an error does not ensure that the query was canceled. As specified in the documentation, there is no way to be sure a query was canceled. See https://www.postgresql.org/docs/11/protocol-flow.html#id-1.10.5.7.9
Open a cancellation request to the same server. The address is taken from the net.Conn directly instead of reusing the connection config. This is important in high availability configurations where fallback connections may be specified or DNS may be used to load balance.
	 := .conn.RemoteAddr()
	,  := .config.DialFunc(, .Network(), .String())
	if  != nil {
		return 
	}
	defer .Close()

	if  != context.Background() {
		 := ctxwatch.NewContextWatcher(
			func() { .SetDeadline(time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC)) },
			func() { .SetDeadline(time.Time{}) },
		)
		.Watch()
		defer .Unwatch()
	}

	 := make([]byte, 16)
	binary.BigEndian.PutUint32([0:4], 16)
	binary.BigEndian.PutUint32([4:8], 80877102)
	binary.BigEndian.PutUint32([8:12], uint32(.pid))
	binary.BigEndian.PutUint32([12:16], uint32(.secretKey))
	_,  = .Write()
	if  != nil {
		return 
	}

	_,  = .Read()
	if  != io.EOF {
		return 
	}

	return nil
}
WaitForNotification waits for a LISTON/NOTIFY message to be received. It returns an error if a notification was not received.
func ( *PgConn) ( context.Context) error {
	if  := .lock();  != nil {
		return 
	}
	defer .unlock()

	if  != context.Background() {
		select {
		case <-.Done():
			return .Err()
		default:
		}

		.contextWatcher.Watch()
		defer .contextWatcher.Unwatch()
	}

	for {
		,  := .receiveMessage()
		if  != nil {
			return 
		}

		switch .(type) {
		case *pgproto3.NotificationResponse:
			return nil
		}
	}
}
Exec executes SQL via the PostgreSQL simple query protocol. SQL may contain multiple queries. Execution is implicitly wrapped in a transaction unless a transaction is already in progress or SQL contains transaction control statements. Prefer ExecParams unless executing arbitrary SQL that may contain multiple queries.
func ( *PgConn) ( context.Context,  string) *MultiResultReader {
	if  := .lock();  != nil {
		return &MultiResultReader{
			closed: true,
			err:    ,
		}
	}

	.multiResultReader = MultiResultReader{
		pgConn: ,
		ctx:    ,
	}
	 := &.multiResultReader
	if  != context.Background() {
		select {
		case <-.Done():
			.closed = true
			.err = &contextAlreadyDoneError{err: .Err()}
			.unlock()
			return 
		default:
		}
		.contextWatcher.Watch()
	}

	 := .wbuf
	 = (&pgproto3.Query{String: }).Encode()

	,  := .conn.Write()
	if  != nil {
		.asyncClose()
		.contextWatcher.Unwatch()
		.closed = true
		.err = &writeError{err: , safeToRetry:  == 0}
		.unlock()
		return 
	}

	return 
}
ReceiveResults reads the result that might be returned by Postgres after a SendBytes (e.a. after sending a CopyDone in a copy-both situation). This is a very low level method that requires deep understanding of the PostgreSQL wire protocol to use correctly. See https://www.postgresql.org/docs/current/protocol.html.
func ( *PgConn) ( context.Context) *MultiResultReader {
	if  := .lock();  != nil {
		return &MultiResultReader{
			closed: true,
			err:    ,
		}
	}

	.multiResultReader = MultiResultReader{
		pgConn: ,
		ctx:    ,
	}
	 := &.multiResultReader
	if  != context.Background() {
		select {
		case <-.Done():
			.closed = true
			.err = &contextAlreadyDoneError{err: .Err()}
			.unlock()
			return 
		default:
		}
		.contextWatcher.Watch()
	}

	return 
}
ExecParams executes a command via the PostgreSQL extended query protocol. sql is a SQL command string. It may only contain one query. Parameter substitution is positional using $1, $2, $3, etc. paramValues are the parameter values. It must be encoded in the format given by paramFormats. paramOIDs is a slice of data type OIDs for paramValues. If paramOIDs is nil, the server will infer the data type for all parameters. Any paramOID element that is 0 that will cause the server to infer the data type for that parameter. ExecParams will panic if len(paramOIDs) is not 0, 1, or len(paramValues). paramFormats is a slice of format codes determining for each paramValue column whether it is encoded in text or binary format. If paramFormats is nil all params are text format. ExecParams will panic if len(paramFormats) is not 0, 1, or len(paramValues). resultFormats is a slice of format codes determining for each result column whether it is encoded in text or binary format. If resultFormats is nil all results will be in text format. ResultReader must be closed before PgConn can be used again.
func ( *PgConn) ( context.Context,  string,  [][]byte,  []uint32,  []int16,  []int16) *ResultReader {
	 := .execExtendedPrefix(, )
	if .closed {
		return 
	}

	 := .wbuf
	 = (&pgproto3.Parse{Query: , ParameterOIDs: }).Encode()
	 = (&pgproto3.Bind{ParameterFormatCodes: , Parameters: , ResultFormatCodes: }).Encode()

	.execExtendedSuffix(, )

	return 
}
ExecPrepared enqueues the execution of a prepared statement via the PostgreSQL extended query protocol. paramValues are the parameter values. It must be encoded in the format given by paramFormats. paramFormats is a slice of format codes determining for each paramValue column whether it is encoded in text or binary format. If paramFormats is nil all params are text format. ExecPrepared will panic if len(paramFormats) is not 0, 1, or len(paramValues). resultFormats is a slice of format codes determining for each result column whether it is encoded in text or binary format. If resultFormats is nil all results will be in text format. ResultReader must be closed before PgConn can be used again.
func ( *PgConn) ( context.Context,  string,  [][]byte,  []int16,  []int16) *ResultReader {
	 := .execExtendedPrefix(, )
	if .closed {
		return 
	}

	 := .wbuf
	 = (&pgproto3.Bind{PreparedStatement: , ParameterFormatCodes: , Parameters: , ResultFormatCodes: }).Encode()

	.execExtendedSuffix(, )

	return 
}

func ( *PgConn) ( context.Context,  [][]byte) *ResultReader {
	.resultReader = ResultReader{
		pgConn: ,
		ctx:    ,
	}
	 := &.resultReader

	if  := .lock();  != nil {
		.concludeCommand(nil, )
		.closed = true
		return 
	}

	if len() > math.MaxUint16 {
		.concludeCommand(nil, fmt.Errorf("extended protocol limited to %v parameters", math.MaxUint16))
		.closed = true
		.unlock()
		return 
	}

	if  != context.Background() {
		select {
		case <-.Done():
			.concludeCommand(nil, &contextAlreadyDoneError{err: .Err()})
			.closed = true
			.unlock()
			return 
		default:
		}
		.contextWatcher.Watch()
	}

	return 
}

func ( *PgConn) ( []byte,  *ResultReader) {
	 = (&pgproto3.Describe{ObjectType: 'P'}).Encode()
	 = (&pgproto3.Execute{}).Encode()
	 = (&pgproto3.Sync{}).Encode()

	,  := .conn.Write()
	if  != nil {
		.asyncClose()
		.concludeCommand(nil, &writeError{err: , safeToRetry:  == 0})
		.contextWatcher.Unwatch()
		.closed = true
		.unlock()
		return
	}

	.readUntilRowDescription()
}
CopyTo executes the copy command sql and copies the results to w.
func ( *PgConn) ( context.Context,  io.Writer,  string) (CommandTag, error) {
	if  := .lock();  != nil {
		return nil, 
	}

	if  != context.Background() {
		select {
		case <-.Done():
			.unlock()
			return nil, &contextAlreadyDoneError{err: .Err()}
		default:
		}
		.contextWatcher.Watch()
		defer .contextWatcher.Unwatch()
	}
Send copy to command
	 := .wbuf
	 = (&pgproto3.Query{String: }).Encode()

	,  := .conn.Write()
	if  != nil {
		.asyncClose()
		.unlock()
		return nil, &writeError{err: , safeToRetry:  == 0}
	}
Read results
	var  CommandTag
	var  error
	for {
		,  := .receiveMessage()
		if  != nil {
			.asyncClose()
			return nil, 
		}

		switch msg := .(type) {
		case *pgproto3.CopyDone:
		case *pgproto3.CopyData:
			,  := .Write(.Data)
			if  != nil {
				.asyncClose()
				return nil, 
			}
		case *pgproto3.ReadyForQuery:
			.unlock()
			return , 
		case *pgproto3.CommandComplete:
			 = CommandTag(.CommandTag)
		case *pgproto3.ErrorResponse:
			 = ErrorResponseToPgError()
		}
	}
}
CopyFrom executes the copy command sql and copies all of r to the PostgreSQL server. Note: context cancellation will only interrupt operations on the underlying PostgreSQL network connection. Reads on r could still block.
func ( *PgConn) ( context.Context,  io.Reader,  string) (CommandTag, error) {
	if  := .lock();  != nil {
		return nil, 
	}
	defer .unlock()

	if  != context.Background() {
		select {
		case <-.Done():
			return nil, &contextAlreadyDoneError{err: .Err()}
		default:
		}
		.contextWatcher.Watch()
		defer .contextWatcher.Unwatch()
	}
Send copy to command
	 := .wbuf
	 = (&pgproto3.Query{String: }).Encode()

	,  := .conn.Write()
	if  != nil {
		.asyncClose()
		return nil, &writeError{err: , safeToRetry:  == 0}
	}
Read until copy in response or error.
	var  CommandTag
	var  error
	 := true
	for  {
		,  := .receiveMessage()
		if  != nil {
			.asyncClose()
			return nil, 
		}

		switch msg := .(type) {
		case *pgproto3.CopyInResponse:
			 = false
		case *pgproto3.ErrorResponse:
			 = ErrorResponseToPgError()
		case *pgproto3.ReadyForQuery:
			return , 
		}
	}
Send copy data
	 := make(chan struct{})
	 := make(chan error, 1)
	 := .signalMessage()

	go func() {
		 := make([]byte, 0, 65536)
		 = append(, 'd')
		 := len()

		for {
			,  := .Read([5:cap()])
			if  > 0 {
				 = [0 : +5]
				pgio.SetInt32([:], int32(+4))

				,  := .conn.Write()
Write errors are always fatal, but we can't use asyncClose because we are in a different goroutine.
					.conn.Close()

					 <- 
					return
				}
			}
			if  != nil {
				 <- 
				return
			}

			select {
			case <-:
				return
			default:
			}
		}
	}()

	var  error
	for  == nil &&  == nil {
		select {
		case  = <-:
		case <-:
			,  := .receiveMessage()
			if  != nil {
				.asyncClose()
				return nil, 
			}

			switch msg := .(type) {
			case *pgproto3.ErrorResponse:
				 = ErrorResponseToPgError()
			default:
				 = .signalMessage()
			}
		}
	}
	close()

	 = [:0]
	if  == io.EOF ||  != nil {
		 := &pgproto3.CopyDone{}
		 = .Encode()
	} else {
		 := &pgproto3.CopyFail{Message: .Error()}
		 = .Encode()
	}
	_,  = .conn.Write()
	if  != nil {
		.asyncClose()
		return nil, 
	}
Read results
	for {
		,  := .receiveMessage()
		if  != nil {
			.asyncClose()
			return nil, 
		}

		switch msg := .(type) {
		case *pgproto3.ReadyForQuery:
			return , 
		case *pgproto3.CommandComplete:
			 = CommandTag(.CommandTag)
		case *pgproto3.ErrorResponse:
			 = ErrorResponseToPgError()
		}
	}
}
MultiResultReader is a reader for a command that could return multiple results such as Exec or ExecBatch.
ReadAll reads all available results. Calling ReadAll is mutually exclusive with all other MultiResultReader methods.
func ( *MultiResultReader) () ([]*Result, error) {
	var  []*Result

	for .NextResult() {
		 = append(, .ResultReader().Read())
	}
	 := .Close()

	return , 
}

func ( *MultiResultReader) () (pgproto3.BackendMessage, error) {
	,  := .pgConn.receiveMessage()

	if  != nil {
		.pgConn.contextWatcher.Unwatch()
		.err = 
		.closed = true
		.pgConn.asyncClose()
		return nil, .err
	}

	switch msg := .(type) {
	case *pgproto3.ReadyForQuery:
		.pgConn.contextWatcher.Unwatch()
		.closed = true
		.pgConn.unlock()
	case *pgproto3.ErrorResponse:
		.err = ErrorResponseToPgError()
	}

	return , nil
}
NextResult returns advances the MultiResultReader to the next result and returns true if a result is available.
func ( *MultiResultReader) () bool {
	for !.closed && .err == nil {
		,  := .receiveMessage()
		if  != nil {
			return false
		}

		switch msg := .(type) {
		case *pgproto3.RowDescription:
			.pgConn.resultReader = ResultReader{
				pgConn:            .pgConn,
				multiResultReader: ,
				ctx:               .ctx,
				fieldDescriptions: .Fields,
			}
			.rr = &.pgConn.resultReader
			return true
		case *pgproto3.CommandComplete:
			.pgConn.resultReader = ResultReader{
				commandTag:       CommandTag(.CommandTag),
				commandConcluded: true,
				closed:           true,
			}
			.rr = &.pgConn.resultReader
			return true
		case *pgproto3.EmptyQueryResponse:
			return false
		}
	}

	return false
}
ResultReader returns the current ResultReader.
func ( *MultiResultReader) () *ResultReader {
	return .rr
}
Close closes the MultiResultReader and returns the first error that occurred during the MultiResultReader's use.
func ( *MultiResultReader) () error {
	for !.closed {
		,  := .receiveMessage()
		if  != nil {
			return .err
		}
	}

	return .err
}
Result is the saved query response that is returned by calling Read on a ResultReader.
Read saves the query response to a Result.
func ( *ResultReader) () *Result {
	 := &Result{}

	for .NextRow() {
		if .FieldDescriptions == nil {
			.FieldDescriptions = make([]pgproto3.FieldDescription, len(.FieldDescriptions()))
			copy(.FieldDescriptions, .FieldDescriptions())
		}

		 := make([][]byte, len(.Values()))
		copy(, .Values())
		.Rows = append(.Rows, )
	}

	.CommandTag, .Err = .Close()

	return 
}
NextRow advances the ResultReader to the next row and returns true if a row is available.
func ( *ResultReader) () bool {
	for !.commandConcluded {
		,  := .receiveMessage()
		if  != nil {
			return false
		}

		switch msg := .(type) {
		case *pgproto3.DataRow:
			.rowValues = .Values
			return true
		}
	}

	return false
}
FieldDescriptions returns the field descriptions for the current result set. The returned slice is only valid until the ResultReader is closed.
Values returns the current row data. NextRow must have been previously been called. The returned [][]byte is only valid until the next NextRow call or the ResultReader is closed. However, the underlying byte data is safe to retain a reference to and mutate.
func ( *ResultReader) () [][]byte {
	return .rowValues
}
Close consumes any remaining result data and returns the command tag or error.
func ( *ResultReader) () (CommandTag, error) {
	if .closed {
		return .commandTag, .err
	}
	.closed = true

	for !.commandConcluded {
		,  := .receiveMessage()
		if  != nil {
			return nil, .err
		}
	}

	if .multiResultReader == nil {
		for {
			,  := .receiveMessage()
			if  != nil {
				return nil, .err
			}

Detect a deferred constraint violation where the ErrorResponse is sent after CommandComplete.
readUntilRowDescription ensures the ResultReader's fieldDescriptions are loaded. It does not return an error as any error will be stored in the ResultReader.
Peek before receive to avoid consuming a DataRow if the result set does not include a RowDescription method. This should never happen under normal pgconn usage, but it is possible if SendBytes and ReceiveResults are manually used to construct a query that does not issue a describe statement.
		,  := .pgConn.peekMessage()
		if ,  := .(*pgproto3.DataRow);  {
			return
		}
Consume the message
		, _ = .receiveMessage()
		if ,  := .(*pgproto3.RowDescription);  {
			return
		}
	}
}

func ( *ResultReader) () ( pgproto3.BackendMessage,  error) {
	if .multiResultReader == nil {
		,  = .pgConn.receiveMessage()
	} else {
		,  = .multiResultReader.receiveMessage()
	}

	if  != nil {
		.concludeCommand(nil, )
		.pgConn.contextWatcher.Unwatch()
		.closed = true
		if .multiResultReader == nil {
			.pgConn.asyncClose()
		}

		return nil, .err
	}

	switch msg := .(type) {
	case *pgproto3.RowDescription:
		.fieldDescriptions = .Fields
	case *pgproto3.CommandComplete:
		.concludeCommand(CommandTag(.CommandTag), nil)
	case *pgproto3.EmptyQueryResponse:
		.concludeCommand(nil, nil)
	case *pgproto3.ErrorResponse:
		.concludeCommand(nil, ErrorResponseToPgError())
	}

	return , nil
}

Keep the first error that is recorded. Store the error before checking if the command is already concluded to allow for receiving an error after CommandComplete but before ReadyForQuery.
	if  != nil && .err == nil {
		.err = 
	}

	if .commandConcluded {
		return
	}

	.commandTag = 
	.rowValues = nil
	.commandConcluded = true
}
Batch is a collection of queries that can be sent to the PostgreSQL server in a single round-trip.
type Batch struct {
	buf []byte
}
ExecParams appends an ExecParams command to the batch. See PgConn.ExecParams for parameter descriptions.
func ( *Batch) ( string,  [][]byte,  []uint32,  []int16,  []int16) {
	.buf = (&pgproto3.Parse{Query: , ParameterOIDs: }).Encode(.buf)
	.ExecPrepared("", , , )
}
ExecPrepared appends an ExecPrepared e command to the batch. See PgConn.ExecPrepared for parameter descriptions.
func ( *Batch) ( string,  [][]byte,  []int16,  []int16) {
	.buf = (&pgproto3.Bind{PreparedStatement: , ParameterFormatCodes: , Parameters: , ResultFormatCodes: }).Encode(.buf)
	.buf = (&pgproto3.Describe{ObjectType: 'P'}).Encode(.buf)
	.buf = (&pgproto3.Execute{}).Encode(.buf)
}
ExecBatch executes all the queries in batch in a single round-trip. Execution is implicitly transactional unless a transaction is already in progress or SQL contains transaction control statements.
func ( *PgConn) ( context.Context,  *Batch) *MultiResultReader {
	if  := .lock();  != nil {
		return &MultiResultReader{
			closed: true,
			err:    ,
		}
	}

	.multiResultReader = MultiResultReader{
		pgConn: ,
		ctx:    ,
	}
	 := &.multiResultReader

	if  != context.Background() {
		select {
		case <-.Done():
			.closed = true
			.err = &contextAlreadyDoneError{err: .Err()}
			.unlock()
			return 
		default:
		}
		.contextWatcher.Watch()
	}

	.buf = (&pgproto3.Sync{}).Encode(.buf)
A large batch can deadlock without concurrent reading and writing. If the Write fails the underlying net.Conn is closed. This is all that can be done without introducing a race condition or adding a concurrent safe communication channel to relay the error back. The practical effect of this is that the underlying Write error is not reported. The error the code reading the batch results receives will be a closed connection error. See https://github.com/jackc/pgx/issues/374.
	go func() {
		,  := .conn.Write(.buf)
		if  != nil {
			.conn.Close()
		}
	}()

	return 
}
EscapeString escapes a string such that it can safely be interpolated into a SQL command string. It does not include the surrounding single quotes. The current implementation requires that standard_conforming_strings=on and client_encoding="UTF8". If these conditions are not met an error will be returned. It is possible these restrictions will be lifted in the future.
func ( *PgConn) ( string) (string, error) {
	if .ParameterStatus("standard_conforming_strings") != "on" {
		return "", errors.New("EscapeString must be run with standard_conforming_strings=on")
	}

	if .ParameterStatus("client_encoding") != "UTF8" {
		return "", errors.New("EscapeString must be run with client_encoding=UTF8")
	}

	return strings.Replace(, "'", "''", -1), nil
}
HijackedConn is the result of hijacking a connection. Due to the necessary exposure of internal implementation details, it is not covered by the semantic versioning compatibility.
type HijackedConn struct {
	Conn              net.Conn          // the underlying TCP or unix domain socket connection
	PID               uint32            // backend pid
	SecretKey         uint32            // key to use to send a cancel query message to the server
	ParameterStatuses map[string]string // parameters that have been reported by the server
	TxStatus          byte
	Frontend          Frontend
	Config            *Config
}
Hijack extracts the internal connection data. pgConn must be in an idle state. pgConn is unusable after hijacking. Hijacking is typically only useful when using pgconn to establish a connection, but taking complete control of the raw connection after that (e.g. a load balancer or proxy). Due to the necessary exposure of internal implementation details, it is not covered by the semantic versioning compatibility.
func ( *PgConn) () (*HijackedConn, error) {
	if  := .lock();  != nil {
		return nil, 
	}
	.status = connStatusClosed

	return &HijackedConn{
		Conn:              .conn,
		PID:               .pid,
		SecretKey:         .secretKey,
		ParameterStatuses: .parameterStatuses,
		TxStatus:          .txStatus,
		Frontend:          .frontend,
		Config:            .config,
	}, nil
}
Construct created a PgConn from an already established connection to a PostgreSQL server. This is the inverse of PgConn.Hijack. The connection must be in an idle state. Due to the necessary exposure of internal implementation details, it is not covered by the semantic versioning compatibility.
func ( *HijackedConn) (*PgConn, error) {
	 := &PgConn{
		conn:              .Conn,
		pid:               .PID,
		secretKey:         .SecretKey,
		parameterStatuses: .ParameterStatuses,
		txStatus:          .TxStatus,
		frontend:          .Frontend,
		config:            .Config,

		status: connStatusIdle,

		wbuf:        make([]byte, 0, wbufLen),
		cleanupDone: make(chan struct{}),
	}

	.contextWatcher = ctxwatch.NewContextWatcher(
		func() { .conn.SetDeadline(time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC)) },
		func() { .conn.SetDeadline(time.Time{}) },
	)

	return , nil