]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Implement table storage options in postgresql dialect compiler
authorThomas Stephenson <ovangle@gmail.com>
Mon, 12 May 2025 01:39:08 +0000 (11:39 +1000)
committerThomas Stephenson <ovangle@gmail.com>
Mon, 12 May 2025 02:11:09 +0000 (12:11 +1000)
These seem to be the most useful table options to pass in most
cases anyway. Reflects the implementation in PGDDLCompiler

lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_compiler.py

index ee4a168e377d48f0cb5524ffde487b7d5a1355db..b98c0d69630ea45ee8c38b7bfc5b1f4e9c83c2ad 100644 (file)
@@ -1202,10 +1202,19 @@ dialect in conjunction with the :class:`_schema.Table` construct:
 
   .. versionadded:: 2.0.26
 
+* ``WITH``:
+
+    Table("some_table", metadata, ..., postgresql_with={"fillfactor": 100})
+
+    Set various storage options on the table
+
 * ``WITH OIDS``::
 
     Table("some_table", metadata, ..., postgresql_with_oids=True)
 
+    Automatically add object identifiers to table rows. Legacy feature, removed
+    in postgresql 12.
+
 * ``WITHOUT OIDS``::
 
     Table("some_table", metadata, ..., postgresql_with_oids=False)
@@ -2583,6 +2592,12 @@ class PGDDLCompiler(compiler.DDLCompiler):
         if pg_opts["using"]:
             table_opts.append("\n USING %s" % pg_opts["using"])
 
+        if pg_opts["with"]:
+            storage_params = (
+                "%s = %s" % (k, v) for (k, v) in pg_opts["with"].items()
+            )
+            table_opts.append(" WITH (%s)" % ", ".join(storage_params))
+
         if pg_opts["with_oids"] is True:
             table_opts.append("\n WITH OIDS")
         elif pg_opts["with_oids"] is False:
@@ -3198,6 +3213,7 @@ class PGDialect(default.DefaultDialect):
                 "tablespace": None,
                 "partition_by": None,
                 "with_oids": None,
+                "with": None,
                 "on_commit": None,
                 "inherits": None,
                 "using": None,
index f98ea9645b043e40d90bd7289c67ca0654752094..d87269c0d2918bf860a276c82a49fe51f523aac7 100644 (file)
@@ -582,6 +582,43 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             "CREATE TABLE anothertable (id INTEGER) WITHOUT OIDS",
         )
 
+    def test_create_table_with_storage_parameters(self):
+        m = MetaData()
+
+        tbl = Table("atable1", m, postgresql_with={"fillfactor": 100})
+
+        self.assert_compile(
+            schema.CreateTable(tbl),
+            "CREATE TABLE atable1 () " "WITH (fillfactor = 100)",
+        )
+
+        tbl2 = Table(
+            "atable2",
+            m,
+            postgresql_with={"toast.autovacuum_insert_scale_factor": 1.25},
+        )
+
+        self.assert_compile(
+            schema.CreateTable(tbl2),
+            "CREATE TABLE atable2 () "
+            "WITH (toast.autovacuum_insert_scale_factor = 1.25)",
+        )
+
+        tbl3 = Table(
+            "atable3",
+            m,
+            postgresql_with={
+                "user_catalog_table": False,
+                "parallel_workers": 15,
+            },
+        )
+
+        self.assert_compile(
+            schema.CreateTable(tbl3),
+            "CREATE TABLE atable3 () "
+            "WITH (user_catalog_table = False, parallel_workers = 15)",
+        )
+
     def test_create_table_with_oncommit_option(self):
         m = MetaData()
         tbl = Table(