Skip to content
This repository was archived by the owner on Sep 23, 2026. It is now read-only.

Modify worker query to return ids of inserted records - #1

Merged
ploomiz merged 11 commits into
masterfrom
make_query_return_ids
Mar 19, 2018
Merged

ploomiz merged 11 commits into
masterfrom
make_query_return_ids

Conversation

@ploomiz

@ploomiz ploomiz commented Mar 9, 2018 •

Copy link
Copy Markdown

These changes add a "RETURNING id" clause to the end of a bulk insert query, causing the ids of inserted records to be returned. For example, you could get the returned ids like this:

worker = Billings::Progress.bulk_insert
worker.add(params)
worker.save!
worker.result_sets # array of query result sets (one for each call to save!) that contain ids

Here's a link to the docs describing the RETURNING clause

@ploomiz ploomiz added the enhancement New feature or request label Mar 9, 2018
@ploomiz ploomiz self-assigned this Mar 9, 2018
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default

@ploomiz ploomiz Mar 9, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is only used for a dummy test database. I changed it from the default SQLite db to postgres, since only postgres allows the "RETURNING" clause.

Comment thread lib/bulk_insert.rb Outdated
nil
else
worker
[]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i assume that no one in procore is consuming the result of bulk insert, whether using the values, block or neither variant. cause before looks like we used to return the worker and now we return the result

Comment thread lib/bulk_insert/worker.rb Outdated
end

self
result || []

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so now if you are a block, bulk_insert returns a collection, but if you are the 'value' variant, you get a worker back right?

@ploomiz ploomiz Mar 10, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the lib/bulk_insert file, save! is called after the block and after Model.bulk_insert values: some_values. So you should get back a collection with either way.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we have a liskov violation now right if you aren't a block or value variant

@ploomiz ploomiz Mar 10, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean because bulk_insert returns a BulkInsert::Worker if no block or values are passed but returns a collection otherwise?

@ploomiz ploomiz Mar 10, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see how this is a problem. I'm going to change things so that the result is stored on the worker in an ivar that is readable. Then we don't have to make any changes to what is returned; the worker will still be returned, but we can fetch the result from the worker.

Comment thread lib/bulk_insert/worker.rb Outdated
def execute_query
if query = compose_insert_query
result_set = @connection.execute(query)
@result_sets.push(result_set)

@ploomiz ploomiz Mar 10, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There can be multiple result sets because bulk_insert inserts records in batches of 500. For example, if you have to insert 1000 records, save! would be called for the first 500 and the result set containing the ids would be pushed onto @result_sets. Same thing for the second 500.

The reason for using an array of result sets instead of an array of the results is so that the worker doesn't have to build a new array of 1000 items; it can just build an array of 2 result sets returning from the query. Of course, if we care about the returned ids, we'll still have to use @result_sets go through all 1000 results. But most of the time we don't care about the returned ids.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a great solution. I love that it doesn't break the existing interface and therefore reduces the risk of this change we are making.

My last question is, if the inserts happen in series of 500, how can the consumer of the worker know that there are no outstanding series to run. I.e. if you call bulk insert and immediately call worker.result_sets, how we be sure bulk insert has finished.

Maybe the answer is that the worker is not actually executing in an async manner, and that when bulk_insert returns, all queries have already been executed

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh yes, i just dug into the implementation of worker. It appears as then when save! is called it really does execute and then return. batches are run on the fly as the number of records in the worker increases.

This works great then!

Comment thread test/bulk_insert/worker_test.rb Outdated
%w(greeting age happy created_at updated_at color)
)
worker.add greeting: "first"
worker.add geeting: "second"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does geeting column exist? I'm not sure why a result is being returned for this.

@ploomiz ploomiz Mar 13, 2018 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. For that one, it's probably inserting a record with nulls in all columns.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is why a result is still returned

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya just confirmed that's what's going on. I'll fix the typo though.

Comment thread lib/bulk_insert/worker.rb Outdated
end

def primary_key_return_statement
if adapter_name =~ /\APostgreSQL/i

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The on_conflict_statement supports both postgresql and postgis as adapter names. Perhaps line 143 should use the same logic? postgis is a postgresql extension, and thus also supports RETURNING.

Comment thread lib/bulk_insert/worker.rb
if !rows.empty?
sql << rows.join(",")
sql << on_conflict_statement
sql << primary_key_return_statement

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there may be a use case for making returning primary keys optional so we only do it when we need to. It would at least give more confidence that this is not going to break an existing places we use bulk insert in case we rely on the return value somewhere.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another reason to make it optional is so that the return isn't different depending on what backend you happen to be using. Inconsistency in interfaces should be opt in.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the primary_key_return_statement method to make it opt-in

Comment thread lib/bulk_insert/worker.rb Outdated

def primary_key_return_statement
if adapter_name =~ /\APostgreSQL/i
' RETURNING id'

@numeraltwo numeraltwo Mar 14, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id is just the default primary key. Consider adding primary_key as an argument to the constructor for BulkInsert::Worker and assign it in BulkInsert::ClassMethods L#9, e.g.

worker = BulkInsert::Worker.new(connection, table_name, primary_key,  columns, set_size, ignore, update_duplicates)

Comment thread lib/bulk_insert/worker.rb Outdated

def execute_query
if query = compose_insert_query
result_set = @connection.execute(query)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

execute returns the raw result from the adapter, in our case PG::Result. There is a long story here, but the TL;DR is PG::Result has a quasi-memory leak so consider using exec_query when possible, which returns an ActiveRecord::Result.

Comment thread lib/bulk_insert.rb Outdated
def bulk_insert(*columns, values: nil, set_size:500, ignore: false, update_duplicates: false, return_primary_keys: false)
columns = default_bulk_columns if columns.empty?
worker = BulkInsert::Worker.new(connection, table_name, columns, set_size, ignore, update_duplicates)
worker = BulkInsert::Worker.new(connection, table_name, self.primary_key, columns, set_size, ignore, update_duplicates, return_primary_keys)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self on self.primary_key is redundant and not required

@numeraltwo numeraltwo left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍
perfect!

Comment thread lib/bulk_insert/worker.rb
# INSERT IGNORE only fails inserts with duplicate keys or unallowed nulls not the whole set of inserts
@ignore = ignore
@update_duplicates = update_duplicates
@return_primary_keys = return_primary_keys

@cagmz cagmz Mar 19, 2018 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nb: I think return_primary_keys is redundant. Can return_primary_keys = true be derived based on whether a user passes in an argument for primary_key?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user never passes primary_key. primary_key is retrieved from the active record class. So it's always there.

@ploomiz
ploomiz merged commit 7322020 into master Mar 19, 2018
@ploomiz
ploomiz deleted the make_query_return_ids branch March 19, 2018 22:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants