Skip to content

Commit 23f331a

Browse files
committed
add --postgrest-query-cost-limit option 📝
1 parent d3ad1f9 commit 23f331a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

cli.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ const {
7171
'postgrest-password': {
7272
type: 'string',
7373
},
74+
'postgrest-query-cost-limit': {
75+
type: 'string',
76+
},
7477
'import-metadata': {
7578
type: 'boolean',
7679
}
@@ -135,6 +138,12 @@ Options:
135138
https://postgrest.org/
136139
--postgrest-password Password for the PostgREST PostgreSQL user \`web_anon\`.
137140
Default: $POSTGREST_PGPASSWORD, fallback random.
141+
--postgrest-query-cost-limit Define a cost limit [1] for queries executed by PostgREST
142+
on behalf of a user. It is only enforced if
143+
pg_plan_filter [2] is installed in the database!
144+
Must be a positive float. Default: none
145+
[1] https://www.postgresql.org/docs/14/using-explain.html
146+
[2] https://github.com/pgexperts/pg_plan_filter
138147
--import-metadata Create functions returning import metadata:
139148
- gtfs_data_imported_at (timestamp with time zone)
140149
- gtfs_via_postgres_version (text)
@@ -192,6 +201,14 @@ if ('postgraphile-password' in flags) {
192201
if ('postgrest-password' in flags) {
193202
opt.postgrestPassword = flags['postgrest-password']
194203
}
204+
if ('postgrest-query-cost-limit' in flags) {
205+
const limit = parseFloat(flags['postgrest-query-cost-limit'])
206+
if (!Number.isFinite(limit) || limit < 0) {
207+
console.error('Invalid --postgrest-query-cost-limit value.')
208+
process.exit(1)
209+
}
210+
opt.lowerCaseLanguageCodes = limit
211+
}
195212

196213
pipeline(
197214
convertGtfsToSql(files, opt),

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const convertGtfsToSql = async function* (files, opt = {}) {
2929
postgraphilePassword: process.env.POSTGRAPHILE_PGPASSWORD || null,
3030
postgrest: false,
3131
postgrestPassword: process.env.POSTGREST_PASSWORD || null,
32+
// see https://github.com/pgexperts/pg_plan_filter
33+
// see also https://www.postgresql.org/docs/14/using-explain.html
34+
postgrestQueryCostLimit: null, // or float
3235
importMetadata: false,
3336
...opt,
3437
}
@@ -345,6 +348,11 @@ GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA "${opt.schema}" TO web_anon;
345348
346349
GRANT web_anon TO postgrest;
347350
351+
${opt.postgrestQueryCostLimit !== null ? `
352+
-- If pg_plan_filter is installed, limit the cost of queries made by PostgREST users.
353+
ALTER USER web_anon SET plan_filter.statement_cost_limit = ${opt.postgrestQueryCostLimit};
354+
` : ''}
355+
348356
COMMENT ON SCHEMA "${opt.schema}" IS
349357
$$GTFS REST API
350358
This REST API is created by running [PostgREST](https://postgrest.org/) on top of a [PostgreSQL](https://www.postgresql.org) DB generated using [gtfs-via-postgres](https://github.com/public-transport/gtfs-via-postgres).

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ Options:
194194
https://postgrest.org/
195195
--postgrest-password Password for the PostgREST PostgreSQL user `web_anon`.
196196
Default: $POSTGREST_PGPASSWORD, fallback random.
197+
--postgrest-query-cost-limit Define a cost limit [1] for queries executed by PostgREST
198+
on behalf of a user. It is only enforced if
199+
pg_plan_filter [2] is installed in the database!
200+
Must be a positive float. Default: none
201+
[1] https://www.postgresql.org/docs/14/using-explain.html
202+
[2] https://github.com/pgexperts/pg_plan_filter
197203
--import-metadata Create functions returning import metadata:
198204
- gtfs_data_imported_at (timestamp with time zone)
199205
- gtfs_via_postgres_version (text)

0 commit comments

Comments
 (0)