Set PostgreSQL default index tablespace

133 views
Skip to first unread message

sector119

unread,
Jan 24, 2021, 9:42:38 AM1/24/21
to sqlalchemy
Hello

Is it possible to set default tablespace for all indexes?

I know that I cat set it with
Index('my_index', my_table.c.data, postgresql_tablespace='my_tablespace')

But I want to set set it by default somehow, that when I just put "index=True" on column, I get index created at some tablespace.

Thank You

Mike Bayer

unread,
Jan 24, 2021, 2:46:03 PM1/24/21
to noreply-spamdigest via sqlalchemy
Hey there -

we have the usual slate of answers, I know you've used SQLAlchemy for a long time so these should be familiar:

1. make your own function (easiest)

     def tablespace_index(*arg, **kw):
        kw["postgresql_tablespace"] = "my_tablespace"
        return Index(*arg, **kw)

2. intercept DDL at the Python construction level (probably the smoothest approach, you can look at "parent" to check the Table)

   @event.listens_for(Index, "before_parent_attach")
   def before_parent_attach(target, parent):
       target.dialect_kwargs["postgresql_tablespace"] = "my_tablespace"

3. intercept DDL at the SQL execution level (heavy handed)

   @event.listens_for(connection, "before_execute")
   def before_execute(elem, clauseelement, multiparams, params, execution_options):
       if isinstance(clauseelement, CreateIndex):
            clauseelement.element.dialect_kwargs["postgresql_tablespace"] = "my_tablespace"

that's what I got for this one.




Thank You


--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages