title: Types and enums description: PostgreSQL result mappings, nullable values, destination annotations, and enum registration. #
Input parameters use the Go types written in the declaration. Result types come from PostgreSQL's statement description and catalog metadata, unless an explicit annotation supplies a destination type.
Default scalar results #
| PostgreSQL | Go |
|---|---|
smallint |
int16 |
integer |
int32 |
bigint |
int64 |
text, varchar, char(n) |
string |
boolean |
bool |
real |
float32 |
double precision |
float64 |
numeric, decimal |
pgtype.Numeric |
interval |
pgtype.Interval |
bytea |
[]byte |
json, jsonb |
json.RawMessage |
date, timestamp, timestamptz |
time.Time |
uuid |
Standard-library uuid.UUID |
| Locally declared enum | Generated enum type, or its Go type override |
UUID results use Go 1.27's standard-library uuid, rather than
github.com/google/uuid. Applications using another UUID type need an explicit
destination mapping or conversion.
Nullability #
Validated NOT NULL columns use ordinary scalar Go types. Nullable columns and
columns on the nullable side of an outer join use sql.Null[T]. Types such as
pgtype.Numeric already carry NULL state and do not get another wrapper.
Computed expressions, CTE outputs, derived sources, and view columns generally
use conservative nullable mappings. A direct count(*) result uses int64;
other aggregates follow their specific nullability rules. SQL aliases determine
Go field names: author_id becomes AuthorID.
Pointers and sql.Null[T] can also be inputs. A nil pointer or invalid
sql.Null[T] supplies SQL NULL; it does not omit the parameter or request a
database default. PostgreSQL enforces the target's constraints at execution.
Arrays #
Scalar arrays use pgtype.Array[sql.Null[T]] when both array and element NULL
state must be preserved. Arrays of native nullable pgx values retain those value
types. ARRAY subqueries with validated non-null elements can produce ordinary
slices; their mappings depend on the projected expression.
A Go slice binds as one driver value. It does not expand into an arbitrary list
of placeholders. Write the SQL operation on that array explicitly, for example
IN (SELECT unnest({ids}::bigint[])) where that SQL shape is supported.
Enums #
schema {
CREATE TYPE book_status AS ENUM (
'draft',
'published',
'out_of_print'
);
}
Generation emits:
type BookStatus string
const (
BookStatusDraft BookStatus = "draft"
BookStatusPublished BookStatus = "published"
BookStatusOutOfPrint BookStatus = "out_of_print"
)
Inferred results across the package use BookStatus; parameters can name it too.
Nullable results use sql.Null[BookStatus]. Labels retain their SQL spelling and
order. PostgreSQL validates labels at execution; a Go string-backed enum can
still hold an invalid value.
Register types on each connection #
Packages declaring enums also generate:
func RegisterTypes(ctx context.Context, conn *pgx.Conn) error
The helper loads runtime OIDs and registers each enum and its array type. Use it before reading enum arrays or using pgx's binary COPY. For a pool:
config, err := pgxpool.ParseConfig(databaseURL)
if err != nil {
return err
}
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
return store.RegisterTypes(ctx, conn)
}
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
return err
}
defer pool.Close()
For a standalone connection, call store.RegisterTypes(ctx, conn) and check the
error. If you already have an AfterConnect hook, call the helper inside it after
your connection setup. The schema must exist first. Set the connection's
search_path before registration when enums use unqualified SQL names.
Registration covers Go type overrides as well as generated types. Repeated calls are allowed. Errors include the SQL type name; earlier registrations may remain on the connection if a later type fails. Packages without enum declarations do not generate the helper. Generated COPY queries remain a separate, unimplemented feature; the helper works with pgx COPY used directly by an application.
Control enum names and mappings #
schema {
@stitchql(name: Status)
CREATE TYPE book_status AS ENUM ('draft', 'published');
@stitchql(type: string)
CREATE TYPE visibility AS ENUM ('public', 'private');
}
name: Status generates that type and prefixed constants. type: string retains
plain Go strings and suppresses enum types and constants. An existing local or
imported Go type can also be used. An enum annotation accepts either name or
type; it does not change the SQL type's name or values. Enum types found only
in the database fall back to strings and are not included in RegisterTypes.
Custom result destinations #
Use annotations to choose a Go field name, scalar destination, or named result group type. An explicit type is used exactly, so it must handle the SQL value's nullability. Use query result methods when a caller wants to map the entire result into its own Go struct.