postgres

package
v0.0.0-...-e2c362c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MoneyOID        uint32 = 790
	TxidSnapshotOID uint32 = 2970
	TsvectorOID     uint32 = 3614
	TsqueryOID      uint32 = 3615
)

OID constants for types not covered by pgtype's built-in map.

Variables

This section is empty.

Functions

func BuildColumnSchemaMap

func BuildColumnSchemaMap(
	fields []pgconn.FieldDescription,
	typeMap *pgtype.Map,
	customTypeMapping map[uint32]CustomDataType,
) (map[string]ColumnSchema, error)

BuildColumnSchemaMap assembles a ColumnSchema map from pgx FieldDescriptions.

func CheckColumnExists

func CheckColumnExists(srcColName, dstColName string, dstColumns map[string]ColumnSchema, dstTableIdentifier string) error

CheckColumnExists returns an error if dstColName is not present in dstColumns.

func CheckColumnTypeCompatibility

func CheckColumnTypeCompatibility(
	srcColName, srcType string, srcTypeMod int32,
	dstColName, dstType string, dstTypeMod int32,
	dstTableIdentifier string,
) error

CheckColumnTypeCompatibility returns an error if the destination column type is not compatible with the source column type.

Compatibility rules (using pg_type.typname values, e.g. "int4", "varchar"):

  • Exact type match: always compatible, subject to typmod rules below.
  • varchar / bpchar → text: always compatible (text is a superset).
  • Integer promotion: destination may be a wider integer (int2 → int4 → int8).
  • varchar / bpchar same type: unbounded source into bounded destination is rejected; bounded source into narrower destination is rejected.
  • numeric same type: both sides constrained with different precision/scale → rejected; either side unbounded (typmod == -1) → compatible.

func CheckSchemaExists

func CheckSchemaExists(ctx context.Context, conn *pgx.Conn, schema string) error

CheckSchemaExists returns an error if the given schema does not exist in the database.

func CheckTableEmpty

func CheckTableEmpty(ctx context.Context, conn *pgx.Conn, tableIdentifier string) error

CheckTableEmpty returns an error if the given table already contains rows.

func GetCustomDataTypes

func GetCustomDataTypes(ctx context.Context, conn *pgx.Conn) (map[uint32]CustomDataType, error)

GetCustomDataTypes fetches all user-defined types from the PostgreSQL catalog.

func GetDestinationTableSchema

func GetDestinationTableSchema(ctx context.Context, conn *pgx.Conn, tableIdentifier string) (map[string]ColumnSchema, error)

GetDestinationTableSchema queries a PostgreSQL table's column type information using SELECT * FROM tableIdentifier LIMIT 0 to get field descriptions with OIDs. Returns pgx.ErrNoRows if the table exists but has no columns.

func OIDToName

func OIDToName(typeMap *pgtype.Map, oid uint32, customTypeMapping map[uint32]CustomDataType) (string, error)

OIDToName resolves a PostgreSQL type OID to its pg_type.typname string. It consults typeMap first, then falls back to a small set of well-known OIDs not covered by pgtype, and finally looks up customTypeMapping for enums/composites.

func ParseNumericTypmod

func ParseNumericTypmod(typmod int32) (int16, int16)

This is to reverse what make_numeric_typmod of Postgres does: https://github.com/postgres/postgres/blob/21912e3c0262e2cfe64856e028799d6927862563/src/backend/utils/adt/numeric.c#L897

func ResolveDestinationColumnName

func ResolveDestinationColumnName(srcColName string, mappings []ColumnMapping) string

ResolveDestinationColumnName returns the destination column name for srcColName, applying the first matching rename from mappings. Falls back to srcColName if none match.

Types

type ColumnMapping

type ColumnMapping struct {
	SourceName      string
	DestinationName string
}

ColumnMapping holds a source→destination column name pair for rename resolution.

type ColumnSchema

type ColumnSchema struct {
	TypeName string
	TypeMod  int32
}

ColumnSchema holds the PostgreSQL type information for a single column, as returned by FieldDescriptions after a "SELECT * FROM table LIMIT 0" query.

type CustomDataType

type CustomDataType struct {
	Name  string
	Type  byte
	Delim byte // non-zero for array types
}

CustomDataType holds metadata for a PostgreSQL custom type (enum, composite, domain, array).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL