-
Notifications
You must be signed in to change notification settings - Fork 19
/
command.go
596 lines (498 loc) · 18.7 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package wire
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"net"
"strings"
"github.com/jeroenrinzema/psql-wire/codes"
psqlerr "github.com/jeroenrinzema/psql-wire/errors"
"github.com/jeroenrinzema/psql-wire/pkg/buffer"
"github.com/jeroenrinzema/psql-wire/pkg/types"
"github.com/lib/pq/oid"
)
// NewErrUnimplementedMessageType is called whenever an unimplemented message
// type is sent. This error indicates to the client that the sent message cannot
// be processed at this moment in time.
func NewErrUnimplementedMessageType(t types.ClientMessage) error {
err := fmt.Errorf("unimplemented client message type: %d", t)
return psqlerr.WithSeverity(psqlerr.WithCode(err, codes.ConnectionDoesNotExist), psqlerr.LevelFatal)
}
// NewErrUnkownStatement is returned whenever no executable has been found for
// the given name.
func NewErrUnkownStatement(name string) error {
err := fmt.Errorf("unknown executeable: %s", name)
return psqlerr.WithSeverity(psqlerr.WithCode(err, codes.InvalidPreparedStatementDefinition), psqlerr.LevelFatal)
}
// NewErrUndefinedStatement is returned whenever no statement has been defined
// within the incoming query.
func NewErrUndefinedStatement() error {
err := errors.New("no statement has been defined")
return psqlerr.WithSeverity(psqlerr.WithCode(err, codes.Syntax), psqlerr.LevelError)
}
// NewErrMultipleCommandsStatements is returned whenever multiple statements have been
// given within a single query during the extended query protocol.
func NewErrMultipleCommandsStatements() error {
err := errors.New("cannot insert multiple commands into a prepared statement")
return psqlerr.WithSeverity(psqlerr.WithCode(err, codes.Syntax), psqlerr.LevelError)
}
// newErrClientCopyFailed is returned whenever the client aborts a copy operation.
func newErrClientCopyFailed(desc string) error {
err := fmt.Errorf("client aborted copy: %s", desc)
// TODO: What error code should this really be?
return psqlerr.WithSeverity(psqlerr.WithCode(err, codes.Uncategorized), psqlerr.LevelError)
}
type Session struct {
*Server
Statements StatementCache
Portals PortalCache
}
// consumeCommands consumes incoming commands sent over the Postgres wire connection.
// Commands consumed from the connection are returned through a go channel.
// Responses for the given message type are written back to the client.
// This method keeps consuming messages until the client issues a close message
// or the connection is terminated.
func (srv *Session) consumeCommands(ctx context.Context, conn net.Conn, reader *buffer.Reader, writer *buffer.Writer) error {
srv.logger.Debug("ready for query... starting to consume commands")
// TODO: Include a value to identify unique connections
//
// include a identification value inside the context that
// could be used to identify connections at a later stage.
err := readyForQuery(writer, types.ServerIdle)
if err != nil {
return err
}
for {
if err = srv.consumeSingleCommand(ctx, reader, writer, conn); err != nil {
return err
}
}
}
func (srv *Session) consumeSingleCommand(ctx context.Context, reader *buffer.Reader, writer *buffer.Writer, conn net.Conn) error {
t, length, err := reader.ReadTypedMsg()
if err == io.EOF {
return nil
}
// NOTE: we could recover from this scenario
if errors.Is(err, buffer.ErrMessageSizeExceeded) {
err = handleMessageSizeExceeded(reader, writer, err)
if err != nil {
return err
}
return nil
}
if err != nil {
return err
}
if srv.closing.Load() {
return nil
}
// NOTE: we increase the wait group by one in order to make sure that idle
// connections are not blocking a close.
srv.wg.Add(1)
srv.logger.Debug("<- incoming command", slog.Int("length", length), slog.String("type", t.String()))
err = srv.handleCommand(ctx, conn, t, reader, writer)
srv.wg.Done()
if errors.Is(err, io.EOF) {
return nil
}
return err
}
// handleMessageSizeExceeded attempts to unwrap the given error message as
// message size exceeded. The expected message size will be consumed and
// discarded from the given reader. An error message is written to the client
// once the expected message size is read.
//
// The given error is returned if it does not contain an message size exceeded
// type. A fatal error is returned when an unexpected error is returned while
// consuming the expected message size or when attempting to write the error
// message back to the client.
func handleMessageSizeExceeded(reader *buffer.Reader, writer *buffer.Writer, exceeded error) (err error) {
unwrapped, has := buffer.UnwrapMessageSizeExceeded(exceeded)
if !has {
return exceeded
}
err = reader.Slurp(unwrapped.Size)
if err != nil {
return err
}
return ErrorCode(writer, exceeded)
}
// handleCommand handles the given client message. A client message includes a
// message type and reader buffer containing the actual message. The type
// indecates a action executed by the client.
// https://www.postgresql.org/docs/14/protocol-message-formats.html
func (srv *Session) handleCommand(ctx context.Context, conn net.Conn, t types.ClientMessage, reader *buffer.Reader, writer *buffer.Writer) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
switch t {
case types.ClientSimpleQuery:
return srv.handleSimpleQuery(ctx, reader, writer)
case types.ClientExecute:
return srv.handleExecute(ctx, reader, writer)
case types.ClientParse:
return srv.handleParse(ctx, reader, writer)
case types.ClientDescribe:
// The Describe message (portal variant) specifies the name of an
// existing portal (or an empty string for the unnamed portal). The
// response is a RowDescription message describing the rows that will be
// returned by executing the portal; or a NoData message if the portal
// does not contain a query that will return rows; or ErrorResponse if
// there is no such portal.
//
// The Describe message (statement variant) specifies the name of an
// existing prepared statement (or an empty string for the unnamed
// prepared statement). The response is a ParameterDescription message
// describing the parameters needed by the statement, followed by a
// RowDescription message describing the rows that will be returned when
// the statement is eventually executed (or a NoData message if the
// statement will not return rows). ErrorResponse is issued if there is
// no such prepared statement. Note that since Bind has not yet been
// issued, the formats to be used for returned columns are not yet known
// to the backend; the format code fields in the RowDescription message
// will be zeroes in this case.
// https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
return srv.handleDescribe(ctx, reader, writer)
case types.ClientSync:
// TODO: Include the ability to catch sync messages in order to
// close the current transaction.
//
// At completion of each series of extended-query messages, the frontend
// should issue a Sync message. This parameterless message causes the
// backend to close the current transaction if it's not inside a
// BEGIN/COMMIT transaction block (“close” meaning to commit if no
// error, or roll back if error). Then a ReadyForQuery response is
// issued. The purpose of Sync is to provide a resynchronization point
// for error recovery. When an error is detected while processing any
// extended-query message, the backend issues ErrorResponse, then reads
// and discards messages until a Sync is reached, then issues
// ReadyForQuery and returns to normal message processing. (But note
// that no skipping occurs if an error is detected while processing Sync
// — this ensures that there is one and only one ReadyForQuery sent for
// each Sync.)
// https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
return readyForQuery(writer, types.ServerIdle)
case types.ClientBind:
return srv.handleBind(ctx, reader, writer)
case types.ClientFlush:
// TODO: Flush all remaining rows inside connection buffer if
// any are remaining.
//
// The Flush message does not cause any specific
// output to be generated, but forces the backend to deliver any data
// pending in its output buffers. A Flush must be sent after any
// extended-query command except Sync, if the frontend wishes to examine
// the results of that command before issuing more commands. Without
// Flush, messages returned by the backend will be combined into the
// minimum possible number of packets to minimize network overhead.
// https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
return nil
case types.ClientCopyData, types.ClientCopyDone, types.ClientCopyFail:
// We're supposed to ignore these messages, per the protocol spec. This
// state will happen when an error occurs on the server-side during a copy
// operation: the server will send an error and a ready message back to
// the client, and must then ignore further copy messages. See:
// https://github.com/postgres/postgres/blob/6e1dd2773eb60a6ab87b27b8d9391b756e904ac3/src/backend/tcop/postgres.c#L4295
return nil
case types.ClientClose:
// TODO: close the statement or portal
writer.Start(types.ServerCloseComplete) //nolint:errcheck
writer.End() //nolint:errcheck
return nil
case types.ClientTerminate:
err := srv.handleConnTerminate(ctx)
if err != nil {
return err
}
err = conn.Close()
if err != nil {
return err
}
return io.EOF
default:
return ErrorCode(writer, NewErrUnimplementedMessageType(t))
}
}
func (srv *Session) handleSimpleQuery(ctx context.Context, reader *buffer.Reader, writer *buffer.Writer) error {
if srv.parse == nil {
return ErrorCode(writer, NewErrUnimplementedMessageType(types.ClientSimpleQuery))
}
query, err := reader.GetString()
if err != nil {
return err
}
srv.logger.Debug("incoming simple query", slog.String("query", query))
// NOTE: If a completely empty (no contents other than whitespace) query
// string is received, the response is EmptyQueryResponse followed by
// ReadyForQuery.
// https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
if strings.TrimSpace(query) == "" {
writer.Start(types.ServerEmptyQuery)
err = writer.End()
if err != nil {
return err
}
return readyForQuery(writer, types.ServerIdle)
}
statements, err := srv.parse(ctx, query)
if err != nil {
return ErrorCode(writer, err)
}
if len(statements) == 0 {
return ErrorCode(writer, NewErrUndefinedStatement())
}
// NOTE: it is possible to send multiple statements in one simple query.
for index := range statements {
err = statements[index].columns.Define(ctx, writer, nil)
if err != nil {
return ErrorCode(writer, err)
}
err = statements[index].fn(ctx, NewDataWriter(ctx, statements[index].columns, nil, reader, writer), nil)
if err != nil {
return ErrorCode(writer, err)
}
}
return readyForQuery(writer, types.ServerIdle)
}
func (srv *Session) handleParse(ctx context.Context, reader *buffer.Reader, writer *buffer.Writer) error {
if srv.parse == nil || srv.Statements == nil {
return ErrorCode(writer, NewErrUnimplementedMessageType(types.ClientParse))
}
name, err := reader.GetString()
if err != nil {
return err
}
query, err := reader.GetString()
if err != nil {
return err
}
// NOTE: the number of parameter data types specified (can be
// zero). Note that this is not an indication of the number of parameters
// that might appear in the query string, only the number that the frontend
// wants to prespecify types for.
parameters, err := reader.GetUint16()
if err != nil {
return err
}
srv.logger.Debug("predefined parameters", slog.Int("parameters", int(parameters)))
for i := uint16(0); i < parameters; i++ {
// TODO: Specifies the object ID of the parameter data type
//
// Specifies the object ID of the parameter data type. Placing a zero here
// is equivalent to leaving the type unspecified.
// `reader.GetUint32()`
}
statement, err := singleStatement(srv.parse(ctx, query))
if err != nil {
return ErrorCode(writer, err)
}
srv.logger.Debug("incoming extended query", slog.String("query", query), slog.String("name", name), slog.Int("parameters", len(statement.parameters)))
err = srv.Statements.Set(ctx, name, statement)
if err != nil {
return ErrorCode(writer, err)
}
writer.Start(types.ServerParseComplete)
return writer.End()
}
func (srv *Session) handleDescribe(ctx context.Context, reader *buffer.Reader, writer *buffer.Writer) error {
d, err := reader.GetBytes(1)
if err != nil {
return err
}
name, err := reader.GetString()
if err != nil {
return err
}
srv.logger.Debug("incoming describe request", slog.String("type", types.DescribeMessage(d[0]).String()), slog.String("name", name))
switch types.DescribeMessage(d[0]) {
case types.DescribeStatement:
statement, err := srv.Statements.Get(ctx, name)
if err != nil {
return err
}
if statement == nil {
return ErrorCode(writer, errors.New("unknown statement"))
}
err = srv.writeParameterDescription(writer, statement.parameters)
if err != nil {
return err
}
// NOTE: the format codes are not yet known at this point in time.
return srv.writeColumnDescription(ctx, writer, nil, statement.columns)
case types.DescribePortal:
portal, err := srv.Portals.Get(ctx, name)
if err != nil {
return err
}
if portal == nil {
return ErrorCode(writer, errors.New("unknown portal"))
}
return srv.writeColumnDescription(ctx, writer, portal.formats, portal.statement.columns)
}
return ErrorCode(writer, fmt.Errorf("unknown describe command: %s", string(d[0])))
}
// https://www.postgresql.org/docs/15/protocol-message-formats.html
func (srv *Session) writeParameterDescription(writer *buffer.Writer, parameters []oid.Oid) error {
writer.Start(types.ServerParameterDescription)
writer.AddInt16(int16(len(parameters)))
for _, parameter := range parameters {
writer.AddInt32(int32(parameter))
}
return writer.End()
}
// writeColumnDescription attempts to write the statement column descriptions
// back to the writer buffer. Information about the returned columns is written
// to the client.
// https://www.postgresql.org/docs/15/protocol-message-formats.html
func (srv *Session) writeColumnDescription(ctx context.Context, writer *buffer.Writer, formats []FormatCode, columns Columns) error {
if len(columns) == 0 {
writer.Start(types.ServerNoData)
return writer.End()
}
return columns.Define(ctx, writer, formats)
}
func (srv *Session) handleBind(ctx context.Context, reader *buffer.Reader, writer *buffer.Writer) error {
name, err := reader.GetString()
if err != nil {
return err
}
statement, err := reader.GetString()
if err != nil {
return err
}
parameters, err := srv.readParameters(ctx, reader)
if err != nil {
return err
}
formats, err := srv.readColumnTypes(reader)
if err != nil {
return err
}
stmt, err := srv.Statements.Get(ctx, statement)
if err != nil {
return err
}
if stmt == nil {
return NewErrUnkownStatement(statement)
}
err = srv.Portals.Bind(ctx, name, stmt, parameters, formats)
if err != nil {
return err
}
writer.Start(types.ServerBindComplete)
return writer.End()
}
// readParameters attempts to read all incoming parameters from the given
// reader. The parameters are parsed and returned.
// https://www.postgresql.org/docs/14/protocol-message-formats.html
func (srv *Session) readParameters(ctx context.Context, reader *buffer.Reader) ([]Parameter, error) {
// NOTE: read the total amount of parameter format length that will be send
// by the client. This can be zero to indicate that there are no parameters
// or that the parameters all use the default format (text); or one, in
// which case the specified format code is applied to all parameters; or it
// can equal the actual number of parameters.
length, err := reader.GetUint16()
if err != nil {
return nil, err
}
srv.logger.Debug("reading parameters format codes", slog.Uint64("length", uint64(length)))
defaultFormat := TextFormat
formats := make([]FormatCode, length)
for i := uint16(0); i < length; i++ {
format, err := reader.GetUint16()
if err != nil {
return nil, err
}
// NOTE: we have to set the default format code to the given format code
// if only one is given according to the protocol specs. The for loop
// should not be aborted since the formats slice is buffered.
if length == 1 {
defaultFormat = FormatCode(format)
}
formats[i] = FormatCode(format)
}
// NOTE: read the total amount of parameter values that will be send
// by the client.
length, err = reader.GetUint16()
if err != nil {
return nil, err
}
srv.logger.Debug("reading parameters values", slog.Uint64("length", uint64(length)))
parameters := make([]Parameter, length)
for i := 0; i < int(length); i++ {
length, err := reader.GetUint32()
if err != nil {
return nil, err
}
value, err := reader.GetBytes(int(length))
if err != nil {
return nil, err
}
srv.logger.Debug("incoming parameter", slog.String("value", string(value)))
format := defaultFormat
if len(formats) > int(i) {
format = formats[i]
}
parameters[i] = NewParameter(TypeMap(ctx), format, value)
}
return parameters, nil
}
func (srv *Session) readColumnTypes(reader *buffer.Reader) ([]FormatCode, error) {
length, err := reader.GetUint16()
if err != nil {
return nil, err
}
srv.logger.Debug("reading column format codes", slog.Uint64("length", uint64(length)))
columns := make([]FormatCode, length)
for i := uint16(0); i < length; i++ {
format, err := reader.GetUint16()
if err != nil {
return nil, err
}
columns[i] = FormatCode(format)
}
return columns, nil
}
func (srv *Session) handleExecute(ctx context.Context, reader *buffer.Reader, writer *buffer.Writer) error {
if srv.Statements == nil {
return ErrorCode(writer, NewErrUnimplementedMessageType(types.ClientExecute))
}
name, err := reader.GetString()
if err != nil {
return err
}
// TODO: Limit the maximum number of records to be returned.
//
// Maximum number of limit to return, if portal contains a
// query that returns limit (ignored otherwise). Zero denotes “no limit”.
limit, err := reader.GetUint32()
if err != nil {
return err
}
srv.logger.Debug("executing", slog.String("name", name), slog.Uint64("limit", uint64(limit)))
err = srv.Portals.Execute(ctx, name, reader, writer)
if err != nil {
return ErrorCode(writer, err)
}
return nil
}
func (srv *Session) handleConnTerminate(ctx context.Context) error {
if srv.TerminateConn == nil {
return nil
}
return srv.TerminateConn(ctx)
}
func singleStatement(stmts PreparedStatements, err error) (*PreparedStatement, error) {
if err != nil {
return nil, err
}
if len(stmts) > 1 {
return nil, NewErrMultipleCommandsStatements()
}
if len(stmts) == 0 {
return nil, NewErrUndefinedStatement()
}
return stmts[0], nil
}