This question is going to be a bit specific because I have tried A LOT of things out there and none of it has worked for me. I'm hoping someone out there might have another idea.
I am working with PostgreSQL on a Mac (OS High Sierra) and I am trying to improve the performance for generating a materialized view, but can't compare my changes anymore because it seems PostgreSQL has cached the materialized view. It used to take ~12 minutes to generate the materialized view, and now it's taking less than 10 seconds (same code, I reverted the changes).
I used EXPLAIN (ANALYZE, BUFFERS) to confirm that almost all of the data getting fetched by the query to generate the materialized view is a hit (cached), and there were almost no disk reads.
I do not know if the information is cached in PostgreSQL's shared buffers or in the OS cache because at this point I've done things that I thought would have cleared both.
Here is what I have tried for emptying the PostgreSQL cache:
- Restarted PostgreSQL server using
brew services stop postgres, and thenbrew services start postgres(also tried callingsync && sudo purgein between). I confirmed with top as well as grep that postgres was no longer running. - Used
DISCARD ALL, as well as DISCARD with its other options. - Set the shared_buffers setting in postgresql.conf to the minimum (128k).
- Installed, compiled, and used pg_dropcache.
- I looked at
pg_ctlfor a bit but I'll admit I couldn't figure out how to use it. I got the errorno database directory specified and environment variable PGDATA unset, and I am not sure what to set the -D/pgdata option to for my case. VACUUM. I know this shouldn't have had an effect, but I tried it anyway.
Here is what I have tried for emptying the operating system's cache:
- Restarted computer.
- Emptied
~/Library/Cachesand/Library/Caches. sync && sudo purgeas well assync && purge.- Booted up in Safe Mode.
I have also tried a few other things that I thought would force PostgreSQL to generate the materialized view from scratch (these would have been fine since I only need to test performance in dev for now):
- Cloned the main table used in the materialized view, and generated the materialized view from the clone. It still generated within 10 seconds.
- Scrambled some column values (first_name, last_name, mem_id (not the primary key)). It still generated within 10 seconds (and the materialized view was generated correctly with the newly scrambled values).
I am stuck and do not know what to try anymore. Any ideas/help would be appreciated!