-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: multicorn2 (Postgres FDW) backend (#397)
* feat: multicorn2 (Postgres FDW) backend * Adding tests * Adding tests * Optimizing SELECT * Fix tests * Write API * Query cost * Add docs * Add integration test * Different strategy * Another approach * Rebase * Fix docker * Remove entrypoint * Fix tests
- Loading branch information
1 parent
57adc2d
commit b43fc70
Showing
30 changed files
with
1,352 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,3 +105,5 @@ ENV/ | |
*.sqlite | ||
*.db | ||
*.swp | ||
|
||
multicorn2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.. _postgres: | ||
|
||
================ | ||
Postgres backend | ||
================ | ||
|
||
Since version 1.3 Shillelagh ships with an experimental backend that uses Postgres instead of SQLite. The backend implements a custom [pyscopg2](https://pypi.org/project/psycopg2/) cursor that automatically registers a foreign data wrapper (FDW) whenever a supported table is accessed. It's based on the [multicorn2](http://multicorn2.org/) extension and Python package. | ||
|
||
To use the backend you need to: | ||
|
||
1. Install the [Multicorn2](http://multicorn2.org/) extension. | ||
2. Install the multicorn2 Python package in the machine running Postgres. Note that this is not the "multicorn" package available on PyPI. You need to download the source and install it manually. | ||
3. Install Shillelagh in the machine running Postgres. | ||
|
||
Note that you need to install Python packages in a way that they are available to the process running Postgres. You can either install them globally, or install them in a virtual environment and have it activated in the process that starts Postgres. | ||
|
||
The ``postgres/`` directory has a Docker configuration that can be used to test the backend, or as a basis for installation. To run it, execute: | ||
|
||
.. code-block:: bash | ||
docker compose -f postgres/docker-compose.yml up | ||
You should then be able to run the example script in `examples/postgres.py`_ to test that everything works. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
Simple multicorn2 test. | ||
Multicorn2 is an extension for PostgreSQL that allows you to create foreign data wrappers | ||
in Python. To use it, you need to install on the machine running Postgres the extension, | ||
the multicorn2 package (not on (PyPI), and the shillelagh package. | ||
If you want to play with it Shillelagh has a `docker-compose.yml` file that will run | ||
Postgres with the extension and the Python packages. Just run: | ||
$ cd postgres/ | ||
$ docker compose up --build -d | ||
Then you can run this script. | ||
""" | ||
|
||
from sqlalchemy import create_engine | ||
|
||
# the backend uses psycopg2 under the hood, so any valid connection string for it will | ||
# work; just replace the scheme with `shillelagh+multicorn2` | ||
engine = create_engine( | ||
"shillelagh+multicorn2://shillelagh:shillelagh123@localhost:5432/shillelagh", | ||
) | ||
connection = engine.connect() | ||
|
||
SQL = ( | ||
'SELECT * FROM "https://docs.google.com/spreadsheets/d/' | ||
'1LcWZMsdCl92g7nA-D6qGRqg1T5TiHyuKJUY1u9XAnsk/edit#gid=0"' | ||
) | ||
for row in connection.execute(SQL): | ||
print(row) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Use the official Postgres image as a base | ||
FROM postgres:13 | ||
|
||
WORKDIR /code | ||
COPY . /code | ||
|
||
# Use root for package installation | ||
USER root | ||
|
||
# Install system dependencies | ||
RUN apt-get update && apt-get install -y \ | ||
build-essential \ | ||
git \ | ||
postgresql-server-dev-13 \ | ||
python3 \ | ||
python3-dev \ | ||
python3-pip \ | ||
python3-venv \ | ||
wget | ||
|
||
# Download, build, and install multicorn2 | ||
RUN wget https://github.com/pgsql-io/multicorn2/archive/refs/tags/v2.5.tar.gz && \ | ||
tar -xvf v2.5.tar.gz && \ | ||
cd multicorn2-2.5 && \ | ||
make && \ | ||
make install | ||
|
||
|
||
# Create a virtual environment and install dependencies | ||
RUN python3 -m venv /code/venv && \ | ||
/code/venv/bin/pip install --upgrade pip && \ | ||
/code/venv/bin/pip install -e '.[all]' | ||
|
||
# Set environment variable for PostgreSQL to use the virtual environment | ||
ENV PATH="/code/venv/bin:$PATH" | ||
|
||
# Switch back to the default postgres user | ||
USER postgres |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
version: '3.8' | ||
|
||
services: | ||
postgres: | ||
build: | ||
context: .. | ||
dockerfile: postgres/Dockerfile | ||
environment: | ||
POSTGRES_PASSWORD: shillelagh123 | ||
POSTGRES_USER: shillelagh | ||
POSTGRES_DB: shillelagh | ||
volumes: | ||
- db_data:/var/lib/postgresql/data | ||
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro | ||
ports: | ||
- "5432:5432" | ||
|
||
volumes: | ||
db_data: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CREATE EXTENSION IF NOT EXISTS multicorn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.