pgcentralfoundation / pgrx

Build Postgres Extensions with Rust!
Other
3.68k stars 247 forks source link

RFC: Range support #498

Closed Hoverbear closed 1 year ago

Hoverbear commented 2 years ago

We'd like PGX to support Postgres Ranges (and to provide clean ways to pass ranges between Rust and PostgreSQL).

PostgreSQL has several builtin range types:

We'd like to provide equivalent types and mappings in PGX for these.

Both Rust and Postgres support inclusive/exclusive ranges, as well as unbounded ranges. Both also support empty ranges.

User defined ranges

Users can define a new range type like:

CREATE TYPE floatrange AS RANGE (
    subtype = float8,
    subtype_diff = float8mi
);

SELECT '[1.234, 5.678]'::floatrange;

pgx does not currently support user defined types, so it also doesn't support user defined range types.

mhov commented 2 years ago

This is the last missing feature I'd need for my company to start migrating our c->pgx! A zero copy implementation like pgx::Array<T> would be awesome. Even just exposing these functions (and their multirange counterparts) for manual use would be a great workaround.

RangeType* range_serialize(TypeCacheEntry *typcache, RangeBound *lower, RangeBound *upper, bool empty)
void range_deserialize(TypeCacheEntry *typcache, const RangeType *range,  RangeBound *lower, RangeBound *upper, bool *empty)
TypeCacheEntry *range_get_typcache(FunctionCallInfo fcinfo, Oid rngtypid)
johnrballard commented 2 years ago

Tasks

mhov commented 2 years ago

@johnrballard there's a yet-to-be PR'd task I'm working on for +/- infinity Timestamps which Range would also depend on for tsrange/tstzrange This draft PR is rather old and @workingjubilee has illuminated many subjects to me and your newer best practices, (rather than me mimicking older pgx data type approaches) so once those dependent tasks are done I'll likely restart this PR from scratch.

johnrballard commented 2 years ago

@mhov I attached it here as a mental prompt. My strikethrough was my attempt as saying that we are not doing this but here it is for reference.