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)
| 1814 | self._drop_local_type_cache() |
| 1815 | |
| 1816 | async def reload_schema_state(self): |
| 1817 | """Indicate that the database schema information must be reloaded. |
| 1818 | |
| 1819 | For performance reasons, asyncpg caches certain aspects of the |
| 1820 | database schema, such as the layout of composite types. Consequently, |
| 1821 | when the database schema changes, and asyncpg is not able to |
| 1822 | gracefully recover from an error caused by outdated schema |
| 1823 | assumptions, an :exc:`~asyncpg.exceptions.OutdatedSchemaCacheError` |
| 1824 | is raised. To prevent the exception, this method may be used to inform |
| 1825 | asyncpg that the database schema has changed. |
| 1826 | |
| 1827 | Example: |
| 1828 | |
| 1829 | .. code-block:: pycon |
| 1830 | |
| 1831 | >>> import asyncpg |
| 1832 | >>> import asyncio |
| 1833 | >>> async def change_type(con): |
| 1834 | ... result = await con.fetch('SELECT id, info FROM tbl') |
| 1835 | ... # Change composite's attribute type "int"=>"text" |
| 1836 | ... await con.execute('ALTER TYPE custom DROP ATTRIBUTE y') |
| 1837 | ... await con.execute('ALTER TYPE custom ADD ATTRIBUTE y text') |
| 1838 | ... await con.reload_schema_state() |
| 1839 | ... for id_, info in result: |
| 1840 | ... new = (info['x'], str(info['y'])) |
| 1841 | ... await con.execute( |
| 1842 | ... 'UPDATE tbl SET info=$2 WHERE id=$1', id_, new) |
| 1843 | ... |
| 1844 | >>> async def run(): |
| 1845 | ... # Initial schema: |
| 1846 | ... # CREATE TYPE custom AS (x int, y int); |
| 1847 | ... # CREATE TABLE tbl(id int, info custom); |
| 1848 | ... con = await asyncpg.connect(user='postgres') |
| 1849 | ... async with con.transaction(): |
| 1850 | ... # Prevent concurrent changes in the table |
| 1851 | ... await con.execute('LOCK TABLE tbl') |
| 1852 | ... await change_type(con) |
| 1853 | ... |
| 1854 | >>> asyncio.run(run()) |
| 1855 | |
| 1856 | .. versionadded:: 0.14.0 |
| 1857 | """ |
| 1858 | self._drop_global_type_cache() |
| 1859 | self._drop_global_statement_cache() |
| 1860 | |
| 1861 | async def _execute( |
| 1862 | self, |