From 2f6c7677fc8a69464125fda7f89545c1b38a8cb5 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Mon, 21 Oct 2013 23:15:24 +0200 Subject: [PATCH] Fix "reset sequences" option to only target just loaded tables. --- src/mysql.lisp | 17 +++++++++++++---- src/pgsql-queries.lisp | 14 ++++++++++---- test/parse/hans.goeuro.load | 2 +- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/mysql.lisp b/src/mysql.lisp index 1ddc628..9b94e5d 100644 --- a/src/mysql.lisp +++ b/src/mysql.lisp @@ -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"))) diff --git a/src/pgsql-queries.lisp b/src/pgsql-queries.lisp index eae85b4..68fe74c 100644 --- a/src/pgsql-queries.lisp +++ b/src/pgsql-queries.lisp @@ -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)))))) diff --git a/test/parse/hans.goeuro.load b/test/parse/hans.goeuro.load index 655ba78..e57884b 100644 --- a/test/parse/hans.goeuro.load +++ b/test/parse/hans.goeuro.load @@ -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