Skip to content

feat: PostgreSQL type OID mapping + encode/decode #127

@FumingPower3925

Description

@FumingPower3925

Summary

Implement PostgreSQL type OID mapping and value encoding/decoding for the most common data types. This bridges Go types to PostgreSQL wire format.

Design

Package: driver/postgres/protocol/types.go

Core Type OIDs

OID PostgreSQL Type Go Type
16 bool bool
20 int8 int64
21 int2 int16
23 int4 int32
25 text string
700 float4 float32
701 float8 float64
1043 varchar string
1082 date time.Time
1114 timestamp time.Time
1184 timestamptz time.Time
17 bytea []byte
2950 uuid [16]byte / string
3802 jsonb []byte / json.RawMessage
1700 numeric string (or big.Rat)
1009 text[] []string
1016 int8[] []int64

Encoding (Go → PG wire)

type Encoder struct{}

func (e *Encoder) Encode(oid uint32, value any, buf []byte) ([]byte, error)
func (e *Encoder) EncodeBinary(oid uint32, value any, buf []byte) ([]byte, error)
func (e *Encoder) EncodeText(oid uint32, value any, buf []byte) ([]byte, error)

Decoding (PG wire → Go)

type Decoder struct{}

func (d *Decoder) Decode(oid uint32, format int16, data []byte) (any, error)
func (d *Decoder) DecodeBinary(oid uint32, data []byte) (any, error)
func (d *Decoder) DecodeText(oid uint32, data []byte) (any, error)

Binary Format Details

  • int2/int4/int8: big-endian fixed-width
  • float4/float8: IEEE 754 big-endian
  • bool: single byte (0/1)
  • text/varchar: raw UTF-8 bytes
  • timestamp: microseconds since 2000-01-01 00:00:00 UTC
  • date: days since 2000-01-01
  • bytea: raw bytes
  • uuid: 16 raw bytes
  • arrays: array header (ndim, flags, element OID) + dimensions + elements

database/sql.Scanner / driver.Valuer Support

  • Types implementing driver.Valuer are encoded via their Value() method
  • Scan targets implementing sql.Scanner receive decoded Go values

Acceptance Criteria

  • OID constants for all common types
  • Text encoding/decoding for all listed types
  • Binary encoding/decoding for int, float, bool, text, bytea, timestamp, uuid
  • Array encoding/decoding (text[] and int8[] at minimum)
  • NULL handling (nil → SQL NULL, SQL NULL → nil)
  • driver.Valuer and sql.Scanner interface support
  • Unit tests with known PG binary representations
  • Benchmark: encode/decode throughput for common types

Dependencies

  • Depends on 123 (wire protocol framing — uses Writer/Reader)

Metadata

Metadata

Labels

area/driverDatabase/cache driver infrastructuredriver/postgresPostgreSQL driver

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions