Skip to content

Commit

Permalink
Better support for Supabase Studio index advisor
Browse files Browse the repository at this point in the history
  • Loading branch information
N2D4 committed Dec 29, 2024
1 parent 2b09cfb commit 6e0d6b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion apps/backend/src/prisma-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ async function rawQueryArray<Q extends RawQuery<any>[]>(queries: Q): Promise<[]
}, async () => {
if (queries.length === 0) return [] as any;

const query = Prisma.sql`
// Prisma does a query for every rawQuery call by default, even if we batch them with transactions
// So, instead we combine all queries into one using WITH, and then return them as a single JSON result
const withQuery = Prisma.sql`
WITH ${Prisma.join(queries.map((q, index) => {
return Prisma.sql`${Prisma.raw("q" + index)} AS (
${q.sql}
Expand All @@ -83,6 +85,11 @@ async function rawQueryArray<Q extends RawQuery<any>[]>(queries: Q): Promise<[]
`;
}), "\nUNION ALL\n")}
`;

// Supabase's index advisor only analyzes rows that start with "SELECT" (for some reason)
// Since ours starts with "WITH", we prepend a SELECT to it
const query = Prisma.sql`SELECT * FROM (${withQuery}) AS _`;

const rawResult = await prismaClient.$queryRaw(query) as { type: string, json: any }[];
const unprocessed = new Array(queries.length).fill(null).map(() => [] as any[]);
for (const row of rawResult) {
Expand Down
2 changes: 2 additions & 0 deletions docker/dependencies/postgres-with-extensions/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ RUN cd /index_advisor && make install
RUN echo "CREATE EXTENSION pg_stat_statements;" >> /docker-entrypoint-initdb.d/init.sql
RUN echo "CREATE EXTENSION hypopg;" >> /docker-entrypoint-initdb.d/init.sql
RUN echo "CREATE EXTENSION index_advisor;" >> /docker-entrypoint-initdb.d/init.sql
RUN echo "CREATE ROLE anon;" >> /docker-entrypoint-initdb.d/init.sql
RUN echo "CREATE ROLE authenticated;" >> /docker-entrypoint-initdb.d/init.sql

# Add args to Postgres entrypoint
ENTRYPOINT ["sh", "-c", "\
Expand Down

0 comments on commit 6e0d6b9

Please sign in to comment.