Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ez0VG-0000Tf-QR for pgsql-admin@arkaria.postgresql.org; Thu, 22 Mar 2018 13:43:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ez0VF-00053M-Mf for pgsql-admin@arkaria.postgresql.org; Thu, 22 Mar 2018 13:43:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ez0VF-000537-F0 for pgsql-admin@lists.postgresql.org; Thu, 22 Mar 2018 13:43:37 +0000 Received: from omr2.cc.ipv6.vt.edu ([2607:b400:92:8400:0:33:fb76:806e] helo=omr2.cc.vt.edu) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ez0V7-0007KJ-HC for pgsql-admin@lists.postgresql.org; Thu, 22 Mar 2018 13:43:36 +0000 Received: from mr2.cc.vt.edu (smtp.ipv6.vt.edu [IPv6:2607:b400:92:9:0:9d:8fcb:4116]) by omr2.cc.vt.edu (8.14.4/8.14.4) with ESMTP id w2MDhPqP011187 for ; Thu, 22 Mar 2018 09:43:25 -0400 Received: from mail-qk0-f198.google.com (mail-qk0-f198.google.com [209.85.220.198]) by mr2.cc.vt.edu (8.14.7/8.14.7) with ESMTP id w2MDhKsU022850 for ; Thu, 22 Mar 2018 09:43:25 -0400 Received: by mail-qk0-f198.google.com with SMTP id m188so4193643qkd.15 for ; Thu, 22 Mar 2018 06:43:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=xL6pPzPv7z1RbrliAfa+sxUJUvTcIs8ns+eVDCC+ZoQ=; b=G5rs6mnXNvwvKOvYmmM3jV+4msbR5uRWM00yhVv7XWWqNapSCEU2WdQszqweJiZBaT VqrvOo5UUZzaZR4uuhspEQA3PTNruuebtV5+ryb25G5lhorH7dl+w9G4MNN2L/abhIET n2Yo54pONqTY00jJ+J8iANfgN/O39ZUMvZaq8onwJMgEheoaYj91F2dVQgoZK5Zi4dbk Ml/VTfL2SjrK8oukQk4ItgO1MMfw3mzdNjMGQsGbi5lVw4qiCYw8Tt7FZZySr9lQJ3sI 2bgsdDh/EF96mUFI8nC7k2YpAJMqQJqIPHEovRZWlpIfmfrlf4XemWkVL38OAgB8qvE5 dZAQ== X-Gm-Message-State: AElRT7HABJM4kNEMaLhCPG89UEfBL+eC46KppQF+dp/tlwKDO8IpDyzN j4XUt/Wfy5fkw6/sDLrpzfBcMx/vTAIPMUC+D4lyokwesZgLR+DFo03pMn1pX7jAOFavBS0ChlI QPBQImw0hXhYTVYZl9VPyC6ClTF+2QQ3CNphIBULLY/sLGmi69R5qO6V5k0rENiSBSEZY2+1I/c WCEP5tb83c X-Received: by 10.237.41.129 with SMTP id o1mr35506908qtd.202.1521726200083; Thu, 22 Mar 2018 06:43:20 -0700 (PDT) X-Google-Smtp-Source: AG47ELsIsLuO6I7ENaIy+ct5O2ZGETIwIy4GbtrGB6zI07kPSmWEvJm7RfVei8lYirnhx4Y+UratuQ== X-Received: by 10.237.41.129 with SMTP id o1mr35506879qtd.202.1521726199815; Thu, 22 Mar 2018 06:43:19 -0700 (PDT) Received: from stell.cns.vt.edu ([2001:468:c80:2102:807a:2094:4aa7:2cdf]) by smtp.gmail.com with ESMTPSA id p2sm4952989qkf.71.2018.03.22.06.43.18 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Mar 2018 06:43:18 -0700 (PDT) Subject: Re: pg_stat_activity doubts To: pgsql-admin@lists.postgresql.org References: From: Ray Stell Message-ID: <12be0586-aad1-4b4d-57f6-05514c52627e@vt.edu> Date: Thu, 22 Mar 2018 09:43:18 -0400 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------133CE2591AF18ECEB492E7DA" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------133CE2591AF18ECEB492E7DA Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 3/22/18 8:45 AM, Debraj Manna wrote: > Hi > > Can someone resolve my below doubts about pg_stat_activity :- > > 1. Can someone let me know how pg_stat_activity is populated? If I am > executing a query like `Select * from pg_stat_activity` does it > produce the details from some snapshot maintained by postgres or > it checks the current connections open in postgres to provide the > details? > 2. Can some let me know what does the state IDLE denotes in the row > of pg_stat_activity? Is it like it checks all the open connections > to postgres and if the connection is not executing any query then > IDLE denotes the last query executed by the connection? > 3. In the output of select * from pg_stat_activity I am seeing rows > with state IDLE and the query_start_date more than 2 days old? > What does this signify ? Should I be worried about this? > 4. > You can use psql -E to trace the source : template1=# \d pg_stat_activity ********* QUERY ********** SELECT c.oid,   n.nspname,   c.relname FROM pg_catalog.pg_class c      LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname OPERATOR(pg_catalog.~) '^(pg_stat_activity)$'   AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 2, 3; ************************** ********* QUERY ********** SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, c.relhastriggers, c.relhasoids, '', c.reltablespace, CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, c.relpersistence, c.relreplident FROM pg_catalog.pg_class c  LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid) WHERE c.oid = '11283'; ************************** ********* QUERY ********** SELECT a.attname,   pg_catalog.format_type(a.atttypid, a.atttypmod),   (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)    FROM pg_catalog.pg_attrdef d    WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),   a.attnotnull, a.attnum,   (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t    WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation,   NULL AS indexdef,   NULL AS attfdwoptions FROM pg_catalog.pg_attribute a WHERE a.attrelid = '11283' AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum; **************************            View "pg_catalog.pg_stat_activity"       Column      |           Type           | Modifiers ------------------+--------------------------+-----------  datid            | oid                      |  datname          | name                     |  pid              | integer                  |  usesysid         | oid                      |  usename          | name                     |  application_name | text                     |  client_addr      | inet                     |  client_hostname  | text                     |  client_port      | integer                  |  backend_start    | timestamp with time zone |  xact_start       | timestamp with time zone |  query_start      | timestamp with time zone |  state_change     | timestamp with time zone |  waiting          | boolean                  |  state            | text                     |  backend_xid      | xid                      |  backend_xmin     | xid                      |  query            | text                     | --------------133CE2591AF18ECEB492E7DA Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

On 3/22/18 8:45 AM, Debraj Manna wrote:

Hi

Can someone resolve my below doubts about pg_stat_activity :-

  1. Can someone let me know how pg_stat_activity is populated? If I am executing a query like `Select * from pg_stat_activity` does it produce the details from some snapshot maintained by postgres or it checks the current connections open in postgres to provide the details?
  2. Can some let me know what does the state IDLE denotes in the row of pg_stat_activity? Is it like it checks all the open connections to postgres and if the connection is not executing any query then IDLE denotes the last query executed by the connection?
  3. In the output of select * from pg_stat_activity I am seeing rows with state IDLE and the query_start_date more than 2 days old? What does this signify ? Should I be worried about this?
  4.  

You can use psql -E to trace the source :

template1=# \d pg_stat_activity

********* QUERY **********
SELECT c.oid,
  n.nspname,
  c.relname
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname OPERATOR(pg_catalog.~) '^(pg_stat_activity)$'
  AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 2, 3;
**************************

********* QUERY **********
SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, c.relhastriggers, c.relhasoids, '', c.reltablespace, CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, c.relpersistence, c.relreplident
FROM pg_catalog.pg_class c
 LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)
WHERE c.oid = '11283';
**************************

********* QUERY **********
SELECT a.attname,
  pg_catalog.format_type(a.atttypid, a.atttypmod),
  (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
   FROM pg_catalog.pg_attrdef d
   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),
  a.attnotnull, a.attnum,
  (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t
   WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation,
  NULL AS indexdef,
  NULL AS attfdwoptions
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = '11283' AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;
**************************

           View "pg_catalog.pg_stat_activity"
      Column      |           Type           | Modifiers
------------------+--------------------------+-----------
 datid            | oid                      |
 datname          | name                     |
 pid              | integer                  |
 usesysid         | oid                      |
 usename          | name                     |
 application_name | text                     |
 client_addr      | inet                     |
 client_hostname  | text                     |
 client_port      | integer                  |
 backend_start    | timestamp with time zone |
 xact_start       | timestamp with time zone |
 query_start      | timestamp with time zone |
 state_change     | timestamp with time zone |
 waiting          | boolean                  |
 state            | text                     |
 backend_xid      | xid                      |
 backend_xmin     | xid                      |
 query            | text                     |


--------------133CE2591AF18ECEB492E7DA--