-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathcopy_from.go
1358 lines (1272 loc) · 43 KB
/
copy_from.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"bytes"
"context"
"encoding/binary"
"io"
"strings"
"time"
"unicode/utf8"
"unsafe"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/coldataext"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverbase"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/resolver"
"github.com/cockroachdb/cockroach/pkg/sql/colmem"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgwirebase"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/rowcontainer"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/buildutil"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/encoding/csv"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/retry"
"github.com/cockroachdb/cockroach/pkg/util/timeutil/pgdate"
"github.com/cockroachdb/errors"
"github.com/dustin/go-humanize"
)
// CopyBatchRowSizeDefault is the number of rows we insert in one insert
// statement.
const CopyBatchRowSizeDefault = 100
// Vector wise inserts scale much better and this is suitable default.
// Empirically determined limit where we start to see diminishing speedups.
const CopyBatchRowSizeVectorDefault = 32 << 10
// When this many rows are in the copy buffer, they are inserted.
var CopyBatchRowSize = util.ConstantWithMetamorphicTestRange("copy-batch-size", CopyBatchRowSizeDefault, 1, 50_000)
// SetCopyFromBatchSize exports overriding copy batch size for test code.
func SetCopyFromBatchSize(i int) int {
old := CopyBatchRowSize
if buildutil.CrdbTestBuild {
CopyBatchRowSize = i
} else {
// We don't want non-test code mutating globals.
panic("SetCopyFromBatchSize is a test utility that requires crdb_test tag")
}
return old
}
type copyMachineInterface interface {
run(ctx context.Context) error
numInsertedRows() int
// Close closes memory accounts associated with copy.
Close(ctx context.Context)
}
type copyOptions struct {
csvEscape rune
csvExpectHeader bool
delimiter byte
format tree.CopyFormat
null string
}
// TODO(#sql-sessions): copy all pre-condition checks from the PG code
// https://github.com/postgres/postgres/blob/1de58df4fec7325d91f5a8345757314be7ac05da/src/backend/commands/copy.c#L405
func processCopyOptions(
ctx context.Context, p *planner, opts tree.CopyOptions,
) (copyOptions, error) {
c := copyOptions{
format: opts.CopyFormat,
csvExpectHeader: opts.Header,
}
switch c.format {
case tree.CopyFormatText:
c.null = `\N`
c.delimiter = '\t'
case tree.CopyFormatCSV:
c.null = ""
c.delimiter = ','
}
if opts.Header && c.format != tree.CopyFormatCSV {
return c, pgerror.Newf(pgcode.FeatureNotSupported, "HEADER only supported with CSV format")
}
if opts.Quote != nil {
if c.format != tree.CopyFormatCSV {
return c, pgerror.Newf(pgcode.FeatureNotSupported, "QUOTE only supported with CSV format")
}
if opts.Quote.RawString() != `"` {
return c, unimplemented.NewWithIssuef(85574, `QUOTE value %s unsupported`, opts.Quote.RawString())
}
}
exprEval := p.ExprEvaluator("COPY")
if opts.Delimiter != nil {
if c.format == tree.CopyFormatBinary {
return c, pgerror.Newf(
pgcode.Syntax,
"DELIMITER unsupported in BINARY format",
)
}
delim, err := exprEval.String(ctx, opts.Delimiter)
if err != nil {
return c, err
}
if len(delim) != 1 || !utf8.ValidString(delim) {
return c, pgerror.Newf(
pgcode.FeatureNotSupported,
"delimiter must be a single-byte character",
)
}
c.delimiter = delim[0]
}
if opts.Null != nil {
if c.format == tree.CopyFormatBinary {
return c, pgerror.Newf(
pgcode.Syntax,
"NULL unsupported in BINARY format",
)
}
null, err := exprEval.String(ctx, opts.Null)
if err != nil {
return c, err
}
c.null = null
}
if opts.Escape != nil {
s := opts.Escape.RawString()
if len(s) != 1 {
return c, pgerror.Newf(
pgcode.FeatureNotSupported,
"ESCAPE must be a single one-byte character",
)
}
if c.format != tree.CopyFormatCSV {
return c, pgerror.Newf(
pgcode.FeatureNotSupported,
"ESCAPE can only be specified for CSV",
)
}
c.csvEscape, _ = utf8.DecodeRuneInString(s)
}
if opts.Destination != nil {
return c, pgerror.Newf(
pgcode.FeatureNotSupported,
"DESTINATION can only be specified when table is external storage table",
)
}
return c, nil
}
// copyMachine supports the Copy-in pgwire subprotocol (COPY...FROM STDIN). The
// machine is created by the Executor when that statement is executed; from that
// moment on, the machine takes control of the pgwire connection until
// copyMachine.run() returns. During this time, the machine is responsible for
// sending all the protocol messages (including the messages that are usually
// associated with statement results). Errors however are not sent on the
// connection by the machine; the higher layer is responsible for sending them.
//
// Incoming data is buffered and batched; batches are turned into insertNodes
// that are executed. INSERT privileges are required on the destination table.
//
// See: https://www.postgresql.org/docs/current/static/sql-copy.html
// and: https://www.postgresql.org/docs/current/static/protocol-flow.html#PROTOCOL-COPY
type copyMachine struct {
copyFromAST *tree.CopyFrom
table tree.TableExpr
columns tree.NameList
resultColumns colinfo.ResultColumns
expectedHiddenColumnIdxs []int
copyOptions
// textDelim is delimiter converted to a []byte so that we don't have to do that per row.
textDelim []byte
binaryState binaryState
// forceNotNull disables converting values matching the null string to
// NULL. The spec says this is only supported for CSV, and also must specify
// which columns it applies to.
forceNotNull bool
csvInput bytes.Buffer
csvReader *csv.Reader
// buf is used to parse input data into rows. It also accumulates a partial
// row between protocol messages.
buf bytes.Buffer
// rows accumulates a batch of rows to be eventually inserted.
rows rowcontainer.RowContainer
// insertedRows keeps track of the total number of rows inserted by the
// machine.
insertedRows int
// copyMon tracks copy's memory usage.
copyMon *mon.BytesMonitor
// rowsMemAcc accounts for memory used by `rows`.
rowsMemAcc mon.BoundAccount
// bufMemAcc accounts for memory used by `buf`; it is kept in sync with
// buf.Cap().
bufMemAcc mon.BoundAccount
// conn is the pgwire connection from which data is to be read.
conn pgwirebase.Conn
// execInsertPlan is a function to be used to execute the plan (stored in the
// planner) which performs an INSERT.
execInsertPlan func(ctx context.Context, p *planner, res RestrictedCommandResult) error
txnOpt copyTxnOpt
// p is the planner used to plan inserts. preparePlanner() needs to be called
// before preparing each new statement.
p *planner
// parsingEvalCtx is an EvalContext used for the very limited needs to strings
// parsing. Is it not correctly initialized with timestamps, transactions and
// other things that statements more generally need.
parsingEvalCtx *eval.Context
processRows func(ctx context.Context, finalBatch bool) error
scratchRow []tree.Datum
batch coldata.Batch
accHelper colmem.SetAccountingHelper
typs []*types.T
valueHandlers []tree.ValueHandler
ph pgdate.ParseHelper
// For testing we want to be able to override this on the instance level.
copyBatchRowSize int
maxRowMem int64
implicitTxn bool
copyFastPath bool
vectorized bool
}
// newCopyMachine creates a new copyMachine.
func newCopyMachine(
ctx context.Context,
conn pgwirebase.Conn,
n *tree.CopyFrom,
p *planner,
txnOpt copyTxnOpt,
parentMon *mon.BytesMonitor,
implicitTxn bool,
execInsertPlan func(ctx context.Context, p *planner, res RestrictedCommandResult) error,
) (_ *copyMachine, retErr error) {
cOpts, err := processCopyOptions(ctx, p, n.Options)
if err != nil {
return nil, err
}
c := ©Machine{
conn: conn,
copyFromAST: n,
// TODO(georgiah): Currently, insertRows depends on Table and Columns,
// but that dependency can be removed by refactoring it.
table: &n.Table,
columns: n.Columns,
copyOptions: cOpts,
txnOpt: txnOpt,
p: p,
execInsertPlan: execInsertPlan,
implicitTxn: implicitTxn,
}
// We need a planner to do the initial planning, in addition
// to those used for the main execution of the COPY afterwards.
cleanup := c.p.preparePlannerForCopy(ctx, &c.txnOpt, false /* finalBatch */, c.implicitTxn)
defer func() {
retErr = cleanup(ctx, retErr)
}()
c.parsingEvalCtx = c.p.EvalContext()
flags := tree.ObjectLookupFlags{
Required: true,
DesiredObjectKind: tree.TableObject,
DesiredTableDescKind: tree.ResolveRequireTableDesc,
}
_, tableDesc, err := resolver.ResolveExistingTableObject(ctx, c.p, &n.Table, flags)
if err != nil {
return nil, err
}
if err := c.p.CheckPrivilege(ctx, tableDesc, privilege.INSERT); err != nil {
return nil, err
}
cols, err := colinfo.ProcessTargetColumns(tableDesc, n.Columns,
true /* ensureColumns */, false /* allowMutations */)
if err != nil {
return nil, err
}
c.resultColumns = make(colinfo.ResultColumns, len(cols))
typs := make([]*types.T, len(cols))
for i, col := range cols {
c.resultColumns[i] = colinfo.ResultColumn{
Name: col.GetName(),
Typ: col.GetType(),
TableID: tableDesc.GetID(),
PGAttributeNum: uint32(col.GetPGAttributeNum()),
}
typs[i] = col.GetType()
}
c.typs = typs
// If there are no column specifiers and we expect non-visible columns
// to have field data then we have to populate the expectedHiddenColumnIdxs
// field with the columns indexes we expect to be hidden.
if c.p.SessionData().ExpectAndIgnoreNotVisibleColumnsInCopy && len(n.Columns) == 0 {
for i, col := range tableDesc.PublicColumns() {
if col.IsHidden() {
c.expectedHiddenColumnIdxs = append(c.expectedHiddenColumnIdxs, i)
}
}
}
c.initMonitoring(ctx, parentMon)
c.processRows = c.insertRows
c.copyFastPath = c.p.SessionData().CopyFastPathEnabled
// We want to do as many rows as we can keeping things under command mem
// limit. Conservatively target a fraction of kv command size. If we
// exceed this due to large dynamic values we will bail early and
// insert the rows we have so far. Note once the coldata.Batch is full
// we still have all the encoder allocations to make.
c.maxRowMem = kvserverbase.MaxCommandSize.Get(c.p.execCfg.SV()) / 3
if c.canSupportVectorized(tableDesc) {
c.initVectorizedCopy(ctx, typs)
} else {
c.copyBatchRowSize = CopyBatchRowSize
c.vectorized = false
c.rows.Init(c.rowsMemAcc, colinfo.ColTypeInfoFromResCols(c.resultColumns), c.copyBatchRowSize)
c.scratchRow = make(tree.Datums, len(c.resultColumns))
}
return c, nil
}
func (c *copyMachine) canSupportVectorized(table catalog.TableDescriptor) bool {
// TODO(cucaroach): support vectorized binary.
if c.format == tree.CopyFormatBinary {
return false
}
// Vectorized requires avoiding materializing the rows for the optimizer.
if !c.copyFastPath {
return false
}
if c.p.SessionData().VectorizeMode == sessiondatapb.VectorizeOff {
return false
}
// Vectorized COPY doesn't support foreign key checks, no reason it couldn't
// but it doesn't work right now because we don't have the ability to
// hold the results in a bufferNode. We wouldn't want to enable it
// until we were sure that all the checks could be vectorized so the
// "bufferNode" used doesn't just get materialized into a datum based
// row container. I think that requires a vectorized version of lookup
// join. TODO(cucaroach): extend the vectorized insert code to support
// insertFastPath style FK checks.
return len(table.EnforcedOutboundForeignKeys()) == 0
}
func (c *copyMachine) initVectorizedCopy(ctx context.Context, typs []*types.T) {
if buildutil.CrdbTestBuild {
// We have to honor metamorphic default in testing, the transaction
// commit tests rely on it, specifically they override it to
// 1.
c.copyBatchRowSize = CopyBatchRowSize
} else {
batchSize := CopyBatchRowSizeVectorDefault
minBatchSize := 100
// When the coldata.Batch memory usage exceeds maxRowMem we flush the
// rows we have so we want the batch's initial memory usage to
// be smaller so we don't flush every row. We also want to
// leave a comfortable buffer so some dynamic values (ie
// strings, json) don't unnecessarily push us past the limit
// but if we encounter lots of huge dynamic values we do want
// to flush the batch.
targetBatchMemUsage := c.maxRowMem / 2
// Now adjust batch size down based on EstimateBatchSizeBytes. Rather than
// try to unpack EstimateBatchSizeBytes just use a simple
// iterative algorithm to arrive at a reasonable batch size.
// Basically we want something from 100 to maxBatchSize but we
// don't want to have a bunch of unused memory in the
// coldata.Batch so dial it in using EstimateBatchSizeBytes.
for colmem.EstimateBatchSizeBytes(typs, batchSize) > targetBatchMemUsage &&
batchSize > minBatchSize {
batchSize /= 2
}
// Go back up by tenths to make up for 1/2 reduction overshoot.
for colmem.EstimateBatchSizeBytes(typs, batchSize) < targetBatchMemUsage &&
batchSize < CopyBatchRowSizeVectorDefault {
batchSize += batchSize / 10
}
if batchSize > CopyBatchRowSizeVectorDefault {
batchSize = CopyBatchRowSizeVectorDefault
}
// Note its possible we overshot minBatchSize and schema was so wide we
// didn't go back over it. Worst case we end up with a batch size of 50
// but if the schema has that many columns smaller is probably better.
c.copyBatchRowSize = batchSize
}
log.VEventf(ctx, 2, "vectorized copy chose %d for batch size", c.copyBatchRowSize)
c.vectorized = true
factory := coldataext.NewExtendedColumnFactory(c.p.EvalContext())
alloc := colmem.NewLimitedAllocator(ctx, &c.rowsMemAcc, nil /*optional unlimited memory account*/, factory)
alloc.SetMaxBatchSize(c.copyBatchRowSize)
// TODO(cucaroach): Avoid allocating selection vector.
c.accHelper.Init(alloc, c.maxRowMem, typs, false /*alwaysReallocate*/)
// Start with small number of rows, compromise between going too big and
// overallocating memory and avoiding some doubling growth batches.
c.batch, _ = c.accHelper.ResetMaybeReallocate(c.typs, c.batch, 64)
initialMemUsage := c.rowsMemAcc.Used()
if initialMemUsage > c.maxRowMem {
// Some tests set the max raft command size lower and if the metamorphic
// batch size is big enough this can happen. The affect is
// that every row will be flushed which is fine for testing so
// ignore it.
if !buildutil.CrdbTestBuild {
// The logic above failed us, this shouldn't happen, basically this
// means EstimateBatchSizeBytes off by a factor of 2.
panic(errors.AssertionFailedf("EstimateBatchSizeBytes estimated %s for %d row but actual was %s and maxRowMem was %s",
humanize.IBytes(uint64(colmem.EstimateBatchSizeBytes(typs, c.copyBatchRowSize))),
c.copyBatchRowSize,
humanize.IBytes(uint64(initialMemUsage)),
humanize.IBytes(uint64(c.maxRowMem))))
}
}
c.valueHandlers = make([]tree.ValueHandler, len(typs))
for i := range typs {
c.valueHandlers[i] = coldataext.MakeVecHandler(c.batch.ColVec(i))
}
}
func (c *copyMachine) numInsertedRows() int {
if c == nil {
return 0
}
return c.insertedRows
}
func (c *copyMachine) initMonitoring(ctx context.Context, parentMon *mon.BytesMonitor) {
// Create a monitor for the COPY command so it can be tracked separate from transaction or session.
memMetrics := &MemoryMetrics{}
const noteworthyCopyMemoryUsageBytes = 10 << 20
c.copyMon = mon.NewMonitor("copy",
mon.MemoryResource,
memMetrics.CurBytesCount, memMetrics.MaxBytesHist,
0, /* increment */
noteworthyCopyMemoryUsageBytes, c.p.ExecCfg().Settings)
c.copyMon.StartNoReserved(ctx, parentMon)
c.bufMemAcc = c.copyMon.MakeBoundAccount()
c.rowsMemAcc = c.copyMon.MakeBoundAccount()
}
// copyTxnOpt contains information about the transaction in which the copying
// should take place. Can be empty, in which case the copyMachine is responsible
// for managing its own transactions.
type copyTxnOpt struct {
// If set, txn is the transaction within which all writes have to be
// performed. Committing the txn is left to the higher layer. If not set, the
// machine will split writes between multiple transactions that it will
// initiate.
txn *kv.Txn
txnTimestamp time.Time
stmtTimestamp time.Time
initPlanner func(ctx context.Context, p *planner)
resetPlanner func(ctx context.Context, p *planner, txn *kv.Txn, txnTS time.Time, stmtTS time.Time)
}
func (c *copyMachine) Close(ctx context.Context) {
c.rows.Close(ctx)
// TODO(cucaroach): if this isn't close'd the Stop below errors out
// saying there's 10240 bytes left, investigate.
c.rowsMemAcc.Close(ctx)
c.bufMemAcc.Close(ctx)
c.copyMon.Stop(ctx)
}
// run consumes all the copy-in data from the network connection and inserts it
// in the database.
func (c *copyMachine) run(ctx context.Context) error {
format := pgwirebase.FormatText
if c.format == tree.CopyFormatBinary {
format = pgwirebase.FormatBinary
}
// Send the message describing the columns to the client.
if err := c.conn.BeginCopyIn(ctx, c.resultColumns, format); err != nil {
return err
}
// Read from the connection until we see an ClientMsgCopyDone.
readBuf := pgwirebase.MakeReadBuffer(
pgwirebase.ReadBufferOptionWithClusterSettings(&c.p.execCfg.Settings.SV),
)
switch c.format {
case tree.CopyFormatText:
c.textDelim = []byte{c.delimiter}
case tree.CopyFormatCSV:
c.csvInput.Reset()
c.csvReader = csv.NewReader(&c.csvInput)
c.csvReader.Comma = rune(c.delimiter)
c.csvReader.ReuseRecord = true
c.csvReader.FieldsPerRecord = len(c.resultColumns) + len(c.expectedHiddenColumnIdxs)
if c.csvEscape != 0 {
c.csvReader.Escape = c.csvEscape
}
}
Loop:
for {
typ, _, err := readBuf.ReadTypedMsg(c.conn.Rd())
if err != nil {
if pgwirebase.IsMessageTooBigError(err) && typ == pgwirebase.ClientMsgCopyData {
// Slurp the remaining bytes.
_, slurpErr := readBuf.SlurpBytes(c.conn.Rd(), pgwirebase.GetMessageTooBigSize(err))
if slurpErr != nil {
return errors.CombineErrors(err, errors.Wrapf(slurpErr, "error slurping remaining bytes in COPY"))
}
// As per the pgwire spec, we must continue reading until we encounter
// CopyDone or CopyFail. We don't support COPY in the extended
// protocol, so we don't need to look for Sync messages. See
// https://www.postgresql.org/docs/13/protocol-flow.html#PROTOCOL-COPY
for {
typ, _, slurpErr = readBuf.ReadTypedMsg(c.conn.Rd())
if typ == pgwirebase.ClientMsgCopyDone || typ == pgwirebase.ClientMsgCopyFail {
break
}
if slurpErr != nil && !pgwirebase.IsMessageTooBigError(slurpErr) {
return errors.CombineErrors(err, errors.Wrapf(slurpErr, "error slurping remaining bytes in COPY"))
}
_, slurpErr = readBuf.SlurpBytes(c.conn.Rd(), pgwirebase.GetMessageTooBigSize(slurpErr))
if slurpErr != nil {
return errors.CombineErrors(err, errors.Wrapf(slurpErr, "error slurping remaining bytes in COPY"))
}
}
}
return err
}
switch typ {
case pgwirebase.ClientMsgCopyData:
if err := c.processCopyData(
ctx, unsafeUint8ToString(readBuf.Msg), false, /* final */
); err != nil {
return err
}
case pgwirebase.ClientMsgCopyDone:
if err := c.processCopyData(
ctx, "" /* data */, true, /* final */
); err != nil {
return err
}
break Loop
case pgwirebase.ClientMsgCopyFail:
return pgerror.Newf(pgcode.QueryCanceled, "COPY from stdin failed: %s", string(readBuf.Msg))
case pgwirebase.ClientMsgFlush, pgwirebase.ClientMsgSync:
// Spec says to "ignore Flush and Sync messages received during copy-in mode".
default:
return pgwirebase.NewUnrecognizedMsgTypeErr(typ)
}
}
return nil
}
const (
lineDelim = '\n'
endOfData = `\.`
)
// processCopyData buffers incoming data and, once the buffer fills up, inserts
// the accumulated rows.
//
// Args:
// final: If set, buffered data is written even if the buffer is not full.
func (c *copyMachine) processCopyData(ctx context.Context, data string, final bool) (retErr error) {
// At the end, adjust the mem accounting to reflect what's left in the buffer.
defer func() {
if err := c.bufMemAcc.ResizeTo(ctx, int64(c.buf.Cap())); err != nil && retErr == nil {
retErr = err
}
}()
if len(data) > (c.buf.Cap() - c.buf.Len()) {
// If it looks like the buffer will need to allocate to accommodate data,
// account for the memory here. This is not particularly accurate - we don't
// know how much the buffer will actually grow by.
if err := c.bufMemAcc.ResizeTo(ctx, int64(len(data))); err != nil {
return err
}
}
c.buf.WriteString(data)
var readFn func(ctx context.Context, final bool) (brk bool, err error)
switch c.format {
case tree.CopyFormatText:
readFn = c.readTextData
case tree.CopyFormatBinary:
readFn = c.readBinaryData
case tree.CopyFormatCSV:
readFn = c.readCSVData
default:
panic("unknown copy format")
}
for c.buf.Len() > 0 {
brk, err := readFn(ctx, final)
if err != nil {
return err
}
var batchDone bool
if !brk && c.vectorized {
batchDone = c.accHelper.AccountForSet(c.batch.Length() - 1)
}
// If we have a full batch of rows or we have exceeded maxRowMem process
// them. Only set finalBatch to true if this is the last
// CopyData segment AND we have no more data in the buffer.
if len := c.currentBatchSize(); c.rowsMemAcc.Used() > c.maxRowMem || len == c.copyBatchRowSize || batchDone {
if len != c.copyBatchRowSize {
log.VEventf(ctx, 2, "copy batch of %d rows flushing due to memory usage %d > %d", c.batch.Length(), c.rowsMemAcc.Used(), c.maxRowMem)
}
if err := c.processRows(ctx, final && c.buf.Len() == 0); err != nil {
return err
}
}
if brk {
break
}
}
// If we're done, process any remainder, if we're not done let more rows
// accumulate.
if final {
return c.processRows(ctx, final)
}
return nil
}
func (c *copyMachine) currentBatchSize() int {
if c.vectorized {
return c.batch.Length()
}
return c.rows.Len()
}
func (c *copyMachine) readTextData(ctx context.Context, final bool) (brk bool, err error) {
line, err := c.buf.ReadBytes(lineDelim)
if err != nil {
if err != io.EOF {
return false, err
} else if !final {
// Put the incomplete row back in the buffer, to be processed next time.
c.buf.Write(line)
return true, nil
}
} else {
// Remove lineDelim from end.
line = line[:len(line)-1]
// Remove a single '\r' at EOL, if present.
if len(line) > 0 && line[len(line)-1] == '\r' {
line = line[:len(line)-1]
}
}
if c.buf.Len() == 0 && bytes.Equal(line, []byte(`\.`)) {
return true, nil
}
err = c.readTextTuple(ctx, line)
return false, err
}
func (c *copyMachine) readCSVData(ctx context.Context, final bool) (brk bool, err error) {
var fullLine []byte
quoteCharsSeen := 0
// Keep reading lines until we encounter a newline that is not inside a
// quoted field, and therefore signifies the end of a CSV record.
for {
line, err := c.buf.ReadBytes(lineDelim)
fullLine = append(fullLine, line...)
if err != nil {
if err == io.EOF {
if final {
// If we reached EOF and this is the final chunk of input data, then
// try to process it.
break
} else {
// If there's more CopyData, put the incomplete row back in the
// buffer, to be processed next time.
c.buf.Write(fullLine)
return true, nil
}
} else {
return false, err
}
}
// Now we need to calculate if we are have reached the end of the quote.
// If so, break out.
if c.csvEscape == 0 {
// CSV escape is not specified and hence defaults to '"'.¥
// At this point, we know fullLine ends in '\n'. Keep track of the total
// number of QUOTE chars in fullLine -- if it is even, then it means that
// the quotes are balanced and '\n' is not in a quoted field.
// Currently, the QUOTE char and ESCAPE char are both always equal to '"'
// and are not configurable. As per the COPY spec, any appearance of the
// QUOTE or ESCAPE characters in an actual value must be preceded by an
// ESCAPE character. This means that an escaped '"' also results in an even
// number of '"' characters.
// This branch is kept in the interests of "backporting safely" - this
// was the old code. Users who use COPY ... ESCAPE will be the only
// ones hitting the new code below.
quoteCharsSeen += bytes.Count(line, []byte{'"'})
} else {
// Otherwise, we have to do a manual count of double quotes and
// ignore any escape characters preceding quotes for counting.
// For example, if the escape character is '\', we should ignore
// the intermediate quotes in a string such as `"start"\"\"end"`.
skipNextChar := false
for _, ch := range line {
if skipNextChar {
skipNextChar = false
continue
}
if ch == '"' {
quoteCharsSeen++
}
if rune(ch) == c.csvEscape {
skipNextChar = true
}
}
}
if quoteCharsSeen%2 == 0 {
break
}
}
// If we are using COPY FROM and expecting a header, PostgreSQL ignores
// the header row in all circumstances. Do the same.
if c.csvExpectHeader {
c.csvExpectHeader = false
return false, nil
}
c.csvInput.Write(fullLine)
record, err := c.csvReader.Read()
// Look for end of data before checking for errors, since a field count
// error will still return record data.
if len(record) == 1 && !record[0].Quoted && record[0].Val == endOfData && c.buf.Len() == 0 {
return true, nil
}
if err != nil {
return false, pgerror.Wrap(err, pgcode.BadCopyFileFormat,
"read CSV record")
}
err = c.readCSVTuple(ctx, record)
return false, err
}
func (c *copyMachine) maybeIgnoreHiddenColumnsStr(in []csv.Record) []csv.Record {
if len(c.expectedHiddenColumnIdxs) == 0 {
return in
}
ret := in[:0]
nextStartIdx := 0
for _, toIdx := range c.expectedHiddenColumnIdxs {
ret = append(ret, in[nextStartIdx:toIdx]...)
nextStartIdx = toIdx + 1
}
ret = append(ret, in[nextStartIdx:]...)
return ret
}
func (c *copyMachine) readCSVTuple(ctx context.Context, record []csv.Record) error {
if expected := len(c.resultColumns) + len(c.expectedHiddenColumnIdxs); expected != len(record) {
return pgerror.Newf(pgcode.BadCopyFileFormat,
"expected %d values, got %d", expected, len(record))
}
record = c.maybeIgnoreHiddenColumnsStr(record)
if c.vectorized {
vh := c.valueHandlers
for i, s := range record {
// NB: When we implement FORCE_NULL, then quoted values also are allowed
// to be treated as NULL.
if !s.Quoted && s.Val == c.null {
vh[i].Null()
continue
}
if err := tree.ParseAndRequireStringHandler(c.resultColumns[i].Typ, s.Val, c.parsingEvalCtx, vh[i], &c.ph); err != nil {
return err
}
}
c.batch.SetLength(c.batch.Length() + 1)
} else {
datums := c.scratchRow
for i, s := range record {
// NB: When we implement FORCE_NULL, then quoted values also are allowed
// to be treated as NULL.
if !s.Quoted && s.Val == c.null {
datums[i] = tree.DNull
continue
}
d, _, err := tree.ParseAndRequireString(c.resultColumns[i].Typ, s.Val, c.parsingEvalCtx)
if err != nil {
return err
}
datums[i] = d
}
if _, err := c.rows.AddRow(ctx, datums); err != nil {
return err
}
}
return nil
}
func (c *copyMachine) readBinaryData(ctx context.Context, final bool) (brk bool, err error) {
if len(c.expectedHiddenColumnIdxs) > 0 {
return false, pgerror.Newf(
pgcode.FeatureNotSupported,
"expect_and_ignore_not_visible_columns_in_copy not supported in binary mode",
)
}
switch c.binaryState {
case binaryStateNeedSignature:
if readSoFar, err := c.readBinarySignature(); err != nil {
// If this isn't the last message and we saw incomplete data, then
// put it back in the buffer to process more next time.
if !final && (err == io.EOF || err == io.ErrUnexpectedEOF) {
c.buf.Write(readSoFar)
return true, nil
}
return false, err
}
case binaryStateRead:
if readSoFar, err := c.readBinaryTuple(ctx); err != nil {
// If this isn't the last message and we saw incomplete data, then
// put it back in the buffer to process more next time.
if !final && (err == io.EOF || err == io.ErrUnexpectedEOF) {
c.buf.Write(readSoFar)
return true, nil
}
return false, errors.Wrapf(err, "read binary tuple")
}
case binaryStateFoundTrailer:
if !final {
return false, pgerror.New(pgcode.BadCopyFileFormat,
"copy data present after trailer")
}
return true, nil
default:
panic("unknown binary state")
}
return false, nil
}
func (c *copyMachine) readBinaryTuple(ctx context.Context) (readSoFar []byte, err error) {
var fieldCount int16
var fieldCountBytes [2]byte
n, err := io.ReadFull(&c.buf, fieldCountBytes[:])
readSoFar = append(readSoFar, fieldCountBytes[:n]...)
if err != nil {
return readSoFar, err
}
fieldCount = int16(binary.BigEndian.Uint16(fieldCountBytes[:]))
if fieldCount == -1 {
c.binaryState = binaryStateFoundTrailer
return nil, nil
}
if fieldCount < 1 {
return nil, pgerror.Newf(pgcode.BadCopyFileFormat,
"unexpected field count: %d", fieldCount)
}
datums := make(tree.Datums, fieldCount)
var byteCount int32
var byteCountBytes [4]byte
for i := range datums {
n, err := io.ReadFull(&c.buf, byteCountBytes[:])
readSoFar = append(readSoFar, byteCountBytes[:n]...)
if err != nil {
return readSoFar, err
}
byteCount = int32(binary.BigEndian.Uint32(byteCountBytes[:]))
if byteCount == -1 {
datums[i] = tree.DNull
continue
}
data := make([]byte, byteCount)
n, err = io.ReadFull(&c.buf, data)
readSoFar = append(readSoFar, data[:n]...)
if err != nil {
return readSoFar, err
}
d, err := pgwirebase.DecodeDatum(
ctx,
c.parsingEvalCtx,
c.resultColumns[i].Typ,
pgwirebase.FormatBinary,
data,
)
if err != nil {
return nil, pgerror.Wrapf(err, pgcode.BadCopyFileFormat,
"decode datum as %s: %s", c.resultColumns[i].Typ.SQLString(), data)
}
datums[i] = d
}
_, err = c.rows.AddRow(ctx, datums)
if err != nil {
return nil, err
}
return nil, nil
}
func (c *copyMachine) readBinarySignature() ([]byte, error) {
// This is the standard 11-byte binary signature with the flags and
// header 32-bit integers appended since we only support the zero value
// of them.
const binarySignature = "PGCOPY\n\377\r\n\000" + "\x00\x00\x00\x00" + "\x00\x00\x00\x00"
var sig [11 + 8]byte
if n, err := io.ReadFull(&c.buf, sig[:]); err != nil {
return sig[:n], err
}
if !bytes.Equal(sig[:], []byte(binarySignature)) {
return sig[:], pgerror.New(pgcode.BadCopyFileFormat,
"unrecognized binary copy signature")
}
c.binaryState = binaryStateRead
return sig[:], nil
}
// preparePlannerForCopy resets the planner so that it can be used during
// a COPY operation (either COPY to table, or COPY to file).
//
// Depending on how the requesting COPY machine was configured, a new
// transaction might be created.
//
// It returns a cleanup function that needs to be called when we're done with
// the planner (before preparePlannerForCopy is called again). If
// CopyFromAtomicEnabled is false, the cleanup function commits the txn (if it
// hasn't already been committed) or rolls it back depending on whether it is
// passed an error. If an error is passed in to the cleanup function, the same
// error is returned.
func (p *planner) preparePlannerForCopy(
ctx context.Context, txnOpt *copyTxnOpt, finalBatch bool, implicitTxn bool,
) func(context.Context, error) error {
autoCommit := implicitTxn
txnOpt.resetPlanner(ctx, p, txnOpt.txn, txnOpt.txnTimestamp, txnOpt.stmtTimestamp)
if implicitTxn {
if p.SessionData().CopyFromAtomicEnabled {
// If the COPY should be atomic, only the final batch can commit.
autoCommit = finalBatch
}
}
p.autoCommit = autoCommit && !p.execCfg.TestingKnobs.DisableAutoCommitDuringExec
return func(ctx context.Context, prevErr error) (err error) {
// Ensure that we commit the transaction if atomic copy is off. If it's on,
// the conn executor will commit the transaction.
if implicitTxn && !p.SessionData().CopyFromAtomicEnabled {
if prevErr == nil {
// Ensure that the txn is committed if the copyMachine is in charge of
// committing its transactions and the execution didn't already commit it
// (through the planner.autoCommit optimization).
if !txnOpt.txn.IsCommitted() {
err = txnOpt.txn.Commit(ctx)
if err != nil {
if rollbackErr := txnOpt.txn.Rollback(ctx); rollbackErr != nil {
log.Eventf(ctx, "rollback failed: %s", rollbackErr)
}
return err
}
}
} else if rollbackErr := txnOpt.txn.Rollback(ctx); rollbackErr != nil {
log.Eventf(ctx, "rollback failed: %s", rollbackErr)
}
// Start the implicit txn for the next batch.