Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

21 Commits

Folders and files

Repository files navigation

postgis_rs

Project Status (Finished)

This project started as an exploration of writing PostGIS extensions in Rust through pgrx and FFI bindings, aimed at contributing to postgis_topology without touching C code directly. Along the way it turned into something broader, which I'm happy about.

What this project achieved

The four crates (postgis_src, postgis_sys, postgis_bindings, postgis_rs) compose into a pgrx extension that can use PostGIS internal types like GEOMETRY and GSERIALIZED2 from Rust. The extension is minimal because the point was to validate that the composition works without breaking PostGIS internal invariants.

The geometry_in branch contains a deeper implementation, geometry_in is a core PostGIS function that parses text (in any format) into the binary geometry representation used internally. I reimplemented it in Rust through pgrx as a simplified test - to check whether the end-to-end integration held together. It passes almost all official PostGIS regression tests, this replaces the C version with the rust one on the new extension.

Why I'm changing the approach

Rust today have a gap when it comes to spatial libs, spatial crates like geozero and geo solve the direct usage on their use case, while I'm looking on a more layer division concept where computation, storage, ram, mathematics, each layer is independent.

The path I'm moving toward is to build those layers separately in Rust, starting with a native implementation of GSERIALIZED2 (PostGIS's storage format, optimized for direct memory access rather than for network transmission like EWKT). The same architecture would apply to raster storage protocols later, when it comes to only read can be more easily handled, but be able to support operations is a whole different issue.

This replaces the FFI-bindings approach rather than extending it.

Known pending items in this project

In the geometry_in branch, some tests still fail. Not in the parser itself - the reimplementation works. In one part two tests fail only because PROJ is sending for some reasons some texts two times instead of one, and the other one is because Postgis uses a custom logging system, I could not find how to properly set it from the function to init the pgrx extension, so I need to change the logging system in each call to make it works. I hadn't finished that adjustment when I changed direction.

Introduction

All postgis functions that exists rn are wrong, are there to check and test we can replace postgis functinos with new ones breaking the actual tests they will be replaced with new ones

For a mode deep case, check the geometry_in branch, which replace a core function

This is my formal playground, here will be the docs to be able to use Rust with Postgis.

I know the ideal is be able to use Rust inside Postgis and Postgis inside Rust, but I have no idea how to mix both, and probs I would need to do some major changes on dependencies compilation order while I do not know C.

Remember, I do not know C!

The project is done in 4 steps, this is because a lot of postgis depends on internal types that are not easy to port, like GSERIALIZED and LWGEOM, the idea is be able to make the minimum bindings and write the left in Rust.

  • postgis_src: Source of Postgis to build and use as base for bindings
  • postgis_sys: This project generate the bindings for the requested libs
  • postgis_bindings: This project is to rustify the C-Bindings of postgis_sys
  • postgis_rs: The PG extension which should use postgis_bindings to run

Steps to start using the project:

  1. If this is your first time using pgrx, we need to initialize it
cargo install --locked cargo-pgrx
cargo pgrx init
  1. Clone
git clone https://github.com/latot/postgis_rs
cd postgis_rs
git submodule update --init --recursive
  1. Move to folder postgis_rs
cd postgis_rs
  1. Start pgrx PG16
cargo pgrx start pg16

We need to get from this output the port

  1. Move up to the main project
cd ..
  1. Update env vars

Open .env and update PGPORT with the output of pgrx.

source .env
  1. Build and install postgis on pgrx

With .env we already set most of needed params to build postgis, the only missing one is --prefix="$PREFIX", then it just do make and make install you can do it manually if want.

cargo run -p postgis_src
  1. Gen Bindings
cargo build -p postgis_sys
  1. Install and run the rust/PG extension!
cargo pgrx run -p postgis_rs
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_rs;

To update extensions:

ALTER EXTENSION postgis UPDATE;
ALTER EXTENSION postgis_rs UPDATE;

Drop Extensions:

DROP EXTENSION postgis;
DROP EXTENSION postgis_rs;

Example function with geometry

Now we can use Geometry type from PG and run Rust code directly!

select sample_geom('Point (1 2)'::geometry);

The code can be found in postgis_rs.

Run Postgis tests on PGRX

Well, the whole thing is allow us to replace Postgis functions with our own and run tests.

You need to follow previous steps, have postgis build and the env set, then two steps:

Go to postgis_src/source/regress/hooks/hook-after-create.sql And add this in the end:

-- Drop here all the functions you want to replace with postgis_rs
DROP FUNCTION ST_PolygonFromText(TEXT);

CREATE EXTENSION postgis_rs;

You need to drop every function you want to test first, the create the extension.

Now run the tests:

cd postgis_src/source
make test

There is some special cases like topology where the extensions are installed in the topology schema, in this case we must tweak it a little:

DROP FUNCTION topology.ST_GetFaceGeometry;

CREATE EXTENSION postgis_rs;

ALTER FUNCTION ST_GetFaceGeometry SET SCHEMA topology;

Replace Postgis function with our own (in a pgrx instance)

A very brute example for now, this will replace a working version with a non working version from the new lib, so we can see using the tests the extensions is replacing the internal one with the new one.

DROP EXTENSION IF EXISTS postgis;
DROP EXTENSION IF EXISTS psotgis_rs;
CREATE EXTENSION postgis;

ALTER EXTENSION postgis DROP FUNCTION ST_PolygonFromText(TEXT);
DROP FUNCTION ST_PolygonFromText(TEXT);

CREATE EXTENSION postgis_rs;
ALTER EXTENSION postgis_rs DROP FUNCTION ST_PolygonFromText(TEXT);

ALTER EXTENSION postgis ADD FUNCTION ST_PolygonFromText(TEXT);

SELECT ST_PolygonFromText('hi');

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages