Changing table structures

From Ingres Community Wiki

Jump to: navigation, search

A python script to generate an terminal monitor script to drop primary key constraints and create them as btree with base table structure. This can have positive impact on performance, particularly with concurrency.

import ingresdbi
import re
import getopt
import sys

# Function to affect whether we generate straight DDL or terminal monitor
# script
def dbprint( str ):
    print "%s;\n\\p\\g" %(str)
    return

def usage():
    print "-v<vnode> -d<database>"
    print "-v, --vnode      Vnode name"
    print "-d, --database   Database name"
    print "-h, --help       Print this message"

try:
    opts, args = getopt.getopt(sys.argv[1:], "v:d:h", ["vnode=", "database=", "help"])
except getopt.GetoptError:
    # print help information and exit:
    usage()
    sys.exit(2)
    
vnode = "(local)"
db = None

for o, a in opts:
    if o in ("-v", "--vnode"):
        vnode = a
    if o in ("-d", "--database"):
        db = a
    if o in ("-h", "--help"):
        usage()
        sys.exit(1)

if db != None:
    ingdb = ingresdbi.connect(vnode=vnode, database=db)
    cur = ingdb.cursor();

    cur.execute("select trim(table_name),trim(constraint_name), text_segment \
from iiconstraints \
where constraint_type = 'P' and \
(table_name not like 'ii%') and \
constraint_name like '$%'")
    res = cur.fetchall()
    for row in res:
        rkey = re.compile(r' PRIMARY KEY\(([^)]*)\)')
        k = rkey.match(row[2])
        print "alter table %s drop constraint \"%s\" cascade;\p\g" %( row[0], row[1] )
        print "modify %s to btree unique on %s with unique_scope=statement;\p\g" %(row[0], k.group(1))
        print "alter table %s add primary key(%s) with index=base table structure;\p\g" %(row[0], k.group(1))
Personal tools
Developing With