1. Overview
In my previous blog, I briefly walked through how the bulk/batch insertion was done for postgres_fdw in PG14. In this blog, I am going to run some basic tests to compare the performance for before and after the batch insertion was introduced in postgres_fdw, so that we can have a general idea about whether this feature makes any difference.
2. PG Servers Setup
The key of the blog is to see if there is any difference for batch insertion. To make the testing simple, here is how I set up a simple environment.
As this bulk/batch insertion was introduced for PG14, so we need to switch to the stable PG14 branch, i.e. REL_14_STABLE. After checked out the source code, simply run the commands: configure, make and make install
. Here are the commands used in this blog.
1 | ./configure --prefix=$HOME/sandbox/postgres/pgapp --enable-tap-tests --enable-debug CFLAGS="-g3 -O0" |
In order to test Foreign Data Wrapper, we need to start another PG Server. To make it easy, I simply start a PG Server on a Foreign data cluster and change the default port to a different one, for example, 5433
. Below are the commands used to setup Foreign Server.
1 | export FPGDATA=$HOME/sandbox/postgres/pgdata2 |
After the Foreign data cluster has been initialized, change the port to 5433, then start the Foreign PG Server.
1 | vim $FPGDATA/postgresql.conf |
3. Foreign Tables Setup
Now, we can setup the basic Foreign Data Wrapper testing environment like below.
On the Local PG Server:
3.1. Create a Foreign Server using default batch settings
1 | postgres=# create server fs foreign data wrapper postgres_fdw options (dbname 'postgres', host '127.0.0.1', port '5433'); |
3.2. Create the user mapping
1 | postgres=# create user mapping for david server fs options( user 'david'); |
3.3. Create Foreign Tables on Local PG Server
1 | postgres=# create foreign table ft (id int, test text) server fs options(table_name 't'); |
By default, the batch insertion size has been set to 1 as you can see from the source code.
1 | /* |
Now, repeate the process to create another two Foreign Tables with different batch size correspondingly, i.e. 10 and 100.
1 | postgres=# create foreign table ft_batch10 (id int, test text) server fs options(batch_size '10', table_name 't10'); |
3.4. Create Tables on Foreign Server
On the Foreign PG Server side, create corresponding tables like below. Notes, you need to make the table names match the ones used in Local PG Server.
1 | psql -d postgres -p 5433 |
4. Run the tests
Now, enable the timeing on Local PG Server, and simply run the commands below, and then record the timing for comparison.
1 | postgres=# \timing |
5. Check the results
Here are results from about tests.
1 | 1 10 100 |
6. Summary
In this blog, we simply run some basic tests to see if there is any performance improvement in postgres_fdw for the batch/bulk insertion. The results are very impressive: for 1k insertion, batch size 10 and 100 are 5 and 2 times and faster relatively; for 1 million insertion, batch size 10 and 100 are 5.5 and 6.5 times faser; for 100 millions insertion, batch size 10 and 100 are 5 and 9.5 times better.