Despite the fact that an ETL task is pretty challenging when it comes to loading Big Data, there’s still the scenario in which you can load terabytes of data from Postgres into BigQuery relatively easy 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_at timestamp column. BigQuery and Postgres have great tools in order to do this pretty fast and conveniently.

Preparing Postgres Tables

BigQuery Capacitor storage format, as 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 for both 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_at timestamp in the transactions table. Assuming we’re dealing with an ubuntu machine that hosts the db instance, it can be as simple as:

$ sudo su - postgres
$ psql yourdbname
yourdbname=>create index transactions_created_at on transactions (created_at);

Index creation can take a while if you have a lot of data. You can track process execution using top utility. 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:

#!/bin/bash
function upload_day {
table=$1
sel=$2
day=$3
next_day=$(date -d "$day+1 days" +%Y-%m-%d)
bq_suffix=$(date -d "$day" +%Y%m%d)
echo "Uploading $table: $day..."
psql -c "\\copy (select $sel from $table where created_at >= '$day' and created_at < '$next_day') TO '$table-$day.csv' WITH CSV HEADER"
gzip $table-$day.csv
bq load --allow_quoted_newlines --project_id --replace --source_format=CSV --autodetect --max_bad_records 100 .$table$bq_suffix $table-$day.csv.gz
rm $table-$day.csv.gz
};

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. BigQuery docs says loading of a compressed CSV is slower than uncompressed, but uploading uncompressed data almost always seems slower.

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

upload_day 'transactions' '*' '2018-03-01'

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

function upload_table {
t=$1
s=$2
start_date=$3
end_date=$4
while [ "$start_date" != "$end_date" ]; do
upload_day "$t" "$s" "$start_date"
start_date=$(date -d "$start_date+1 days" +%Y-%m-%d)
done
}

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 entire script will be:

#!/bin/bash
function upload_day {
table=$1
sel=$2
day=$3
next_day=$(date -d "$day+1 days" +%Y-%m-%d)
bq_suffix=$(date -d "$day" +%Y%m%d)
echo "Uploading $table: $day..."
psql -c "\\copy (select $sel from $table where created_at >= '$day' and created_at < '$next_day') TO '$table-$day.csv' WITH CSV HEADER"
gzip $table-$day.csv
bq load --allow_quoted_newlines --project_id --replace --source_format=CSV --autodetect --max_bad_records 100 .$table$bq_suffix $table-$day.csv.gz
rm $table-$day.csv.gz
};
function upload_table {
t=$1
s=$2
start_date=$3
end_date=$4
while [ "$start_date" != "$end_date" ]; do
upload_day "$t" "$s" "$start_date"
start_date=$(date -d "$start_date+1 days" +%Y-%m-%d)
done
}
upload_table "$1" '*' "$2" "$3"

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

$ ./bigquery-upload.sh "transactions" "2017-01-01" "2018-03-01"

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 contact us and we would love to advice you.