Insert multiple blocks. If a block already exists, the data is
        appended. blocks must be a list of tuples where each tuple consists
        of (namespace, offset, key, data)

def insert_or_append_blocks(self, blocks):
        """Insert multiple blocks. If a block already exists, the data is
        appended. blocks must be a list of tuples where each tuple consists
        of (namespace, offset, key, data)"""
        binary = self.psycopg2.Binary
        execute = self.cursor.execute
        query = 'UPDATE gauged_data SET data = data || %s, flags = %s ' \
            'WHERE namespace = %s AND "offset" = %s AND key = %s; ' \
            'INSERT INTO gauged_data (data, flags, namespace, "offset", key)' \
            'SELECT %s, %s, %s, %s, %s WHERE NOT EXISTS (' \
            'SELECT 1 FROM gauged_data WHERE namespace = %s ' \
            'AND "offset" = %s AND key = %s)'
        for namespace, offset, key, data, flags in blocks:
            data = binary(data)
            execute(query, (data, flags, namespace, offset, key, data, flags,
                            namespace, offset, key, namespace, offset, key))