MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / reload_schema_state

Method reload_schema_state

asyncpg/connection.py:1847–1890  ·  view source on GitHub ↗

Indicate that the database schema information must be reloaded. For performance reasons, asyncpg caches certain aspects of the database schema, such as the layout of composite types. Consequently, when the database schema changes, and asyncpg is not able to graceful

(self)

Source from the content-addressed store, hash-verified

1845 self._drop_local_type_cache()
1846
1847 async def reload_schema_state(self):
1848 """Indicate that the database schema information must be reloaded.
1849
1850 For performance reasons, asyncpg caches certain aspects of the
1851 database schema, such as the layout of composite types. Consequently,
1852 when the database schema changes, and asyncpg is not able to
1853 gracefully recover from an error caused by outdated schema
1854 assumptions, an :exc:`~asyncpg.exceptions.OutdatedSchemaCacheError`
1855 is raised. To prevent the exception, this method may be used to inform
1856 asyncpg that the database schema has changed.
1857
1858 Example:
1859
1860 .. code-block:: pycon
1861
1862 >>> import asyncpg
1863 >>> import asyncio
1864 >>> async def change_type(con):
1865 ... result = await con.fetch('SELECT id, info FROM tbl')
1866 ... # Change composite's attribute type "int"=>"text"
1867 ... await con.execute('ALTER TYPE custom DROP ATTRIBUTE y')
1868 ... await con.execute('ALTER TYPE custom ADD ATTRIBUTE y text')
1869 ... await con.reload_schema_state()
1870 ... for id_, info in result:
1871 ... new = (info['x'], str(info['y']))
1872 ... await con.execute(
1873 ... 'UPDATE tbl SET info=$2 WHERE id=$1', id_, new)
1874 ...
1875 >>> async def run():
1876 ... # Initial schema:
1877 ... # CREATE TYPE custom AS (x int, y int);
1878 ... # CREATE TABLE tbl(id int, info custom);
1879 ... con = await asyncpg.connect(user='postgres')
1880 ... async with con.transaction():
1881 ... # Prevent concurrent changes in the table
1882 ... await con.execute('LOCK TABLE tbl')
1883 ... await change_type(con)
1884 ...
1885 >>> asyncio.run(run())
1886
1887 .. versionadded:: 0.14.0
1888 """
1889 self._drop_global_type_cache()
1890 self._drop_global_statement_cache()
1891
1892 async def _execute(
1893 self,

Callers 3

_do_executeMethod · 0.95
wait_and_dropMethod · 0.80
__do_executeMethod · 0.80

Calls 2

Tested by 1

wait_and_dropMethod · 0.64