-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_merge_table.py
executable file
·324 lines (246 loc) · 11 KB
/
pg_merge_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import print_function
import sys
import argparse
import psycopg2
from introspection import *
# Parse and assign arguments
parser = argparse.ArgumentParser(description='Merge a table from one PostgreSQL database to another.')
parser.add_argument('fq_table_name',
help='name of table to merge (optionally schema-qualified, must exist on both servers)')
parser.add_argument('--from', dest='from_connection_string', action='store',
default="host=localhost",
help='connection string for "from" server (defaults to "host=localhost")')
parser.add_argument('--to', dest='to_connection_string', action='store',
default="host=localhost",
help='connection string for "to" server (defaults to "host=localhost")')
parser.add_argument('--key', dest='key', action='store',
default="",
help='key column for matching; it must exist on both tables, and the only column in a unique index')
parser.add_argument('--delete', dest='delete_missing', action='store_true',
default=False,
help='delete entries in "to" table missing in "from" table"')
parser.add_argument('--execute', dest='execute', action='store_true',
default=False,
help='actually run the merge; by default, does a dry run only')
parser.add_argument('--progress', dest='progress', action='store_true',
default=False,
help='write progress every 1,000 rows')
args = parser.parse_args()
fq_table_name = args.fq_table_name
from_connnection_string = args.from_connection_string
to_connection_string = args.to_connection_string
key = args.key
delete_missing = args.delete_missing
execute = args.execute
progress = args.progress
#
from_connection = psycopg2.connect(from_connnection_string)
from_connection.autocommit = False
to_connection = psycopg2.connect(to_connection_string)
to_connection.autocommit = False
# Decompose table name into schema, name
components = fq_table_name.split('.')
if len(components) == 1:
schema_name = 'public'
table_name = components[0]
elif len(components) == 2:
schema_name = components[0]
table_name = components[1]
else:
print('Table name "%s" contains more than one dot.' % fq_table_name, file=sys.stderr)
exit(1)
# Verify table exists on both sides
if not table_exists(from_connection, schema_name, table_name):
print('Table "%s.%s" does not exist on "from" server.' % (schema_name, table_name), file=sys.stderr)
exit(1)
if not table_exists(to_connection, schema_name, table_name):
print('Table "%s.%s" does not exist on "to" server.' % (schema_name, table_name), file=sys.stderr)
exit(1)
# Retrieve the primary key column name, and all table columns
if key:
key_type = type_for_column(from_connection, schema_name, table_name, key)
if not key_type:
print('Table "%s.%s" coolumn %s does not exist on "from" server.' % (schema_name, table_name, key), file
=sys.stderr)
exit(1)
to_key_type = type_for_column(to_connection, schema_name, table_name, key)
if not to_key_type:
print('Table "%s.%s" column "%s" does not exist on "to" server.' % (schema_name, table_name, key), file
=sys.stderr)
exit(1)
if key_type != to_key_type:
print('Table "%s.%s" column "%s" is not the same type on the "from" (%s) and "to" (%s) server.' % \
(schema_name, table_name, key, key_type, to_key_type), file=sys.stderr)
exit(1)
from_curs = from_connection.cursor()
from_curs.execute("""
SELECT COUNT(DISTINCT i.indexrelid)
FROM pg_class c
JOIN pg_namespace ns ON c.relnamespace = ns.oid
JOIN pg_attribute a ON a.attrelid = c.oid
JOIN pg_index i ON i.indrelid = c.oid
WHERE nspname = %s
AND relname = %s
AND attname = %s
AND indisunique
AND attnum = ANY (indkey::integer[])
AND array_length(indkey::integer[], 1) = 1
""", (schema_name, table_name, key))
from_key_index_count = int(from_curs.fetchone()[0])
if from_key_index_count == 0:
print('Table "%s.%s" column "%s" does not appear by itself in a UNIQUE index on the "from" server.' % \
(schema_name, table_name, key), file=sys.stderr)
exit(1)
if from_key_index_count > 1:
print('Table "%s.%s" column "%s" appears by itself in more than one UNIQUE index on the "from" server.' % \
(schema_name, table_name, key), file=sys.stderr)
exit(1)
from_curs.close()
to_curs = to_connection.cursor()
to_curs.execute("""
SELECT COUNT(DISTINCT i.indexrelid)
FROM pg_class c
JOIN pg_namespace ns ON c.relnamespace = ns.oid
JOIN pg_attribute a ON a.attrelid = c.oid
JOIN pg_index i ON i.indrelid = c.oid
WHERE nspname = %s
AND relname = %s
AND attname = %s
AND indisunique
AND attnum = ANY (indkey::integer[])
AND array_length(indkey::integer[], 1) = 1
""", (schema_name, table_name, key))
to_key_index_count = int(to_curs.fetchone()[0])
if to_key_index_count == 0:
print('Table "%s.%s" column "%s" does not appear by itself in a UNIQUE index on the "to" server.' % \
(schema_name, table_name, key), file=sys.stderr)
exit(1)
if to_key_index_count > 1:
print('Table "%s.%s" column "%s" appears by itself in more than one UNIQUE index on the "to" server.' % \
(schema_name, table_name, key), file=sys.stderr)
exit(1)
to_curs.close()
else:
primary_key_from = list(primary_key_for_table(from_connection, schema_name, table_name))
primary_key_to = list(primary_key_for_table(to_connection, schema_name, table_name))
if len(primary_key_from) == 0:
print('Table "%s.%s" does not have a primary key on "from" server.' % (schema_name, table_name), file
=sys.stderr)
exit(1)
elif len(primary_key_from) > 1:
print('Table "%s.%s" has a multi-column primary key on "from" server, currently not supported.' % (
schema_name, table_name), file=sys.stderr)
exit(1)
if len(primary_key_to) == 0:
print('Table "%s.%s" does not have a primary key on "to" server.' % (schema_name, table_name), file=sys.stderr)
exit(1)
elif len(primary_key_to) > 1:
print('Table "%s.%s" has a multi-column primary key on "to" server, currently not supported.' % (
schema_name, table_name), file=sys.stderr)
exit(1)
if primary_key_from[0] != primary_key_to[0]:
print('Table "%s.%s" has different primary key column names on "from" (%s) and "to" (%s)servers.' % \
(schema_name, table_name, primary_key_from[0], primary_key_to[0],), file=sys.stderr)
exit(1)
key = primary_key_from[0]
key_type = type_for_column(from_connection, schema_name, table_name, key)
columns = list(table_columns(from_connection, schema_name, table_name))
# For convenience, we make sure the primary key is the first column in our list.
if columns[0] != key:
columns.remove(key)
columns.insert(0, key)
columns = ['"' + c + '"' for c in columns]
# These are used to build the SQL for SELECT, INSERT, and UPDATE operations.
column_list_string = ', '.join(columns)
replacement_string = ', '.join(['%s'] * len(columns))
select_statement = 'SELECT %s FROM "%s"."%s"' % (column_list_string, schema_name, table_name)
insert_statement = """
INSERT INTO "%s"."%s"(%s) VALUES(%s)
ON CONFLICT (%s) DO UPDATE SET %s
RETURNING (xmax = 0) AS inserted
""" % (schema_name,
table_name,
column_list_string,
replacement_string,
key,
', '.join([column + ' = EXCLUDED.' + column for column in columns[1:]]))
# To avoid memory issues, we use a server-side cursor rather than just issuing what might
# be a truly gigantic SELECT statement
from_curs = from_connection.cursor(name='from_cursor')
from_curs.execute(select_statement)
to_curs = to_connection.cursor()
# If we will be deleting rows, create a temporary table to hold the ones we did insert/update.
if delete_missing:
tracking_table_name = "delete_track_" + table_name
to_curs.execute("""
CREATE TEMPORARY TABLE %s (
pk %s PRIMARY KEY
) ON COMMIT DROP
""" % (tracking_table_name, key_type,))
tracking_table_insert = "INSERT INTO " + tracking_table_name + "(pk) VALUES(%s)"
else:
tracking_table_name = ""
tracking_table_insert = ""
probe_statement = """
SELECT COUNT(*) FROM "%s"."%s" WHERE %s=
""" % (schema_name, table_name, key,)
probe_statement += '%s'
rows_processed = 0
rows_updated = 0
rows_deleted = 0
rows_inserted = 0
for row in from_curs:
# This iterates through the input in "gulps" of the default itersize, which is 2000 in psycopg2.
rows_processed += 1
if tracking_table_insert:
to_curs.execute(tracking_table_insert, row[:1])
if not execute:
to_curs.execute(probe_statement, row[:1])
already_exists = int(to_curs.fetchone()[0])
if already_exists:
rows_updated += 1
else:
rows_inserted += 1
else:
to_curs.execute(insert_statement, row)
inserted = to_curs.fetchone()[0]
if inserted:
rows_inserted += 1
else:
rows_updated += 1
if progress and (rows_processed % 1000) == 0:
print("%s rows processed, %s updated, %s inserted" % \
(rows_processed, rows_updated, rows_inserted,), file=sys.stdout)
from_curs.close()
if delete_missing and progress:
print("deleting.", file=sys.stdout)
if not execute:
to_curs.execute("""
SELECT COUNT(*) FROM "%s"."%s" WHERE %s NOT IN (SELECT pk FROM %s)
""" % (schema_name, table_name, key, tracking_table_name))
rows_deleted = int(to_curs.fetchone()[0])
else:
to_curs.execute("""
DELETE FROM "%s"."%s" WHERE %s NOT IN (SELECT pk FROM %s)
""" % (schema_name, table_name, key, tracking_table_name))
rows_deleted = to_curs.rowcount
from_connection.rollback()
if not execute:
to_connection.rollback()
else:
to_connection.commit()
to_connection.autocommit = True
if progress:
print("vacuuming.", file=sys.stdout)
to_curs.execute("""VACUUM ANALYZE "%s"."%s" """ % (schema_name, table_name,))
to_curs.close()
if not execute:
print("dry run estimates: %s rows processed, %s updated, %s inserted, %s deleted" % \
(rows_processed, rows_updated, rows_inserted, rows_deleted), file=sys.stdout)
else:
print("%s rows processed, %s updated, %s inserted, %s deleted" % \
(rows_processed, rows_updated, rows_inserted, rows_deleted), file=sys.stdout)
exit(0)