Tricking PostgreSQL into using an insane, but faster,  query plan
Deduced schema based on the naming of tables&#x2F;columns:<p><pre><code>    CREATE TABLE accounts (
        id bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
        max_public_parallelism integer NOT NULL,
        PRIMARY KEY (id)
    );
    
    CREATE TABLE worker_pools (
        id bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
        is_public boolean NOT NULL,
        PRIMARY KEY (id)
    );
    
    CREATE TABLE stacks (
        id bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
        worker_pool_id bigint NOT NULL,
        account_id bigint NOT NULL,
        PRIMARY KEY (id),
        FOREIGN KEY (worker_pool_id) REFERENCES worker_pools,
        FOREIGN KEY (account_id) REFERENCES accounts
    );
    
    CREATE TABLE runs (
        id bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
        stack_id bigint,
        worker_id bigint,
        created_at timestamptz NOT NULL DEFAULT now(),
        type integer NOT NULL,
        state integer NOT NULL,
        PRIMARY KEY (id),
        FOREIGN KEY (stack_id) REFERENCES stacks
    );</code></pre>