qail-pg 0.27.2

Fastest async PostgreSQL driver - AST to wire protocol, optional io_uring on Linux
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
//! PostgreSQL Encoder (Visitor Pattern)
//!
//! Compiles Qail AST into PostgreSQL wire protocol bytes.
//! This is pure, synchronous computation - no I/O, no async.
//!
//! # Architecture
//!
//! Layer 2 of the QAIL architecture:
//! - Input: Qail (AST)
//! - Output: BytesMut (ready to send over the wire)
//!
//! The async I/O layer (Layer 3) consumes these bytes.

use super::EncodeError;
use bytes::BytesMut;

/// Takes a Qail and produces wire protocol bytes.
/// This is the "Visitor" in the visitor pattern.
pub struct PgEncoder;

impl PgEncoder {
    /// Wire format code for text columns.
    pub const FORMAT_TEXT: i16 = 0;
    /// Wire format code for binary columns.
    pub const FORMAT_BINARY: i16 = 1;

    #[inline(always)]
    fn param_format_wire_len(param_format: i16) -> usize {
        if param_format == Self::FORMAT_TEXT {
            2 // parameter format count = 0 (server default text)
        } else {
            4 // parameter format count = 1 + one format code for all parameters
        }
    }

    #[inline(always)]
    fn encode_param_formats_vec(content: &mut Vec<u8>, param_format: i16) {
        if param_format == Self::FORMAT_TEXT {
            content.extend_from_slice(&0i16.to_be_bytes());
        } else {
            content.extend_from_slice(&1i16.to_be_bytes());
            content.extend_from_slice(&param_format.to_be_bytes());
        }
    }

    #[inline(always)]
    fn encode_param_formats_bytesmut(buf: &mut BytesMut, param_format: i16) {
        if param_format == Self::FORMAT_TEXT {
            buf.extend_from_slice(&0i16.to_be_bytes());
        } else {
            buf.extend_from_slice(&1i16.to_be_bytes());
            buf.extend_from_slice(&param_format.to_be_bytes());
        }
    }

    #[inline(always)]
    fn result_format_wire_len(result_format: i16) -> usize {
        if result_format == Self::FORMAT_TEXT {
            2 // result format count = 0
        } else {
            4 // result format count = 1 + one format code
        }
    }

    #[inline(always)]
    fn encode_result_formats_vec(content: &mut Vec<u8>, result_format: i16) {
        if result_format == Self::FORMAT_TEXT {
            content.extend_from_slice(&0i16.to_be_bytes());
        } else {
            content.extend_from_slice(&1i16.to_be_bytes());
            content.extend_from_slice(&result_format.to_be_bytes());
        }
    }

    #[inline(always)]
    fn encode_result_formats_bytesmut(buf: &mut BytesMut, result_format: i16) {
        if result_format == Self::FORMAT_TEXT {
            buf.extend_from_slice(&0i16.to_be_bytes());
        } else {
            buf.extend_from_slice(&1i16.to_be_bytes());
            buf.extend_from_slice(&result_format.to_be_bytes());
        }
    }

    #[inline(always)]
    fn content_len_to_wire_len(content_len: usize) -> Result<i32, EncodeError> {
        let total = content_len
            .checked_add(4)
            .ok_or(EncodeError::MessageTooLarge(usize::MAX))?;
        i32::try_from(total).map_err(|_| EncodeError::MessageTooLarge(total))
    }

    #[inline(always)]
    fn usize_to_i16(n: usize) -> Result<i16, EncodeError> {
        i16::try_from(n).map_err(|_| EncodeError::TooManyParameters(n))
    }

    #[inline(always)]
    fn usize_to_i32(n: usize) -> Result<i32, EncodeError> {
        i32::try_from(n).map_err(|_| EncodeError::MessageTooLarge(n))
    }

    #[inline(always)]
    fn has_nul(s: &str) -> bool {
        s.as_bytes().contains(&0)
    }

    /// Fallible simple-query encoder.
    pub fn try_encode_query_string(sql: &str) -> Result<BytesMut, EncodeError> {
        if Self::has_nul(sql) {
            return Err(EncodeError::NullByte);
        }

        let mut buf = BytesMut::new();
        let content_len = sql.len() + 1; // +1 for null terminator
        let total_len = Self::content_len_to_wire_len(content_len)?;

        buf.extend_from_slice(b"Q");
        buf.extend_from_slice(&total_len.to_be_bytes());
        buf.extend_from_slice(sql.as_bytes());
        buf.extend_from_slice(&[0]);
        Ok(buf)
    }

    /// Encode a Terminate message to close the connection.
    pub fn encode_terminate() -> BytesMut {
        let mut buf = BytesMut::new();
        buf.extend_from_slice(&[b'X', 0, 0, 0, 4]);
        buf
    }

    /// Encode a Sync message (end of pipeline in extended query protocol).
    pub fn encode_sync() -> BytesMut {
        let mut buf = BytesMut::new();
        buf.extend_from_slice(&[b'S', 0, 0, 0, 4]);
        buf
    }

    // ==================== Extended Query Protocol ====================

    /// Fallible Parse message encoder.
    pub fn try_encode_parse(
        name: &str,
        sql: &str,
        param_types: &[u32],
    ) -> Result<BytesMut, EncodeError> {
        if Self::has_nul(name) || Self::has_nul(sql) {
            return Err(EncodeError::NullByte);
        }
        if param_types.len() > i16::MAX as usize {
            return Err(EncodeError::TooManyParameters(param_types.len()));
        }

        let mut buf = BytesMut::new();
        buf.extend_from_slice(b"P");

        let mut content = Vec::new();
        content.extend_from_slice(name.as_bytes());
        content.push(0);
        content.extend_from_slice(sql.as_bytes());
        content.push(0);
        let param_count = Self::usize_to_i16(param_types.len())?;
        content.extend_from_slice(&param_count.to_be_bytes());
        for &oid in param_types {
            content.extend_from_slice(&oid.to_be_bytes());
        }

        let len = Self::content_len_to_wire_len(content.len())?;
        buf.extend_from_slice(&len.to_be_bytes());
        buf.extend_from_slice(&content);
        Ok(buf)
    }

    /// Encode a Bind message (bind parameters to a prepared statement).
    /// Wire format:
    /// - 'B' (1 byte) - message type
    /// - length (4 bytes)
    /// - portal name (null-terminated)
    /// - statement name (null-terminated)
    /// - format code section (2-4 bytes) - default path uses 0 (all text)
    /// - parameter count (2 bytes)
    /// - for each parameter: length (4 bytes, -1 for NULL), data
    /// - result format count + codes
    ///
    /// # Arguments
    ///
    /// * `portal` — Destination portal name (empty string for unnamed).
    /// * `statement` — Source prepared statement name (empty string for unnamed).
    /// * `params` — Parameter values; `None` entries encode as SQL NULL.
    pub fn encode_bind(
        portal: &str,
        statement: &str,
        params: &[Option<Vec<u8>>],
    ) -> Result<BytesMut, EncodeError> {
        Self::encode_bind_with_result_format(portal, statement, params, Self::FORMAT_TEXT)
    }

    /// Encode a Bind message with explicit result-column format.
    ///
    /// `result_format` is PostgreSQL wire format code: `0 = text`, `1 = binary`.
    /// For `0`, this encodes "result format count = 0" (server default text).
    /// For non-zero codes, this encodes one explicit result format code.
    pub fn encode_bind_with_result_format(
        portal: &str,
        statement: &str,
        params: &[Option<Vec<u8>>],
        result_format: i16,
    ) -> Result<BytesMut, EncodeError> {
        Self::encode_bind_with_formats(portal, statement, params, Self::FORMAT_TEXT, result_format)
    }

    /// Encode a Bind message with explicit parameter and result format codes.
    ///
    /// `param_format` / `result_format` are PostgreSQL wire format codes:
    /// `0 = text`, `1 = binary`.
    ///
    /// For `param_format = 0`, this encodes "parameter format count = 0"
    /// (server default text). For non-zero, this encodes one explicit format
    /// code applied to all parameters.
    pub fn encode_bind_with_formats(
        portal: &str,
        statement: &str,
        params: &[Option<Vec<u8>>],
        param_format: i16,
        result_format: i16,
    ) -> Result<BytesMut, EncodeError> {
        if Self::has_nul(portal) || Self::has_nul(statement) {
            return Err(EncodeError::NullByte);
        }
        if params.len() > i16::MAX as usize {
            return Err(EncodeError::TooManyParameters(params.len()));
        }

        let mut buf = BytesMut::new();

        // Message type 'B'
        buf.extend_from_slice(b"B");

        let mut content = Vec::new();

        // Portal name (null-terminated)
        content.extend_from_slice(portal.as_bytes());
        content.push(0);

        // Statement name (null-terminated)
        content.extend_from_slice(statement.as_bytes());
        content.push(0);

        // Parameter format codes
        Self::encode_param_formats_vec(&mut content, param_format);

        // Parameter count
        let param_count = Self::usize_to_i16(params.len())?;
        content.extend_from_slice(&param_count.to_be_bytes());

        // Parameters
        for param in params {
            match param {
                None => {
                    // NULL: length = -1
                    content.extend_from_slice(&(-1i32).to_be_bytes());
                }
                Some(data) => {
                    let data_len = Self::usize_to_i32(data.len())?;
                    content.extend_from_slice(&data_len.to_be_bytes());
                    content.extend_from_slice(data);
                }
            }
        }

        // Result format codes: default text (count=0) or explicit code.
        Self::encode_result_formats_vec(&mut content, result_format);

        // Length
        let len = Self::content_len_to_wire_len(content.len())?;
        buf.extend_from_slice(&len.to_be_bytes());
        buf.extend_from_slice(&content);

        Ok(buf)
    }

    /// Fallible Execute message encoder.
    pub fn try_encode_execute(portal: &str, max_rows: i32) -> Result<BytesMut, EncodeError> {
        if Self::has_nul(portal) {
            return Err(EncodeError::NullByte);
        }
        if max_rows < 0 {
            return Err(EncodeError::InvalidMaxRows(max_rows));
        }

        let mut buf = BytesMut::new();
        buf.extend_from_slice(b"E");

        let mut content = Vec::new();
        content.extend_from_slice(portal.as_bytes());
        content.push(0);
        content.extend_from_slice(&max_rows.to_be_bytes());

        let len = Self::content_len_to_wire_len(content.len())?;
        buf.extend_from_slice(&len.to_be_bytes());
        buf.extend_from_slice(&content);
        Ok(buf)
    }

    /// Fallible Describe message encoder.
    pub fn try_encode_describe(is_portal: bool, name: &str) -> Result<BytesMut, EncodeError> {
        if Self::has_nul(name) {
            return Err(EncodeError::NullByte);
        }

        let mut buf = BytesMut::new();
        buf.extend_from_slice(b"D");

        let mut content = Vec::new();
        content.push(if is_portal { b'P' } else { b'S' });
        content.extend_from_slice(name.as_bytes());
        content.push(0);

        let len = Self::content_len_to_wire_len(content.len())?;
        buf.extend_from_slice(&len.to_be_bytes());
        buf.extend_from_slice(&content);
        Ok(buf)
    }

    /// Encode a complete extended query pipeline (OPTIMIZED).
    /// This combines Parse + Bind + Execute + Sync in a single buffer.
    /// Zero intermediate allocations - writes directly to pre-sized BytesMut.
    pub fn encode_extended_query(
        sql: &str,
        params: &[Option<Vec<u8>>],
    ) -> Result<BytesMut, EncodeError> {
        Self::encode_extended_query_with_result_format(sql, params, Self::FORMAT_TEXT)
    }

    /// Encode a complete extended query pipeline with explicit result format.
    ///
    /// `result_format` is PostgreSQL wire format code: `0 = text`, `1 = binary`.
    pub fn encode_extended_query_with_result_format(
        sql: &str,
        params: &[Option<Vec<u8>>],
        result_format: i16,
    ) -> Result<BytesMut, EncodeError> {
        Self::encode_extended_query_with_formats(sql, params, Self::FORMAT_TEXT, result_format)
    }

    /// Encode a complete extended query pipeline with explicit parameter and result formats.
    ///
    /// `param_format` / `result_format` are PostgreSQL wire format codes:
    /// `0 = text`, `1 = binary`.
    pub fn encode_extended_query_with_formats(
        sql: &str,
        params: &[Option<Vec<u8>>],
        param_format: i16,
        result_format: i16,
    ) -> Result<BytesMut, EncodeError> {
        if Self::has_nul(sql) {
            return Err(EncodeError::NullByte);
        }
        if params.len() > i16::MAX as usize {
            return Err(EncodeError::TooManyParameters(params.len()));
        }

        // Calculate total size upfront to avoid reallocations
        // Bind: 1 + 4 + 1 + 1 + param_formats + 2 + params_data + result_formats
        // Execute: 1 + 4 + 1 + 4 = 10
        // Sync: 5
        let params_size = params.iter().try_fold(0usize, |acc, p| {
            let field_size = 4usize
                .checked_add(p.as_ref().map_or(0usize, |v| v.len()))
                .ok_or(EncodeError::MessageTooLarge(usize::MAX))?;
            acc.checked_add(field_size)
                .ok_or(EncodeError::MessageTooLarge(usize::MAX))
        })?;
        let param_formats_size = Self::param_format_wire_len(param_format);
        let result_formats_size = Self::result_format_wire_len(result_format);
        let total_size = 9usize
            .checked_add(sql.len())
            .and_then(|v| v.checked_add(9))
            .and_then(|v| v.checked_add(params_size))
            .and_then(|v| v.checked_add(param_formats_size))
            .and_then(|v| v.checked_add(result_formats_size))
            .and_then(|v| v.checked_add(10))
            .and_then(|v| v.checked_add(5))
            .ok_or(EncodeError::MessageTooLarge(usize::MAX))?;

        let mut buf = BytesMut::with_capacity(total_size);

        // ===== PARSE =====
        buf.extend_from_slice(b"P");
        let parse_content_len = 1usize
            .checked_add(sql.len())
            .and_then(|v| v.checked_add(1))
            .and_then(|v| v.checked_add(2))
            .ok_or(EncodeError::MessageTooLarge(usize::MAX))?;
        let parse_len = Self::content_len_to_wire_len(parse_content_len)?;
        buf.extend_from_slice(&parse_len.to_be_bytes());
        buf.extend_from_slice(&[0]); // Unnamed statement
        buf.extend_from_slice(sql.as_bytes());
        buf.extend_from_slice(&[0]); // Null terminator
        buf.extend_from_slice(&0i16.to_be_bytes()); // No param types (infer)

        // ===== BIND =====
        buf.extend_from_slice(b"B");
        let bind_content_len = 1usize
            .checked_add(1)
            .and_then(|v| v.checked_add(2))
            .and_then(|v| v.checked_add(param_formats_size))
            .and_then(|v| v.checked_add(params_size))
            .and_then(|v| v.checked_add(result_formats_size))
            .ok_or(EncodeError::MessageTooLarge(usize::MAX))?;
        let bind_len = Self::content_len_to_wire_len(bind_content_len)?;
        buf.extend_from_slice(&bind_len.to_be_bytes());
        buf.extend_from_slice(&[0]); // Unnamed portal
        buf.extend_from_slice(&[0]); // Unnamed statement
        Self::encode_param_formats_bytesmut(&mut buf, param_format);
        let param_count = Self::usize_to_i16(params.len())?;
        buf.extend_from_slice(&param_count.to_be_bytes());
        for param in params {
            match param {
                None => buf.extend_from_slice(&(-1i32).to_be_bytes()),
                Some(data) => {
                    let data_len = Self::usize_to_i32(data.len())?;
                    buf.extend_from_slice(&data_len.to_be_bytes());
                    buf.extend_from_slice(data);
                }
            }
        }
        Self::encode_result_formats_bytesmut(&mut buf, result_format);

        // ===== EXECUTE =====
        buf.extend_from_slice(b"E");
        buf.extend_from_slice(&9i32.to_be_bytes()); // len = 4 + 1 + 4
        buf.extend_from_slice(&[0]); // Unnamed portal
        buf.extend_from_slice(&0i32.to_be_bytes()); // Unlimited rows

        // ===== SYNC =====
        buf.extend_from_slice(&[b'S', 0, 0, 0, 4]);

        Ok(buf)
    }

    /// Fallible CopyFail encoder.
    pub fn try_encode_copy_fail(reason: &str) -> Result<BytesMut, EncodeError> {
        if Self::has_nul(reason) {
            return Err(EncodeError::NullByte);
        }

        let mut buf = BytesMut::new();
        buf.extend_from_slice(b"f");
        let content_len = reason.len() + 1; // +1 for null terminator
        let len = Self::content_len_to_wire_len(content_len)?;
        buf.extend_from_slice(&len.to_be_bytes());
        buf.extend_from_slice(reason.as_bytes());
        buf.extend_from_slice(&[0]);
        Ok(buf)
    }

    /// Fallible Close encoder.
    pub fn try_encode_close(is_portal: bool, name: &str) -> Result<BytesMut, EncodeError> {
        if Self::has_nul(name) {
            return Err(EncodeError::NullByte);
        }

        let mut buf = BytesMut::new();
        buf.extend_from_slice(b"C");
        let content_len = 1 + name.len() + 1; // type + name + null
        let len = Self::content_len_to_wire_len(content_len)?;
        buf.extend_from_slice(&len.to_be_bytes());
        buf.extend_from_slice(&[if is_portal { b'P' } else { b'S' }]);
        buf.extend_from_slice(name.as_bytes());
        buf.extend_from_slice(&[0]);
        Ok(buf)
    }
}

// ==================== ULTRA-OPTIMIZED Hot Path Encoders ====================
//
// These encoders are designed to beat C:
// - Direct integer writes (no temp arrays, no bounds checks)
// - Borrowed slice params (zero-copy)
// - Single store instructions via BufMut
//

use bytes::BufMut;

/// Zero-copy parameter for ultra-fast encoding.
/// Uses borrowed slices to avoid any allocation or copy.
pub enum Param<'a> {
    /// SQL NULL value.
    Null,
    /// Non-null parameter as a borrowed byte slice.
    Bytes(&'a [u8]),
}

impl PgEncoder {
    /// Direct i32 write - no temp array, no bounds check.
    /// LLVM emits a single store instruction.
    #[inline(always)]
    fn put_i32_be(buf: &mut BytesMut, v: i32) {
        buf.put_i32(v);
    }

    #[inline(always)]
    fn put_i16_be(buf: &mut BytesMut, v: i16) {
        buf.put_i16(v);
    }

    /// Encode Bind message - ULTRA OPTIMIZED.
    /// - Direct integer writes (no temp arrays)
    /// - Borrowed params (zero-copy)
    /// - Single allocation check
    #[inline]
    pub fn encode_bind_ultra<'a>(
        buf: &mut BytesMut,
        statement: &str,
        params: &[Param<'a>],
    ) -> Result<(), EncodeError> {
        Self::encode_bind_ultra_with_result_format(buf, statement, params, Self::FORMAT_TEXT)
    }

    /// Encode Bind message with explicit result-column format.
    #[inline]
    pub fn encode_bind_ultra_with_result_format<'a>(
        buf: &mut BytesMut,
        statement: &str,
        params: &[Param<'a>],
        result_format: i16,
    ) -> Result<(), EncodeError> {
        Self::encode_bind_ultra_with_formats(
            buf,
            statement,
            params,
            Self::FORMAT_TEXT,
            result_format,
        )
    }

    /// Encode Bind message with explicit parameter and result format codes.
    #[inline]
    pub fn encode_bind_ultra_with_formats<'a>(
        buf: &mut BytesMut,
        statement: &str,
        params: &[Param<'a>],
        param_format: i16,
        result_format: i16,
    ) -> Result<(), EncodeError> {
        if Self::has_nul(statement) {
            return Err(EncodeError::NullByte);
        }
        if params.len() > i16::MAX as usize {
            return Err(EncodeError::TooManyParameters(params.len()));
        }

        // Calculate content length upfront
        let params_size = params.iter().try_fold(0usize, |acc, p| {
            let field_size = match p {
                Param::Null => 4usize,
                Param::Bytes(b) => 4usize
                    .checked_add(b.len())
                    .ok_or(EncodeError::MessageTooLarge(usize::MAX))?,
            };
            acc.checked_add(field_size)
                .ok_or(EncodeError::MessageTooLarge(usize::MAX))
        })?;
        let param_formats_size = Self::param_format_wire_len(param_format);
        let result_formats_size = Self::result_format_wire_len(result_format);
        let content_len = 1usize
            .checked_add(statement.len())
            .and_then(|v| v.checked_add(1))
            .and_then(|v| v.checked_add(2))
            .and_then(|v| v.checked_add(param_formats_size))
            .and_then(|v| v.checked_add(params_size))
            .and_then(|v| v.checked_add(result_formats_size))
            .ok_or(EncodeError::MessageTooLarge(usize::MAX))?;
        let wire_len = Self::content_len_to_wire_len(content_len)?;

        // Single reserve - no more allocations
        buf.reserve(1 + 4 + content_len);

        // Message type 'B'
        buf.put_u8(b'B');

        // Length (includes itself) - DIRECT WRITE
        Self::put_i32_be(buf, wire_len);

        // Portal name (empty, null-terminated)
        buf.put_u8(0);

        // Statement name (null-terminated)
        buf.extend_from_slice(statement.as_bytes());
        buf.put_u8(0);

        // Parameter format codes
        Self::encode_param_formats_bytesmut(buf, param_format);

        // Parameter count
        let param_count = Self::usize_to_i16(params.len())?;
        Self::put_i16_be(buf, param_count);

        // Parameters - ZERO COPY from borrowed slices
        for param in params {
            match param {
                Param::Null => Self::put_i32_be(buf, -1),
                Param::Bytes(data) => {
                    let data_len = Self::usize_to_i32(data.len())?;
                    Self::put_i32_be(buf, data_len);
                    buf.extend_from_slice(data);
                }
            }
        }

        // Result format codes
        Self::encode_result_formats_bytesmut(buf, result_format);
        Ok(())
    }

    /// Encode Execute message - ULTRA OPTIMIZED.
    #[inline(always)]
    pub fn encode_execute_ultra(buf: &mut BytesMut) {
        // Execute: 'E' + len(9) + portal("") + max_rows(0)
        // = 'E' 00 00 00 09 00 00 00 00 00
        buf.extend_from_slice(&[b'E', 0, 0, 0, 9, 0, 0, 0, 0, 0]);
    }

    /// Encode Sync message - ULTRA OPTIMIZED.
    #[inline(always)]
    pub fn encode_sync_ultra(buf: &mut BytesMut) {
        buf.extend_from_slice(&[b'S', 0, 0, 0, 4]);
    }

    // Keep the original methods for compatibility

    /// Encode Bind message directly into existing buffer (ZERO ALLOCATION).
    /// This is the hot path optimization - no intermediate Vec allocation.
    #[inline]
    pub fn encode_bind_to(
        buf: &mut BytesMut,
        statement: &str,
        params: &[Option<Vec<u8>>],
    ) -> Result<(), EncodeError> {
        Self::encode_bind_to_with_result_format(buf, statement, params, Self::FORMAT_TEXT)
    }

    /// Encode Bind into existing buffer with explicit result-column format.
    #[inline]
    pub fn encode_bind_to_with_result_format(
        buf: &mut BytesMut,
        statement: &str,
        params: &[Option<Vec<u8>>],
        result_format: i16,
    ) -> Result<(), EncodeError> {
        Self::encode_bind_to_with_formats(buf, statement, params, Self::FORMAT_TEXT, result_format)
    }

    /// Encode Bind into existing buffer with explicit parameter and result formats.
    #[inline]
    pub fn encode_bind_to_with_formats(
        buf: &mut BytesMut,
        statement: &str,
        params: &[Option<Vec<u8>>],
        param_format: i16,
        result_format: i16,
    ) -> Result<(), EncodeError> {
        if Self::has_nul(statement) {
            return Err(EncodeError::NullByte);
        }
        if params.len() > i16::MAX as usize {
            return Err(EncodeError::TooManyParameters(params.len()));
        }

        // Calculate content length upfront
        // portal(1) + statement(len+1) + param_formats + param_count(2)
        // + params_data + result_formats(2 or 4)
        let params_size = params.iter().try_fold(0usize, |acc, p| {
            let field_size = 4usize
                .checked_add(p.as_ref().map_or(0usize, |v| v.len()))
                .ok_or(EncodeError::MessageTooLarge(usize::MAX))?;
            acc.checked_add(field_size)
                .ok_or(EncodeError::MessageTooLarge(usize::MAX))
        })?;
        let param_formats_size = Self::param_format_wire_len(param_format);
        let result_formats_size = Self::result_format_wire_len(result_format);
        let content_len = 1usize
            .checked_add(statement.len())
            .and_then(|v| v.checked_add(1))
            .and_then(|v| v.checked_add(2))
            .and_then(|v| v.checked_add(param_formats_size))
            .and_then(|v| v.checked_add(params_size))
            .and_then(|v| v.checked_add(result_formats_size))
            .ok_or(EncodeError::MessageTooLarge(usize::MAX))?;
        let wire_len = Self::content_len_to_wire_len(content_len)?;

        buf.reserve(1 + 4 + content_len);

        // Message type 'B'
        buf.put_u8(b'B');

        // Length (includes itself) - DIRECT WRITE
        Self::put_i32_be(buf, wire_len);

        // Portal name (empty, null-terminated)
        buf.put_u8(0);

        // Statement name (null-terminated)
        buf.extend_from_slice(statement.as_bytes());
        buf.put_u8(0);

        // Parameter format codes
        Self::encode_param_formats_bytesmut(buf, param_format);

        // Parameter count
        let param_count = Self::usize_to_i16(params.len())?;
        Self::put_i16_be(buf, param_count);

        // Parameters
        for param in params {
            match param {
                None => Self::put_i32_be(buf, -1),
                Some(data) => {
                    let data_len = Self::usize_to_i32(data.len())?;
                    Self::put_i32_be(buf, data_len);
                    buf.extend_from_slice(data);
                }
            }
        }

        // Result format codes
        Self::encode_result_formats_bytesmut(buf, result_format);
        Ok(())
    }

    /// Encode Execute message directly into existing buffer (ZERO ALLOCATION).
    #[inline]
    pub fn encode_execute_to(buf: &mut BytesMut) {
        // Content: portal(1) + max_rows(4) = 5 bytes
        buf.extend_from_slice(&[b'E', 0, 0, 0, 9, 0, 0, 0, 0, 0]);
    }

    /// Encode Sync message directly into existing buffer (ZERO ALLOCATION).
    #[inline]
    pub fn encode_sync_to(buf: &mut BytesMut) {
        buf.extend_from_slice(&[b'S', 0, 0, 0, 4]);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // NOTE: test_encode_simple_query removed - use AstEncoder instead
    #[test]
    fn test_encode_query_string() {
        let sql = "SELECT 1";
        let bytes = PgEncoder::try_encode_query_string(sql).unwrap();

        // Message type
        assert_eq!(bytes[0], b'Q');

        // Length: 4 (length field) + 8 (query) + 1 (null) = 13
        let len = i32::from_be_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]);
        assert_eq!(len, 13);

        // Query content
        assert_eq!(&bytes[5..13], b"SELECT 1");

        // Null terminator
        assert_eq!(bytes[13], 0);
    }

    #[test]
    fn test_encode_terminate() {
        let bytes = PgEncoder::encode_terminate();
        assert_eq!(bytes.as_ref(), &[b'X', 0, 0, 0, 4]);
    }

    #[test]
    fn test_encode_sync() {
        let bytes = PgEncoder::encode_sync();
        assert_eq!(bytes.as_ref(), &[b'S', 0, 0, 0, 4]);
    }

    #[test]
    fn test_encode_parse() {
        let bytes = PgEncoder::try_encode_parse("", "SELECT $1", &[]).unwrap();

        // Message type 'P'
        assert_eq!(bytes[0], b'P');

        // Content should include query
        let content = String::from_utf8_lossy(&bytes[5..]);
        assert!(content.contains("SELECT $1"));
    }

    #[test]
    fn test_encode_bind() {
        let params = vec![
            Some(b"42".to_vec()),
            None, // NULL
        ];
        let bytes = PgEncoder::encode_bind("", "", &params).unwrap();

        // Message type 'B'
        assert_eq!(bytes[0], b'B');

        // Should have proper length
        let len = i32::from_be_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]);
        assert!(len > 4); // At least header
    }

    #[test]
    fn test_encode_bind_binary_result_format() {
        let bytes =
            PgEncoder::encode_bind_with_result_format("", "", &[], PgEncoder::FORMAT_BINARY)
                .unwrap();

        // B + len + portal + statement + param formats + param count + result formats
        // Result format section for binary should be: count=1, format=1.
        assert_eq!(&bytes[11..15], &[0, 1, 0, 1]);
    }

    #[test]
    fn test_encode_bind_binary_param_and_result_format() {
        let bytes = PgEncoder::encode_bind_with_formats(
            "",
            "",
            &[],
            PgEncoder::FORMAT_BINARY,
            PgEncoder::FORMAT_BINARY,
        )
        .unwrap();

        // portal, statement, param formats(count+code), param count, result formats(count+code)
        assert_eq!(&bytes[7..11], &[0, 1, 0, 1]);
        assert_eq!(&bytes[11..13], &[0, 0]);
        assert_eq!(&bytes[13..17], &[0, 1, 0, 1]);
    }

    #[test]
    fn test_encode_execute() {
        let bytes = PgEncoder::try_encode_execute("", 0).unwrap();

        // Message type 'E'
        assert_eq!(bytes[0], b'E');

        // Length: 4 + 1 (null) + 4 (max_rows) = 9
        let len = i32::from_be_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]);
        assert_eq!(len, 9);
    }

    #[test]
    fn test_encode_execute_negative_max_rows_returns_error() {
        let err = PgEncoder::try_encode_execute("", -1).expect_err("must reject negative max_rows");
        assert_eq!(err, EncodeError::InvalidMaxRows(-1));
    }

    #[test]
    fn test_encode_extended_query() {
        let params = vec![Some(b"hello".to_vec())];
        let bytes = PgEncoder::encode_extended_query("SELECT $1", &params).unwrap();

        // Should contain all 4 message types: P, B, E, S
        assert!(bytes.windows(1).any(|w| w == [b'P']));
        assert!(bytes.windows(1).any(|w| w == [b'B']));
        assert!(bytes.windows(1).any(|w| w == [b'E']));
        assert!(bytes.windows(1).any(|w| w == [b'S']));
    }

    #[test]
    fn test_encode_extended_query_binary_result_format() {
        let bytes = PgEncoder::encode_extended_query_with_result_format(
            "SELECT 1",
            &[],
            PgEncoder::FORMAT_BINARY,
        )
        .unwrap();

        let parse_len = i32::from_be_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]) as usize;
        let bind_start = 1 + parse_len;
        assert_eq!(bytes[bind_start], b'B');

        let bind_len = i32::from_be_bytes([
            bytes[bind_start + 1],
            bytes[bind_start + 2],
            bytes[bind_start + 3],
            bytes[bind_start + 4],
        ]);
        assert_eq!(bind_len, 14);

        let bind_content = &bytes[bind_start + 5..bind_start + 1 + bind_len as usize];
        assert_eq!(&bind_content[6..10], &[0, 1, 0, 1]);
    }

    #[test]
    fn test_encode_extended_query_binary_param_and_result_format() {
        let bytes = PgEncoder::encode_extended_query_with_formats(
            "SELECT 1",
            &[],
            PgEncoder::FORMAT_BINARY,
            PgEncoder::FORMAT_BINARY,
        )
        .unwrap();

        let parse_len = i32::from_be_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]) as usize;
        let bind_start = 1 + parse_len;
        let bind_len = i32::from_be_bytes([
            bytes[bind_start + 1],
            bytes[bind_start + 2],
            bytes[bind_start + 3],
            bytes[bind_start + 4],
        ]);
        assert_eq!(bind_len, 16);

        let bind_content = &bytes[bind_start + 5..bind_start + 1 + bind_len as usize];
        assert_eq!(&bind_content[2..6], &[0, 1, 0, 1]);
        assert_eq!(&bind_content[6..8], &[0, 0]);
        assert_eq!(&bind_content[8..12], &[0, 1, 0, 1]);
    }

    #[test]
    fn test_encode_copy_fail() {
        let bytes = PgEncoder::try_encode_copy_fail("bad data").unwrap();
        assert_eq!(bytes[0], b'f');
        let len = i32::from_be_bytes([bytes[1], bytes[2], bytes[3], bytes[4]]);
        assert_eq!(len as usize, 4 + "bad data".len() + 1);
        assert_eq!(&bytes[5..13], b"bad data");
        assert_eq!(bytes[13], 0);
    }

    #[test]
    fn test_encode_close_statement() {
        let bytes = PgEncoder::try_encode_close(false, "my_stmt").unwrap();
        assert_eq!(bytes[0], b'C');
        assert_eq!(bytes[5], b'S'); // Statement type
        assert_eq!(&bytes[6..13], b"my_stmt");
        assert_eq!(bytes[13], 0);
    }

    #[test]
    fn test_encode_close_portal() {
        let bytes = PgEncoder::try_encode_close(true, "").unwrap();
        assert_eq!(bytes[0], b'C');
        assert_eq!(bytes[5], b'P'); // Portal type
        assert_eq!(bytes[6], 0); // Empty name null terminator
    }

    #[test]
    fn test_encode_parse_too_many_param_types_returns_error() {
        let param_types = vec![0u32; (i16::MAX as usize) + 1];
        let err =
            PgEncoder::try_encode_parse("s", "SELECT 1", &param_types).expect_err("must reject");
        assert_eq!(err, EncodeError::TooManyParameters(param_types.len()));
    }

    #[test]
    fn test_encode_bind_to_binary_result_format() {
        let mut buf = BytesMut::new();
        PgEncoder::encode_bind_to_with_result_format(&mut buf, "", &[], PgEncoder::FORMAT_BINARY)
            .unwrap();

        assert_eq!(&buf[11..15], &[0, 1, 0, 1]);
    }

    #[test]
    fn test_encode_bind_to_binary_param_and_result_format() {
        let mut buf = BytesMut::new();
        PgEncoder::encode_bind_to_with_formats(
            &mut buf,
            "",
            &[],
            PgEncoder::FORMAT_BINARY,
            PgEncoder::FORMAT_BINARY,
        )
        .unwrap();

        assert_eq!(&buf[7..11], &[0, 1, 0, 1]);
        assert_eq!(&buf[11..13], &[0, 0]);
        assert_eq!(&buf[13..17], &[0, 1, 0, 1]);
    }

    #[test]
    fn test_encode_bind_ultra_binary_result_format() {
        let mut buf = BytesMut::new();
        PgEncoder::encode_bind_ultra_with_result_format(
            &mut buf,
            "",
            &[],
            PgEncoder::FORMAT_BINARY,
        )
        .unwrap();

        assert_eq!(&buf[11..15], &[0, 1, 0, 1]);
    }

    #[test]
    fn test_encode_bind_ultra_binary_param_and_result_format() {
        let mut buf = BytesMut::new();
        PgEncoder::encode_bind_ultra_with_formats(
            &mut buf,
            "",
            &[],
            PgEncoder::FORMAT_BINARY,
            PgEncoder::FORMAT_BINARY,
        )
        .unwrap();

        assert_eq!(&buf[7..11], &[0, 1, 0, 1]);
        assert_eq!(&buf[11..13], &[0, 0]);
        assert_eq!(&buf[13..17], &[0, 1, 0, 1]);
    }

    #[test]
    fn test_encode_query_string_with_nul_returns_empty() {
        let err =
            PgEncoder::try_encode_query_string("select 1\0select 2").expect_err("must reject NUL");
        assert_eq!(err, EncodeError::NullByte);
    }

    #[test]
    fn test_encode_parse_with_nul_returns_empty() {
        let err = PgEncoder::try_encode_parse("s", "SELECT 1\0", &[]).expect_err("must reject");
        assert_eq!(err, EncodeError::NullByte);
    }

    #[test]
    fn test_encode_bind_with_nul_rejected() {
        let err = PgEncoder::encode_bind_with_result_format("\0", "", &[], PgEncoder::FORMAT_TEXT)
            .expect_err("bind with NUL portal must fail");
        assert_eq!(err, EncodeError::NullByte);
    }

    #[test]
    fn test_encode_extended_query_with_nul_rejected() {
        let err = PgEncoder::encode_extended_query_with_result_format(
            "SELECT 1\0UNION SELECT 2",
            &[],
            PgEncoder::FORMAT_TEXT,
        )
        .expect_err("extended query with NUL SQL must fail");
        assert_eq!(err, EncodeError::NullByte);
    }

    #[test]
    fn test_encode_copy_fail_with_nul_returns_empty() {
        let err = PgEncoder::try_encode_copy_fail("bad\0data").expect_err("must reject");
        assert_eq!(err, EncodeError::NullByte);
    }
}