question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Multiple/redundant queries logged in the console

See original GitHub issue

Bug 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:closed
  • Created 2 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
janpiocommented, Jun 22, 2021

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.

0reactions
janpiocommented, Jun 29, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unnecessary duplicate logging of errors to console #3530
When I go to a page where the useGetMessage hook is used, I first of all don't really expect any errors in the...
Read more >
5 Common Hibernate Mistakes That Cause Dozens of ...
5 Common Hibernate Mistakes That Cause Dozens of Unexpected Queries ; 1 1. Not Initializing Lazily Fetched Associations. 1.1 ; 2 2. Using...
Read more >
Duplicate Queries - WordPress.org
FWIW, I do see two duplicate queries, but they are not the main query, and more importantly, one of the duplicates is due...
Read more >
Using the Console - Oracle Help Center
Multi -region search allows you to centrally run queries from the same location, rather than having to run a duplicate query in other...
Read more >
Log all queries in mysql - Stack Overflow
Enable Query logging on the database. SET global general_log = 1; SET global log_output = 'table'; ; View the log. select * from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found