Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dkoqY-0006vg-SQ for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Aug 2017 09:54:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dkoqY-0005Ps-F4 for pgsql-bugs@arkaria.postgresql.org; Thu, 24 Aug 2017 09:54:42 +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.84_2) (envelope-from ) id 1dkmcr-0007YX-FX for pgsql-bugs@postgresql.org; Thu, 24 Aug 2017 07:32:25 +0000 Received: from mail-ua0-x233.google.com ([2607:f8b0:400c:c08::233]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dkmcn-0003ho-Sz for pgsql-bugs@postgresql.org; Thu, 24 Aug 2017 07:32:24 +0000 Received: by mail-ua0-x233.google.com with SMTP id n29so7997582uai.5 for ; Thu, 24 Aug 2017 00:32:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=4YLJdOw6pIdDY9vAOU5xCs8kcC6uDmePuVTRhYsdXN0=; b=YfjwUPfNo8DLbX51svsR5ddDz7yEBuuIqHKe+g5p4ViM5iUMst/vTYmUW2YKJSkwY5 l7DNvaDqRTwglvDz+jGPSyfqqm1i89dflyV8A35//D/+CAkygxPkllDrOWYYOEXieI0q keve4nYoGeTmykQgIn3EAhWbRcWVOw6gnd5yczvNIGL7k//ugSBgdmOYqZtRvaziqOw0 sAZkNv5Vaa2CD/YB+IbayEKgXm1pjnxhGd9F9vPb1wcESgLxeIXvmVtCDJmYP/7za9rN yDEl/NixPeaOb4C1BHTdQ/B3+caadiA88kElQjeIYGlmKkA48iIcMWIDg7PrAppy5kz5 VPnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=4YLJdOw6pIdDY9vAOU5xCs8kcC6uDmePuVTRhYsdXN0=; b=HWMohzqL2U0sAnLfgNSp+2VaiE9oRyC2iuRcy9QD9U6yJFMsQXh+G61UtQy5mfY2EC 2yvAO8VFjJlmOek7U5u8XYTHLfV5gtnz8eNEL2CJtPZECaPEw0A09EPWEJSXWd+2E3Dy TxUFZPqne59R0OGgV+2cIlO4jYn8IY86ts6G8UupcvuIGdKGznvhteBSt4C27JTwxyv7 6u/xa5H4pVOJxSV22bxRLMoz613hoYyhgp5KtwgNbwdeSYsZm9Uc8pWS6UYiq1/wUihX uXdgGLfV3wyDQK8uRNmRYWCVLr7GCRtb1nv2LnbavEKSZvzqFgVMKci0JdYxmDs6z44J sFCw== X-Gm-Message-State: AHYfb5jiI2ryjp1r/kgmyhAswj1yZWfMZVk0YIY+7mmgnWgkF9a+Hwc5 DUAFyXplMwd1CpkH5GRZP6idpude9w== X-Received: by 10.176.5.4 with SMTP id 4mr3722105uax.166.1503559940136; Thu, 24 Aug 2017 00:32:20 -0700 (PDT) MIME-Version: 1.0 Received: by 10.31.178.82 with HTTP; Thu, 24 Aug 2017 00:32:19 -0700 (PDT) In-Reply-To: <14186.1503550801@sss.pgh.pa.us> References: <20170802103952.8425.5477@wrigleys.postgresql.org> <14186.1503550801@sss.pgh.pa.us> From: =?UTF-8?Q?Marcin_Barczy=C5=84ski?= Date: Thu, 24 Aug 2017 09:32:19 +0200 X-Google-Sender-Auth: HYQJTTLM0Djge3ouH6afWrr2kd8 Message-ID: Subject: Re: BUG #14768: CREATE INDEX CONCURRENTLY IF NOT EXISTS cancels autovacuum even if the index already exists. To: Tom Lane Cc: Michael Paquier , =?UTF-8?Q?Marcin_Barczy=C5=84ski?= , PostgreSQL mailing lists Content-Type: multipart/alternative; boundary="94eb2c12517c21699505577ad4bd" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --94eb2c12517c21699505577ad4bd Content-Type: text/plain; charset="UTF-8" On Thu, Aug 24, 2017 at 7:00 AM, Tom Lane wrote: > > I'm afraid this complaint is just wishful/sloppy thinking. Cancelling autovacuum was a problem for two reasons: 1. We ran a bunch of CREATE INDEX CONCURRENTLY IF NOT EXISTS queries on our service start up. We thought it's a no-op if an index already exists, and wanted to have some kind of background migrations: the service works normally, but some operations are slower until the index is created. 2. Autovacuum takes days/weeks in our scale (billions of rows), so effectively it never completed due to service restarts. We investigated the problem, and it turned out that most of the time was spent on vacuuming a GIST index on timestamp range. I took a look at the code, and during vacuum GIST index is traversed in a logical order which translates into random disk accesses (function gistbulkdelete in gistvacuum.c). Our index has almost 800 GB, so random accesses affect us badly. On the other hand, btree indexes are vacuumed in physical order (function btvacuumscan in nbtree.c). We replaced CREATE INDEX CONCURRENTLY IF NOT EXISTS with proper migrations. By the way, CREATE INDEX IF NOT EXISTS also cancels autovacuum task. I get your point with locks, but from a user's perspective, I don't understand why I have to resort to the following code to avoid cancelling autovacuum: DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = 'my_namespace' AND c.relname = 'my_index') THEN CREATE INDEX my_index ...; END IF; END$$; As for the long autovacuum, maybe I should report it as a separate bug. For now, I'm planning to replace all uses of 'contains' operator with the following function: CREATE OR REPLACE FUNCTION tstzrange_contains( range tstzrange, ts timestamptz) RETURNS bool AS $$ SELECT (ts >= lower(range) AND (lower_inc(range) OR ts > lower(range))) AND (ts <= upper(range) AND (upper_inc(range) OR ts < upper(range))) $$ LANGUAGE SQL IMMUTABLE; and create btree indexes on lower and upper bound: CREATE INDEX my_table_time_range_lower_idx ON my_table (lower(time_range)); CREATE INDEX my_table_time_range_upper_idx ON my_table (upper(time_range)); Is it the best approach? -- Regards, Marcin --94eb2c12517c21699505577ad4bd Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On T= hu, Aug 24, 2017 at 7:00 AM, Tom Lane <tgl@sss.pgh.pa.us> wr= ote:
I'm afraid this c= omplaint is just wishful/sloppy thinking. =C2=A0

= Cancelling autovacuum was a problem for two reasons:

1. We ran a bunch of CREATE INDEX CONCURRENTLY IF NOT EXISTS queries on = our service start up. We thought it's a no-op if an index already exist= s, and wanted to have some kind of background migrations: the service works= normally, but some operations are slower until the index is created.
=

2. Autovacuum takes days/weeks in our scale (billions o= f rows), so effectively it never completed due to service restarts. We inve= stigated the problem, and it turned out that most of the time was spent on = vacuuming a GIST index on timestamp range. I took a look at the code, and d= uring vacuum GIST index is traversed in a logical order which translates in= to random disk accesses (function gistbulkdelete in gistvacuum.c). Our inde= x has almost 800 GB, so random accesses affect us badly. On the other hand,= btree indexes are vacuumed in physical order (function btvacuumscan in nbt= ree.c).

We replaced CREATE INDEX CONCURRENTLY IF N= OT EXISTS with proper migrations.=C2=A0
By the way, CREATE INDEX = IF NOT EXISTS also cancels autovacuum task. I get your point with locks, bu= t from a user's perspective, I don't understand why I have to resor= t to the following code to avoid cancelling autovacuum:

=C2=A0 =C2=A0 DO $$
=C2=A0 =C2=A0 BEGIN
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 IF NOT EXISTS (
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 SELECT 1
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 FROM pg_class c JOIN pg_namespace= n ON n.oid =3D c.relnamespace
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 WHERE n.nspname =3D 'my_namespace&#= 39; AND c.relname =3D 'my_index') THEN
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 CREATE INDEX my_index ...;
=C2=A0 =C2=A0= =C2=A0 =C2=A0 END IF;
=C2=A0 =C2=A0 END$$;

<= div>
As for the long autovacuum, maybe I should report it as = a separate bug. For now, I'm planning to replace all uses of 'conta= ins' operator with the following function:

=C2= =A0 =C2=A0 CREATE OR REPLACE FUNCTION tstzrange_contains(
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 range tstzrange,
=C2=A0 =C2=A0 =C2=A0 =C2=A0= ts timestamptz)
=C2=A0 =C2=A0 RETURNS bool AS
=C2=A0 = =C2=A0 $$
=C2=A0 =C2=A0 SELECT (ts >=3D lower(range) AND (lowe= r_inc(range) OR ts > lower(range)))
=C2=A0 =C2=A0 =C2=A0 =C2= =A0AND (ts <=3D upper(range) AND (upper_inc(range) OR ts < upper(rang= e)))
=C2=A0 =C2=A0 $$ LANGUAGE SQL IMMUTABLE;

and create btree indexes on lower and upper bound:

=C2=A0 =C2=A0 CREATE INDEX my_table_time_range_lower_idx ON my_table= (lower(time_range));
=C2=A0 =C2=A0 CREATE INDEX my_table_time_ra= nge_upper_idx ON my_table (upper(time_range));

Is it th= e best approach?=C2=A0

--=C2=A0
Regards,
Marcin
--94eb2c12517c21699505577ad4bd--