---
title: "如何查看PostgreSQL正在执行的SQL"
author: "radzhang"
date: "2019-05-15T15:32:10Z"
tags: []
categories: ["数据库"]
slug: "postgresql-sql"
draft: false
lastmod: "2019-05-15T15:32:10Z"
id: 2440969
url: "https://radzhang.iteye.com/blog/2440969"
---

SELECT 

procpid, 

start, 

now() - start AS lap, 

current\_query 

FROM 

(SELECT 

backendid, 

pg\_stat\_get\_backend\_pid(S.backendid) AS procpid, 

pg\_stat\_get\_backend\_activity\_start(S.backendid) AS start, 

pg\_stat\_get\_backend\_activity(S.backendid) AS current\_query 

FROM 

(SELECT pg\_stat\_get\_backend\_idset() AS backendid) AS S 

) AS S 

WHERE 

current\_query &lt;&gt; '&lt;IDLE&gt;' 

ORDER BY 

lap DESC;