Creating an in-memory table in PostgreSQL?

Viewed 12883

My understanding of an in-memory table is a table that will be created in memory and would resort to disk as little as possible, if at all. I am assuming that I have enough RAM to fit the table there, or at least most of it. I do not want to use an explicit function to load tables (like pg_prewarm) in memory, I just want the table to be there by default as soon as I issue a CREATE TABLE or CREATE TABLE AS select statement, unless memory is full or unless I indicate otherwise. I do not particularly care about logging to disk.

7 years ago, a similar question was asked here PostgreSQL equivalent of MySQL memory tables?. It has received 2 answers and one of them was a bit late (4 years later).

One answer says to create a RAM disk and to add a tablespace for it. Or to use an UNLOGGED table. Or to wait for global temporary tables. However, I do not have special hardware, I only have regular RAM - so I am not sure how to go about that. I can use UNLOGGED feature, but as I understand, there is still quite a bit of disk interaction involved (this is what I am trying to reduce) and I am not sure if tables will be loaded in memory by default. Furthermore, I do not see how global temporary spaces are related. My understanding of them is that they are just tables in spaces that can be shared.

Another answer recommends an in-memory column store engine. And to then use a function to load everything in memory. The issue I have with this approach is that the engine being referred to looks old and unmaintained and I cannot find any other. Also, I was hoping I wouldn't have to explicitly resort to using a 'load into memory' function, but instead that everything will happen by default.

I was just wondering how to get in-memory tables now in Postgres 12, 7 years later.

2 Answers

Postgres has not in-memory tables, and I have not any information about serious work on this topic now. If you need this feature, then you can use special in-memory databases like REDIS, MEMCACHED or MonetDB. There are FDW drivers to these databases. So you can create in-memory tables in specialized database and you can work with these tables from Postgres via foreign tables.

MySQL memory tables was necessary when there was only MyISAM engine, because this engine has very primitive work with IO and MySQL had not own buffers. Now, MySQL has InnoDB engine (with modern form of joins like other databases) and lot of arguments for using MySQL in-memory tables are obsolete. Against old MySQL Postgres has own buffers and doesn't bypass file system caches, so all RAM is available for your data and you have to do nothing. Ten years ago we had to use MySQL inmemory engine to have good enough performance. But after migration to Postgres we had better performance without necessary work with inmemory tables.

If you have lot of memory, then Postgres can use it by default - via file system cache.

As This question is specific to Postgres

There is no in-memory table but in-memory view, Materialize view which can also be refreshed. See if your requirements fits in

Related