From c54b969703aa02821620b4775fd89aa0199e9f90 Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sun, 12 Jul 2026 20:40:31 +0000 Subject: [PATCH] Add pgrust pgrust is a rewrite of PostgreSQL in Rust targeting Postgres 18.3 compatibility (https://github.com/malisper/pgrust). Runs from the official Docker image (malisper/pgrust:v0.1); schema and queries are identical to the postgresql entry. pgrust v0.1 has a COPY input-buffer bug that rejects valid UTF-8 when a 64 KiB buffer refill boundary splits a multi-byte character, so the load script splits the TSV into line-aligned <=64 KiB pieces and loads each with its own COPY statement. See pgrust/README.md. Co-Authored-By: Claude Fable 5 --- pgrust/README.md | 28 +++++++++++ pgrust/benchmark.sh | 5 ++ pgrust/check | 4 ++ pgrust/create.sql | 108 +++++++++++++++++++++++++++++++++++++++++++ pgrust/data-size | 5 ++ pgrust/install | 10 ++++ pgrust/load | 40 ++++++++++++++++ pgrust/queries.sql | 43 +++++++++++++++++ pgrust/query | 26 +++++++++++ pgrust/start | 43 +++++++++++++++++ pgrust/stop | 3 ++ pgrust/template.json | 11 +++++ 12 files changed, 326 insertions(+) create mode 100644 pgrust/README.md create mode 100755 pgrust/benchmark.sh create mode 100755 pgrust/check create mode 100644 pgrust/create.sql create mode 100755 pgrust/data-size create mode 100755 pgrust/install create mode 100755 pgrust/load create mode 100644 pgrust/queries.sql create mode 100755 pgrust/query create mode 100755 pgrust/start create mode 100755 pgrust/stop create mode 100644 pgrust/template.json diff --git a/pgrust/README.md b/pgrust/README.md new file mode 100644 index 0000000000..35e46602df --- /dev/null +++ b/pgrust/README.md @@ -0,0 +1,28 @@ +pgrust is a rewrite of PostgreSQL in Rust targeting compatibility with +Postgres 18.3: https://github.com/malisper/pgrust + +It runs from the official Docker image (`malisper/pgrust:v0.1`), which is a +drop-in replacement for the official `postgres` image. The Docker entrypoint +applies the settings the upstream README requires (`io_method=sync`, +`max_stack_depth=60000`, enlarged stack limits); no further tuning is done. + +The table schema and queries are identical to the `postgresql` entry. + +### Data loading workaround + +pgrust v0.1 has a COPY input-buffer bug: encoding verification fails whenever +the 64 KiB raw-buffer refill boundary falls inside a multi-byte UTF-8 +character, so `COPY hits FROM 'hits.tsv'` rejects the (valid UTF-8) dataset +with `invalid byte sequence for encoding "UTF8"`. The client-side `\copy` +path fails the same way, and `ENCODING 'SQL_ASCII'` does not help, because +the bug is in the buffer bookkeeping rather than in the verifier proper. +Minimal reproducer: COPY of any file larger than 64 KiB consisting of mostly +multi-byte characters (e.g. 3000 repetitions of a Cyrillic line) fails on the +first buffer refill. + +Inputs of at most 64 KiB are processed in a single buffer fill and are +unaffected, so `load` splits the TSV into line-aligned <=64 KiB pieces +(`split -C 64k`) and loads each with its own COPY statement in a single +session and transaction. The data is not modified. The per-statement overhead +is about 5 ms, which adds roughly an hour to the load of the full dataset +compared to a hypothetical single COPY. diff --git a/pgrust/benchmark.sh b/pgrust/benchmark.sh new file mode 100755 index 0000000000..432bd3ae17 --- /dev/null +++ b/pgrust/benchmark.sh @@ -0,0 +1,5 @@ +#!/bin/bash +# Thin shim — actual flow is in lib/benchmark-common.sh. +export BENCH_DOWNLOAD_SCRIPT="download-hits-tsv" +export BENCH_DURABLE=yes +exec ../lib/benchmark-common.sh diff --git a/pgrust/check b/pgrust/check new file mode 100755 index 0000000000..f161a08203 --- /dev/null +++ b/pgrust/check @@ -0,0 +1,4 @@ +#!/bin/bash +set -e + +PGPASSWORD=test psql -h localhost -U postgres -c 'SELECT 1' >/dev/null diff --git a/pgrust/create.sql b/pgrust/create.sql new file mode 100644 index 0000000000..41c961c00f --- /dev/null +++ b/pgrust/create.sql @@ -0,0 +1,108 @@ +CREATE TABLE hits +( + WatchID BIGINT NOT NULL, + JavaEnable SMALLINT NOT NULL, + Title TEXT NOT NULL, + GoodEvent SMALLINT NOT NULL, + EventTime TIMESTAMP NOT NULL, + EventDate Date NOT NULL, + CounterID INTEGER NOT NULL, + ClientIP INTEGER NOT NULL, + RegionID INTEGER NOT NULL, + UserID BIGINT NOT NULL, + CounterClass SMALLINT NOT NULL, + OS SMALLINT NOT NULL, + UserAgent SMALLINT NOT NULL, + URL TEXT NOT NULL, + Referer TEXT NOT NULL, + IsRefresh SMALLINT NOT NULL, + RefererCategoryID SMALLINT NOT NULL, + RefererRegionID INTEGER NOT NULL, + URLCategoryID SMALLINT NOT NULL, + URLRegionID INTEGER NOT NULL, + ResolutionWidth SMALLINT NOT NULL, + ResolutionHeight SMALLINT NOT NULL, + ResolutionDepth SMALLINT NOT NULL, + FlashMajor SMALLINT NOT NULL, + FlashMinor SMALLINT NOT NULL, + FlashMinor2 TEXT NOT NULL, + NetMajor SMALLINT NOT NULL, + NetMinor SMALLINT NOT NULL, + UserAgentMajor SMALLINT NOT NULL, + UserAgentMinor VARCHAR(255) NOT NULL, + CookieEnable SMALLINT NOT NULL, + JavascriptEnable SMALLINT NOT NULL, + IsMobile SMALLINT NOT NULL, + MobilePhone SMALLINT NOT NULL, + MobilePhoneModel TEXT NOT NULL, + Params TEXT NOT NULL, + IPNetworkID INTEGER NOT NULL, + TraficSourceID SMALLINT NOT NULL, + SearchEngineID SMALLINT NOT NULL, + SearchPhrase TEXT NOT NULL, + AdvEngineID SMALLINT NOT NULL, + IsArtifical SMALLINT NOT NULL, + WindowClientWidth SMALLINT NOT NULL, + WindowClientHeight SMALLINT NOT NULL, + ClientTimeZone SMALLINT NOT NULL, + ClientEventTime TIMESTAMP NOT NULL, + SilverlightVersion1 SMALLINT NOT NULL, + SilverlightVersion2 SMALLINT NOT NULL, + SilverlightVersion3 INTEGER NOT NULL, + SilverlightVersion4 SMALLINT NOT NULL, + PageCharset TEXT NOT NULL, + CodeVersion INTEGER NOT NULL, + IsLink SMALLINT NOT NULL, + IsDownload SMALLINT NOT NULL, + IsNotBounce SMALLINT NOT NULL, + FUniqID BIGINT NOT NULL, + OriginalURL TEXT NOT NULL, + HID INTEGER NOT NULL, + IsOldCounter SMALLINT NOT NULL, + IsEvent SMALLINT NOT NULL, + IsParameter SMALLINT NOT NULL, + DontCountHits SMALLINT NOT NULL, + WithHash SMALLINT NOT NULL, + HitColor CHAR NOT NULL, + LocalEventTime TIMESTAMP NOT NULL, + Age SMALLINT NOT NULL, + Sex SMALLINT NOT NULL, + Income SMALLINT NOT NULL, + Interests SMALLINT NOT NULL, + Robotness SMALLINT NOT NULL, + RemoteIP INTEGER NOT NULL, + WindowName INTEGER NOT NULL, + OpenerName INTEGER NOT NULL, + HistoryLength SMALLINT NOT NULL, + BrowserLanguage TEXT NOT NULL, + BrowserCountry TEXT NOT NULL, + SocialNetwork TEXT NOT NULL, + SocialAction TEXT NOT NULL, + HTTPError SMALLINT NOT NULL, + SendTiming INTEGER NOT NULL, + DNSTiming INTEGER NOT NULL, + ConnectTiming INTEGER NOT NULL, + ResponseStartTiming INTEGER NOT NULL, + ResponseEndTiming INTEGER NOT NULL, + FetchTiming INTEGER NOT NULL, + SocialSourceNetworkID SMALLINT NOT NULL, + SocialSourcePage TEXT NOT NULL, + ParamPrice BIGINT NOT NULL, + ParamOrderID TEXT NOT NULL, + ParamCurrency TEXT NOT NULL, + ParamCurrencyID SMALLINT NOT NULL, + OpenstatServiceName TEXT NOT NULL, + OpenstatCampaignID TEXT NOT NULL, + OpenstatAdID TEXT NOT NULL, + OpenstatSourceID TEXT NOT NULL, + UTMSource TEXT NOT NULL, + UTMMedium TEXT NOT NULL, + UTMCampaign TEXT NOT NULL, + UTMContent TEXT NOT NULL, + UTMTerm TEXT NOT NULL, + FromTag TEXT NOT NULL, + HasGCLID SMALLINT NOT NULL, + RefererHash BIGINT NOT NULL, + URLHash BIGINT NOT NULL, + CLID INTEGER NOT NULL +); diff --git a/pgrust/data-size b/pgrust/data-size new file mode 100755 index 0000000000..ed211391bf --- /dev/null +++ b/pgrust/data-size @@ -0,0 +1,5 @@ +#!/bin/bash +set -eu + +# PGDATA is bind-mounted from ./db, so du includes indexes and WAL. +sudo du -bcs db/ | grep total | awk '{print $1}' diff --git a/pgrust/install b/pgrust/install new file mode 100755 index 0000000000..cc0e11b2a9 --- /dev/null +++ b/pgrust/install @@ -0,0 +1,10 @@ +#!/bin/bash +set -eu + +sudo apt-get update -y +sudo apt-get install -y docker.io postgresql-client gzip + +sudo docker pull malisper/pgrust:v0.1 + +mkdir -p data db +chmod -R 777 data db diff --git a/pgrust/load b/pgrust/load new file mode 100755 index 0000000000..93b026454b --- /dev/null +++ b/pgrust/load @@ -0,0 +1,40 @@ +#!/bin/bash +set -eu + +# pgrust v0.1 has a COPY input-buffer bug: encoding verification fails +# whenever its 64 KiB raw-buffer refill boundary falls inside a multi-byte +# UTF-8 character, rejecting valid UTF-8 input ("invalid byte sequence for +# encoding UTF8"). Inputs of at most 64 KiB are processed in a single buffer +# fill and are unaffected, so the TSV is split into line-aligned <=64 KiB +# pieces, each loaded with its own COPY statement (single session, single +# transaction). The data itself is not modified. +mkdir -p data/pieces +split -C 64k -a 5 hits.tsv data/pieces/p +rm -f hits.tsv +chmod -R 777 data + +# Drop+create the table so this is idempotent. +PGPASSWORD=test psql -h localhost -U postgres -v ON_ERROR_STOP=1 -t -c 'DROP TABLE IF EXISTS hits' +PGPASSWORD=test psql -h localhost -U postgres -v ON_ERROR_STOP=1 -t < create.sql + +# TRUNCATE and COPY FREEZE in a single transaction. Postgres requires the +# table to be created or truncated in the current subtransaction for COPY +# FREEZE to be allowed. The pieces are read server-side from the /data mount +# inside the container. ON_ERROR_STOP makes psql exit non-zero on data errors +# so set -e in this script catches them. +{ + echo "BEGIN;" + echo "TRUNCATE TABLE hits;" + for f in data/pieces/p*; do + echo "COPY hits FROM '/$f' WITH (FREEZE);" + done + echo "COMMIT;" +} > load_pieces.sql + +PGPASSWORD=test psql -h localhost -U postgres -v ON_ERROR_STOP=1 -q -f load_pieces.sql + +PGPASSWORD=test psql -h localhost -U postgres -v ON_ERROR_STOP=1 -t -c 'VACUUM ANALYZE hits' + +# Only remove inputs once load completes successfully. +rm -rf data/pieces load_pieces.sql +sync diff --git a/pgrust/queries.sql b/pgrust/queries.sql new file mode 100644 index 0000000000..31f65fc898 --- /dev/null +++ b/pgrust/queries.sql @@ -0,0 +1,43 @@ +SELECT COUNT(*) FROM hits; +SELECT COUNT(*) FROM hits WHERE AdvEngineID <> 0; +SELECT SUM(AdvEngineID), COUNT(*), AVG(ResolutionWidth) FROM hits; +SELECT AVG(UserID) FROM hits; +SELECT COUNT(DISTINCT UserID) FROM hits; +SELECT COUNT(DISTINCT SearchPhrase) FROM hits; +SELECT MIN(EventDate), MAX(EventDate) FROM hits; +SELECT AdvEngineID, COUNT(*) FROM hits WHERE AdvEngineID <> 0 GROUP BY AdvEngineID ORDER BY COUNT(*) DESC; +SELECT RegionID, COUNT(DISTINCT UserID) AS u FROM hits GROUP BY RegionID ORDER BY u DESC LIMIT 10; +SELECT RegionID, SUM(AdvEngineID), COUNT(*) AS c, AVG(ResolutionWidth), COUNT(DISTINCT UserID) FROM hits GROUP BY RegionID ORDER BY c DESC LIMIT 10; +SELECT MobilePhoneModel, COUNT(DISTINCT UserID) AS u FROM hits WHERE MobilePhoneModel <> '' GROUP BY MobilePhoneModel ORDER BY u DESC LIMIT 10; +SELECT MobilePhone, MobilePhoneModel, COUNT(DISTINCT UserID) AS u FROM hits WHERE MobilePhoneModel <> '' GROUP BY MobilePhone, MobilePhoneModel ORDER BY u DESC LIMIT 10; +SELECT SearchPhrase, COUNT(*) AS c FROM hits WHERE SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY c DESC LIMIT 10; +SELECT SearchPhrase, COUNT(DISTINCT UserID) AS u FROM hits WHERE SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY u DESC LIMIT 10; +SELECT SearchEngineID, SearchPhrase, COUNT(*) AS c FROM hits WHERE SearchPhrase <> '' GROUP BY SearchEngineID, SearchPhrase ORDER BY c DESC LIMIT 10; +SELECT UserID, COUNT(*) FROM hits GROUP BY UserID ORDER BY COUNT(*) DESC LIMIT 10; +SELECT UserID, SearchPhrase, COUNT(*) FROM hits GROUP BY UserID, SearchPhrase ORDER BY COUNT(*) DESC LIMIT 10; +SELECT UserID, SearchPhrase, COUNT(*) FROM hits GROUP BY UserID, SearchPhrase LIMIT 10; +SELECT UserID, extract(minute FROM EventTime) AS m, SearchPhrase, COUNT(*) FROM hits GROUP BY UserID, m, SearchPhrase ORDER BY COUNT(*) DESC LIMIT 10; +SELECT UserID FROM hits WHERE UserID = 435090932899640449; +SELECT COUNT(*) FROM hits WHERE URL LIKE '%google%'; +SELECT SearchPhrase, MIN(URL), COUNT(*) AS c FROM hits WHERE URL LIKE '%google%' AND SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY c DESC LIMIT 10; +SELECT SearchPhrase, MIN(URL), MIN(Title), COUNT(*) AS c, COUNT(DISTINCT UserID) FROM hits WHERE Title LIKE '%Google%' AND URL NOT LIKE '%.google.%' AND SearchPhrase <> '' GROUP BY SearchPhrase ORDER BY c DESC LIMIT 10; +SELECT * FROM hits WHERE URL LIKE '%google%' ORDER BY EventTime LIMIT 10; +SELECT SearchPhrase FROM hits WHERE SearchPhrase <> '' ORDER BY EventTime LIMIT 10; +SELECT SearchPhrase FROM hits WHERE SearchPhrase <> '' ORDER BY SearchPhrase LIMIT 10; +SELECT SearchPhrase FROM hits WHERE SearchPhrase <> '' ORDER BY EventTime, SearchPhrase LIMIT 10; +SELECT CounterID, AVG(length(URL)) AS l, COUNT(*) AS c FROM hits WHERE URL <> '' GROUP BY CounterID HAVING COUNT(*) > 100000 ORDER BY l DESC LIMIT 25; +SELECT REGEXP_REPLACE(Referer, '^https?://(?:www\.)?([^/]+)/.*$', '\1') AS k, AVG(length(Referer)) AS l, COUNT(*) AS c, MIN(Referer) FROM hits WHERE Referer <> '' GROUP BY k HAVING COUNT(*) > 100000 ORDER BY l DESC LIMIT 25; +SELECT SUM(ResolutionWidth), SUM(ResolutionWidth + 1), SUM(ResolutionWidth + 2), SUM(ResolutionWidth + 3), SUM(ResolutionWidth + 4), SUM(ResolutionWidth + 5), SUM(ResolutionWidth + 6), SUM(ResolutionWidth + 7), SUM(ResolutionWidth + 8), SUM(ResolutionWidth + 9), SUM(ResolutionWidth + 10), SUM(ResolutionWidth + 11), SUM(ResolutionWidth + 12), SUM(ResolutionWidth + 13), SUM(ResolutionWidth + 14), SUM(ResolutionWidth + 15), SUM(ResolutionWidth + 16), SUM(ResolutionWidth + 17), SUM(ResolutionWidth + 18), SUM(ResolutionWidth + 19), SUM(ResolutionWidth + 20), SUM(ResolutionWidth + 21), SUM(ResolutionWidth + 22), SUM(ResolutionWidth + 23), SUM(ResolutionWidth + 24), SUM(ResolutionWidth + 25), SUM(ResolutionWidth + 26), SUM(ResolutionWidth + 27), SUM(ResolutionWidth + 28), SUM(ResolutionWidth + 29), SUM(ResolutionWidth + 30), SUM(ResolutionWidth + 31), SUM(ResolutionWidth + 32), SUM(ResolutionWidth + 33), SUM(ResolutionWidth + 34), SUM(ResolutionWidth + 35), SUM(ResolutionWidth + 36), SUM(ResolutionWidth + 37), SUM(ResolutionWidth + 38), SUM(ResolutionWidth + 39), SUM(ResolutionWidth + 40), SUM(ResolutionWidth + 41), SUM(ResolutionWidth + 42), SUM(ResolutionWidth + 43), SUM(ResolutionWidth + 44), SUM(ResolutionWidth + 45), SUM(ResolutionWidth + 46), SUM(ResolutionWidth + 47), SUM(ResolutionWidth + 48), SUM(ResolutionWidth + 49), SUM(ResolutionWidth + 50), SUM(ResolutionWidth + 51), SUM(ResolutionWidth + 52), SUM(ResolutionWidth + 53), SUM(ResolutionWidth + 54), SUM(ResolutionWidth + 55), SUM(ResolutionWidth + 56), SUM(ResolutionWidth + 57), SUM(ResolutionWidth + 58), SUM(ResolutionWidth + 59), SUM(ResolutionWidth + 60), SUM(ResolutionWidth + 61), SUM(ResolutionWidth + 62), SUM(ResolutionWidth + 63), SUM(ResolutionWidth + 64), SUM(ResolutionWidth + 65), SUM(ResolutionWidth + 66), SUM(ResolutionWidth + 67), SUM(ResolutionWidth + 68), SUM(ResolutionWidth + 69), SUM(ResolutionWidth + 70), SUM(ResolutionWidth + 71), SUM(ResolutionWidth + 72), SUM(ResolutionWidth + 73), SUM(ResolutionWidth + 74), SUM(ResolutionWidth + 75), SUM(ResolutionWidth + 76), SUM(ResolutionWidth + 77), SUM(ResolutionWidth + 78), SUM(ResolutionWidth + 79), SUM(ResolutionWidth + 80), SUM(ResolutionWidth + 81), SUM(ResolutionWidth + 82), SUM(ResolutionWidth + 83), SUM(ResolutionWidth + 84), SUM(ResolutionWidth + 85), SUM(ResolutionWidth + 86), SUM(ResolutionWidth + 87), SUM(ResolutionWidth + 88), SUM(ResolutionWidth + 89) FROM hits; +SELECT SearchEngineID, ClientIP, COUNT(*) AS c, SUM(IsRefresh), AVG(ResolutionWidth) FROM hits WHERE SearchPhrase <> '' GROUP BY SearchEngineID, ClientIP ORDER BY c DESC LIMIT 10; +SELECT WatchID, ClientIP, COUNT(*) AS c, SUM(IsRefresh), AVG(ResolutionWidth) FROM hits WHERE SearchPhrase <> '' GROUP BY WatchID, ClientIP ORDER BY c DESC LIMIT 10; +SELECT WatchID, ClientIP, COUNT(*) AS c, SUM(IsRefresh), AVG(ResolutionWidth) FROM hits GROUP BY WatchID, ClientIP ORDER BY c DESC LIMIT 10; +SELECT URL, COUNT(*) AS c FROM hits GROUP BY URL ORDER BY c DESC LIMIT 10; +SELECT 1, URL, COUNT(*) AS c FROM hits GROUP BY 1, URL ORDER BY c DESC LIMIT 10; +SELECT ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3, COUNT(*) AS c FROM hits GROUP BY ClientIP, ClientIP - 1, ClientIP - 2, ClientIP - 3 ORDER BY c DESC LIMIT 10; +SELECT URL, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND DontCountHits = 0 AND IsRefresh = 0 AND URL <> '' GROUP BY URL ORDER BY PageViews DESC LIMIT 10; +SELECT Title, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND DontCountHits = 0 AND IsRefresh = 0 AND Title <> '' GROUP BY Title ORDER BY PageViews DESC LIMIT 10; +SELECT URL, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND IsRefresh = 0 AND IsLink <> 0 AND IsDownload = 0 GROUP BY URL ORDER BY PageViews DESC LIMIT 10 OFFSET 1000; +SELECT TraficSourceID, SearchEngineID, AdvEngineID, CASE WHEN (SearchEngineID = 0 AND AdvEngineID = 0) THEN Referer ELSE '' END AS Src, URL AS Dst, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND IsRefresh = 0 GROUP BY TraficSourceID, SearchEngineID, AdvEngineID, Src, Dst ORDER BY PageViews DESC LIMIT 10 OFFSET 1000; +SELECT URLHash, EventDate, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND IsRefresh = 0 AND TraficSourceID IN (-1, 6) AND RefererHash = 3594120000172545465 GROUP BY URLHash, EventDate ORDER BY PageViews DESC LIMIT 10 OFFSET 100; +SELECT WindowClientWidth, WindowClientHeight, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-01' AND EventDate <= '2013-07-31' AND IsRefresh = 0 AND DontCountHits = 0 AND URLHash = 2868770270353813622 GROUP BY WindowClientWidth, WindowClientHeight ORDER BY PageViews DESC LIMIT 10 OFFSET 10000; +SELECT DATE_TRUNC('minute', EventTime) AS M, COUNT(*) AS PageViews FROM hits WHERE CounterID = 62 AND EventDate >= '2013-07-14' AND EventDate <= '2013-07-15' AND IsRefresh = 0 AND DontCountHits = 0 GROUP BY DATE_TRUNC('minute', EventTime) ORDER BY DATE_TRUNC('minute', EventTime) LIMIT 10 OFFSET 1000; diff --git a/pgrust/query b/pgrust/query new file mode 100755 index 0000000000..b182eff761 --- /dev/null +++ b/pgrust/query @@ -0,0 +1,26 @@ +#!/bin/bash +# Reads a SQL query from stdin, runs it via psql against pgrust. +# Stdout: query result. +# Stderr: query runtime in fractional seconds on the last line (parsed from +# psql's `\timing` "Time: ms" output). +# Exit non-zero on error. +set -e + +query=$(cat) + +raw=$(PGPASSWORD=test psql -h localhost -U postgres -t -c '\timing' -c "$query" 2>&1) && exit_code=0 || exit_code=$? + +if [ "$exit_code" -ne 0 ] || printf '%s\n' "$raw" | grep -qE '^ERROR|psql: error'; then + printf '%s\n' "$raw" >&2 + exit 1 +fi + +printf '%s\n' "$raw" | grep -v '^Time:' + +ms=$(printf '%s\n' "$raw" | grep -oP 'Time:\s*\K[0-9.]+' | tail -n1) +if [ -z "$ms" ]; then + echo "no Time: in psql output" >&2 + exit 1 +fi + +awk -v m="$ms" 'BEGIN { printf "%.3f\n", m / 1000 }' >&2 diff --git a/pgrust/start b/pgrust/start new file mode 100755 index 0000000000..25c6d3ae6c --- /dev/null +++ b/pgrust/start @@ -0,0 +1,43 @@ +#!/bin/bash +set -eu + +if PGPASSWORD=test psql -h localhost -U postgres -c 'SELECT 1' >/dev/null 2>&1; then + exit 0 +fi + +# After a VM snapshot+restore, dockerd's in-memory networking/cgroup state +# is out of sync with the (also-restored) kernel-side resources, and the +# next `docker run` either fails or starts a container that can't be +# reached on its mapped port. Restarting dockerd reconciles it. No-op on +# initial provision (the daemon was just started). +sudo systemctl restart docker +for _ in $(seq 1 30); do + sudo docker info >/dev/null 2>&1 && break + sleep 1 +done + +# `docker run --rm` cleans up container on exit; we run detached. The data +# directory lives on a host mount, so it survives container restarts. +sudo docker stop pgrust >/dev/null 2>&1 || true +sudo docker rm pgrust >/dev/null 2>&1 || true + +if ! sudo docker run -d --rm -p 5432:5432 \ + -v "$(pwd)/data:/data" \ + -v "$(pwd)/db:/var/lib/postgresql/data" \ + -e POSTGRES_PASSWORD=test \ + --name pgrust malisper/pgrust:v0.1; then + echo "docker run failed; ps -a:" >&2 + sudo docker ps -a >&2 || true + exit 1 +fi + +# First-boot initdb inside the container can take a while before the server +# actually listens. pg_isready exits fast once the daemon is up, so the long +# budget only matters on failure. +for _ in $(seq 1 600); do + pg_isready -h localhost --dbname postgres -U postgres >/dev/null 2>&1 && exit 0 + sleep 1 +done +echo "pgrust did not become ready in 600 s; container logs:" >&2 +sudo docker logs pgrust 2>&1 | tail -40 >&2 || true +exit 1 diff --git a/pgrust/stop b/pgrust/stop new file mode 100755 index 0000000000..a9f63ef76d --- /dev/null +++ b/pgrust/stop @@ -0,0 +1,3 @@ +#!/bin/bash + +sudo docker stop pgrust >/dev/null 2>&1 || true diff --git a/pgrust/template.json b/pgrust/template.json new file mode 100644 index 0000000000..d05acf1706 --- /dev/null +++ b/pgrust/template.json @@ -0,0 +1,11 @@ +{ + "system": "pgrust", + "proprietary": "no", + "hardware": "cpu", + "tuned": "no", + "tags": [ + "Rust", + "row-oriented", + "PostgreSQL compatible" + ] +}