cloudberry

package
v0.0.0-...-ad8a6e1 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package cloudberry provides a database/sql driver for Apache Cloudberry (Incubating) and Greenplum-derived MPP databases.

The driver wraps the pgx/v5 stdlib driver but configures the connection to behave like the C libpq client (psql): it forces the PostgreSQL *simple* (text) query protocol and disables server-side prepared-statement caching.

Motivation

Cloudberry coordinates work across segment backends. Some Cloudberry server builds mishandle the PostgreSQL *extended* query protocol (Parse/Bind/Execute with cached prepared statements) when commands are dispatched to segments, which can terminate a segment backend. The reference C client (libpq, used by psql) issues statements through the simple query protocol and does not cache prepared statements, and does not trigger that behaviour. This driver mirrors that behaviour so MPP tooling written in Go (e.g. gpbackup/gprestore) talks to segments the same way psql does.

Usage

import (
    "database/sql"
    _ "github.com/vyrodovalexey/cloudberry-sql/cloudberry"
)

db, err := sql.Open("cloudberry", "host=localhost port=5432 user=gpadmin dbname=mydb sslmode=disable")

The driver is registered under the names "cloudberry" and (for drop-in compatibility with tools that hard-code the pgx driver name) "pgx".

Index

Constants

View Source
const CompatDriverName = "pgx"

CompatDriverName is an additional name registered for drop-in compatibility with tooling (such as cloudberry-go-libs / gpbackup) that opens the database using the historical "pgx" driver name.

View Source
const DriverName = "cloudberry"

DriverName is the primary name this driver registers under.

Variables

This section is empty.

Functions

func CopyFromProgramOnSegment

func CopyFromProgramOnSegment(ctx context.Context, execer execerContext, table, program string) error

CopyFromProgramOnSegment runs a parallel, per-segment COPY that loads each segment's slice of the table from an external program. As with CopyToProgramOnSegment, program must contain the <SEGID> token. This is the gprestore data path.

func CopyToProgramOnSegment

func CopyToProgramOnSegment(ctx context.Context, execer execerContext, table, program string) error

CopyToProgramOnSegment runs a parallel, per-segment COPY that pipes each segment's slice of the table to an external program. The program string must contain the literal token <SEGID>, which Cloudberry substitutes with each segment's content id at execution time (this is the standard gpbackup data path). table should be a schema-qualified, already-quoted identifier.

Example:

CopyToProgramOnSegment(ctx, db, "public.customers",
    "cat > /backup/seg_<SEGID>.dat")

func ExportSnapshot

func ExportSnapshot(ctx context.Context, tx *sql.Tx) (string, error)

ExportSnapshot exports a transaction snapshot id from tx, for use by sibling connections that want a consistent view via SetTransactionSnapshot. tx must already be in an open transaction.

func NewConnector

func NewConnector(dsn string) (driver.Connector, error)

NewConnector builds a driver.Connector for the given DSN. It is a convenience wrapper around sql.OpenDB for callers that want to construct a *sql.DB directly without registering/looking up the driver by name.

func Open

func Open(dsn string) (*sql.DB, error)

Open is a convenience helper that returns a *sql.DB backed by this driver.

func SetSerializable

func SetSerializable(ctx context.Context, execer execerContext) error

SetSerializable sets the session's transaction isolation to serializable. On Cloudberry/Greenplum the server transparently falls back to repeatable read (serializable is not yet supported); this helper hides that detail and is a no-op-equivalent on those servers.

func SetTransactionSnapshot

func SetTransactionSnapshot(ctx context.Context, tx *sql.Tx, snapshotID string) error

SetTransactionSnapshot makes tx use the snapshot exported by ExportSnapshot, giving it the same consistent view. tx must be a freshly-begun transaction.

Types

type Driver

type Driver struct{}

Driver is the database/sql driver for Cloudberry. It implements driver.Driver and driver.DriverContext.

func (*Driver) Open

func (d *Driver) Open(dsn string) (driver.Conn, error)

Open returns a new connection to the database using a DSN. It satisfies driver.Driver. The DSN accepts the standard libpq keyword/value or URL connection-string formats understood by pgx.

func (*Driver) OpenConnector

func (d *Driver) OpenConnector(dsn string) (driver.Connector, error)

OpenConnector parses the DSN and returns a driver.Connector configured for Cloudberry-safe (simple-protocol, no statement cache) connections.

type Flavor

type Flavor string

Flavor identifies the database product behind a connection.

const (
	// FlavorCloudberry indicates an Apache Cloudberry (Incubating) server.
	FlavorCloudberry Flavor = "cloudberry"
	// FlavorGreenplum indicates a Greenplum Database server.
	FlavorGreenplum Flavor = "greenplum"
	// FlavorPostgres indicates a vanilla PostgreSQL server.
	FlavorPostgres Flavor = "postgres"
	// FlavorUnknown indicates the product could not be determined.
	FlavorUnknown Flavor = "unknown"
)

type Segment

type Segment struct {
	Content       int
	Role          SegmentRole
	PreferredRole SegmentRole
	Status        string // "u" (up) or "d" (down)
	Mode          string // "s" (synced) or "n" (not in sync)
	Hostname      string
	Address       string
	Port          int
	DataDir       string
}

Segment describes one entry from gp_segment_configuration.

func GetSegmentConfiguration

func GetSegmentConfiguration(ctx context.Context, db *sql.DB) ([]Segment, error)

GetSegmentConfiguration returns the cluster segment configuration from gp_segment_configuration. It requires an MPP (Cloudberry/Greenplum) server.

func (Segment) IsCoordinator

func (s Segment) IsCoordinator() bool

IsCoordinator reports whether this segment entry is the coordinator (content -1).

func (Segment) IsUp

func (s Segment) IsUp() bool

IsUp reports whether the segment is up.

type SegmentRole

type SegmentRole string

SegmentRole identifies a segment's role in the cluster.

const (
	// RolePrimary is a primary segment (or the coordinator, content -1).
	RolePrimary SegmentRole = "p"
	// RoleMirror is a mirror segment.
	RoleMirror SegmentRole = "m"
)

type VersionInfo

type VersionInfo struct {
	// Raw is the full version() string.
	Raw string
	// Flavor is the detected product family.
	Flavor Flavor
	// Major/Minor/Patch are the parsed product version numbers. For Cloudberry
	// and Greenplum these reflect the MPP product version (e.g. 2.1.0), not the
	// embedded PostgreSQL version.
	Major int
	Minor int
	Patch int
}

VersionInfo describes the server product and version behind a connection.

func DetectVersion

func DetectVersion(ctx context.Context, q queryRower) (VersionInfo, error)

DetectVersion queries the server and returns its product and version. The querier may be a *sql.DB or a *sql.Conn.

func (VersionInfo) IsCloudberry

func (v VersionInfo) IsCloudberry() bool

IsCloudberry reports whether the server is Apache Cloudberry.

func (VersionInfo) IsGreenplum

func (v VersionInfo) IsGreenplum() bool

IsGreenplum reports whether the server is Greenplum.

func (VersionInfo) IsMPP

func (v VersionInfo) IsMPP() bool

IsMPP reports whether the server is an MPP product (Cloudberry or Greenplum) rather than vanilla PostgreSQL.

Jump to

Keyboard shortcuts

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