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 1iRiTE-0000aq-J0 for pgsql-performance@arkaria.postgresql.org; Mon, 04 Nov 2019 19:57:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iRiTD-0000vF-0l for pgsql-performance@arkaria.postgresql.org; Mon, 04 Nov 2019 19:56:59 +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 1iRiTC-0000us-JC for pgsql-performance@lists.postgresql.org; Mon, 04 Nov 2019 19:56:58 +0000 Received: from mail-yw1-xc41.google.com ([2607:f8b0:4864:20::c41]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iRiT9-0007q8-FS for pgsql-performance@lists.postgresql.org; Mon, 04 Nov 2019 19:56:57 +0000 Received: by mail-yw1-xc41.google.com with SMTP id a83so5990397ywe.11 for ; Mon, 04 Nov 2019 11:56:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=GArculHR3uJQ5qMBGpK4NEINP3NL/V64OhJSwfUBqYY=; b=e4CX7UHNjow8Y2fB61vfS1nabFkpzAXmKejrNTDODbI7LBr+upAp3cJXknwIRN+0ox 4ImeSH/JJ33wLKgO5WIMPync9HDvuQTJuC/S8p7BqDiw11WZ8R0T1z1VuhbwIe9tOPzq 5atQDk91SmBjJSv057doHt3lGwsdyk5g+S5Rjwh0ff6nKN1od3GTZA6f9pnfci4FPuJo piIzIkFPJ2MFEMzoOgqJuOQVEIG3UAw4RXelgKVOdQJWRddhx5B8BadDHlKifJCRcH+/ IcJ+zXmYxMRlcJQPXDyHWr2fz2RGKFoPkjGcNTf2HEqfADjj/zU66Tj7gVrqSA0w7AOg 4tGQ== 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:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=GArculHR3uJQ5qMBGpK4NEINP3NL/V64OhJSwfUBqYY=; b=KBdqOZsjN6cdRI/3u5BxQ4h94BEk1KsNPwPwy0oM6VXoVon3F9aXjxTXDRS8vkuFg6 mVgmOln/lDDPcDbPJEh8l7CkfzoDS5QjF6X/oQqPZLR8qP+1dlrOOAbUxQ5B4OnJuZAc LZjL03esYpacQnZYSXqqgfesBD4KeZzBaAlVQ1Lshp6upuUbZc7uD76p/djmO2huFe9W 8r94F+OCIqJXTzbochO9eREdr3wlmP9TQ5yeFopfIiRF246yt+c6lngy4gkD2lB7Ih7c bo4IUtU9QK7GFQ1wei9sprtkYvhWOVsSRinRk/MD3htnL11wR6pa2g9IUNWVZFjcxcyJ DS7A== X-Gm-Message-State: APjAAAXSf9bk+LCdpXAunpmOIj97xQZQUyy14wkdJF+sW6o0Ap2Akfkj i5ClMWncdFnFIxiictpkimL8OS3NBUM= X-Google-Smtp-Source: APXvYqxKUiSOrwXU5xYpxRX2hPgDOsEKC/22RzYcKyFLCCyc81RObqf0YYuBQpDOVqNttSNCHhXHlg== X-Received: by 2002:a81:7506:: with SMTP id q6mr21112120ywc.187.1572897412719; Mon, 04 Nov 2019 11:56:52 -0800 (PST) Received: from pryzbyj (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id m201sm7354850ywd.7.2019.11.04.11.56.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 04 Nov 2019 11:56:51 -0800 (PST) Received: by pryzbyj (Postfix, from userid 1000) id 033AC8011CA; Mon, 4 Nov 2019 13:56:49 -0600 (CST) Date: Mon, 4 Nov 2019 13:56:49 -0600 From: Justin Pryzby To: Scott Rankin Cc: "pgsql-performance@lists.postgresql.org" Subject: Re: Huge shared hit for small table Message-ID: <20191104195649.GB4999@telsasoft.com> References: <266BF5C6-03DC-404A-9240-00FA081BC0A0@motus.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <266BF5C6-03DC-404A-9240-00FA081BC0A0@motus.com> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Nov 04, 2019 at 07:38:40PM +0000, Scott Rankin wrote: > In the staging environment, we get this: > > Index Scan using "programPK" on public.program prog (cost=0.29..0.35 rows=1 width=16) (actual time=0.002..0.003 rows=1 loops=21965) > Output: prog.id, prog.version, prog.active, prog.created_date, prog.last_modified_date, prog.created_by, prog.last_modified_by, prog.client_id, prog.scheme_id, prog.name, prog.legacy_group_id, prog.custom_fields, prog.setup_complete, prog.setup_messages, prog.legacy_program_type > Index Cond: (prog.id = per.program) > Buffers: shared hit=87860 > > In the production environment, we get this: > > Index Scan using "programPK" on public.program prog (cost=0.29..0.36 rows=1 width=16) (actual time=0.017..4.251 rows=1 loops=21956) > Output: prog.id, prog.version, prog.active, prog.created_date, prog.last_modified_date, prog.created_by, prog.last_modified_by, prog.client_id, prog.scheme_id, prog.name, prog.legacy_group_id, prog.custom_fields, prog.setup_complete, prog.setup_messages, prog.legacy_program_type > Index Cond: (prog.id = per.program) > Buffers: shared hit=25437716 > > The tables in both environments are about the same size (18MB) and the indexes are about the same size (360kb/410kb) – and the shared hits are pretty much the same on the other nodes of the query between the two environments. I think it's because some heap pages are being visited many times, due to the index tuples being badly "fragmented". Note, I'm not talking about fragmentation of index *pages*, which is what pgstattuple reports (which wouldn't have nearly so detrimental effect). I could probably say that the index tuples are badly "correlated" with the heap. I'm guessing there are perhaps 25437716/87860 = 290 index tuples per page, and they rarely point to same heap page as their siblings. "Hit" means that this affects you even though it's cached (by postgres). So this is apparently slow due to reading each page ~300 times rather than once to get its tuples all at once. > This has happened one time before, and we did a “REINDEX” on the program table – and that made the problem mostly go away. Now it seems to be back, and I’m not sure what to make of it. ..which is consistent with my hypothesis. You can use pg_repack or CREATE INDEX+DROP+RENAME hack (which is what pg_repack -i does). In a fresh index, its tuples are sorted by heap TID. You could CLUSTER the table itself (or pg_repack -t) on that index column. In PG v12 you can use REINDEX CONCURRENTLY (but beware there's a crash affecting its progress reporting, fix to be included in v12.1). Justin