|
3 | 3 | import argparse, psycopg2, re, sys, time |
4 | 4 | from multiprocessing import Process |
5 | 5 |
|
6 | | -partman_version = "3.1.0" |
| 6 | +partman_version = "4.1.0" |
7 | 7 |
|
8 | 8 | parser = argparse.ArgumentParser(description="Script for reapplying indexes on child tables in a partition set to match the parent table. Any indexes that currently exist on the children and match the definition on the parent will be left as is. There is an option to recreate matching as well indexes if desired, as well as the primary key. Indexes that do not exist on the parent will be dropped. Commits are done after each index is dropped/created to help prevent long running transactions & locks.", epilog="NOTE: New index names are made based off the child table name & columns used, so their naming may differ from the name given on the parent. This is done to allow the tool to account for long or duplicate index names. If an index name would be duplicated, an incremental counter is added on to the end of the index name to allow it to be created. Use the --dryrun option first to see what it will do and which names may cause dupes to be handled like this.") |
9 | 9 | parser.add_argument('-p', '--parent', help="Parent table of an already created partition set. (Required)") |
|
20 | 20 | parser.add_argument('--version', action="store_true", help="Print out the minimum version of pg_partman this script is meant to work with. The version of pg_partman installed may be greater than this.") |
21 | 21 | args = parser.parse_args() |
22 | 22 |
|
| 23 | +def check_compatibility(conn, partman_schema): |
| 24 | + cur = conn.cursor() |
| 25 | + |
| 26 | + sql = """SELECT current_setting('server_version_num')::int""" |
| 27 | + cur.execute(sql) |
| 28 | + pg_version = int(cur.fetchone()[0]) |
| 29 | + |
| 30 | + if pg_version < 90400: |
| 31 | + print("ERROR: This script requires PostgreSQL minimum version of 9.4.0") |
| 32 | + sys.exit(2) |
| 33 | + |
| 34 | + sql = "SELECT partition_type FROM " + partman_schema + ".part_config WHERE parent_table = %s" |
| 35 | + cur.execute(sql, [args.parent]) |
| 36 | + partition_type = cur.fetchone()[0] |
| 37 | + |
| 38 | + if pg_version >= 110000 and partition_type == "native": |
| 39 | + print("This script cannot currently work with native partition sets in PG11+. Please use native index inheritance methods if possible.") |
| 40 | + cur.close() |
| 41 | + conn_close(conn) |
| 42 | + sys.exit(2) |
| 43 | + |
| 44 | + cur.close() |
| 45 | + |
| 46 | + |
23 | 47 | def create_conn(): |
24 | 48 | conn = psycopg2.connect(args.connection) |
25 | 49 | return conn |
@@ -71,7 +95,7 @@ def create_index(conn, partman_schema, child_schemaname, child_tablename, child_ |
71 | 95 | while True: |
72 | 96 | sql = "SELECT count(*) FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid WHERE n.nspname = %s AND c.relname = %s" |
73 | 97 | cur = conn.cursor() |
74 | | - cur.execute(sql, [parent_schemaname, index_name]) |
| 98 | + cur.execute(sql, [child_schemaname, index_name]) |
75 | 99 | index_exists = cur.fetchone()[0] |
76 | 100 | if index_exists != None and index_exists > 0: |
77 | 101 | index_name = child_tablename |
@@ -280,14 +304,8 @@ def reindex_proc(child_schemaname, child_tablename, parent, parent_index_list, p |
280 | 304 |
|
281 | 305 | conn = create_conn() |
282 | 306 |
|
283 | | - cur = conn.cursor() |
284 | | - cur.execute("SELECT current_setting('server_version_num')::int > 90400") |
285 | | - if cur.fetchone()[0] == False: |
286 | | - print("ERROR: This script requires PostgreSQL minimum version of 9.4.0") |
287 | | - sys.exit(2) |
288 | | - cur.close() |
289 | | - |
290 | 307 | partman_schema = get_partman_schema(conn) |
| 308 | + check_compatibility(conn,partman_schema) |
291 | 309 | parent = get_parent(conn, partman_schema) |
292 | 310 | parent_index_list = get_parent_index_list(conn, parent) |
293 | 311 | child_list = get_children(conn, partman_schema) |
|
0 commit comments