惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
罗磊的独立博客
F
Future of Privacy Forum
The Register - Security
The Register - Security
MyScale Blog
MyScale Blog
P
Privacy & Cybersecurity Law Blog
V
Visual Studio Blog
T
Tenable Blog
F
Fortinet All Blogs
D
Docker
V
Vulnerabilities – Threatpost
Cyberwarzone
Cyberwarzone
A
Arctic Wolf
T
Threat Research - Cisco Blogs
I
Intezer
T
Tor Project blog
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
博客园 - 司徒正美
AWS News Blog
AWS News Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
G
GRAHAM CLULEY
T
Threatpost
美团技术团队
K
Kaspersky official blog
F
Fox-IT International blog
Hugging Face - Blog
Hugging Face - Blog
Vercel News
Vercel News
P
Palo Alto Networks Blog
Google DeepMind News
Google DeepMind News
T
The Blog of Author Tim Ferriss
S
Schneier on Security
腾讯CDC
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园 - 叶小钗
I
InfoQ
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
T
True Tiger Recordings
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
J
Java Code Geeks
C
CERT Recently Published Vulnerability Notes

DEV Community

How I self-hosted my Flask app on an old laptop for almost free I built a free DSA interview prep site because I was tired of the existing options I built an AI agent that migrates Next.js Pages Router to App Router Prisma query logging y PostgreSQL: dónde termina el ORM y empieza la base From Browser to Server : The Journey of an HTTP Request (Demystifying the Web’s Infrastructure) Santa Augmentcode Intent Ep.6 I Benchmarked 17 ESLint Security Plugins. Only One Found Every Vulnerability. How to Build a High-Performance Image Optimization Pipeline in 5 Minutes 50 Linux Commands Every DevOps Engineer Must Know Less Toil, More Flow - Automating the Path from Request to Implementation The Code Review Checklist I Actually Use How I run a small blog on Astro 5 + Content Collections Git: Best Practices for Professionals How IBM Bob Became My Everyday Coding Companion Solana Passkey Wallet: Replacing Seed Phrases with SIMD-0075 I built a small browser puzzle game about arrows I wrapped Claude Code in a zsh function. Here's every decision I almost got wrong. Mobile Game Optimization: A Unity Developer's Checklist Git: Best Practices for Beginners Three days I lost chasing a ghost that was already dead on disk Why Too Many Parts Hurt ClickHouse Performance Guardrails for Agent Output: Pluggable Validation Before and After LLM Calls Gemma Forge: Local AI Without the Setup Wall From Half‑dead Prototype to Local‑Only AI Medical Assistant: Rewiring MedClinic with GitHub Copilot Runninig a forkbomb in Jenkins What’s Actually Happening When You Use Git Preventing Recursive Tool Loops in LangChain Agents Building a Rock-Paper-Scissors CLI with TypeScript — Union Types, Conditionals, and Jest Your AI Coding Agent Wastes 80% of Its Context. Fixed That with Graph Theory. Why Flutter Has Become the Go-To Framework for Fintech App Development We built a scripting language just for AI agents. Here's why. Stop building AI inboxes. Build decision layers instead. Meme Monday Why I Built @editora/ui-react? Are AI tools the next level of abstraction in software development? Identity on Solana: Your Wallet Is Your Account One API Call Changed Everything The Internet Career Nobody Talks About Enough: What Is DevRel? Solar Panel Wiring Diagram: Series vs Parallel Hello everyone! Glad to join the dev.to community I Built an AI Agent That Tailors My Resume - Here's How Agents Actually Work I Built a WhatsApp OTP + AI Chatbot Platform for African Businesses MTP Explained — And Why It Matters for Android on Mac Most Beginners Learn Full-Stack Development Backwards GitHub Glow-Up: Open Source, READMEs, Badges, Streaks, Git and gh CLI System Design Cheat Sheet: Concepts Every Developer Should Know Are Junior Developer Roles Actually Dying? A Fresher's Honest Take Using DigitalOcean Droplets as Ephemeral Sandboxes for AI Agents I built a VSCode extension that visualises your code navigation as a call tree — made for legacy codebase pain Vite predev/prebuild: chaining scripts without losing your mind A website to save you from messy browser tabs Dear Web2 Developer... Solana is here calling Postgres JSONB indexes: GIN vs BTREE on the same column The $5 AI That Remembers Everything What are your goals for the week? #180 Zettelkasten for Developers: A Practical Method That Works OpenClaw vs Hermes Agent: Stars, Downloads & Usage 2026 `act` vs. `waitFor` Global Teams Don’t Struggle With Time Zones. They Struggle With Context Python as a JavaScript Dev $5.4 Billion in Damage. 8.5 Million Machines Down. Three YAML Controls Would Have Prevented It. Here's the Structural Analysis. 🚫 Stop Using PN532 V1 for Your NFC Projects (Real Debugging Experience) Probabilistic Graph Neural Inference for smart agriculture microgrid orchestration for extreme data sparsity scenarios Inference Is Becoming the New Steady-State Cost Center Why AI-Generated Code Is Always Good Enough — And Never Great I built a dark admin dashboard template in HTML — no React, no npm, just pure HTML What is the Difference Between Lattice-Based and Hash-Based Signatures? Next.js App Router caching: revalidate, dynamic, and no-store without the folklore Next.js App Router caching: revalidate, dynamic y no-store sin folklore I built Stashly — a full-stack content manager with a rich text editor published: false tags: react, node, mongodb, typescript Why I Started Building React Projects Instead of Just Watching Tutorials ? Every Tool Eventually Becomes Tuesday Nobody Warns You That Real Software Engineering Feels Chaotic Tích hợp VNPay, Stripe trong Odoo 19 BeautifulSoup and Requests for Web Scraping With Python: When Simple Still Works I Was Stuck Debugging React — Then Developer Tools Changed It Buck Converter Ripple: Sizing the Inductor and Capacitor With Confidence AWS Just Made Its MCP Server Generally Available. Here's What It Actually Gives AI Agents. RAMPART Tests Your AI Agents in Dev. What Catches Malicious Tool Calls in Production? Vibe Team Software Engineering: What a Real AI Human Dev Team Workflow Actually Looks Like An npm Package for AI Agent Orchestration Just Shipped With Its Front Door Unlocked. Here's What the CVE Actually Reveals. Microsoft Foundry Just Added CI/CD for AI Agents. Here's What That Actually Changes. The Best Career Insurance Is a Tech Event You Don't Want to Attend Your GitHub Profile Already Tells Recruiters More Than Your Resume. Most Devs Just Don't Surface It. How to Add Execution Budgets to OpenAI Agents SDK Binary Tree Interview Problems: 6 Traversal Patterns, 15 Problems We trained a personal voice DoRA on Qwen3-8B for $1.50 — beat stock model 100% in blind A/B Stop Leaking API Keys: Why I Built a Local-First Vault for Developers 🔐 RAG Explained: How Retrieval-Augmented Generation Actually Works I Built a Fast Async JioSaavn API Wrapper in Python 🎧 chown & chgrp Deploying Your First App on Kubernetes: A Beginner's Guide (Minikube & Kind) Logs in code It's called a PR "review" for a reason DePIN GPU Market: The Failed Job Receipt Developers Should Demand Why Your AI Agent Monitoring is Wrong (And How to Fix It) Lock Down Your Cloud Shares: A Beginner’s Guide to Azure Files Security. Building a Multi-Channel Content Syndication Pipeline with EmDash Plugins Turn Your Phone Into Voice Input for Any React Text Field Which package is bloating your Docker image?
Prisma Query Logging and PostgreSQL: Where the ORM Ends and the Database Begins
Juan Torchia · 2026-05-25 · via DEV Community

Prisma Query Logging and PostgreSQL: Where the ORM Ends and the Database Begins

I turned on query logging in Prisma, watched queries rolling into the console, and assumed I had full visibility into what was happening in the database. Spoiler: I didn't.

Prisma logs show the query the client sends and how long it took from the ORM's perspective — including serialization, network, and driver overhead. What they don't show is what PostgreSQL actually does with that query on the inside: whether it used an index, whether it did a sequential scan, whether there was a lock wait, whether the planner picked a bad plan. That stuff lives in Postgres, not in the ORM.

My thesis: Prisma query logs are a pattern-debugging tool, not a database diagnostics tool. Confusing the two leads you to look for the problem in the wrong place and make optimization decisions without real evidence.


What the Official Prisma Docs Say — and What They Don't

The official Prisma logging documentation is clear about what the system offers: three log levels (INFO, WARN, ERROR) plus the special query level, which emits the SQL query, parameters, duration, and target.

The basic setup looks like this:

// Initialize the client with query logging enabled
const prisma = new PrismaClient({
  log: [
    {
      emit: 'event',   // emit as event so we can handle it ourselves
      level: 'query',
    },
    {
      emit: 'stdout',  // errors and warnings go straight to console
      level: 'error',
    },
    {
      emit: 'stdout',
      level: 'warn',
    },
  ],
})

// Listen to the query event to log with structure
prisma.$on('query', (e) => {
  console.log({
    query: e.query,       // SQL generated by Prisma
    params: e.params,     // bound parameters
    duration: e.duration, // duration in ms from the Prisma client
    target: e.target,     // datasource name (e.g. "db")
  })
})

Enter fullscreen mode Exit fullscreen mode

What the docs don't mention explicitly is that e.duration measures the time from when the Prisma client sends the query to when it gets the response back. That number includes network latency, driver parsing, result serialization, and potential connection pool contention. It is not the time PostgreSQL spent executing the query. Those are different things, and mixing them up produces bad diagnoses.

To capture real execution time in Postgres, you need pg_stat_statements or EXPLAIN ANALYZE directly on the database. Those tools live on the engine side, not the ORM side.


The Most Common Mistake: Confusing Client Duration with Postgres Execution Time

A classic pattern in teams just getting started with Prisma: they see a query with duration: 800 in the logs and conclude "this query is slow." Maybe it is. But it could also be that the query runs in 20ms inside Postgres and the remaining 780ms are pool contention, network latency, or deserialization overhead from a bloated result set.

Without separating those times, any optimization you make is speculative.

A concrete scenario where this bites you: you're querying a table with lots of columns and pulling SELECT * because Prisma, by default with findMany(), fetches every field. Execution time in Postgres might be perfectly reasonable, but transfer time and payload serialization could be what's inflating the duration you see in the log. The fix isn't an index — it's an explicit select:

// Instead of fetching all fields (default findMany behavior)
const users = await prisma.user.findMany()

// Select only what we actually need
const users = await prisma.user.findMany({
  select: {
    id: true,
    email: true,
    createdAt: true,
    // exclude heavy columns like avatarBase64, metadataJson, etc.
  },
})

Enter fullscreen mode Exit fullscreen mode

This change can drop the duration you see in logs without touching a single index. If you'd gone straight to Postgres to "optimize the query," you would've burned time hunting a problem that wasn't there.


When Prisma Logging Is Enough and When You Need to Look at PostgreSQL

This is the technical decision that matters most. Here's a criteria guide based on what each layer can and can't show you:

Prisma query logging is enough when:

  • You're spotting an N+1: you see dozens of identical queries in the log for a single request. This is where Prisma logging genuinely shines. If you want to go deeper on N+1 patterns in Server Actions, there's more context in this post on Prisma and Next.js 16.
  • You're hunting unnecessary queries: logs show you if a screen is making queries it has no business making.
  • You're verifying an explicit select works: you can confirm Prisma generates the right SQL before it ever hits the database.
  • You're debugging badly written filters: the logged query shows you whether your where clause translates the way you expect.
  • You're mapping query frequency by endpoint: with event-based emit you can count and group without any external tooling.

You need to look at PostgreSQL directly when:

  • Client duration is high but the query pattern looks correct: dig into pg_stat_statements to see real execution time in Postgres.
  • You suspect a sequential scan: EXPLAIN ANALYZE on the same query tells you if there's an index that isn't being used.
  • There are locks or deadlocks: pg_locks and pg_stat_activity are the tools. Prisma can't see any of this.
  • The problem shows up under load but not locally: that's likely pool contention or autovacuum triggering at real volume. Neither of those shows up in ORM logs.
  • You want to understand the query planner's plan: the plan can change with real data and real table statistics. Only EXPLAIN ANALYZE shows you that.
-- Run this directly in PostgreSQL to see the real execution plan
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT u.id, u.email
FROM "User" u
WHERE u.status = 'active'
ORDER BY u."createdAt" DESC
LIMIT 50;

-- BUFFERS shows how many blocks were read from disk vs cache
-- ANALYZE actually executes the query (be careful on tables with heavy writes)

Enter fullscreen mode Exit fullscreen mode


Diagnostic Checklist: Where to Start

Before optimizing anything, answer these questions in order:

1. Does the Prisma log show many queries for a single operation?
   → Yes: check for N+1, eager loading, misconfigured relation loading
   → No: keep going

2. Does the generated SQL make sense? Are we pulling columns we don't use?
   → Problem: add explicit select in Prisma
   → OK: keep going

3. Is the duration in Prisma consistently high or sporadic?
   → Sporadic: investigate pool contention, exhausted connections
   → Consistent: keep going

4. Do you have pg_stat_statements enabled in PostgreSQL?
   → No: enabling it is the next step before you continue diagnosing
   → Yes: find the query by query text and check real mean_exec_time

5. Does the execution plan use an index or a sequential scan?
   → EXPLAIN ANALYZE on the real query with real data
   → If there's a seq scan on a large table with filters, that's your problem

Enter fullscreen mode Exit fullscreen mode


Hard Limits: What You Cannot Conclude from Prisma Logs Alone

This matters and not enough people say it clearly:

  • You can't conclude "the query is slow" based only on e.duration without knowing how much of that time is Postgres vs driver overhead vs network.
  • You can't detect lock waits or deadlocks from the ORM client. A query waiting on a lock will show up with a high duration, but the reason is invisible from Prisma.
  • You can't see if autovacuum is competing with your writes. That background noise shows up as intermittent slowness that doesn't correlate with any pattern in the client log.
  • You can't validate that an index is being used without EXPLAIN. Prisma generating a correct WHERE clause doesn't guarantee Postgres will pick the index you expect.
  • You can't reproduce behavior under real load with local logs alone. The pool has a max size (configurable with connection_limit in the datasource), and contention only appears with real concurrency.

If the diagnosis requires any of those points, Prisma logs are a starting point, not the answer.


FAQ: Prisma Query Logging and PostgreSQL

How do I enable query logging in Prisma without dumping everything to stdout?

Use emit: 'event' instead of emit: 'stdout' and handle it via prisma.$on('query', handler). That way you can filter, structure, or ship it to your logging system without polluting standard output in production.

Is the duration in Prisma logs the same as execution time in PostgreSQL?

No. Prisma client duration includes serialization, network latency, and driver overhead. Real execution time in Postgres comes from pg_stat_statements or EXPLAIN ANALYZE. They can differ significantly depending on result size and network latency.

How do I enable pg_stat_statements in PostgreSQL?

Add pg_stat_statements to shared_preload_libraries in postgresql.conf, restart the server, and run CREATE EXTENSION IF NOT EXISTS pg_stat_statements; on your database. From there you can query pg_stat_statements to see real execution times per query.

Does it make sense to log queries in production?

Depends on the volume. In production with high traffic, logging every query can generate significant I/O overhead. A more sensible approach is logging only queries that exceed a duration threshold, or using OpenTelemetry with sampling. I covered observability with traces in the context of Spring Boot but the principles are the same — more detail in the OpenTelemetry post.

Does Prisma have any native way to run EXPLAIN ANALYZE?

Not natively. You can use prisma.$queryRaw to run EXPLAIN ANALYZE manually:

// Run EXPLAIN ANALYZE via queryRaw to see the real execution plan
const plan = await prisma.$queryRaw`
  EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
  SELECT id, email FROM "User" WHERE status = 'active'
`
console.log(JSON.stringify(plan, null, 2))

Enter fullscreen mode Exit fullscreen mode

This is useful in development to validate that the planner is using the indexes you expect.

If I don't see slow queries in Prisma logs, can I assume the database is fine?

No. The absence of slow queries on the client side doesn't guarantee there are no problems in Postgres. There can be table bloat, stale indexes, delayed autovacuum, or queries that run fast individually but create cumulative pressure. Database diagnostics require their own tools.


My Take: These Are Different Layers, Not Alternatives

The uncomfortable thing about this topic is that most Prisma documentation — including the official docs — shows you how to configure logging without explicitly clarifying what it measures and what it doesn't. That creates a reasonable but wrong assumption: that having query logging turned on equals having visibility into database behavior.

It doesn't. Prisma logging is ORM-layer debugging. PostgreSQL has its own observability layer and needs its own tools. Both are necessary and they complement each other, but neither replaces the other.

My practical recommendation: use Prisma logging to catch query patterns — N+1, unnecessary selects, duplicate queries per request. When the pattern looks fine and the problem persists, move to pg_stat_statements and EXPLAIN ANALYZE. Don't skip the first step because it's easier to enable, but don't stay there if the answer doesn't show up.

The concrete next step: if you have pg_stat_statements disabled on your database, that's the first thing I'd enable. Without it, you're diagnosing blind in the layer that matters most.


Original sources:


This article was originally published on juanchi.dev