[HACKERS] Improve catcache/syscache performance. - Mailing list pgsql-hackers

From Andres Freund
Subject [HACKERS] Improve catcache/syscache performance.
Date
Msg-id 20170914061207.zxotvyopetm7lrrp@alap3.anarazel.de
Whole thread Raw
Responses Re: [HACKERS] Improve catcache/syscache performance.  (amul sul <sulamul@gmail.com>)
Re: [HACKERS] Improve catcache/syscache performance.  (Andres Freund <andres@anarazel.de>)
Re: [HACKERS] Improve catcache/syscache performance.  (Robert Haas <robertmhaas@gmail.com>)
List pgsql-hackers
Hi,

There's plenty workloads where SearchSysCache()/SearchCatCache() shows
up as a major runtime factor. Primarily in workloads with very fast
queries.

A fair while ago, before I had my commit bit, I'd posted [1]. Looking at
the profiles/benchmarks I was easily able to confirm that it still
helps, but that there's also still a lot left on the table.

Attached is a patch that tries to improve sys/catcache performance,
going further than the patch referenced earlier.

This primarily includes four pieces:

1) Avoidance of FunctionCallInfo based function calls, replaced by
   more efficient functions with a native C argument interface.
2) Only initializing the ScanKey when necessary, i.e. catcache misses,
   reduces cache unnecessary cpu cache misses.
3) Allowing the compiler to specialize critical SearchCatCache for a
   specific number of attributes allows to unroll loops and avoid
   other nkeys dependant initialization.
4) Split of the heap lookup from the hash lookup, reducing stack
   allocations etc in the common case.

There's further potential:
- replace open coded hash with simplehash - the list walk right now
  shows up in profiles.
- As oid is the only system column supported, avoid the use of
  heap_getsysattr(), by adding an explicit branch for
  ObjectIdAttributeNumber. This shows up in profiles.
- move cache initialization out of the search path
- add more proper functions, rather than macros for
  SearchSysCacheCopyN etc., but right now they don't show up in profiles.

The reason the macro wrapper for syscache.c/h have to be changed,
rather than just catcache, is that doing otherwise would require
exposing the SysCache array to the outside.  That might be a good idea
anyway, but it's for another day.

This patch gives me roughly 8% speedup in a workload that consists out
of a fast query that returns a lot of columns.  If I apply a few
other performance patches, this patch itself starts to make a bigger
difference, of around 11%.

Greetings,

Andres Freund

[1] https://www.postgresql.org/message-id/20130905191323.GC490889@alap2.anarazel.de

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Attachment

pgsql-hackers by date:

Previous
From: Andres Freund
Date:
Subject: Re: [HACKERS] More efficient truncation of pg_stat_activity querystrings
Next
From: Andres Freund
Date:
Subject: [HACKERS] SendRowDescriptionMessage() is slow for queries with a lot of columns