How to Use the pg_fastload Extension in PostgreSQL

Loading and exporting amounts of data is something that many people do in PostgreSQL. This happens especially when you need to move data for database migrations, data warehousing, reporting, or ETL workflows. PostgreSQL gives you the built-in COPY command for bulk data operations. Sometimes you need more. You might need processing, progress monitoring, flexible import and export options, or better handling of invalid or duplicate records.

The pg_fastload extension is made to meet these needs. It gives a way to transfer bulk data. In this article, we will install the pg_fastload extension, look at its functions, and walk through examples of exporting and importing data in CSV and binary formats. We will also show how to handle errors and how to watch the progress of data loads.

Clone the repository.

git clone https://github.com/yuansong/pg_fastload.git

Now, change the directory.

cybrosys@cybrosys:~$ cd pg_fastload/

Check the contents inside the directory.

cybrosys@cybrosys:~/pg_fastload$ ls

Result:

bin  COPYRIGHT  docs  include  lib  LICENSE  Makefile  msvc  NOTICE  README.md  README.zh-CN.md  RELEASE_NOTES_v1.0.0.md  RELEASE_NOTES_v1.0.0.zh-CN.md  SPECS  util

Check the current clusters in your system by using this command.

cybrosys@cybrosys:~$ pg_lsclusters 

Result:

Ver Cluster Port Status Owner    Data directory               Log file
17  main    5434 online postgres /var/lib/postgresql/17/main  /var/log/postgresql/postgresql-17-main.log
18  main    5432 online postgres /var/lib/postgresql/18/main  /var/log/postgresql/postgresql-18-main.log
18  main2   5435 online postgres /var/lib/postgresql/18/main2 /var/log/postgresql/postgresql-18-main2.log
19  main    5433 online postgres /var/lib/postgresql/19/main  /var/log/postgresql/postgresql-19-main.log

Now, compile this extension against the system-installed Postgres based on its version.

Here, we are compiling this extension against the system-installed Postgres version 18.

make USE_PGXS=1 PG_CONFIG=/usr/lib/postgresql/18/bin/pg_config

Then, install this extension against the system-installed Postgres version 18.

sudo make USE_PGXS=1 PG_CONFIG=/usr/lib/postgresql/18/bin/pg_config install

Now, check that the extension is available for the creation inside PostgreSQL like this.

select * from pg_available_extensions where name = 'pg_fastload';

Result:

    name     | default_version | installed_version |                             comment                             
-------------+-----------------+-------------------+-----------------------------------------------------------------
 pg_fastload | 1.0.0           |                   | pg_fastload is a high speed data loading utility for PostgreSQL
(1 row)

Now, create the extension.

create extension pg_fastload ;

Result:

CREATE EXTENSION

Now, check the installed version of this extension.

select * from pg_available_extensions where name = 'pg_fastload';

Result:

    name     | default_version | installed_version |                             comment                             
-------------+-----------------+-------------------+-----------------------------------------------------------------
 pg_fastload | 1.0.0           | 1.0.0             | pg_fastload is a high speed data loading utility for PostgreSQL
(1 row)

Check the functionalities of this extension.

\dx+ pg_fastload 

Result:

       Objects in extension "pg_fastload"
               Object description               
------------------------------------------------
 function pgfastload.fl_progress_rows()
 function pgfastload.pg_fastload_export(text[])
 function pgfastload.pg_fastload(text[])
 schema pgfastload
 type pgfastload.pg_fastload_progress
 type pgfastload.pg_fastload_progress[]
 view pgfastload.pg_fastload_progress
(7 rows)

Here, we can see the three functions related to this extension.

Check each function's metadata by using the command \df+

\df+ pgfastload.fl_progress_rows

Result:

List of functions
-[ RECORD 1 ]-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Schema              | pgfastload
Name                | fl_progress_rows
Result data type    | SETOF record
Argument data types | OUT pid integer, OUT backend_start timestamp with time zone, OUT started timestamp with time zone, OUT direction text, OUT datname name, OUT relname text, OUT infile text, OUT input_type text, OUT mode text, OUT state text, OUT elapsed_seconds double precision, OUT rows bigint, OUT bytes_read bigint, OUT total_bytes bigint, OUT total_rows bigint, OUT percent double precision, OUT bytes_per_second double precision, OUT rows_per_second double precision, OUT eta_seconds double precision, OUT interval_ms integer
Type                | func
Volatility          | volatile
Parallel            | unsafe
Owner               | postgres
Security            | invoker
Leakproof?          | no
Access privileges   | postgres=X/postgres
Language            | c
Internal name       | pg_fastload_progress
Description         |

Purpose:

This function can be used to track the live progress of parallel bulk copy using the functionalities of the extension named pg_fastload.

Check this function’s metadata.

\df+ pgfastload.pg_fastload_export

Result:

List of functions
-[ RECORD 1 ]-------+-------------------
Schema              | pgfastload
Name                | pg_fastload_export
Result data type    | bigint
Argument data types | options text[]
Type                | func
Volatility          | volatile
Parallel            | unsafe
Owner               | postgres
Security            | invoker
Leakproof?          | no
Access privileges   | 
Language            | c
Internal name       | pg_fastload_export
Description         | 

Purpose:

This function can be used to export data from a table to other types of files like .csv, .bin, etc.

Check this function’s metadata.

\df+ pgfastload.pg_fastload

Result:

List of functions
-[ RECORD 1 ]-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Schema              | pgfastload
Name                | pg_fastload
Result data type    | record
Argument data types | options text[], OUT skip bigint, OUT count bigint, OUT parse_errors bigint, OUT duplicate_new bigint, OUT duplicate_old bigint, OUT system_time double precision, OUT user_time double precision, OUT duration double precision
Type                | func
Volatility          | volatile
Parallel            | unsafe
Owner               | postgres
Security            | invoker
Leakproof?          | no
Access privileges   | 
Language            | c
Internal name       | pg_fastload
Description         |

Purpose:

This can be used to copy the data from the files to the corresponding tables.

Now, create a sample table for testing purposes.

CREATE TABLE public.customers (
    id         integer PRIMARY KEY,
    name       text    NOT NULL,
    city       text,
    balance    numeric(10,2),
    joined_on  date
);

Insert some sample data.

INSERT INTO public.customers (id, name, city, balance, joined_on) VALUES
    (1, 'Arjun Nair',      'Kochi',     1500.50, '2024-01-10'),
    (2, 'Meera Menon',     'Calicut',   2300.00, '2024-02-15'),
    (3, 'Rahul Varma',     'Kochi',      750.25, '2024-03-01'),
    (4, 'Anjali Pillai',   'Trivandrum',   0.00, '2024-03-20'),
    (5, 'Vishnu Das',      NULL,         120.00, '2024-04-05'),   
    (6, 'Fathima, K.',     'Kochi',     9999.99, '2024-05-12'),  
    (7, 'Joseph "Joe" M',  'Thrissur',   450.75, '2024-06-30');

Now, create a table named big_sales.

CREATE TABLE public.big_sales (
    sale_id    bigint,
    customer   integer,
    amount     double precision,
    note       text
);

Insert some huge values into this table named big_sales.

INSERT INTO public.big_sales
SELECT g, (g % 7) + 1, round((random() * 1000)::numeric, 2), 'sale number ' || g
FROM generate_series(1, 5000000) AS g;

Now, create two tables with the same structure related to the tables named customers and big_sales.

CREATE TABLE public.customers_copy (LIKE public.customers INCLUDING ALL);
CREATE TABLE public.big_sales_copy (LIKE public.big_sales);

Now, create another table named customers_bin.

CREATE TABLE public.customers_bin (
    id       integer,
    balance  double precision,
    city     varchar(12)
);

Now, use the function named pgfastload.pg_fastload_export.

SELECT pgfastload.pg_fastload_export(ARRAY[
    'TABLE=public.customers',
    'OUTPUT=/tmp/customers_data.csv',
    'TYPE=CSV',
    'HEADER=ON'
]) AS rows_written;

Result:

 rows_written 
--------------
            7
(1 row)

Here, we use this function to export target data from the table named customers to the file named customers_data.csv, and the type of this file is CSV, and the header is also enabled.

Now, inspect the data from the file named customers_data.csv.

cat /tmp/customers_data.csv

Result:

id,name,city,balance,joined_on
1,Arjun Nair,Kochi,1500.50,2024-01-10
2,Meera Menon,Calicut,2300.00,2024-02-15
3,Rahul Varma,Kochi,750.25,2024-03-01
4,Anjali Pillai,Trivandrum,0.00,2024-03-20
5,Vishnu Das,,120.00,2024-04-05
6,"Fathima, K.",Kochi,9999.99,2024-05-12
7,"Joseph ""Joe"" M",Thrissur,450.75,2024-06-30

We can see the data with their corresponding headers also.

Now, use the same function with different parameters like this.

SELECT pgfastload.pg_fastload_export(ARRAY[
    'QUERY=SELECT id, name, balance FROM public.customers WHERE city = ''Kochi'' ORDER BY id',
    'OUTPUT=/tmp/kochi_data.csv',
    'TYPE=CSV',
    'DELIMITER=|',
    'NULL=\N',
    'HEADER=ON'
]) AS rows_written;

Result:

 rows_written 
--------------
            3
(1 row)

Here, we can use the input source as a direct query also.

Now, inspect the contents inside this CSV file.

 cat /tmp/kochi_data.csv'

Result:

id|name|balance
1|Arjun Nair|1500.50
3|Rahul Varma|750.25
6|Fathima, K.|9999.99

We can also create the binary file by using this extension function named pgfastload.pg_fastload_export.

SELECT pgfastload.pg_fastload_export(ARRAY[
    'QUERY=SELECT id, balance, city FROM public.customers ORDER BY id',
    'OUTPUT=/tmp/customers_data.bin',
    'TYPE=BINARY',
    'OUT_COL=INTEGER',
    'OUT_COL=DOUBLE',
    'OUT_COL=CHAR(12) NULLIF ''############'''  ]) AS rows_written;

Result:

 rows_written 
--------------
            7
(1 row)

Inspect the content inside the .bin file.

cat /tmp/customers_data.bin

Result:

r'@Kochi       ''@Calicut     r'@Kochi       Trivandrum  ^@############''Q''''@Kochi       ,|@Thrissur   

Now, use the parallel worker option with this function.

SELECT pgfastload.pg_fastload_export(ARRAY[
    'TABLE=public.big_sales',
    'OUTPUT=/tmp/big_sales_data.csv',
    'TYPE=CSV',
    'PARALLEL_WORKERS=4'
]) AS rows_written;

Result:

 rows_written 
--------------
      5000000
(1 row)

SELECT * FROM pgfastload.pg_fastload(ARRAY[
    'INPUT=/tmp/customers_data.csv',
    'OUTPUT=public.customers_copy',
    'TYPE=CSV',
    'SKIP=1'
]);

Result:

 skip | count | parse_errors | duplicate_new | duplicate_old | system_time | user_time | duration 
------+-------+--------------+---------------+---------------+-------------+-----------+----------
    1 |     7 |            0 |             0 |             0 |    0.001056 |  0.001028 | 0.006089
(1 row)

Here, we can also skip the first N numbers of rows during the export option into the file. We use skip=1 in this query example; it means we skip the header here.

Now, check the results from the table named customers_copy like this.

select * from customers_copy ;

Result:

 id |      name      |    city    | balance | joined_on  
----+----------------+------------+---------+------------
  1 | Arjun Nair     | Kochi      | 1500.50 | 2024-01-10
  2 | Meera Menon    | Calicut    | 2300.00 | 2024-02-15
  3 | Rahul Varma    | Kochi      |  750.25 | 2024-03-01
  4 | Anjali Pillai  | Trivandrum |    0.00 | 2024-03-20
  5 | Vishnu Das     |            |  120.00 | 2024-04-05
  6 | Fathima, K.    | Kochi      | 9999.99 | 2024-05-12
  7 | Joseph "Joe" M | Thrissur   |  450.75 | 2024-06-30
(7 rows)

Now, try to import the data from the file named /tmp/customers_data.bin to the table named public.customers_bin.

SELECT * FROM pgfastload.pg_fastload(ARRAY[
    'INPUT=/tmp/customers_data.bin',
    'OUTPUT=public.customers_bin',
    'TYPE=BINARY',
    'COL=INTEGER',
    'COL=DOUBLE',
    'COL=CHAR(12) NULLIF ''############'''     
]);

Result:

 skip | count | parse_errors | duplicate_new | duplicate_old | system_time | user_time | duration 
------+-------+--------------+---------------+---------------+-------------+-----------+----------
    0 |     7 |            0 |             0 |             0 |    0.000845 |   5.3e-05 | 0.004393
(1 row)

Here, we can see the result of exporting this data in the table like this.

Now, check the results from the table named public.customers_bin.

SELECT * FROM public.customers_bin ORDER BY id;

Result:

 id | balance |    city    
----+---------+------------
  1 |  1500.5 | Kochi
  2 |    2300 | Calicut
  3 |  750.25 | Kochi
  4 |       0 | Trivandrum
  5 |     120 | 
  6 | 9999.99 | Kochi
  7 |  450.75 | Thrissur
(7 rows)

Now, export sample values into the new file named customers_dirty.csv like this.

COPY (VALUES ('8','Nikhil Raj','Kannur','300.00','2024-07-01'),
             ('9','Bad Balance','Kochi','not-a-number','2024-07-02'), 
             ('1','Duplicate Id','Kochi','1.00','2024-07-03'),          
             ('10','Sneha Paul','Kollam','880.40','2024-07-04'))
TO '/tmp/customers_dirty.csv' WITH (FORMAT csv);

Result:

COPY 4

Now, export the data from this file named customers_dirty.csv to the table named customers_copy like this.

SELECT * FROM pgfastload.pg_fastload(ARRAY[
    'INPUT=/tmp/customers_dirty.csv',
    'OUTPUT=public.customers_copy',
    'TYPE=CSV',
    'PARSE_ERRORS=10',
    'PARSE_BADFILE=/tmp/customers_dirty.bad.csv',
    'DUPLICATE_ERRORS=10',
    'DUPLICATE_BADFILE=/tmp/customers_dirty.dup.csv',
    'ON_DUPLICATE_KEEP=OLD'
]);

Result:

 skip | count | parse_errors | duplicate_new | duplicate_old | system_time | user_time | duration 
------+-------+--------------+---------------+---------------+-------------+-----------+----------
    0 |     2 |            1 |             1 |             0 |    0.001743 |         0 | 0.005006
(1 row)

Now, check the results from the file named customers_copy.

SELECT * FROM public.customers_copy ORDER BY id;

Result:

 id |      name      |    city    | balance | joined_on  
----+----------------+------------+---------+------------
  1 | Arjun Nair     | Kochi      | 1500.50 | 2024-01-10
  2 | Meera Menon    | Calicut    | 2300.00 | 2024-02-15
  3 | Rahul Varma    | Kochi      |  750.25 | 2024-03-01
  4 | Anjali Pillai  | Trivandrum |    0.00 | 2024-03-20
  5 | Vishnu Das     |            |  120.00 | 2024-04-05
  6 | Fathima, K.    | Kochi      | 9999.99 | 2024-05-12
  7 | Joseph "Joe" M | Thrissur   |  450.75 | 2024-06-30
  8 | Nikhil Raj     | Kannur     |  300.00 | 2024-07-01
 10 | Sneha Paul     | Kollam     |  880.40 | 2024-07-04
(9 rows)

Now, try a bulk data import from the file named big_sales_data.csv to the table named big_sales_copy like this.

 SELECT * FROM pgfastload.pg_fastload(ARRAY[
    'INPUT=/tmp/big_sales_data.csv',
    'OUTPUT=public.big_sales_copy',
    'TYPE=CSV',
    'TRUNCATE=YES',
    'PARALLEL=ON',
    'PARALLEL_WORKERS=4',
    'PROGRESS_NOTICE=ON'
]);

Result:

NOTICE:  pg_fastload progress: start, input 173.2 MiB, interval 1000 ms
NOTICE:  pg_fastload progress: 79.7% | rows 3,334,144 | 138.0 MiB / 173.2 MiB | 138.0 MiB/s | 3.33M rows/s | elapsed 00:00:01 | ETA 00:00:00
NOTICE:  pg_fastload progress: 100.0% | rows 5,000,000 | 173.2 MiB / 173.2 MiB | 124.3 MiB/s | 3.59M rows/s | elapsed 00:00:01 | done
 skip |  count  | parse_errors | duplicate_new | duplicate_old | system_time | user_time | duration 
------+---------+--------------+---------------+---------------+-------------+-----------+----------
    0 | 5000000 |            0 |             0 |             0 |    0.616073 |  0.790612 | 1.493765
(1 row)

Here, you can see the live progress also.

Open another psql terminal and execute the function named pgfastload.fl_progress_rows() during a bulk parallel copy query execution like the one above.

Now, you can see the live progress of this.

select * from pgfastload.fl_progress_rows();

Result:

 pid   |          backend_start           |             started              | direction | datname  |        relname        |         infile          | input_type |   mode   |  state  |  elapsed_seconds  | rows | bytes_read | total_bytes | total_rows | percent | bytes_per_second | rows_per_second | eta_seconds | interval_ms 
---------+----------------------------------+----------------------------------+-----------+----------+-----------------------+-------------------------+------------+----------+---------+-------------------+------+------------+-------------+------------+---------+------------------+-----------------+-------------+-------------
 1019662 | 2026-11-21 10:05:20.748243+05:30 | 2026-11-21 10:20:51.935517+05:30 | load      | postgres | public.big_sales_copy | /tmp/big_sales_data.csv | CSV        | parallel | running | 0.656606912612915 |    0 |          0 |   181628114 |          0 |       0 |                0 |               0 |             |        1000
(1 row)

The pg_fastload extension offers a practical solution for performing high-speed data import and export operations in PostgreSQL. Along with support for CSV and binary formats, it provides features such as parallel execution, live progress tracking, configurable error handling, and duplicate record management.

Throughout this guide, we installed the extension, examined its available functions, and tested different data loading and exporting scenarios. These capabilities make pg_fastload a useful option when working with large datasets that demand both performance and flexibility.

WhatsApp