Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When this query is performed, do all the records get loaded into physical memory?

Tags:

sql

postgresql

I have a table where i have millions of records. The total size of that table only is somewhere 6-7 GigaByte. This table is my application log table. This table is growing really fast, which makes sense. Now I want to move records from log table into backup table. Here is the scenario and here is my question.

Table Log_A
Insert into Log_b select * from Log_A;
Delete from Log_A;

I am using postgres database. the question is

When this query is performed Does all the records from Log_A gets load in physical memory ? NOTE: My both of the above query runs inside a stored procedure. If No, then how will it works ?

I hope this question applies for all database.

I hope if somebody could provide me some idea on this.

like image 440
prakashpoudel Avatar asked Jan 19 '26 08:01

prakashpoudel


1 Answers

In PostgreSQL, that's likely to execute a sequential scan, loading some records into shared_buffers, inserting them, writing the dirty buffers out, and carrying on.

All the records will pass through main memory, but they don't all have to be in memory at once. Because they all get read from disk using normal buffered reads (pread) it will affect the operating system disk cache, potentially pushing other data out of the cache.

Other databases may vary. Some could execute the whole SELECT before processing the INSERT (though I'd be surprised if any serious ones did). Some do use O_DIRECT reads or raw disk I/O to avoid the OS cache affects, so the buffer cache effects might be different. I'd be amazed if any database relied on loading the whole SELECT into memory, though.

When you want to see what PostgreSQL is doing and how, the EXPLAIN and EXPLAIN (BUFFERS, ANALYZE) commands are quite useful. See the manual.

You may find writable common table expressions interesting for this purpose; it lets you do all this in one statement. In this simple case there's probably little benefit, but it can be a big win in more complex data migrations.

BTW, make sure to run that pair of queries wrapped in BEGIN and COMMIT.

like image 168
Craig Ringer Avatar answered Jan 21 '26 04:01

Craig Ringer