From: Thomas Stephenson Date: Mon, 12 May 2025 01:39:08 +0000 (+1000) Subject: Implement table storage options in postgresql dialect compiler X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4f06dc5fb10540958b4985abc579c93dd28c109b;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Implement table storage options in postgresql dialect compiler These seem to be the most useful table options to pass in most cases anyway. Reflects the implementation in PGDDLCompiler --- diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index ee4a168e37..b98c0d6963 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -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, diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py index f98ea9645b..d87269c0d2 100644 --- a/test/dialect/postgresql/test_compiler.py +++ b/test/dialect/postgresql/test_compiler.py @@ -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(