Skip to content

Commit f99bb89

Browse files
cptwunderlichBenjamin Maurer
andauthored
Fix postgrest (#10064)
* Fix response media type for plaintext. * raw-media-types option was removed. * Rename json route, as json is a reserved keyword. * Use stricter stability modifiers. * Rename update path according to wiki * Minor improvement to docker-compose file. * Minor changes. * Fix fortunes benchmark. * Add clarifying comment for content-type. * Disable 'fortunes' benchmark, as harness doesn't count queries correctly. --------- Co-authored-by: Benjamin Maurer <[email protected]>
1 parent f6e38d4 commit f99bb89

File tree

10 files changed

+49
-54
lines changed

10 files changed

+49
-54
lines changed

frameworks/Haskell/postgrest/README.md

100755100644
Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* [DB](src/db.sql)
88
* [QUERY](src/query.sql)
99
* [CACHED QUERY] Not Implemented
10-
* [UPDATE] Not Implemented
10+
* [UPDATE](src/update.sql)
1111
* [FORTUNES](src/fortunes.sql)
1212

1313
## Important Libraries
@@ -17,7 +17,7 @@ The tests were run with:
1717
## Test URLs
1818
### JSON
1919

20-
http://localhost:3000/rpc/json
20+
http://localhost:3000/rpc/jsonser
2121

2222
### PLAINTEXT
2323

@@ -31,14 +31,25 @@ http://localhost:3000/rpc/db
3131

3232
http://localhost:3000/rpc/query?queries=
3333

34-
### CACHED QUERY Not Implemented
34+
### CACHED QUERY - Not Implemented
3535

3636
http://localhost:8080/cached_query?queries=
3737

38-
### UPDATE Not Implemented
38+
### UPDATE - Not Working
3939

40-
http://localhost:3000/rpc/update?queries=
40+
http://localhost:3000/rpc/updates?queries=
4141

42-
### FORTUNES
42+
Technically, this is implemented (maybe not correctly though).
43+
However, the benchmark issues this as a GET request.
44+
PostgREST sets the transaction to READ ONLY for GET requests,
45+
as they are supposed to be idempotent.
46+
Hence this results in an error. Calling the endpoint with POST
47+
works though.
48+
49+
### FORTUNES - Disabled
4350

4451
http://localhost:3000/rpc/fortunes.html
52+
53+
This is supposed to work, but somehow the benchmark harness
54+
doesn't count the queries correctly?
55+
Was not able to figure this one out.

frameworks/Haskell/postgrest/benchmark_config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
"tests": [
44
{
55
"default": {
6-
"json_url": "/rpc/json",
6+
"json_url": "/rpc/jsonser",
77
"plaintext_url": "/rpc/plaintext",
88
"db_url": "/rpc/db",
99
"query_url": "/rpc/queries?queries=",
10-
"fortunes_url": "/rpc/fortunes.html",
1110
"port": 3000,
1211
"approach": "Realistic",
1312
"classification": "Micro",

frameworks/Haskell/postgrest/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ urls.plaintext = "/rpc/plaintext"
66
urls.json = "/rpc/json"
77
urls.db = "/rpc/db"
88
urls.query = "/rpc/queries?queries="
9+
urls.fortune = "/rpc/fortunes"
910
approach = "Realistic"
1011
classification = "Micro"
1112
database = "postgres"

frameworks/Haskell/postgrest/docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: '3'
21
services:
32
tfb-database:
43
build:
@@ -13,4 +12,6 @@ services:
1312
dockerfile: postgrest.dockerfile
1413
context: .
1514
ports:
16-
- 3030:3000
15+
- 3030:3000
16+
depends_on:
17+
- tfb-database

frameworks/Haskell/postgrest/postgrest.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ role-claim-key = "$(PGRST_ROLE_CLAIM_KEY)"
1616
max-rows = "$(PGRST_MAX_ROWS)"
1717
pre-request = "$(PGRST_PRE_REQUEST)"
1818
root-spec = "$(PGRST_ROOT_SPEC)"
19-
raw-media-types = "$(PGRST_RAW_MEDIA_TYPES)"

frameworks/Haskell/postgrest/postgrest.dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ ENV PGRST_MAX_ROWS=
2323
ENV PGRST_PRE_REQUEST=
2424
ENV PGRST_ROLE_CLAIM_KEY=.role
2525
ENV PGRST_ROOT_SPEC=
26-
ENV PGRST_RAW_MEDIA_TYPES=
2726

2827
ENV PGRST_DB_URI=postgres://benchmarkdbuser:benchmarkdbpass@tfb-database/hello_world
2928
ENV PGRST_DB_SCHEMA=public
3029
ENV PGRST_DB_ANON_ROLE=benchmarkdbuser
31-
ENV PGRST_RAW_MEDIA_TYPES="text/html, text/plain"
3230
ENV PGRST_DB_POOL=64
3331
RUN mkdir /app
3432
COPY src /app
@@ -37,4 +35,4 @@ WORKDIR /app
3735

3836
EXPOSE 3000
3937

40-
ENTRYPOINT [ "/app/entrypoint.sh" ]
38+
ENTRYPOINT [ "/app/entrypoint.sh" ]
Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
1-
CREATE TYPE fortune_t AS (id int, message text);
1+
create domain "text/html" as text;
22

3-
create or replace function fortune_template(f fortune_t) returns text as $$
4-
SELECT format('<tr><td>%s</td><td>%s</td></tr>', $1.id, regexp_replace($1.message, '<', '&lt;','g'));
5-
$$ language sql volatile;
3+
create or replace function sanitize_html(text) returns text as $$
4+
select replace(replace(replace(replace(replace($1, '&', '&amp;'), '"', '&quot;'),'>', '&gt;'),'<', '&lt;'), '''', '&apos;')
5+
$$ language sql immutable;
66

7-
create or replace function fortunes_template(fortunes fortune_t[]) returns text as $$
8-
WITH header AS (
9-
SELECT 0 as id,'<!DOCTYPE html>
10-
<html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>' as html
11-
), footer AS (
12-
SELECT 2,'</table></body></html>' as html
13-
), fortunes AS (
14-
SELECT unnest as fortune from unnest($1)
15-
), additional AS (
16-
SELECT (-1, 'Additional fortune added at request time.')::fortune_t as f
17-
), all_fortunes AS (
18-
SELECT * from (SELECT * FROM fortunes UNION ALL SELECT * from additional) p ORDER BY (fortune).message
19-
), fortunes_html AS (
20-
SELECT 1,string_agg(fortune_template(fortune), '') from all_fortunes
21-
), html AS (
22-
SELECT * FROM header UNION SELECT * FROM fortunes_html UNION SELECT * from footer ORDER BY id
23-
)
24-
SELECT string_agg(html,'') from html;
25-
$$ language sql volatile;
7+
create or replace function fortune_template("Fortune") returns text as $$
8+
SELECT format('<tr><td>%s</td><td>%s</td></tr>', $1.id, sanitize_html($1.message));
9+
$$ language sql immutable;
2610

27-
create or replace function "fortunes.html"() returns bytea as $$
28-
DECLARE
29-
fortunes fortune_t[];
30-
BEGIN
31-
SET LOCAL "response.headers" = '[{"Content-Type": "text/html"}]';
32-
SELECT array_agg(CAST((id,message) AS fortune_t)) FROM "Fortunes" INTO fortunes;
33-
RETURN convert_to(fortunes_template(fortunes), 'UTF8');
34-
END
35-
$$ language plpgsql volatile;
11+
create or replace function fortunes() returns "text/html" as $$
12+
-- This is only necessary bc. of the benchmark: The domain gives us content-type: text/html,
13+
-- but the benchmark explicitly tests for the charset in the content-type.
14+
select set_config('response.headers', '[{"Content-Type": "text/html; charset=utf-8"}]', true);
15+
16+
select '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>'
17+
|| string_agg(fortune_template(f), NULL order by f.message collate unicode asc)
18+
|| '</table></body></html>'
19+
from (select * from "Fortune" union all select 0, 'Additional fortune added at request time.') f;
20+
$$ language sql volatile;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
create function json() returns json as $$
1+
create function jsonser() returns json as $$
22
SELECT json_build_object('message', 'Hello, World!');
3-
$$ language sql volatile;
3+
$$ language sql immutable;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
create function plaintext() returns text as $$
1+
create domain "text/plain" as text;
2+
3+
create function plaintext() returns "text/plain" as $$
24
SELECT 'Hello, World!';
3-
$$ language sql volatile;
5+
$$ language sql immutable;

frameworks/Haskell/postgrest/src/update.sql

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
create or replace function update(queries text default '') returns jsonb as $$
1+
create or replace function updates(queries text default '') returns jsonb as $$
22
DECLARE
33
r "World"%ROWTYPE;
44
j jsonb := jsonb_build_array();
55
new_rnd int;
66
rnd_id int;
77
count int;
88
BEGIN
9-
SET TRANSACTION READ WRITE;
109
IF queries ~ '^[1-9]\d{0,2}$' THEN
1110
count := CAST(queries as int);
12-
ELSE
13-
count := 1;
11+
ELSE
12+
count := 1;
1413
END IF;
1514
IF count > 500 THEN
1615
count := 500;

0 commit comments

Comments
 (0)