Skip to content

Slow root (aka Swagger) endpoint in version 14 #4733

@ewan-escience

Description

@ewan-escience

Environment

  • PostgreSQL version: 17.9 (running in Docker)
  • PostgREST version: 14.X (running in Docker)
  • Operating system: Linux

Description of issue

I noticed that when upgrading from V13 to V14, the root endpoint, which returns data for Swagger, is very slow. On our product, the loading time has increased from < 100ms to about 3 seconds.

I tried to create a small example to demonstrate this. Do the following in a dedicated empty directory.

First create docker-compose.yml (adapted from here) with:

version: '3'
services:
  server:
    image: postgrest/postgrest:v14.6
    ports:
      - "3000:3000"
    environment:
      PGRST_SERVER_HOST: 0.0.0.0 # necessary for `postgrest --ready` flag to work
      PGRST_DB_URI: postgres://app_user:password@db:5432/app_db
      PGRST_OPENAPI_SERVER_PROXY_URI: http://127.0.0.1:3000
      PGRST_DB_ANON_ROLE: web_anon
    depends_on:
      - db
  db:
    image: postgres:17.9
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: app_db
      POSTGRES_USER: app_user
      POSTGRES_PASSWORD: password
    volumes:
      - ./init:/docker-entrypoint-initdb.d/

Then run the following commands to so the database gets initialised with some structures and start to start the database and PostgREST:

mkdir init
rm init/*;

for i in {1..100}; do
	echo "CREATE TABLE table_${i} (id UUID PRIMARY KEY);" >> init/001-createManyTables.sql;
done;

echo "CREATE EXTENSION IF NOT EXISTS pgcrypto;" > init/002-create-extensions.sql;
echo "CREATE EXTENSION IF NOT EXISTS citext;" >> init/002-create-extensions.sql;

echo "CREATE TYPE my_enum AS ENUM ('aaa', 'bbb');" > init/003-create-enums.sql;
echo "CREATE TYPE my_enum2 AS ENUM ('ccc', 'ddd');" >> init/003-create-enums.sql;

echo "CREATE FUNCTION my_function() RETURNS VOID LANGUAGE plpgsql AS \$\$BEGIN RETURN; END\$\$;" > init/004-create-functions.sql;

docker compose down --volumes && docker compose up --detach;
sleep 3;
docker compose exec db psql --dbname=app_db --username=app_user --command="create role web_anon nologin; grant usage on schema public to web_anon; grant select on all tables in schema public to web_anon;";

Visit http://localhost:3000. This takes about 800ms for me to load. This is reduced to < 100ms if the two extensions are commented out. The tables, enums and function don't seem to influence the loading time significantly. There are probably more types of objects that will slow it down even more (since I have 3 seconds loading time in my application), but I hope this is enough to expose the problem.

This also happens on PostgREST version v14.0 (I didn't try the versions in between)), but on v13.0.7, it is again fast with < 100ms.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions