PGObject::Util::BulkLoad - Bulk load records into PostgreSQL
To insert all rows into a table using COPY: my ($dbh, @objects); PGObject::Util::BulkLoad->copy( {table => 'mytable', insert_cols => ['col1', 'col2'], dbh => $dbh}, @objects );
To copy to a temp table and then upsert: my ($dbh, @objects); PGObject::Util::BulkLoad->upsert( {table => 'mytable', insert_cols => ['col1', 'col2'], update_cols => ['col1'], key_cols => ['col2'], dbh => $dbh}, @objects );
Or if you prefer to run the statements yourself:
PGObject::Util::BulkLoad->statement( table => 'mytable', type => 'temp', tempname => 'foo_123' ); PGObject::Util::BulkLoad->statement( table => 'mytable', type => 'copy', insert_cols => ['col1', 'col2'] ); PGObject::Util::BulkLoad->statement( type => 'upsert', tempname => 'foo_123', table => 'mytable', insert_cols => ['col1', 'col2'], update_cols => ['col1'], key_cols => ['col2'] );
If you are running repetitive calls, you may be able to trade time for memory using Memoize by unning the following:
PGObject::Util::BulkLoad->memoize_statements;
To unmemoize:
PGObject::Util::BulkLoad->unmemoize;
To flush cache
PGObject::Util::BulkLoad->flush_memoization;
This function exists to memoize statement calls, i.e. generate the exact same statements on the same argument calls. This isn't too likely to be useful in most cases but it may be if you have repeated bulk loader calls in a persistent script (for example real-time importing of csv data from a frequent source).
Unmemoizes the statement calls.
Flushes the cache for statement memoization. Does *not* flush the cache for escaping memoization since that is a bigger win and a pure function accepting simple strings.
This takes the following arguments and returns a suitable SQL statement
Type of statement. Options are:
Create a temporary table
sql COPY statement
Update/Insert CTE pulling temp table
Get stats on pending upsert, grouped by an arbitrary column.
Name of table
Name of temp table
Column names for insert
Column names for update
Names of columns in primary key.
Names of columns to group stats by
Creates a temporary table named "pg_object.bulkload" and copies the data there
If the first argument is an object, then if there is a function by the name of the object, it will provide the value.
Table to upsert into
Columns to insert (by name)
Columns to update (by name)
Key columns (by name)
This is an array of column names for optional stats retrieval and grouping. If it is set then we will grab the stats and return them. Note this has a performance penalty because it means an extra scan of the temp table and an extra join against the parent table. See get_stats for the return value information if this is set.
Copies data into the specified table. The following arguments are used:
Takes the same arguments as upsert plus group_stats_by
Returns an array of hashrefs representing the number of inserts and updates that an upsert will perform. It must be performed before the upsert statement actually runs. Typically this is run via the upsert command (which automatically runs this if group_stats_by is set in the argumements hash).
There is a performance penalty here since an unindexed left join is required between the temp and the normal table.
This function requires tempname, table, and group_stats_by to be set in the argument hashref. The return value is a list of hashrefs with the following keys:
Hashref with keys inserts and updates including numbers of rows.
Hashref for key columns and their values, by name
Chris Travers, <chris.travers at gmail.com>
<chris.travers at gmail.com>
<perl at binary.com>
Please report any bugs or feature requests to bug-pgobject-util-bulkupload at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Util-BulkLoad. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-pgobject-util-bulkupload at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc PGObject::Util::BulkLoad
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=PGObject-Util-BulkLoad
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/PGObject-Util-BulkLoad
CPAN Ratings
http://cpanratings.perl.org/d/PGObject-Util-BulkLoad
Search CPAN
http://search.cpan.org/dist/PGObject-Util-BulkLoad/
To install PGObject::Util::BulkLoad, copy and paste the appropriate command in to your terminal.
cpanm
cpanm PGObject::Util::BulkLoad
CPAN shell
perl -MCPAN -e shell install PGObject::Util::BulkLoad
For more information on module installation, please visit the detailed CPAN module installation guide.