Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrent db table indexing through alembic script

Is it possible to create concurrent indexes for DB table through alembic script?

I'm using postgres DB, and able to create concurrent table indexes through sql command on postgres prompt.(create index concurrently on ();)

But couldn't find way to create same through Db migration(alembic) script. If we create normal index(not concurrent) , it'll lock DB table so can't perform any query in parallel. So just want to know how to create concurrent index through alembic(DB migration) script

like image 708
user2853625 Avatar asked Nov 20 '13 09:11

user2853625


People also ask

How does Alembic know what version of the database it is?

When Alembic ran our first migration, it also created its own table to track the current schema and which version of the database it is. When we make changes to our models, we can tell Alembic to compare the new model to the current database, and automatically create a revision based on those changes.

How to migrate an employee table in Alembic?

Run db migrate command again, this time Alembic will detect the addition of new employees table and will generate a migration script along with the logic to create and drop the employees table.

What is alembic and how does it work?

A tool like Alembic allows us to change database schema as our application evolves. It also keeps track of the changes made to the database, so that you can move forward or backward in time.

What are the different types of indexing in DBMS?

The primary Indexing in DBMS is also further divided into two types. In a dense index, a record is created for every search key valued in the database. This helps you to search faster but needs more space to store index records. In this Indexing, method records contain search key value and points to the real record on the disk.


2 Answers

Alembic supports PostgreSQL concurrently indexes creation

def upgrade():
    op.execute('COMMIT')
    op.create_index('ix_1', 't1', ['col1'], postgresql_concurrently=True)
like image 157
Logovsky Dmitry Avatar answered Sep 19 '22 01:09

Logovsky Dmitry


I'm not using Postgres and I am not able to test it, but it should be possible. According to:

http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html

Concurrent indexes are allowed in the Postgres dialect from version 0.9.9. However, a migration script like this should work with older versions (direct SQL creation):

from alembic import op, context
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.sql import text

# ---------- COMMONS
# Base objects for SQL operations are:
#     - use op = INSERT, UPDATE, DELETE
#     - use connection = SELECT (and also INSERT, UPDATE, DELETE but this object has lot of logics)
metadata = MetaData()
connection = context.get_bind()

tbl = Table('test', metadata, Column('data', Integer), Column("unique_key", String))
# If you want to define a index on the current loaded schema:
# idx1 = Index('test_idx1', tbl.c.data, postgresql_concurrently=True)


def upgrade():
    ...
    queryc = \
    """
    CREATE INDEX CONCURRENTLY test_idx1 ON test (data, unique_key);
    """
    # it should be possible to create an index here (direct SQL):
    connection.execute(text(queryc))
    ...
like image 30
J_Zar Avatar answered Sep 19 '22 01:09

J_Zar