Skip to content

Commit

Permalink
add pgbench test
Browse files Browse the repository at this point in the history
Also for pipeline mode
  • Loading branch information
steve-chavez committed Mar 9, 2023
1 parent da1dd80 commit 412b414
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
15 changes: 15 additions & 0 deletions test/pgbench/1567/new.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
INSERT INTO "test"."complex_items"("arr_data", "field-with_sep", "id", "name")
SELECT pgrst_body."arr_data", pgrst_body."field-with_sep", pgrst_body."id", pgrst_body."name"
FROM (
SELECT '[{"id": 4, "name": "Vier"}, {"id": 5, "name": "Funf", "arr_data": null}, {"id": 6, "name": "Sechs", "arr_data": [1, 2, 3], "field-with_sep": 6}]'::jsonb as json_data
) pgrst_payload,
LATERAL (
SELECT CASE WHEN jsonb_typeof(pgrst_payload.json_data) = 'array' THEN pgrst_payload.json_data ELSE jsonb_build_array(pgrst_payload.json_data) END AS val
) pgrst_uniform_json,
LATERAL (
SELECT jsonb_agg(jsonb_build_object('field-with_sep', 1) || elem) AS vals from jsonb_array_elements(pgrst_uniform_json.val) elem
) pgrst_json_defs,
LATERAL (
SELECT * FROM jsonb_to_recordset (pgrst_json_defs.vals) AS _ ("arr_data" integer[], "field-with_sep" integer, "id" bigint, "name" text)
) pgrst_body
RETURNING "test"."complex_items".*;
12 changes: 12 additions & 0 deletions test/pgbench/1567/old.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
INSERT INTO "test"."complex_items"("arr_data", "field-with_sep", "id", "name")
SELECT pgrst_body."arr_data", pgrst_body."field-with_sep", pgrst_body."id", pgrst_body."name"
FROM (
SELECT '[{"id": 4, "name": "Vier"}, {"id": 5, "name": "Funf", "arr_data": null}, {"id": 6, "name": "Sechs", "arr_data": [1, 2, 3], "field-with_sep": 6}]'::jsonb as json_data
) pgrst_payload,
LATERAL (
SELECT CASE WHEN jsonb_typeof(pgrst_payload.json_data) = 'array' THEN pgrst_payload.json_data ELSE jsonb_build_array(pgrst_payload.json_data) END AS val
) pgrst_uniform_json,
LATERAL (
SELECT * FROM jsonb_to_recordset (pgrst_uniform_json.val) AS _ ("arr_data" integer[], "field-with_sep" integer, "id" bigint, "name" text)
) pgrst_body
RETURNING "test"."complex_items".*
17 changes: 15 additions & 2 deletions test/pgbench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
Can be used as:

```
postgrest-with-postgresql-15 -f test/pgbench/fixtures.sql pgbench -n -T 10 -f test/pgbench/2677/old.sql
postgrest-with-postgresql-15 -f test/pgbench/fixtures.sql pgbench -n -T 10 -f test/pgbench/2677/new.sql
postgrest-with-postgresql-15 -f test/pgbench/fixtures.sql pgbench -n -T 10 -f test/pgbench/1567/old.sql
postgrest-with-postgresql-15 -f test/pgbench/fixtures.sql pgbench -n -T 10 -f test/pgbench/1567/new.sql
```

Pipeline mode needs the extended query protocol:

```
postgrest-with-postgresql-15 -f test/pgbench/fixtures.sql pgbench -M extended -n -T 10 -f test/pgbench/pipeline_mode/old.sql
postgrest-with-postgresql-15 -f test/pgbench/fixtures.sql pgbench -M extended -n -T 10 -f test/pgbench/pipeline_mode/new.sql
```

## Directory structure

The directory name is the issue number on github.
30 changes: 30 additions & 0 deletions test/pgbench/pipeline_mode/new.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
\startpipeline

select
set_config('search_path', 'test', true),
set_config('role', 'postgrest_test_anonymous', true),
set_config('request.jwt.claims', '{"role":"postgrest_test_anonymous"}', true),
set_config('request.method', 'POST', true),
set_config('request.path', '/complex_items', true),
set_config('request.headers', '{"host":"localhost:3000","content-type":"application/json","accept":"application/json, */*;q=0.5","user-agent":"HTTPie/2.6.0","content-length":"151","accept-encoding":"gzip, deflate, br","connection":"keep-alive"}', true),
set_config('request.cookies', '{}', true);

WITH pgrst_source AS (
WITH
pgrst_payload AS (SELECT '[{"id": 4, "name": "Vier"}, {"id": 5, "name": "Funf", "arr_data": null}, {"id": 6, "name": "Sechs", "arr_data": [1, 2, 3], "field-with_sep": 6}]'::json AS json_data),
pgrst_body AS ( SELECT CASE WHEN json_typeof(json_data) = 'array' THEN json_data ELSE json_build_array(json_data) END AS val FROM pgrst_payload)
INSERT INTO "test"."complex_items"("arr_data", "field-with_sep", "id", "name")
SELECT "arr_data", "field-with_sep", "id", "name"
FROM json_to_recordset ((SELECT val FROM pgrst_body)) AS _ ("arr_data" integer[], "field-with_sep" integer, "id" bigint, "name" text)
RETURNING "test"."complex_items".*
)
SELECT
'' AS total_result_set,
pg_catalog.count(_postgrest_t) AS page_total,
array[]::text[] AS header,
coalesce(json_agg(_postgrest_t), '[]')::character varying AS body,
nullif(current_setting('response.headers', true), '') AS response_headers,
nullif(current_setting('response.status', true), '') AS response_status
FROM (SELECT "complex_items".* FROM "pgrst_source" AS "complex_items") _postgrest_t;

\endpipeline
26 changes: 26 additions & 0 deletions test/pgbench/pipeline_mode/old.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
select
set_config('search_path', 'test', true),
set_config('role', 'postgrest_test_anonymous', true),
set_config('request.jwt.claims', '{"role":"postgrest_test_anonymous"}', true),
set_config('request.method', 'POST', true),
set_config('request.path', '/complex_items', true),
set_config('request.headers', '{"host":"localhost:3000","content-type":"application/json","accept":"application/json, */*;q=0.5","user-agent":"HTTPie/2.6.0","content-length":"151","accept-encoding":"gzip, deflate, br","connection":"keep-alive"}', true),
set_config('request.cookies', '{}', true);

WITH pgrst_source AS (
WITH
pgrst_payload AS (SELECT '[{"id": 4, "name": "Vier"}, {"id": 5, "name": "Funf", "arr_data": null}, {"id": 6, "name": "Sechs", "arr_data": [1, 2, 3], "field-with_sep": 6}]'::json AS json_data),
pgrst_body AS ( SELECT CASE WHEN json_typeof(json_data) = 'array' THEN json_data ELSE json_build_array(json_data) END AS val FROM pgrst_payload)
INSERT INTO "test"."complex_items"("arr_data", "field-with_sep", "id", "name")
SELECT "arr_data", "field-with_sep", "id", "name"
FROM json_to_recordset ((SELECT val FROM pgrst_body)) AS _ ("arr_data" integer[], "field-with_sep" integer, "id" bigint, "name" text)
RETURNING "test"."complex_items".*
)
SELECT
'' AS total_result_set,
pg_catalog.count(_postgrest_t) AS page_total,
array[]::text[] AS header,
coalesce(json_agg(_postgrest_t), '[]')::character varying AS body,
nullif(current_setting('response.headers', true), '') AS response_headers,
nullif(current_setting('response.status', true), '') AS response_status
FROM (SELECT "complex_items".* FROM "pgrst_source" AS "complex_items") _postgrest_t;

0 comments on commit 412b414

Please sign in to comment.