pg_extension

package
v0.52.3 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

README

Finding Extension Function Imports

These are commands that can be used to find the functions that an extension imports, so that we know which ones we need to implement for the extension to load.

Windows

On Windows, we make use of dumpbin, which is installed alongside Visual Studio (the full version, not Code). We are generally only interested in the functions under postgres.exe, as the library should load other DLLs as necessary.

dumpbin /imports "C:/Program Files/PostgreSQL/15/lib/LIBRARY_NAME.dll"

Linux

On Linux, we make use of the built-in nm command. We are interested in the U functions that do not have an @ near the end (as those are usually implemented in external libraries).

nm -D -u /usr/lib/postgresql/15/lib/LIBRARY_NAME.so

Documentation

Index

Constants

View Source
const PLATFORM = "LIN"

PLATFORM specifies which platform applies to the current library loader. This will always be a three-letter string.

Variables

This section is empty.

Functions

func Free

func Free[T any](val *T)

Free frees the given pointer from C heap. Generally, this is paired with a pointer returned from Malloc.

func FreeDatum

func FreeDatum(val Datum)

FreeDatum frees the given Datum. Care should be exercised as datums may refer to static memory, and attempting to free static memory will result in a crash.

func FromDatum

func FromDatum[T any](d Datum) *T

FromDatum converts the given datum to the type.

func FromDatumGoBytes

func FromDatumGoBytes(d Datum, n uint) []byte

FromDatumGoBytes converts the given datum to a byte array of length N.

func FromDatumGoString

func FromDatumGoString(d Datum) string

FromDatumGoString converts the given datum to a string.

func LoadExtensions

func LoadExtensions() (map[string]*ExtensionFiles, error)

LoadExtensions loads information for all extensions that are in the extensions directory of a local Postgres installation.

func Malloc

func Malloc[T any]() *T

Malloc allocates the given type within the C heap. These should always be followed up with a Free at some point afterward.

func PostgresDirectories

func PostgresDirectories() (libDir string, extensionDir string, err error)

PostgresDirectories returns the installation directories of a local Postgres instance.

func ZeroMemory

func ZeroMemory[T any](val *T)

ZeroMemory writes all zeroes to the memory location occupied by the given pointer.

Types

type Control

type Control struct {
	Directory      string
	DefaultVersion Version
	Comment        string
	Encoding       string
	ModulePathname string
	Requires       []string
	Superuser      bool
	Trusted        bool
	Relocatable    bool
	Schema         string
	Extra          map[string]string // All entries in here could not be matched to an expected field
}

Control contains the contents of the control file. https://www.postgresql.org/docs/15/extend-extensions.html#id-1.8.3.20.11

type Datum

type Datum uintptr

Datum is a C pointer to some data. Depending on the function being called, it may not be a pointer that should be freed, as some functions return pointers to static memory.

func CallFmgrFunction

func CallFmgrFunction(fn uintptr, args ...NullableDatum) (result Datum, isNotNull bool)

CallFmgrFunction calls the given function and forwards the arguments.

func ToDatum

func ToDatum[T any](val *T) Datum

ToDatum converts the given pointer to a Datum.

func ToDatumGoBytes

func ToDatumGoBytes(data []byte) Datum

ToDatumGoBytes converts the given byte slice to a Datum.

func ToDatumGoString

func ToDatumGoString(str string) Datum

ToDatumGoString converts the given string to a Datum.

type ExtensionFiles

type ExtensionFiles struct {
	Name            string
	ControlFileName string
	SQLFileNames    []string
	LibraryFileName string
	ControlFileDir  string
	LibraryFileDir  string
	Control         Control
}

ExtensionFiles contains all of the files that are related to or used by an extension.

func (*ExtensionFiles) LoadLibrary

func (extFile *ExtensionFiles) LoadLibrary() (*Library, error)

LoadLibrary loads the extension as a library.

func (*ExtensionFiles) LoadSQLFiles

func (extFile *ExtensionFiles) LoadSQLFiles() ([]string, error)

LoadSQLFiles loads the contents of the SQL files used by the extension. These will be in the order that they need to be executed.

func (*ExtensionFiles) LoadSQLFunctionNames

func (extFile *ExtensionFiles) LoadSQLFunctionNames() ([]string, error)

LoadSQLFunctionNames loads all of the library function names that are used by the extension.

type FilenameVersions

type FilenameVersions struct {
	From Version
	To   Version
}

FilenameVersions returns the versions that were encoded in a filename. `From` is the first number, while `To` is the second number. If a filename only specifies a single version, this both `From` and `To` will equal one another.

func DecodeFilenameVersions

func DecodeFilenameVersions(name string, fileName string) FilenameVersions

DecodeFilenameVersions decodes the version information within the file name. The `sqlFileName` should be the full file name (excluding the path), and the SQL file name should contain only the name as

type Function

type Function struct {
	Name       string
	Ptr        uintptr
	Args       []int
	APIVersion int
}

Function represents an internal library function.

type InternalLoadedLibrary

type InternalLoadedLibrary interface {
	Lookup(sym string) (uintptr, error)
	Close() error
}

InternalLoadedLibrary is an interface that is implemented by the specific platform to handle library operations.

type Library

type Library struct {
	Magic   PgMagicStruct
	Funcs   map[string]Function
	Version Version
	// contains filtered or unexported fields
}

Library is a fully-loaded extension library.

func LoadLibrary

func LoadLibrary(path string, funcNames []string) (*Library, error)

LoadLibrary loads the library of the extension, along with preloading all of the functions given.

type NullableDatum

type NullableDatum struct {
	Value  Datum
	IsNull bool
}

NullableDatum is used for arguments to Fmgr function calls.

type PgFunctionInfo

type PgFunctionInfo struct {
	APIVersion int32
}

PgFunctionInfo is a stand-in for the C struct that reports the function information.

type PgMagicStruct

type PgMagicStruct struct {
	Len          int32
	Version      int32
	FuncMaxArgs  int32
	IndexMaxKeys int32
	NameDataLen  int32
	Float4ByVal  int32
	Float8ByVal  int32
}

PgMagicStruct is a stand-in for the C struct that reports the information of the library.

type Version

type Version uint32

Version specifies the major and minor version numbers for an extension.

func ToVersion

func ToVersion(major uint16, minor uint16) Version

ToVersion creates a version from the given major and minor version numbers.

func (Version) Major

func (v Version) Major() uint16

Major returns the encoded major version number.

func (Version) Minor

func (v Version) Minor() uint16

Minor returns the encoded minor version number.

func (Version) String

func (v Version) String() string

String returns the version in the `major.minor` format.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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