Fix "reset sequences" option to only target just loaded tables.

This commit is contained in:
Dimitri Fontaine 2013-10-21 23:15:24 +02:00
parent f977c5a503
commit 2f6c7677fc
3 changed files with 24 additions and 9 deletions

View File

@ -548,6 +548,7 @@ order by ordinal_position" dbname table-name)))
;; get the list of tables and have at it
(let* ((*state* (make-pgstate))
(idx-state (make-pgstate))
(seq-state (make-pgstate))
(copy-kernel (make-kernel 2))
(all-columns (list-all-columns dbname :only-tables only-tables))
(all-indexes (list-all-indexes dbname))
@ -601,12 +602,16 @@ order by ordinal_position" dbname table-name)))
;; don't forget to reset sequences, but only when we did actually import
;; the data.
(when (and (not schema-only) reset-sequences)
(let ((only-tables
(let ((tables
(mapcar
(lambda (name) (apply-identifier-case name identifier-case))
only-tables)))
(log-message :notice "Resetting all sequences")
(pgloader.pgsql:reset-all-sequences pg-dbname :only-tables only-tables)))
(or only-tables
(mapcar #'car all-columns)))))
(log-message :notice "Reset sequences")
(with-stats-collection (pg-dbname "reset sequences"
:use-result-as-rows t
:state seq-state)
(pgloader.pgsql:reset-all-sequences pg-dbname :tables tables))))
;; now end the kernels
(let ((lp:*kernel* idx-kernel)) (lp:end-kernel))
@ -621,5 +626,9 @@ order by ordinal_position" dbname table-name)))
(report-summary)
(format t pgloader.utils::*header-line*)
(report-summary :state idx-state :header nil :footer nil)
(report-summary :state seq-state :header nil :footer nil)
;; don't forget to add up the RESET SEQUENCES timings
(incf (pgloader.utils::pgstate-secs *state*)
(pgloader.utils::pgstate-secs seq-state))
(report-pgstate-stats *state* "Total streaming time")))

View File

@ -131,14 +131,19 @@ select relname, array_agg(case when typname in ('date', 'timestamptz')
(with-pgsql-transaction (dbname)
(pomo:query "select word from pg_get_keywords() where catcode = 'R'" :column)))
(defun reset-all-sequences (dbname &key only-tables)
(defun reset-all-sequences (dbname &key tables)
"Reset all sequences to the max value of the column they are attached to."
(pomo:with-connection (get-connection-spec dbname)
(pomo:execute "set client_min_messages to warning;")
(pomo:execute "listen seqs")
(when tables
(pomo:execute
(format nil "create temp table reloids(oid) as values ~{('~a'::regclass)~^,~}"
tables)))
(handler-case
(pomo:execute (format nil "
(let ((sql (format nil "
DO $$
DECLARE
n integer := 0;
@ -159,7 +164,7 @@ BEGIN
and a.atthasdef
WHERE relkind = 'r' and a.attnum > 0
and pg_get_expr(d.adbin, d.adrelid) ~~ '^nextval'
~@[and c.oid in (~{'~a'::regclass~^, ~})~]
~@[and c.oid in (select oid from reloids)~]
LOOP
n := n + 1;
EXECUTE r.sql;
@ -167,7 +172,8 @@ BEGIN
PERFORM pg_notify('seqs', n::text);
END;
$$; " only-tables))
$$; " tables)))
(pomo:execute sql))
;; now get the notification signal
(cl-postgres:postgresql-notification (c)
(parse-integer (cl-postgres:postgresql-notification-payload c))))))

View File

@ -1,6 +1,6 @@
load database
from mysql://root@localhost:3306/goeuro
into postgresql://dim@localhost:54393/goeuro
into postgresql://dim@localhost:54393/godollar
WITH drop tables, create tables, create indexes, reset sequences