Loading Terabytes of Data From Postgres Into BigQuery

We take a look at how you can ingest large amounts of data from a PostgreSQL database using the popular BigQuery framework.

Xavier Fox
Radio Africa TechBlog
3 min readMay 28, 2019

--

Despite the fact that an ETL task is pretty challenging when it comes to loading big data sets, there’s still the scenario in which you can load terabytes of data from Postgres into BigQuery relatively easily and very efficiently. This is the case when you have a lot of immutable data distributed in tables by some timestamp. For example, a transactions table with a created_attimestamp column. BigQuery and Postgres have great tools in order to do this pretty quickly and conveniently.

Preparing Postgres Tables

BigQuery's Capacitor storage format, like many other big data formats, is optimized for a one-time write of an entire table. To distribute data between tables, BigQuery heavily relies on the wild card tables pattern. Typical usage is to create tables with names suffixed by some field value. Usually, date suffixes are used for this purpose. For example, the table transactions20180301 can contain transactions created on March 1, 2018. This approach allows both for the creation of very efficient storage on the BigQuery side, and for ease of loading the data source on the storage side.

In order to implement this loading approach, we need to ensure we have the right indexes in the Postgres database before we start loading processes from Postgres into BigQuery. In order to create this, you can use PSQL to connect to your database. Let’s create an index for a created_attimestamp in the transactions table. Assuming we’re dealing with an Ubuntu machine that hosts theDBb instance, it can be as simple as:

Index creation can take a while if you have a lot of data. You can track process execution using toputility. You should repeat index creation for each table you’re going to load into BigQuery.

Loading Data Into BigQuery

To load data into BigQuery we’re going to use BigQuery CLI, which is a very versatile tool. You can install it using these instructions. As we’re on Linux, we’ll be using bash script in order to perform all the work. I assume BigQuery CLI is installed and authorized.

Let’s create bigquery-upload.sh and add the following function in order to upload a single day from a specific table:

This function has three arguments: table, columns for selection, and date to upload. As you can see, it uses the \copy operation to download a CSV from Postgres and then compresses it. The BigQuery docs say the loading of a compressed CSV is slower than uncompressed, but uploading uncompressed data almost always seems slower.

You can call this function by simply adding a line at the end of the script:

This will create the transactions20180301 table with one day of data. To automate the uploading process we can introduce another function:

This upload_table function has 4 arguments: table name, columns for select, start dates, and end dates.

You can make this script parametrized by adding upload_table "$1" '*' "$2" "$3" as the last line so that the entire script will be:

Don’t forget to $ chmod +x bigquery-upload.sh your script. After it, you can run it as:

The great thing about this script of loading data from Postgres into BigQuery is you can use it to upload just a single day and it can be put in the /etc/cron.daily or /etc/cron.hourly directory so that you can get your data in nearly real time. The --replace option script can be run more than once for the same day to replace data as it is refreshed.

If all is done right, it should load your terabyte of data in a day or so. Please do not hesitate to leave your comments!

--

--

Xavier Fox
Radio Africa TechBlog

S.R.E | Gooner | Quirky | what is dead may never die! | #ilovenairobi