Multiple/redundant queries logged in the console
See original GitHub issueBug description
Activating the logs for prisma displays the executed queries multiple times, when the client itself executed the API request only once. Which make me think, this is either a bug, or I’m doing something wrong. here’s an example of my logs:
prisma:info Fetched a connection from the pool
Query: BEGIN []
Query: BEGIN []
Query: BEGIN []
Query: BEGIN []
Query: BEGIN []
Query: BEGIN []
Query: BEGIN []
Query: BEGIN []
Query: BEGIN []
Query: DEALLOCATE ALL []
Query: DEALLOCATE ALL []
Query: DEALLOCATE ALL []
Query: DEALLOCATE ALL []
Query: DEALLOCATE ALL []
Query: DEALLOCATE ALL []
Query: DEALLOCATE ALL []
Query: DEALLOCATE ALL []
Query: DEALLOCATE ALL []
Query: SELECT "public"."table"."created_by" FROM "public"."table" WHERE 1=1 GROUP BY "public"."table"."created_by" OFFSET $1 [0]
Query: SELECT "public"."table"."created_by" FROM "public"."table" WHERE 1=1 GROUP BY "public"."table"."created_by" OFFSET $1 [0]
Query: SELECT "public"."table"."created_by" FROM "public"."table" WHERE 1=1 GROUP BY "public"."table"."created_by" OFFSET $1 [0]
Query: SELECT "public"."table"."created_by" FROM "public"."table" WHERE 1=1 GROUP BY "public"."table"."created_by" OFFSET $1 [0]
Query: SELECT "public"."table"."created_by" FROM "public"."table" WHERE 1=1 GROUP BY "public"."table"."created_by" OFFSET $1 [0]
Query: SELECT "public"."table"."created_by" FROM "public"."table" WHERE 1=1 GROUP BY "public"."table"."created_by" OFFSET $1 [0]
Query: SELECT "public"."table"."created_by" FROM "public"."table" WHERE 1=1 GROUP BY "public"."table"."created_by" OFFSET $1 [0]
Query: SELECT "public"."table"."created_by" FROM "public"."table" WHERE 1=1 GROUP BY "public"."table"."created_by" OFFSET $1 [0]
Query: SELECT "public"."table"."created_by" FROM "public"."table" WHERE 1=1 GROUP BY "public"."table"."created_by" OFFSET $1 [0]
Query: COMMIT []
Query: COMMIT []
Query: COMMIT []
Query: COMMIT []
Query: COMMIT []
Query: COMMIT []
Query: COMMIT []
Query: COMMIT []
Query: COMMIT []
How to reproduce
I’m using Next.js for the frontend and backend. Defining the client as mentioned below and making a request from the client/insomnia/postman will trigger one API call, which will affect the Database once (I can say that because this happens also with INSERT queries bounded with unique constraints), but the executed queries will be logged multiple times.
Expected behavior
The query should be logged only once.
Prisma information
This is how I intiate the prisma client (in a config file globaly and then import it everywhere):
import { Prisma, PrismaClient } from '@prisma/client';
const prismaClientOptions: Prisma.PrismaClientOptions = {
log: [
{
emit: 'event',
level: 'query',
},
{
emit: 'stdout',
level: 'error',
},
{
emit: 'stdout',
level: 'info',
},
{
emit: 'stdout',
level: 'warn',
},
],
};
/* this is needed to avoid creating new client on hot reload of nextjs
* issue --> https://github.com/prisma/prisma/issues/1983
*/
let prisma: PrismaClient;
if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient(prismaClientOptions);
} else {
//@ts-ignore
if (!global.prisma) {
//@ts-ignore
global.prisma = new PrismaClient(prismaClientOptions);
}
//@ts-ignore
prisma = global.prisma;
}
if (!process.env.NO_PRISMA_LOGGING) {
//@ts-ignore
prisma.$on('query', e => {
//@ts-ignore
console.log('Query: ' + e.query, e.params);
//@ts-ignore
if (e.duration > 1000) {
//@ts-ignore
console.log('Duration: ' + e.duration + 'ms');
}
});
}
export { prisma, Prisma };
I’m also defining my DATABASE_URL
with both of the following parameters:
- connection_limit=1
- pgbouncer=true
Environment & setup
- OS: Debian
- Database: PostgreSQL
- Node.js version: v14.16.0
Prisma Version
prisma : 2.23.0
@prisma/client : 2.23.0
Current platform : debian-openssl-1.1.x
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (7 by maintainers)
Top GitHub Comments
It says that it fetched a connection from the connection pool. That is the only place where it can get a connection from - so this is all correct.
Then the next step would be for you to create a new project, and add all the necessary code to get to a minimal reproduction. From the code snippets you posted I can not identify the problem, so I would need to have a codebase to experience it myself and then look at what might be going on via debugging. That usually is quite effective and lets me skip the “I can not even reproduce this” step when I jsut try to build this myself.