Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hEIVU-0004Dg-MB for pgsql-general@arkaria.postgresql.org; Wed, 10 Apr 2019 19:03:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hEIVT-0004LC-4K for pgsql-general@arkaria.postgresql.org; Wed, 10 Apr 2019 19:03:35 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1hEIVS-0004L5-NI for pgsql-general@lists.postgresql.org; Wed, 10 Apr 2019 19:03:34 +0000 Received: from mail-it1-x143.google.com ([2607:f8b0:4864:20::143]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hEIVP-0004iQ-Ma for pgsql-general@postgresql.org; Wed, 10 Apr 2019 19:03:34 +0000 Received: by mail-it1-x143.google.com with SMTP id y10so5311141itc.1 for ; Wed, 10 Apr 2019 12:03:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=p5Lq2KkW4AQ2AQAy9ih6pRo9FJqKXBWn2d38Xg0k7uI=; b=yjS2N76o+uTIfMBletqhJoubcgPCEgljaFwwFA6bY1F9q5GpYetcTSACFbAOPrEfcC WjC89E7nRV+ITiPRZ1tfSvMOY0OiL7fvZ3VO2N8LY5YaGWjFjQE1nEFN97mIeHYWAqNL ylhG75jC/J20XWl7Fkp1wrjxicdYTDmTgSWZlIaHgjWxQDSXSZci0Hq9outh2Txs2Lzm kIDLhMkurItn4Q97iOY3/LbepLf2CGDyN3D9dV5i6dVXxEuu8nWHVp7/bvbOkGc0ipQb OseYNH/280qCRem7cIxorQnJqOmMPKA03NHe0nZ0/7QvnQtyZr+nZwSxrFMkdAd9qC5U iveg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=p5Lq2KkW4AQ2AQAy9ih6pRo9FJqKXBWn2d38Xg0k7uI=; b=fT70MbMl7rhFA44+iJNFZcZIzUaErSAImgxj49LzBBTEfv12ABAMjVECJw8gACaoV1 kZnOeE8kCkdzfl6pamC+iIzTq40liIdhq8ePd8sbQIXfdtBNpZO5z5MMQquapjS+pMzI R3GbTEGy7/pLLNpAXuSwLdtJJPescm5Bom4wyWjNW3JlzgwDiO2VWiw25wDaxlEmU7Jz 2nTkCd0mHrmtM5SYYViQajig9/ndJlEMz89x2c/lOeNdA8m9UI5R72CQ6nL60I8CjuAY zNTtVbYTfp0bfxHgMIuM6o+Lzzkh985IW820EP6xA4bKIFWhrj+XgowzyiNtsBnOp+CQ Hzcw== X-Gm-Message-State: APjAAAXTB0hNW1eCPlSgF3qZ+JPJE4Lvgp4JB1ZBDC24NcDIMIu4ZfAw 9plUT6bUmwLbsbi5aV3P7woNreDTD1o= X-Google-Smtp-Source: APXvYqzmwMhdkdNg/E0901Pwe4/fXhRXMH64KLaWoNaow7dWagYit6dhSrqZJmAtc+3EzzAMI/y1fQ== X-Received: by 2002:a02:928c:: with SMTP id b12mr23545034jah.8.1554923009366; Wed, 10 Apr 2019 12:03:29 -0700 (PDT) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id u19sm15044068iol.16.2019.04.10.12.03.27 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Apr 2019 12:03:28 -0700 (PDT) Received: by pryzbyj (Postfix, from userid 1000) id E22CF800FD3; Wed, 10 Apr 2019 14:03:26 -0500 (CDT) Date: Wed, 10 Apr 2019 14:03:26 -0500 From: Justin Pryzby To: pgsql-general@postgresql.org Subject: both force order of evaluation and hit index Message-ID: <20190410190326.GA31889@telsasoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I wrote a script to import CSV query logs as described here: https://www.postgresql.org/docs/current/runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-CSVLOG I also created indices for searching: |CREATE INDEX ON postgres_log_2019_04_09_2300 ((regexp_replace(message, '^duration: ([.0-9]+) ms.*', '\1')::float)) WHERE message~'^duration:' I'm having an issue that queries on long duration 1) need to specify "WHERE message~'^duration'" to hit the index; 2) need to *evaluate* that condition before the next condition: |regexp_replace(message, '^duration: ([.0-9]+) ms.*', '\1')::float))>9999 I've tried using CASE and OFFSET 0 and WITH, it seems like any effort I make to force evaluation of "duration>9999" to happen before "message~'^duration'" also causes a seq scan rather than index scan on query duration. I'm hesitating to use ORDER BY just to encourage index scan, since it doesn't even guarantee that it's going to avoid the error. This is doing index scan on other/"wrong" index: |postgres=# explain SELECT date_trunc('minute',log_time)log_time, error_severity, session_id, user_name, database, left(message,99), left(query,99) |FROM postgres_log_2019_04_10_1000 WHERE log_time>'2019-04-10 10:51' AND |CASE WHEN message~'^duration:' THEN regexp_replace(message, '^duration: ([.0-9]+) ms.*', '\1')::float ELSE NULL END > 9999 AND |(application_name!='psql' OR user_name='postgres') ORDER BY 1 DESC; | Sort (cost=74908.17..74955.87 rows=19080 width=101) | Sort Key: (date_trunc('minute'::text, log_time)) DESC | -> Bitmap Heap Scan on postgres_log_2019_04_10_1000 (cost=22.24..73551.61 rows=19080 width=101) | Recheck Cond: (log_time > '2019-04-10 10:51:00-04'::timestamp with time zone) | Filter: (((application_name <> 'psql'::text) OR (user_name = 'postgres'::text)) AND (CASE WHEN (message ~ '^duration:'::text) THEN (regexp_replace(message, '^duration: ([.0-9]+) ms.*'::text, '\1'::text))::double precision ELSE NULL::double precision END > '9999'::double precision)) | -> Bitmap Index Scan on postgres_log_2019_04_10_1000_log_time_idx (cost=0.00..17.47 rows=58197 width=0) | Index Cond: (log_time > '2019-04-10 10:51:00-04'::timestamp with time zone) This gives a nice plan, but doesn't avoid errors due to casting to float lines which don't match the duration regex. postgres=# explain SELECT date_trunc('minute',log_time)log_time, dur, error_severity, session_id, user_name, database, left(message,99), left(query,99) FROM (SELECT *,regexp_replace(message, '^duration: ([.0-9]+) ms.*', '\1')::float AS dur FROM postgres_log_2019_04_10_1000 WHERE log_time>'2019-04-10 10:50' AND message~'^duration:')x WHERE dur>9999 AND (application_name!='psql' OR user_name='postgres') ; | Bitmap Heap Scan on postgres_log_2019_04_10_1000 (cost=5214.86..50141.68 rows=19742 width=109) | Recheck Cond: ((log_time > '2019-04-10 10:50:00-04'::timestamp with time zone) AND ((regexp_replace(message, '^duration: ([.0-9]+) ms.*'::text, '\1'::text))::double precision > '9999'::double precision) AND (message ~ '^duration:'::text)) | Filter: ((application_name <> 'psql'::text) OR (user_name = 'postgres'::text)) | -> BitmapAnd (cost=5214.86..5214.86 rows=19819 width=0) | -> Bitmap Index Scan on postgres_log_2019_04_10_1000_log_time_idx (cost=0.00..17.50 rows=59462 width=0) | Index Cond: (log_time > '2019-04-10 10:50:00-04'::timestamp with time zone) | -> Bitmap Index Scan on postgres_log_2019_04_10_1000_duration_idx (cost=0.00..5187.23 rows=238241 width=0) | Index Cond: ((regexp_replace(message, '^duration: ([.0-9]+) ms.*'::text, '\1'::text))::double precision > '9999'::double precision) |(8 rows) |ERROR: invalid input syntax for type double precision: "temporary file: path "pg_tblspc/3292386340/PG_11_201809051/pgsql_tmp/pgsql_tmp16493.0", size 603619328" If I write as CTE or OFFSET 0 subq, that doesn't allow scanning on "duration" index, and scans on time index instead: |postgres=# explain analyze WITH x AS (SELECT *,regexp_replace(message, '^duration: ([.0-9]+) ms.*', '\1')::float AS dur FROM postgres_log_2019_04_10_1000 WHERE log_time>'2019-04-10 10:50' AND message~'^duration:') SELECT date_trunc('minute',log_time)log_time, dur, error_severity, session_id, user_name, database, left(message,99), left(query,99) FROM x WHERE dur>9999 AND (application_name!='psql' OR user_name='postgres') ; |postgres=# explain analyze SELECT date_trunc('minute',log_time)log_time, dur, error_severity, session_id, user_name, database, left(message,99), left(query,99) FROM (SELECT *,regexp_replace(message, '^duration: ([.0-9]+) ms.*', '\1')::float AS dur FROM postgres_log_2019_04_10_1000 WHERE log_time>'2019-04-10 10:50' AND message~'^duration:' OFFSET 0)x WHERE dur>9999 AND (application_name!='psql' OR user_name='postgres') ; Thanks in advance for any advice. Justin