You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4446 lines
148KB

  1. # postgresql/base.py
  2. # Copyright (C) 2005-2021 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. r"""
  8. .. dialect:: postgresql
  9. :name: PostgreSQL
  10. :full_support: 9.6, 10, 11, 12, 13
  11. :normal_support: 9.6+
  12. :best_effort: 8+
  13. .. _postgresql_sequences:
  14. Sequences/SERIAL/IDENTITY
  15. -------------------------
  16. PostgreSQL supports sequences, and SQLAlchemy uses these as the default means
  17. of creating new primary key values for integer-based primary key columns. When
  18. creating tables, SQLAlchemy will issue the ``SERIAL`` datatype for
  19. integer-based primary key columns, which generates a sequence and server side
  20. default corresponding to the column.
  21. To specify a specific named sequence to be used for primary key generation,
  22. use the :func:`~sqlalchemy.schema.Sequence` construct::
  23. Table('sometable', metadata,
  24. Column('id', Integer, Sequence('some_id_seq'), primary_key=True)
  25. )
  26. When SQLAlchemy issues a single INSERT statement, to fulfill the contract of
  27. having the "last insert identifier" available, a RETURNING clause is added to
  28. the INSERT statement which specifies the primary key columns should be
  29. returned after the statement completes. The RETURNING functionality only takes
  30. place if PostgreSQL 8.2 or later is in use. As a fallback approach, the
  31. sequence, whether specified explicitly or implicitly via ``SERIAL``, is
  32. executed independently beforehand, the returned value to be used in the
  33. subsequent insert. Note that when an
  34. :func:`~sqlalchemy.sql.expression.insert()` construct is executed using
  35. "executemany" semantics, the "last inserted identifier" functionality does not
  36. apply; no RETURNING clause is emitted nor is the sequence pre-executed in this
  37. case.
  38. To force the usage of RETURNING by default off, specify the flag
  39. ``implicit_returning=False`` to :func:`_sa.create_engine`.
  40. PostgreSQL 10 and above IDENTITY columns
  41. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  42. PostgreSQL 10 and above have a new IDENTITY feature that supersedes the use
  43. of SERIAL. The :class:`_schema.Identity` construct in a
  44. :class:`_schema.Column` can be used to control its behavior::
  45. from sqlalchemy import Table, Column, MetaData, Integer, Computed
  46. metadata = MetaData()
  47. data = Table(
  48. "data",
  49. metadata,
  50. Column(
  51. 'id', Integer, Identity(start=42, cycle=True), primary_key=True
  52. ),
  53. Column('data', String)
  54. )
  55. The CREATE TABLE for the above :class:`_schema.Table` object would be:
  56. .. sourcecode:: sql
  57. CREATE TABLE data (
  58. id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 42 CYCLE),
  59. data VARCHAR,
  60. PRIMARY KEY (id)
  61. )
  62. .. versionchanged:: 1.4 Added :class:`_schema.Identity` construct
  63. in a :class:`_schema.Column` to specify the option of an autoincrementing
  64. column.
  65. .. note::
  66. Previous versions of SQLAlchemy did not have built-in support for rendering
  67. of IDENTITY, and could use the following compilation hook to replace
  68. occurrences of SERIAL with IDENTITY::
  69. from sqlalchemy.schema import CreateColumn
  70. from sqlalchemy.ext.compiler import compiles
  71. @compiles(CreateColumn, 'postgresql')
  72. def use_identity(element, compiler, **kw):
  73. text = compiler.visit_create_column(element, **kw)
  74. text = text.replace(
  75. "SERIAL", "INT GENERATED BY DEFAULT AS IDENTITY"
  76. )
  77. return text
  78. Using the above, a table such as::
  79. t = Table(
  80. 't', m,
  81. Column('id', Integer, primary_key=True),
  82. Column('data', String)
  83. )
  84. Will generate on the backing database as::
  85. CREATE TABLE t (
  86. id INT GENERATED BY DEFAULT AS IDENTITY,
  87. data VARCHAR,
  88. PRIMARY KEY (id)
  89. )
  90. .. _postgresql_ss_cursors:
  91. Server Side Cursors
  92. -------------------
  93. Server-side cursor support is available for the psycopg2, asyncpg
  94. dialects and may also be available in others.
  95. Server side cursors are enabled on a per-statement basis by using the
  96. :paramref:`.Connection.execution_options.stream_results` connection execution
  97. option::
  98. with engine.connect() as conn:
  99. result = conn.execution_options(stream_results=True).execute(text("select * from table"))
  100. Note that some kinds of SQL statements may not be supported with
  101. server side cursors; generally, only SQL statements that return rows should be
  102. used with this option.
  103. .. deprecated:: 1.4 The dialect-level server_side_cursors flag is deprecated
  104. and will be removed in a future release. Please use the
  105. :paramref:`_engine.Connection.stream_results` execution option for
  106. unbuffered cursor support.
  107. .. seealso::
  108. :ref:`engine_stream_results`
  109. .. _postgresql_isolation_level:
  110. Transaction Isolation Level
  111. ---------------------------
  112. Most SQLAlchemy dialects support setting of transaction isolation level
  113. using the :paramref:`_sa.create_engine.execution_options` parameter
  114. at the :func:`_sa.create_engine` level, and at the :class:`_engine.Connection`
  115. level via the :paramref:`.Connection.execution_options.isolation_level`
  116. parameter.
  117. For PostgreSQL dialects, this feature works either by making use of the
  118. DBAPI-specific features, such as psycopg2's isolation level flags which will
  119. embed the isolation level setting inline with the ``"BEGIN"`` statement, or for
  120. DBAPIs with no direct support by emitting ``SET SESSION CHARACTERISTICS AS
  121. TRANSACTION ISOLATION LEVEL <level>`` ahead of the ``"BEGIN"`` statement
  122. emitted by the DBAPI. For the special AUTOCOMMIT isolation level,
  123. DBAPI-specific techniques are used which is typically an ``.autocommit``
  124. flag on the DBAPI connection object.
  125. To set isolation level using :func:`_sa.create_engine`::
  126. engine = create_engine(
  127. "postgresql+pg8000://scott:tiger@localhost/test",
  128. execution_options={
  129. "isolation_level": "REPEATABLE READ"
  130. }
  131. )
  132. To set using per-connection execution options::
  133. with engine.connect() as conn:
  134. conn = conn.execution_options(
  135. isolation_level="REPEATABLE READ"
  136. )
  137. with conn.begin():
  138. # ... work with transaction
  139. Valid values for ``isolation_level`` on most PostgreSQL dialects include:
  140. * ``READ COMMITTED``
  141. * ``READ UNCOMMITTED``
  142. * ``REPEATABLE READ``
  143. * ``SERIALIZABLE``
  144. * ``AUTOCOMMIT``
  145. .. seealso::
  146. :ref:`postgresql_readonly_deferrable`
  147. :ref:`dbapi_autocommit`
  148. :ref:`psycopg2_isolation_level`
  149. :ref:`pg8000_isolation_level`
  150. .. _postgresql_readonly_deferrable:
  151. Setting READ ONLY / DEFERRABLE
  152. ------------------------------
  153. Most PostgreSQL dialects support setting the "READ ONLY" and "DEFERRABLE"
  154. characteristics of the transaction, which is in addition to the isolation level
  155. setting. These two attributes can be established either in conjunction with or
  156. independently of the isolation level by passing the ``postgresql_readonly`` and
  157. ``postgresql_deferrable`` flags with
  158. :meth:`_engine.Connection.execution_options`. The example below illustrates
  159. passing the ``"SERIALIZABLE"`` isolation level at the same time as setting
  160. "READ ONLY" and "DEFERRABLE"::
  161. with engine.connect() as conn:
  162. conn = conn.execution_options(
  163. isolation_level="SERIALIZABLE",
  164. postgresql_readonly=True,
  165. postgresql_deferrable=True
  166. )
  167. with conn.begin():
  168. # ... work with transaction
  169. Note that some DBAPIs such as asyncpg only support "readonly" with
  170. SERIALIZABLE isolation.
  171. .. versionadded:: 1.4 added support for the ``postgresql_readonly``
  172. and ``postgresql_deferrable`` execution options.
  173. .. _postgresql_alternate_search_path:
  174. Setting Alternate Search Paths on Connect
  175. ------------------------------------------
  176. The PostgreSQL ``search_path`` variable refers to the list of schema names
  177. that will be implicitly referred towards when a particular table or other
  178. object is referenced in a SQL statement. As detailed in the next section
  179. :ref:`postgresql_schema_reflection`, SQLAlchemy is generally organized around
  180. the concept of keeping this variable at its default value of ``public``,
  181. however, in order to have it set to any arbitrary name or names when connections
  182. are used automatically, the "SET SESSION search_path" command may be invoked
  183. for all connections in a pool using the following event handler, as discussed
  184. at :ref:`schema_set_default_connections`::
  185. from sqlalchemy import event
  186. from sqlalchemy import create_engine
  187. engine = create_engine("postgresql+psycopg2://scott:tiger@host/dbname")
  188. @event.listens_for(engine, "connect", insert=True)
  189. def set_search_path(dbapi_connection, connection_record):
  190. existing_autocommit = dbapi_connection.autocommit
  191. dbapi_connection.autocommit = True
  192. cursor = dbapi_connection.cursor()
  193. cursor.execute("SET SESSION search_path='%s'" % schema_name)
  194. cursor.close()
  195. dbapi_connection.autocommit = existing_autocommit
  196. The reason the recipe is complicated by use of the ``.autocommit`` DBAPI
  197. attribute is so that when the ``SET SESSION search_path`` directive is invoked,
  198. it is invoked outside of the scope of any transaction and therefore will not
  199. be reverted when the DBAPI connection has a rollback.
  200. .. seealso::
  201. :ref:`schema_set_default_connections` - in the :ref:`metadata_toplevel` documentation
  202. .. _postgresql_schema_reflection:
  203. Remote-Schema Table Introspection and PostgreSQL search_path
  204. ------------------------------------------------------------
  205. **TL;DR;**: keep the ``search_path`` variable set to its default of ``public``,
  206. name schemas **other** than ``public`` explicitly within ``Table`` definitions.
  207. The PostgreSQL dialect can reflect tables from any schema. The
  208. :paramref:`_schema.Table.schema` argument, or alternatively the
  209. :paramref:`.MetaData.reflect.schema` argument determines which schema will
  210. be searched for the table or tables. The reflected :class:`_schema.Table`
  211. objects
  212. will in all cases retain this ``.schema`` attribute as was specified.
  213. However, with regards to tables which these :class:`_schema.Table`
  214. objects refer to
  215. via foreign key constraint, a decision must be made as to how the ``.schema``
  216. is represented in those remote tables, in the case where that remote
  217. schema name is also a member of the current
  218. `PostgreSQL search path
  219. <http://www.postgresql.org/docs/current/static/ddl-schemas.html#DDL-SCHEMAS-PATH>`_.
  220. By default, the PostgreSQL dialect mimics the behavior encouraged by
  221. PostgreSQL's own ``pg_get_constraintdef()`` builtin procedure. This function
  222. returns a sample definition for a particular foreign key constraint,
  223. omitting the referenced schema name from that definition when the name is
  224. also in the PostgreSQL schema search path. The interaction below
  225. illustrates this behavior::
  226. test=> CREATE TABLE test_schema.referred(id INTEGER PRIMARY KEY);
  227. CREATE TABLE
  228. test=> CREATE TABLE referring(
  229. test(> id INTEGER PRIMARY KEY,
  230. test(> referred_id INTEGER REFERENCES test_schema.referred(id));
  231. CREATE TABLE
  232. test=> SET search_path TO public, test_schema;
  233. test=> SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM
  234. test-> pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n
  235. test-> ON n.oid = c.relnamespace
  236. test-> JOIN pg_catalog.pg_constraint r ON c.oid = r.conrelid
  237. test-> WHERE c.relname='referring' AND r.contype = 'f'
  238. test-> ;
  239. pg_get_constraintdef
  240. ---------------------------------------------------
  241. FOREIGN KEY (referred_id) REFERENCES referred(id)
  242. (1 row)
  243. Above, we created a table ``referred`` as a member of the remote schema
  244. ``test_schema``, however when we added ``test_schema`` to the
  245. PG ``search_path`` and then asked ``pg_get_constraintdef()`` for the
  246. ``FOREIGN KEY`` syntax, ``test_schema`` was not included in the output of
  247. the function.
  248. On the other hand, if we set the search path back to the typical default
  249. of ``public``::
  250. test=> SET search_path TO public;
  251. SET
  252. The same query against ``pg_get_constraintdef()`` now returns the fully
  253. schema-qualified name for us::
  254. test=> SELECT pg_catalog.pg_get_constraintdef(r.oid, true) FROM
  255. test-> pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n
  256. test-> ON n.oid = c.relnamespace
  257. test-> JOIN pg_catalog.pg_constraint r ON c.oid = r.conrelid
  258. test-> WHERE c.relname='referring' AND r.contype = 'f';
  259. pg_get_constraintdef
  260. ---------------------------------------------------------------
  261. FOREIGN KEY (referred_id) REFERENCES test_schema.referred(id)
  262. (1 row)
  263. SQLAlchemy will by default use the return value of ``pg_get_constraintdef()``
  264. in order to determine the remote schema name. That is, if our ``search_path``
  265. were set to include ``test_schema``, and we invoked a table
  266. reflection process as follows::
  267. >>> from sqlalchemy import Table, MetaData, create_engine, text
  268. >>> engine = create_engine("postgresql://scott:tiger@localhost/test")
  269. >>> with engine.connect() as conn:
  270. ... conn.execute(text("SET search_path TO test_schema, public"))
  271. ... meta = MetaData()
  272. ... referring = Table('referring', meta,
  273. ... autoload_with=conn)
  274. ...
  275. <sqlalchemy.engine.result.CursorResult object at 0x101612ed0>
  276. The above process would deliver to the :attr:`_schema.MetaData.tables`
  277. collection
  278. ``referred`` table named **without** the schema::
  279. >>> meta.tables['referred'].schema is None
  280. True
  281. To alter the behavior of reflection such that the referred schema is
  282. maintained regardless of the ``search_path`` setting, use the
  283. ``postgresql_ignore_search_path`` option, which can be specified as a
  284. dialect-specific argument to both :class:`_schema.Table` as well as
  285. :meth:`_schema.MetaData.reflect`::
  286. >>> with engine.connect() as conn:
  287. ... conn.execute(text("SET search_path TO test_schema, public"))
  288. ... meta = MetaData()
  289. ... referring = Table('referring', meta,
  290. ... autoload_with=conn,
  291. ... postgresql_ignore_search_path=True)
  292. ...
  293. <sqlalchemy.engine.result.CursorResult object at 0x1016126d0>
  294. We will now have ``test_schema.referred`` stored as schema-qualified::
  295. >>> meta.tables['test_schema.referred'].schema
  296. 'test_schema'
  297. .. sidebar:: Best Practices for PostgreSQL Schema reflection
  298. The description of PostgreSQL schema reflection behavior is complex, and
  299. is the product of many years of dealing with widely varied use cases and
  300. user preferences. But in fact, there's no need to understand any of it if
  301. you just stick to the simplest use pattern: leave the ``search_path`` set
  302. to its default of ``public`` only, never refer to the name ``public`` as
  303. an explicit schema name otherwise, and refer to all other schema names
  304. explicitly when building up a :class:`_schema.Table` object. The options
  305. described here are only for those users who can't, or prefer not to, stay
  306. within these guidelines.
  307. Note that **in all cases**, the "default" schema is always reflected as
  308. ``None``. The "default" schema on PostgreSQL is that which is returned by the
  309. PostgreSQL ``current_schema()`` function. On a typical PostgreSQL
  310. installation, this is the name ``public``. So a table that refers to another
  311. which is in the ``public`` (i.e. default) schema will always have the
  312. ``.schema`` attribute set to ``None``.
  313. .. versionadded:: 0.9.2 Added the ``postgresql_ignore_search_path``
  314. dialect-level option accepted by :class:`_schema.Table` and
  315. :meth:`_schema.MetaData.reflect`.
  316. .. seealso::
  317. `The Schema Search Path
  318. <http://www.postgresql.org/docs/9.0/static/ddl-schemas.html#DDL-SCHEMAS-PATH>`_
  319. - on the PostgreSQL website.
  320. INSERT/UPDATE...RETURNING
  321. -------------------------
  322. The dialect supports PG 8.2's ``INSERT..RETURNING``, ``UPDATE..RETURNING`` and
  323. ``DELETE..RETURNING`` syntaxes. ``INSERT..RETURNING`` is used by default
  324. for single-row INSERT statements in order to fetch newly generated
  325. primary key identifiers. To specify an explicit ``RETURNING`` clause,
  326. use the :meth:`._UpdateBase.returning` method on a per-statement basis::
  327. # INSERT..RETURNING
  328. result = table.insert().returning(table.c.col1, table.c.col2).\
  329. values(name='foo')
  330. print(result.fetchall())
  331. # UPDATE..RETURNING
  332. result = table.update().returning(table.c.col1, table.c.col2).\
  333. where(table.c.name=='foo').values(name='bar')
  334. print(result.fetchall())
  335. # DELETE..RETURNING
  336. result = table.delete().returning(table.c.col1, table.c.col2).\
  337. where(table.c.name=='foo')
  338. print(result.fetchall())
  339. .. _postgresql_insert_on_conflict:
  340. INSERT...ON CONFLICT (Upsert)
  341. ------------------------------
  342. Starting with version 9.5, PostgreSQL allows "upserts" (update or insert) of
  343. rows into a table via the ``ON CONFLICT`` clause of the ``INSERT`` statement. A
  344. candidate row will only be inserted if that row does not violate any unique
  345. constraints. In the case of a unique constraint violation, a secondary action
  346. can occur which can be either "DO UPDATE", indicating that the data in the
  347. target row should be updated, or "DO NOTHING", which indicates to silently skip
  348. this row.
  349. Conflicts are determined using existing unique constraints and indexes. These
  350. constraints may be identified either using their name as stated in DDL,
  351. or they may be inferred by stating the columns and conditions that comprise
  352. the indexes.
  353. SQLAlchemy provides ``ON CONFLICT`` support via the PostgreSQL-specific
  354. :func:`_postgresql.insert()` function, which provides
  355. the generative methods :meth:`_postgresql.Insert.on_conflict_do_update`
  356. and :meth:`~.postgresql.Insert.on_conflict_do_nothing`:
  357. .. sourcecode:: pycon+sql
  358. >>> from sqlalchemy.dialects.postgresql import insert
  359. >>> insert_stmt = insert(my_table).values(
  360. ... id='some_existing_id',
  361. ... data='inserted value')
  362. >>> do_nothing_stmt = insert_stmt.on_conflict_do_nothing(
  363. ... index_elements=['id']
  364. ... )
  365. >>> print(do_nothing_stmt)
  366. {opensql}INSERT INTO my_table (id, data) VALUES (%(id)s, %(data)s)
  367. ON CONFLICT (id) DO NOTHING
  368. {stop}
  369. >>> do_update_stmt = insert_stmt.on_conflict_do_update(
  370. ... constraint='pk_my_table',
  371. ... set_=dict(data='updated value')
  372. ... )
  373. >>> print(do_update_stmt)
  374. {opensql}INSERT INTO my_table (id, data) VALUES (%(id)s, %(data)s)
  375. ON CONFLICT ON CONSTRAINT pk_my_table DO UPDATE SET data = %(param_1)s
  376. .. versionadded:: 1.1
  377. .. seealso::
  378. `INSERT .. ON CONFLICT
  379. <http://www.postgresql.org/docs/current/static/sql-insert.html#SQL-ON-CONFLICT>`_
  380. - in the PostgreSQL documentation.
  381. Specifying the Target
  382. ^^^^^^^^^^^^^^^^^^^^^
  383. Both methods supply the "target" of the conflict using either the
  384. named constraint or by column inference:
  385. * The :paramref:`_postgresql.Insert.on_conflict_do_update.index_elements` argument
  386. specifies a sequence containing string column names, :class:`_schema.Column`
  387. objects, and/or SQL expression elements, which would identify a unique
  388. index:
  389. .. sourcecode:: pycon+sql
  390. >>> do_update_stmt = insert_stmt.on_conflict_do_update(
  391. ... index_elements=['id'],
  392. ... set_=dict(data='updated value')
  393. ... )
  394. >>> print(do_update_stmt)
  395. {opensql}INSERT INTO my_table (id, data) VALUES (%(id)s, %(data)s)
  396. ON CONFLICT (id) DO UPDATE SET data = %(param_1)s
  397. {stop}
  398. >>> do_update_stmt = insert_stmt.on_conflict_do_update(
  399. ... index_elements=[my_table.c.id],
  400. ... set_=dict(data='updated value')
  401. ... )
  402. >>> print(do_update_stmt)
  403. {opensql}INSERT INTO my_table (id, data) VALUES (%(id)s, %(data)s)
  404. ON CONFLICT (id) DO UPDATE SET data = %(param_1)s
  405. * When using :paramref:`_postgresql.Insert.on_conflict_do_update.index_elements` to
  406. infer an index, a partial index can be inferred by also specifying the
  407. use the :paramref:`_postgresql.Insert.on_conflict_do_update.index_where` parameter:
  408. .. sourcecode:: pycon+sql
  409. >>> stmt = insert(my_table).values(user_email='a@b.com', data='inserted data')
  410. >>> stmt = stmt.on_conflict_do_update(
  411. ... index_elements=[my_table.c.user_email],
  412. ... index_where=my_table.c.user_email.like('%@gmail.com'),
  413. ... set_=dict(data=stmt.excluded.data)
  414. ... )
  415. >>> print(stmt)
  416. {opensql}INSERT INTO my_table (data, user_email)
  417. VALUES (%(data)s, %(user_email)s) ON CONFLICT (user_email)
  418. WHERE user_email LIKE %(user_email_1)s DO UPDATE SET data = excluded.data
  419. * The :paramref:`_postgresql.Insert.on_conflict_do_update.constraint` argument is
  420. used to specify an index directly rather than inferring it. This can be
  421. the name of a UNIQUE constraint, a PRIMARY KEY constraint, or an INDEX:
  422. .. sourcecode:: pycon+sql
  423. >>> do_update_stmt = insert_stmt.on_conflict_do_update(
  424. ... constraint='my_table_idx_1',
  425. ... set_=dict(data='updated value')
  426. ... )
  427. >>> print(do_update_stmt)
  428. {opensql}INSERT INTO my_table (id, data) VALUES (%(id)s, %(data)s)
  429. ON CONFLICT ON CONSTRAINT my_table_idx_1 DO UPDATE SET data = %(param_1)s
  430. {stop}
  431. >>> do_update_stmt = insert_stmt.on_conflict_do_update(
  432. ... constraint='my_table_pk',
  433. ... set_=dict(data='updated value')
  434. ... )
  435. >>> print(do_update_stmt)
  436. {opensql}INSERT INTO my_table (id, data) VALUES (%(id)s, %(data)s)
  437. ON CONFLICT ON CONSTRAINT my_table_pk DO UPDATE SET data = %(param_1)s
  438. {stop}
  439. * The :paramref:`_postgresql.Insert.on_conflict_do_update.constraint` argument may
  440. also refer to a SQLAlchemy construct representing a constraint,
  441. e.g. :class:`.UniqueConstraint`, :class:`.PrimaryKeyConstraint`,
  442. :class:`.Index`, or :class:`.ExcludeConstraint`. In this use,
  443. if the constraint has a name, it is used directly. Otherwise, if the
  444. constraint is unnamed, then inference will be used, where the expressions
  445. and optional WHERE clause of the constraint will be spelled out in the
  446. construct. This use is especially convenient
  447. to refer to the named or unnamed primary key of a :class:`_schema.Table`
  448. using the
  449. :attr:`_schema.Table.primary_key` attribute:
  450. .. sourcecode:: pycon+sql
  451. >>> do_update_stmt = insert_stmt.on_conflict_do_update(
  452. ... constraint=my_table.primary_key,
  453. ... set_=dict(data='updated value')
  454. ... )
  455. >>> print(do_update_stmt)
  456. {opensql}INSERT INTO my_table (id, data) VALUES (%(id)s, %(data)s)
  457. ON CONFLICT (id) DO UPDATE SET data = %(param_1)s
  458. The SET Clause
  459. ^^^^^^^^^^^^^^^
  460. ``ON CONFLICT...DO UPDATE`` is used to perform an update of the already
  461. existing row, using any combination of new values as well as values
  462. from the proposed insertion. These values are specified using the
  463. :paramref:`_postgresql.Insert.on_conflict_do_update.set_` parameter. This
  464. parameter accepts a dictionary which consists of direct values
  465. for UPDATE:
  466. .. sourcecode:: pycon+sql
  467. >>> stmt = insert(my_table).values(id='some_id', data='inserted value')
  468. >>> do_update_stmt = stmt.on_conflict_do_update(
  469. ... index_elements=['id'],
  470. ... set_=dict(data='updated value')
  471. ... )
  472. >>> print(do_update_stmt)
  473. {opensql}INSERT INTO my_table (id, data) VALUES (%(id)s, %(data)s)
  474. ON CONFLICT (id) DO UPDATE SET data = %(param_1)s
  475. .. warning::
  476. The :meth:`_expression.Insert.on_conflict_do_update`
  477. method does **not** take into
  478. account Python-side default UPDATE values or generation functions, e.g.
  479. those specified using :paramref:`_schema.Column.onupdate`.
  480. These values will not be exercised for an ON CONFLICT style of UPDATE,
  481. unless they are manually specified in the
  482. :paramref:`_postgresql.Insert.on_conflict_do_update.set_` dictionary.
  483. Updating using the Excluded INSERT Values
  484. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  485. In order to refer to the proposed insertion row, the special alias
  486. :attr:`~.postgresql.Insert.excluded` is available as an attribute on
  487. the :class:`_postgresql.Insert` object; this object is a
  488. :class:`_expression.ColumnCollection`
  489. which alias contains all columns of the target
  490. table:
  491. .. sourcecode:: pycon+sql
  492. >>> stmt = insert(my_table).values(
  493. ... id='some_id',
  494. ... data='inserted value',
  495. ... author='jlh'
  496. ... )
  497. >>> do_update_stmt = stmt.on_conflict_do_update(
  498. ... index_elements=['id'],
  499. ... set_=dict(data='updated value', author=stmt.excluded.author)
  500. ... )
  501. >>> print(do_update_stmt)
  502. {opensql}INSERT INTO my_table (id, data, author)
  503. VALUES (%(id)s, %(data)s, %(author)s)
  504. ON CONFLICT (id) DO UPDATE SET data = %(param_1)s, author = excluded.author
  505. Additional WHERE Criteria
  506. ^^^^^^^^^^^^^^^^^^^^^^^^^
  507. The :meth:`_expression.Insert.on_conflict_do_update` method also accepts
  508. a WHERE clause using the :paramref:`_postgresql.Insert.on_conflict_do_update.where`
  509. parameter, which will limit those rows which receive an UPDATE:
  510. .. sourcecode:: pycon+sql
  511. >>> stmt = insert(my_table).values(
  512. ... id='some_id',
  513. ... data='inserted value',
  514. ... author='jlh'
  515. ... )
  516. >>> on_update_stmt = stmt.on_conflict_do_update(
  517. ... index_elements=['id'],
  518. ... set_=dict(data='updated value', author=stmt.excluded.author),
  519. ... where=(my_table.c.status == 2)
  520. ... )
  521. >>> print(on_update_stmt)
  522. {opensql}INSERT INTO my_table (id, data, author)
  523. VALUES (%(id)s, %(data)s, %(author)s)
  524. ON CONFLICT (id) DO UPDATE SET data = %(param_1)s, author = excluded.author
  525. WHERE my_table.status = %(status_1)s
  526. Skipping Rows with DO NOTHING
  527. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  528. ``ON CONFLICT`` may be used to skip inserting a row entirely
  529. if any conflict with a unique or exclusion constraint occurs; below
  530. this is illustrated using the
  531. :meth:`~.postgresql.Insert.on_conflict_do_nothing` method:
  532. .. sourcecode:: pycon+sql
  533. >>> stmt = insert(my_table).values(id='some_id', data='inserted value')
  534. >>> stmt = stmt.on_conflict_do_nothing(index_elements=['id'])
  535. >>> print(stmt)
  536. {opensql}INSERT INTO my_table (id, data) VALUES (%(id)s, %(data)s)
  537. ON CONFLICT (id) DO NOTHING
  538. If ``DO NOTHING`` is used without specifying any columns or constraint,
  539. it has the effect of skipping the INSERT for any unique or exclusion
  540. constraint violation which occurs:
  541. .. sourcecode:: pycon+sql
  542. >>> stmt = insert(my_table).values(id='some_id', data='inserted value')
  543. >>> stmt = stmt.on_conflict_do_nothing()
  544. >>> print(stmt)
  545. {opensql}INSERT INTO my_table (id, data) VALUES (%(id)s, %(data)s)
  546. ON CONFLICT DO NOTHING
  547. .. _postgresql_match:
  548. Full Text Search
  549. ----------------
  550. SQLAlchemy makes available the PostgreSQL ``@@`` operator via the
  551. :meth:`_expression.ColumnElement.match`
  552. method on any textual column expression.
  553. On a PostgreSQL dialect, an expression like the following::
  554. select(sometable.c.text.match("search string"))
  555. will emit to the database::
  556. SELECT text @@ to_tsquery('search string') FROM table
  557. The PostgreSQL text search functions such as ``to_tsquery()``
  558. and ``to_tsvector()`` are available
  559. explicitly using the standard :data:`.func` construct. For example::
  560. select(func.to_tsvector('fat cats ate rats').match('cat & rat'))
  561. Emits the equivalent of::
  562. SELECT to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat')
  563. The :class:`_postgresql.TSVECTOR` type can provide for explicit CAST::
  564. from sqlalchemy.dialects.postgresql import TSVECTOR
  565. from sqlalchemy import select, cast
  566. select(cast("some text", TSVECTOR))
  567. produces a statement equivalent to::
  568. SELECT CAST('some text' AS TSVECTOR) AS anon_1
  569. Full Text Searches in PostgreSQL are influenced by a combination of: the
  570. PostgreSQL setting of ``default_text_search_config``, the ``regconfig`` used
  571. to build the GIN/GiST indexes, and the ``regconfig`` optionally passed in
  572. during a query.
  573. When performing a Full Text Search against a column that has a GIN or
  574. GiST index that is already pre-computed (which is common on full text
  575. searches) one may need to explicitly pass in a particular PostgreSQL
  576. ``regconfig`` value to ensure the query-planner utilizes the index and does
  577. not re-compute the column on demand.
  578. In order to provide for this explicit query planning, or to use different
  579. search strategies, the ``match`` method accepts a ``postgresql_regconfig``
  580. keyword argument::
  581. select(mytable.c.id).where(
  582. mytable.c.title.match('somestring', postgresql_regconfig='english')
  583. )
  584. Emits the equivalent of::
  585. SELECT mytable.id FROM mytable
  586. WHERE mytable.title @@ to_tsquery('english', 'somestring')
  587. One can also specifically pass in a `'regconfig'` value to the
  588. ``to_tsvector()`` command as the initial argument::
  589. select(mytable.c.id).where(
  590. func.to_tsvector('english', mytable.c.title )\
  591. .match('somestring', postgresql_regconfig='english')
  592. )
  593. produces a statement equivalent to::
  594. SELECT mytable.id FROM mytable
  595. WHERE to_tsvector('english', mytable.title) @@
  596. to_tsquery('english', 'somestring')
  597. It is recommended that you use the ``EXPLAIN ANALYZE...`` tool from
  598. PostgreSQL to ensure that you are generating queries with SQLAlchemy that
  599. take full advantage of any indexes you may have created for full text search.
  600. FROM ONLY ...
  601. -------------
  602. The dialect supports PostgreSQL's ONLY keyword for targeting only a particular
  603. table in an inheritance hierarchy. This can be used to produce the
  604. ``SELECT ... FROM ONLY``, ``UPDATE ONLY ...``, and ``DELETE FROM ONLY ...``
  605. syntaxes. It uses SQLAlchemy's hints mechanism::
  606. # SELECT ... FROM ONLY ...
  607. result = table.select().with_hint(table, 'ONLY', 'postgresql')
  608. print(result.fetchall())
  609. # UPDATE ONLY ...
  610. table.update(values=dict(foo='bar')).with_hint('ONLY',
  611. dialect_name='postgresql')
  612. # DELETE FROM ONLY ...
  613. table.delete().with_hint('ONLY', dialect_name='postgresql')
  614. .. _postgresql_indexes:
  615. PostgreSQL-Specific Index Options
  616. ---------------------------------
  617. Several extensions to the :class:`.Index` construct are available, specific
  618. to the PostgreSQL dialect.
  619. Covering Indexes
  620. ^^^^^^^^^^^^^^^^
  621. The ``postgresql_include`` option renders INCLUDE(colname) for the given
  622. string names::
  623. Index("my_index", table.c.x, postgresql_include=['y'])
  624. would render the index as ``CREATE INDEX my_index ON table (x) INCLUDE (y)``
  625. Note that this feature requires PostgreSQL 11 or later.
  626. .. versionadded:: 1.4
  627. .. _postgresql_partial_indexes:
  628. Partial Indexes
  629. ^^^^^^^^^^^^^^^
  630. Partial indexes add criterion to the index definition so that the index is
  631. applied to a subset of rows. These can be specified on :class:`.Index`
  632. using the ``postgresql_where`` keyword argument::
  633. Index('my_index', my_table.c.id, postgresql_where=my_table.c.value > 10)
  634. .. _postgresql_operator_classes:
  635. Operator Classes
  636. ^^^^^^^^^^^^^^^^
  637. PostgreSQL allows the specification of an *operator class* for each column of
  638. an index (see
  639. http://www.postgresql.org/docs/8.3/interactive/indexes-opclass.html).
  640. The :class:`.Index` construct allows these to be specified via the
  641. ``postgresql_ops`` keyword argument::
  642. Index(
  643. 'my_index', my_table.c.id, my_table.c.data,
  644. postgresql_ops={
  645. 'data': 'text_pattern_ops',
  646. 'id': 'int4_ops'
  647. })
  648. Note that the keys in the ``postgresql_ops`` dictionaries are the
  649. "key" name of the :class:`_schema.Column`, i.e. the name used to access it from
  650. the ``.c`` collection of :class:`_schema.Table`, which can be configured to be
  651. different than the actual name of the column as expressed in the database.
  652. If ``postgresql_ops`` is to be used against a complex SQL expression such
  653. as a function call, then to apply to the column it must be given a label
  654. that is identified in the dictionary by name, e.g.::
  655. Index(
  656. 'my_index', my_table.c.id,
  657. func.lower(my_table.c.data).label('data_lower'),
  658. postgresql_ops={
  659. 'data_lower': 'text_pattern_ops',
  660. 'id': 'int4_ops'
  661. })
  662. Operator classes are also supported by the
  663. :class:`_postgresql.ExcludeConstraint` construct using the
  664. :paramref:`_postgresql.ExcludeConstraint.ops` parameter. See that parameter for
  665. details.
  666. .. versionadded:: 1.3.21 added support for operator classes with
  667. :class:`_postgresql.ExcludeConstraint`.
  668. Index Types
  669. ^^^^^^^^^^^
  670. PostgreSQL provides several index types: B-Tree, Hash, GiST, and GIN, as well
  671. as the ability for users to create their own (see
  672. http://www.postgresql.org/docs/8.3/static/indexes-types.html). These can be
  673. specified on :class:`.Index` using the ``postgresql_using`` keyword argument::
  674. Index('my_index', my_table.c.data, postgresql_using='gin')
  675. The value passed to the keyword argument will be simply passed through to the
  676. underlying CREATE INDEX command, so it *must* be a valid index type for your
  677. version of PostgreSQL.
  678. .. _postgresql_index_storage:
  679. Index Storage Parameters
  680. ^^^^^^^^^^^^^^^^^^^^^^^^
  681. PostgreSQL allows storage parameters to be set on indexes. The storage
  682. parameters available depend on the index method used by the index. Storage
  683. parameters can be specified on :class:`.Index` using the ``postgresql_with``
  684. keyword argument::
  685. Index('my_index', my_table.c.data, postgresql_with={"fillfactor": 50})
  686. .. versionadded:: 1.0.6
  687. PostgreSQL allows to define the tablespace in which to create the index.
  688. The tablespace can be specified on :class:`.Index` using the
  689. ``postgresql_tablespace`` keyword argument::
  690. Index('my_index', my_table.c.data, postgresql_tablespace='my_tablespace')
  691. .. versionadded:: 1.1
  692. Note that the same option is available on :class:`_schema.Table` as well.
  693. .. _postgresql_index_concurrently:
  694. Indexes with CONCURRENTLY
  695. ^^^^^^^^^^^^^^^^^^^^^^^^^
  696. The PostgreSQL index option CONCURRENTLY is supported by passing the
  697. flag ``postgresql_concurrently`` to the :class:`.Index` construct::
  698. tbl = Table('testtbl', m, Column('data', Integer))
  699. idx1 = Index('test_idx1', tbl.c.data, postgresql_concurrently=True)
  700. The above index construct will render DDL for CREATE INDEX, assuming
  701. PostgreSQL 8.2 or higher is detected or for a connection-less dialect, as::
  702. CREATE INDEX CONCURRENTLY test_idx1 ON testtbl (data)
  703. For DROP INDEX, assuming PostgreSQL 9.2 or higher is detected or for
  704. a connection-less dialect, it will emit::
  705. DROP INDEX CONCURRENTLY test_idx1
  706. .. versionadded:: 1.1 support for CONCURRENTLY on DROP INDEX. The
  707. CONCURRENTLY keyword is now only emitted if a high enough version
  708. of PostgreSQL is detected on the connection (or for a connection-less
  709. dialect).
  710. When using CONCURRENTLY, the PostgreSQL database requires that the statement
  711. be invoked outside of a transaction block. The Python DBAPI enforces that
  712. even for a single statement, a transaction is present, so to use this
  713. construct, the DBAPI's "autocommit" mode must be used::
  714. metadata = MetaData()
  715. table = Table(
  716. "foo", metadata,
  717. Column("id", String))
  718. index = Index(
  719. "foo_idx", table.c.id, postgresql_concurrently=True)
  720. with engine.connect() as conn:
  721. with conn.execution_options(isolation_level='AUTOCOMMIT'):
  722. table.create(conn)
  723. .. seealso::
  724. :ref:`postgresql_isolation_level`
  725. .. _postgresql_index_reflection:
  726. PostgreSQL Index Reflection
  727. ---------------------------
  728. The PostgreSQL database creates a UNIQUE INDEX implicitly whenever the
  729. UNIQUE CONSTRAINT construct is used. When inspecting a table using
  730. :class:`_reflection.Inspector`, the :meth:`_reflection.Inspector.get_indexes`
  731. and the :meth:`_reflection.Inspector.get_unique_constraints`
  732. will report on these
  733. two constructs distinctly; in the case of the index, the key
  734. ``duplicates_constraint`` will be present in the index entry if it is
  735. detected as mirroring a constraint. When performing reflection using
  736. ``Table(..., autoload_with=engine)``, the UNIQUE INDEX is **not** returned
  737. in :attr:`_schema.Table.indexes` when it is detected as mirroring a
  738. :class:`.UniqueConstraint` in the :attr:`_schema.Table.constraints` collection
  739. .
  740. .. versionchanged:: 1.0.0 - :class:`_schema.Table` reflection now includes
  741. :class:`.UniqueConstraint` objects present in the
  742. :attr:`_schema.Table.constraints`
  743. collection; the PostgreSQL backend will no longer include a "mirrored"
  744. :class:`.Index` construct in :attr:`_schema.Table.indexes`
  745. if it is detected
  746. as corresponding to a unique constraint.
  747. Special Reflection Options
  748. --------------------------
  749. The :class:`_reflection.Inspector`
  750. used for the PostgreSQL backend is an instance
  751. of :class:`.PGInspector`, which offers additional methods::
  752. from sqlalchemy import create_engine, inspect
  753. engine = create_engine("postgresql+psycopg2://localhost/test")
  754. insp = inspect(engine) # will be a PGInspector
  755. print(insp.get_enums())
  756. .. autoclass:: PGInspector
  757. :members:
  758. .. _postgresql_table_options:
  759. PostgreSQL Table Options
  760. ------------------------
  761. Several options for CREATE TABLE are supported directly by the PostgreSQL
  762. dialect in conjunction with the :class:`_schema.Table` construct:
  763. * ``TABLESPACE``::
  764. Table("some_table", metadata, ..., postgresql_tablespace='some_tablespace')
  765. The above option is also available on the :class:`.Index` construct.
  766. * ``ON COMMIT``::
  767. Table("some_table", metadata, ..., postgresql_on_commit='PRESERVE ROWS')
  768. * ``WITH OIDS``::
  769. Table("some_table", metadata, ..., postgresql_with_oids=True)
  770. * ``WITHOUT OIDS``::
  771. Table("some_table", metadata, ..., postgresql_with_oids=False)
  772. * ``INHERITS``::
  773. Table("some_table", metadata, ..., postgresql_inherits="some_supertable")
  774. Table("some_table", metadata, ..., postgresql_inherits=("t1", "t2", ...))
  775. .. versionadded:: 1.0.0
  776. * ``PARTITION BY``::
  777. Table("some_table", metadata, ...,
  778. postgresql_partition_by='LIST (part_column)')
  779. .. versionadded:: 1.2.6
  780. .. seealso::
  781. `PostgreSQL CREATE TABLE options
  782. <http://www.postgresql.org/docs/current/static/sql-createtable.html>`_
  783. .. _postgresql_table_valued_overview:
  784. Table values, Table and Column valued functions, Row and Tuple objects
  785. -----------------------------------------------------------------------
  786. PostgreSQL makes great use of modern SQL forms such as table-valued functions,
  787. tables and rows as values. These constructs are commonly used as part
  788. of PostgreSQL's support for complex datatypes such as JSON, ARRAY, and other
  789. datatypes. SQLAlchemy's SQL expression language has native support for
  790. most table-valued and row-valued forms.
  791. .. _postgresql_table_valued:
  792. Table-Valued Functions
  793. ^^^^^^^^^^^^^^^^^^^^^^^
  794. Many PostgreSQL built-in functions are intended to be used in the FROM clause
  795. of a SELECT statement, and are capable of returning table rows or sets of table
  796. rows. A large portion of PostgreSQL's JSON functions for example such as
  797. ``json_array_elements()``, ``json_object_keys()``, ``json_each_text()``,
  798. ``json_each()``, ``json_to_record()``, ``json_populate_recordset()`` use such
  799. forms. These classes of SQL function calling forms in SQLAlchemy are available
  800. using the :meth:`_functions.FunctionElement.table_valued` method in conjunction
  801. with :class:`_functions.Function` objects generated from the :data:`_sql.func`
  802. namespace.
  803. Examples from PostgreSQL's reference documentation follow below:
  804. * ``json_each()``::
  805. >>> from sqlalchemy import select, func
  806. >>> stmt = select(func.json_each('{"a":"foo", "b":"bar"}').table_valued("key", "value"))
  807. >>> print(stmt)
  808. SELECT anon_1.key, anon_1.value
  809. FROM json_each(:json_each_1) AS anon_1
  810. * ``json_populate_record()``::
  811. >>> from sqlalchemy import select, func, literal_column
  812. >>> stmt = select(
  813. ... func.json_populate_record(
  814. ... literal_column("null::myrowtype"),
  815. ... '{"a":1,"b":2}'
  816. ... ).table_valued("a", "b", name="x")
  817. ... )
  818. >>> print(stmt)
  819. SELECT x.a, x.b
  820. FROM json_populate_record(null::myrowtype, :json_populate_record_1) AS x
  821. * ``json_to_record()`` - this form uses a PostgreSQL specific form of derived
  822. columns in the alias, where we may make use of :func:`_sql.column` elements with
  823. types to produce them. The :meth:`_functions.FunctionElement.table_valued`
  824. method produces a :class:`_sql.TableValuedAlias` construct, and the method
  825. :meth:`_sql.TableValuedAlias.render_derived` method sets up the derived
  826. columns specification::
  827. >>> from sqlalchemy import select, func, column, Integer, Text
  828. >>> stmt = select(
  829. ... func.json_to_record('{"a":1,"b":[1,2,3],"c":"bar"}').table_valued(
  830. ... column("a", Integer), column("b", Text), column("d", Text),
  831. ... ).render_derived(name="x", with_types=True)
  832. ... )
  833. >>> print(stmt)
  834. SELECT x.a, x.b, x.d
  835. FROM json_to_record(:json_to_record_1) AS x(a INTEGER, b TEXT, d TEXT)
  836. * ``WITH ORDINALITY`` - part of the SQL standard, ``WITH ORDINALITY`` adds an
  837. ordinal counter to the output of a function and is accepted by a limited set
  838. of PostgreSQL functions including ``unnest()`` and ``generate_series()``. The
  839. :meth:`_functions.FunctionElement.table_valued` method accepts a keyword
  840. parameter ``with_ordinality`` for this purpose, which accepts the string name
  841. that will be applied to the "ordinality" column::
  842. >>> from sqlalchemy import select, func
  843. >>> stmt = select(
  844. ... func.generate_series(4, 1, -1).table_valued("value", with_ordinality="ordinality")
  845. ... )
  846. >>> print(stmt)
  847. SELECT anon_1.value, anon_1.ordinality
  848. FROM generate_series(:generate_series_1, :generate_series_2, :generate_series_3) WITH ORDINALITY AS anon_1
  849. .. versionadded:: 1.4.0b2
  850. .. seealso::
  851. :ref:`tutorial_functions_table_valued` - in the :ref:`unified_tutorial`
  852. .. _postgresql_column_valued:
  853. Column Valued Functions
  854. ^^^^^^^^^^^^^^^^^^^^^^^
  855. Similar to the table valued function, a column valued function is present
  856. in the FROM clause, but delivers itself to the columns clause as a single
  857. scalar value. PostgreSQL functions such as ``json_array_elements()``,
  858. ``unnest()`` and ``generate_series()`` may use this form. Column valued functions are available using the
  859. :meth:`_functions.FunctionElement.column_valued` method of :class:`_functions.FunctionElement`:
  860. * ``json_array_elements()``::
  861. >>> from sqlalchemy import select, func
  862. >>> stmt = select(func.json_array_elements('["one", "two"]').column_valued("x"))
  863. >>> print(stmt)
  864. SELECT x
  865. FROM json_array_elements(:json_array_elements_1) AS x
  866. * ``unnest()`` - in order to generate a PostgreSQL ARRAY literal, the
  867. :func:`_postgresql.array` construct may be used::
  868. >>> from sqlalchemy.dialects.postgresql import array
  869. >>> from sqlalchemy import select, func
  870. >>> stmt = select(func.unnest(array([1, 2])).column_valued())
  871. >>> print(stmt)
  872. SELECT anon_1
  873. FROM unnest(ARRAY[%(param_1)s, %(param_2)s]) AS anon_1
  874. The function can of course be used against an existing table-bound column
  875. that's of type :class:`_types.ARRAY`::
  876. >>> from sqlalchemy import table, column, ARRAY, Integer
  877. >>> from sqlalchemy import select, func
  878. >>> t = table("t", column('value', ARRAY(Integer)))
  879. >>> stmt = select(func.unnest(t.c.value).column_valued("unnested_value"))
  880. >>> print(stmt)
  881. SELECT unnested_value
  882. FROM unnest(t.value) AS unnested_value
  883. .. seealso::
  884. :ref:`tutorial_functions_column_valued` - in the :ref:`unified_tutorial`
  885. Row Types
  886. ^^^^^^^^^
  887. Built-in support for rendering a ``ROW`` may be approximated using
  888. ``func.ROW`` with the :attr:`_sa.func` namespace, or by using the
  889. :func:`_sql.tuple_` construct::
  890. >>> from sqlalchemy import table, column, func, tuple_
  891. >>> t = table("t", column("id"), column("fk"))
  892. >>> stmt = t.select().where(
  893. ... tuple_(t.c.id, t.c.fk) > (1,2)
  894. ... ).where(
  895. ... func.ROW(t.c.id, t.c.fk) < func.ROW(3, 7)
  896. ... )
  897. >>> print(stmt)
  898. SELECT t.id, t.fk
  899. FROM t
  900. WHERE (t.id, t.fk) > (:param_1, :param_2) AND ROW(t.id, t.fk) < ROW(:ROW_1, :ROW_2)
  901. .. seealso::
  902. `PostgreSQL Row Constructors
  903. <https://www.postgresql.org/docs/current/sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS>`_
  904. `PostgreSQL Row Constructor Comparison
  905. <https://www.postgresql.org/docs/current/functions-comparisons.html#ROW-WISE-COMPARISON>`_
  906. Table Types passed to Functions
  907. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  908. PostgreSQL supports passing a table as an argument to a function, which it
  909. refers towards as a "record" type. SQLAlchemy :class:`_sql.FromClause` objects
  910. such as :class:`_schema.Table` support this special form using the
  911. :meth:`_sql.FromClause.table_valued` method, which is comparable to the
  912. :meth:`_functions.FunctionElement.table_valued` method except that the collection
  913. of columns is already established by that of the :class:`_sql.FromClause`
  914. itself::
  915. >>> from sqlalchemy import table, column, func, select
  916. >>> a = table( "a", column("id"), column("x"), column("y"))
  917. >>> stmt = select(func.row_to_json(a.table_valued()))
  918. >>> print(stmt)
  919. SELECT row_to_json(a) AS row_to_json_1
  920. FROM a
  921. .. versionadded:: 1.4.0b2
  922. ARRAY Types
  923. -----------
  924. The PostgreSQL dialect supports arrays, both as multidimensional column types
  925. as well as array literals:
  926. * :class:`_postgresql.ARRAY` - ARRAY datatype
  927. * :class:`_postgresql.array` - array literal
  928. * :func:`_postgresql.array_agg` - ARRAY_AGG SQL function
  929. * :class:`_postgresql.aggregate_order_by` - helper for PG's ORDER BY aggregate
  930. function syntax.
  931. JSON Types
  932. ----------
  933. The PostgreSQL dialect supports both JSON and JSONB datatypes, including
  934. psycopg2's native support and support for all of PostgreSQL's special
  935. operators:
  936. * :class:`_postgresql.JSON`
  937. * :class:`_postgresql.JSONB`
  938. HSTORE Type
  939. -----------
  940. The PostgreSQL HSTORE type as well as hstore literals are supported:
  941. * :class:`_postgresql.HSTORE` - HSTORE datatype
  942. * :class:`_postgresql.hstore` - hstore literal
  943. ENUM Types
  944. ----------
  945. PostgreSQL has an independently creatable TYPE structure which is used
  946. to implement an enumerated type. This approach introduces significant
  947. complexity on the SQLAlchemy side in terms of when this type should be
  948. CREATED and DROPPED. The type object is also an independently reflectable
  949. entity. The following sections should be consulted:
  950. * :class:`_postgresql.ENUM` - DDL and typing support for ENUM.
  951. * :meth:`.PGInspector.get_enums` - retrieve a listing of current ENUM types
  952. * :meth:`.postgresql.ENUM.create` , :meth:`.postgresql.ENUM.drop` - individual
  953. CREATE and DROP commands for ENUM.
  954. .. _postgresql_array_of_enum:
  955. Using ENUM with ARRAY
  956. ^^^^^^^^^^^^^^^^^^^^^
  957. The combination of ENUM and ARRAY is not directly supported by backend
  958. DBAPIs at this time. Prior to SQLAlchemy 1.3.17, a special workaround
  959. was needed in order to allow this combination to work, described below.
  960. .. versionchanged:: 1.3.17 The combination of ENUM and ARRAY is now directly
  961. handled by SQLAlchemy's implementation without any workarounds needed.
  962. .. sourcecode:: python
  963. from sqlalchemy import TypeDecorator
  964. from sqlalchemy.dialects.postgresql import ARRAY
  965. class ArrayOfEnum(TypeDecorator):
  966. impl = ARRAY
  967. def bind_expression(self, bindvalue):
  968. return sa.cast(bindvalue, self)
  969. def result_processor(self, dialect, coltype):
  970. super_rp = super(ArrayOfEnum, self).result_processor(
  971. dialect, coltype)
  972. def handle_raw_string(value):
  973. inner = re.match(r"^{(.*)}$", value).group(1)
  974. return inner.split(",") if inner else []
  975. def process(value):
  976. if value is None:
  977. return None
  978. return super_rp(handle_raw_string(value))
  979. return process
  980. E.g.::
  981. Table(
  982. 'mydata', metadata,
  983. Column('id', Integer, primary_key=True),
  984. Column('data', ArrayOfEnum(ENUM('a', 'b, 'c', name='myenum')))
  985. )
  986. This type is not included as a built-in type as it would be incompatible
  987. with a DBAPI that suddenly decides to support ARRAY of ENUM directly in
  988. a new version.
  989. .. _postgresql_array_of_json:
  990. Using JSON/JSONB with ARRAY
  991. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  992. Similar to using ENUM, prior to SQLAlchemy 1.3.17, for an ARRAY of JSON/JSONB
  993. we need to render the appropriate CAST. Current psycopg2 drivers accommodate
  994. the result set correctly without any special steps.
  995. .. versionchanged:: 1.3.17 The combination of JSON/JSONB and ARRAY is now
  996. directly handled by SQLAlchemy's implementation without any workarounds
  997. needed.
  998. .. sourcecode:: python
  999. class CastingArray(ARRAY):
  1000. def bind_expression(self, bindvalue):
  1001. return sa.cast(bindvalue, self)
  1002. E.g.::
  1003. Table(
  1004. 'mydata', metadata,
  1005. Column('id', Integer, primary_key=True),
  1006. Column('data', CastingArray(JSONB))
  1007. )
  1008. """ # noqa E501
  1009. from collections import defaultdict
  1010. import datetime as dt
  1011. import re
  1012. from uuid import UUID as _python_UUID
  1013. from . import array as _array
  1014. from . import hstore as _hstore
  1015. from . import json as _json
  1016. from . import ranges as _ranges
  1017. from ... import exc
  1018. from ... import schema
  1019. from ... import sql
  1020. from ... import util
  1021. from ...engine import characteristics
  1022. from ...engine import default
  1023. from ...engine import reflection
  1024. from ...sql import coercions
  1025. from ...sql import compiler
  1026. from ...sql import elements
  1027. from ...sql import expression
  1028. from ...sql import roles
  1029. from ...sql import sqltypes
  1030. from ...sql import util as sql_util
  1031. from ...sql.ddl import DDLBase
  1032. from ...types import BIGINT
  1033. from ...types import BOOLEAN
  1034. from ...types import CHAR
  1035. from ...types import DATE
  1036. from ...types import FLOAT
  1037. from ...types import INTEGER
  1038. from ...types import NUMERIC
  1039. from ...types import REAL
  1040. from ...types import SMALLINT
  1041. from ...types import TEXT
  1042. from ...types import VARCHAR
  1043. IDX_USING = re.compile(r"^(?:btree|hash|gist|gin|[\w_]+)$", re.I)
  1044. AUTOCOMMIT_REGEXP = re.compile(
  1045. r"\s*(?:UPDATE|INSERT|CREATE|DELETE|DROP|ALTER|GRANT|REVOKE|"
  1046. "IMPORT FOREIGN SCHEMA|REFRESH MATERIALIZED VIEW|TRUNCATE)",
  1047. re.I | re.UNICODE,
  1048. )
  1049. RESERVED_WORDS = set(
  1050. [
  1051. "all",
  1052. "analyse",
  1053. "analyze",
  1054. "and",
  1055. "any",
  1056. "array",
  1057. "as",
  1058. "asc",
  1059. "asymmetric",
  1060. "both",
  1061. "case",
  1062. "cast",
  1063. "check",
  1064. "collate",
  1065. "column",
  1066. "constraint",
  1067. "create",
  1068. "current_catalog",
  1069. "current_date",
  1070. "current_role",
  1071. "current_time",
  1072. "current_timestamp",
  1073. "current_user",
  1074. "default",
  1075. "deferrable",
  1076. "desc",
  1077. "distinct",
  1078. "do",
  1079. "else",
  1080. "end",
  1081. "except",
  1082. "false",
  1083. "fetch",
  1084. "for",
  1085. "foreign",
  1086. "from",
  1087. "grant",
  1088. "group",
  1089. "having",
  1090. "in",
  1091. "initially",
  1092. "intersect",
  1093. "into",
  1094. "leading",
  1095. "limit",
  1096. "localtime",
  1097. "localtimestamp",
  1098. "new",
  1099. "not",
  1100. "null",
  1101. "of",
  1102. "off",
  1103. "offset",
  1104. "old",
  1105. "on",
  1106. "only",
  1107. "or",
  1108. "order",
  1109. "placing",
  1110. "primary",
  1111. "references",
  1112. "returning",
  1113. "select",
  1114. "session_user",
  1115. "some",
  1116. "symmetric",
  1117. "table",
  1118. "then",
  1119. "to",
  1120. "trailing",
  1121. "true",
  1122. "union",
  1123. "unique",
  1124. "user",
  1125. "using",
  1126. "variadic",
  1127. "when",
  1128. "where",
  1129. "window",
  1130. "with",
  1131. "authorization",
  1132. "between",
  1133. "binary",
  1134. "cross",
  1135. "current_schema",
  1136. "freeze",
  1137. "full",
  1138. "ilike",
  1139. "inner",
  1140. "is",
  1141. "isnull",
  1142. "join",
  1143. "left",
  1144. "like",
  1145. "natural",
  1146. "notnull",
  1147. "outer",
  1148. "over",
  1149. "overlaps",
  1150. "right",
  1151. "similar",
  1152. "verbose",
  1153. ]
  1154. )
  1155. _DECIMAL_TYPES = (1231, 1700)
  1156. _FLOAT_TYPES = (700, 701, 1021, 1022)
  1157. _INT_TYPES = (20, 21, 23, 26, 1005, 1007, 1016)
  1158. class BYTEA(sqltypes.LargeBinary):
  1159. __visit_name__ = "BYTEA"
  1160. class DOUBLE_PRECISION(sqltypes.Float):
  1161. __visit_name__ = "DOUBLE_PRECISION"
  1162. class INET(sqltypes.TypeEngine):
  1163. __visit_name__ = "INET"
  1164. PGInet = INET
  1165. class CIDR(sqltypes.TypeEngine):
  1166. __visit_name__ = "CIDR"
  1167. PGCidr = CIDR
  1168. class MACADDR(sqltypes.TypeEngine):
  1169. __visit_name__ = "MACADDR"
  1170. PGMacAddr = MACADDR
  1171. class MONEY(sqltypes.TypeEngine):
  1172. r"""Provide the PostgreSQL MONEY type.
  1173. Depending on driver, result rows using this type may return a
  1174. string value which includes currency symbols.
  1175. For this reason, it may be preferable to provide conversion to a
  1176. numerically-based currency datatype using :class:`_types.TypeDecorator`::
  1177. import re
  1178. import decimal
  1179. from sqlalchemy import TypeDecorator
  1180. class NumericMoney(TypeDecorator):
  1181. impl = MONEY
  1182. def process_result_value(self, value: Any, dialect: Any) -> None:
  1183. if value is not None:
  1184. # adjust this for the currency and numeric
  1185. m = re.match(r"\$([\d.]+)", value)
  1186. if m:
  1187. value = decimal.Decimal(m.group(1))
  1188. return value
  1189. Alternatively, the conversion may be applied as a CAST using
  1190. the :meth:`_types.TypeDecorator.column_expression` method as follows::
  1191. import decimal
  1192. from sqlalchemy import cast
  1193. from sqlalchemy import TypeDecorator
  1194. class NumericMoney(TypeDecorator):
  1195. impl = MONEY
  1196. def column_expression(self, column: Any):
  1197. return cast(column, Numeric())
  1198. .. versionadded:: 1.2
  1199. """
  1200. __visit_name__ = "MONEY"
  1201. class OID(sqltypes.TypeEngine):
  1202. """Provide the PostgreSQL OID type.
  1203. .. versionadded:: 0.9.5
  1204. """
  1205. __visit_name__ = "OID"
  1206. class REGCLASS(sqltypes.TypeEngine):
  1207. """Provide the PostgreSQL REGCLASS type.
  1208. .. versionadded:: 1.2.7
  1209. """
  1210. __visit_name__ = "REGCLASS"
  1211. class TIMESTAMP(sqltypes.TIMESTAMP):
  1212. def __init__(self, timezone=False, precision=None):
  1213. super(TIMESTAMP, self).__init__(timezone=timezone)
  1214. self.precision = precision
  1215. class TIME(sqltypes.TIME):
  1216. def __init__(self, timezone=False, precision=None):
  1217. super(TIME, self).__init__(timezone=timezone)
  1218. self.precision = precision
  1219. class INTERVAL(sqltypes.NativeForEmulated, sqltypes._AbstractInterval):
  1220. """PostgreSQL INTERVAL type."""
  1221. __visit_name__ = "INTERVAL"
  1222. native = True
  1223. def __init__(self, precision=None, fields=None):
  1224. """Construct an INTERVAL.
  1225. :param precision: optional integer precision value
  1226. :param fields: string fields specifier. allows storage of fields
  1227. to be limited, such as ``"YEAR"``, ``"MONTH"``, ``"DAY TO HOUR"``,
  1228. etc.
  1229. .. versionadded:: 1.2
  1230. """
  1231. self.precision = precision
  1232. self.fields = fields
  1233. @classmethod
  1234. def adapt_emulated_to_native(cls, interval, **kw):
  1235. return INTERVAL(precision=interval.second_precision)
  1236. @property
  1237. def _type_affinity(self):
  1238. return sqltypes.Interval
  1239. def as_generic(self, allow_nulltype=False):
  1240. return sqltypes.Interval(native=True, second_precision=self.precision)
  1241. @property
  1242. def python_type(self):
  1243. return dt.timedelta
  1244. def coerce_compared_value(self, op, value):
  1245. return self
  1246. PGInterval = INTERVAL
  1247. class BIT(sqltypes.TypeEngine):
  1248. __visit_name__ = "BIT"
  1249. def __init__(self, length=None, varying=False):
  1250. if not varying:
  1251. # BIT without VARYING defaults to length 1
  1252. self.length = length or 1
  1253. else:
  1254. # but BIT VARYING can be unlimited-length, so no default
  1255. self.length = length
  1256. self.varying = varying
  1257. PGBit = BIT
  1258. class UUID(sqltypes.TypeEngine):
  1259. """PostgreSQL UUID type.
  1260. Represents the UUID column type, interpreting
  1261. data either as natively returned by the DBAPI
  1262. or as Python uuid objects.
  1263. The UUID type may not be supported on all DBAPIs.
  1264. It is known to work on psycopg2 and not pg8000.
  1265. """
  1266. __visit_name__ = "UUID"
  1267. def __init__(self, as_uuid=False):
  1268. """Construct a UUID type.
  1269. :param as_uuid=False: if True, values will be interpreted
  1270. as Python uuid objects, converting to/from string via the
  1271. DBAPI.
  1272. """
  1273. self.as_uuid = as_uuid
  1274. def coerce_compared_value(self, op, value):
  1275. """See :meth:`.TypeEngine.coerce_compared_value` for a description."""
  1276. if isinstance(value, util.string_types):
  1277. return self
  1278. else:
  1279. return super(UUID, self).coerce_compared_value(op, value)
  1280. def bind_processor(self, dialect):
  1281. if self.as_uuid:
  1282. def process(value):
  1283. if value is not None:
  1284. value = util.text_type(value)
  1285. return value
  1286. return process
  1287. else:
  1288. return None
  1289. def result_processor(self, dialect, coltype):
  1290. if self.as_uuid:
  1291. def process(value):
  1292. if value is not None:
  1293. value = _python_UUID(value)
  1294. return value
  1295. return process
  1296. else:
  1297. return None
  1298. PGUuid = UUID
  1299. class TSVECTOR(sqltypes.TypeEngine):
  1300. """The :class:`_postgresql.TSVECTOR` type implements the PostgreSQL
  1301. text search type TSVECTOR.
  1302. It can be used to do full text queries on natural language
  1303. documents.
  1304. .. versionadded:: 0.9.0
  1305. .. seealso::
  1306. :ref:`postgresql_match`
  1307. """
  1308. __visit_name__ = "TSVECTOR"
  1309. class ENUM(sqltypes.NativeForEmulated, sqltypes.Enum):
  1310. """PostgreSQL ENUM type.
  1311. This is a subclass of :class:`_types.Enum` which includes
  1312. support for PG's ``CREATE TYPE`` and ``DROP TYPE``.
  1313. When the builtin type :class:`_types.Enum` is used and the
  1314. :paramref:`.Enum.native_enum` flag is left at its default of
  1315. True, the PostgreSQL backend will use a :class:`_postgresql.ENUM`
  1316. type as the implementation, so the special create/drop rules
  1317. will be used.
  1318. The create/drop behavior of ENUM is necessarily intricate, due to the
  1319. awkward relationship the ENUM type has in relationship to the
  1320. parent table, in that it may be "owned" by just a single table, or
  1321. may be shared among many tables.
  1322. When using :class:`_types.Enum` or :class:`_postgresql.ENUM`
  1323. in an "inline" fashion, the ``CREATE TYPE`` and ``DROP TYPE`` is emitted
  1324. corresponding to when the :meth:`_schema.Table.create` and
  1325. :meth:`_schema.Table.drop`
  1326. methods are called::
  1327. table = Table('sometable', metadata,
  1328. Column('some_enum', ENUM('a', 'b', 'c', name='myenum'))
  1329. )
  1330. table.create(engine) # will emit CREATE ENUM and CREATE TABLE
  1331. table.drop(engine) # will emit DROP TABLE and DROP ENUM
  1332. To use a common enumerated type between multiple tables, the best
  1333. practice is to declare the :class:`_types.Enum` or
  1334. :class:`_postgresql.ENUM` independently, and associate it with the
  1335. :class:`_schema.MetaData` object itself::
  1336. my_enum = ENUM('a', 'b', 'c', name='myenum', metadata=metadata)
  1337. t1 = Table('sometable_one', metadata,
  1338. Column('some_enum', myenum)
  1339. )
  1340. t2 = Table('sometable_two', metadata,
  1341. Column('some_enum', myenum)
  1342. )
  1343. When this pattern is used, care must still be taken at the level
  1344. of individual table creates. Emitting CREATE TABLE without also
  1345. specifying ``checkfirst=True`` will still cause issues::
  1346. t1.create(engine) # will fail: no such type 'myenum'
  1347. If we specify ``checkfirst=True``, the individual table-level create
  1348. operation will check for the ``ENUM`` and create if not exists::
  1349. # will check if enum exists, and emit CREATE TYPE if not
  1350. t1.create(engine, checkfirst=True)
  1351. When using a metadata-level ENUM type, the type will always be created
  1352. and dropped if either the metadata-wide create/drop is called::
  1353. metadata.create_all(engine) # will emit CREATE TYPE
  1354. metadata.drop_all(engine) # will emit DROP TYPE
  1355. The type can also be created and dropped directly::
  1356. my_enum.create(engine)
  1357. my_enum.drop(engine)
  1358. .. versionchanged:: 1.0.0 The PostgreSQL :class:`_postgresql.ENUM` type
  1359. now behaves more strictly with regards to CREATE/DROP. A metadata-level
  1360. ENUM type will only be created and dropped at the metadata level,
  1361. not the table level, with the exception of
  1362. ``table.create(checkfirst=True)``.
  1363. The ``table.drop()`` call will now emit a DROP TYPE for a table-level
  1364. enumerated type.
  1365. """
  1366. native_enum = True
  1367. def __init__(self, *enums, **kw):
  1368. """Construct an :class:`_postgresql.ENUM`.
  1369. Arguments are the same as that of
  1370. :class:`_types.Enum`, but also including
  1371. the following parameters.
  1372. :param create_type: Defaults to True.
  1373. Indicates that ``CREATE TYPE`` should be
  1374. emitted, after optionally checking for the
  1375. presence of the type, when the parent
  1376. table is being created; and additionally
  1377. that ``DROP TYPE`` is called when the table
  1378. is dropped. When ``False``, no check
  1379. will be performed and no ``CREATE TYPE``
  1380. or ``DROP TYPE`` is emitted, unless
  1381. :meth:`~.postgresql.ENUM.create`
  1382. or :meth:`~.postgresql.ENUM.drop`
  1383. are called directly.
  1384. Setting to ``False`` is helpful
  1385. when invoking a creation scheme to a SQL file
  1386. without access to the actual database -
  1387. the :meth:`~.postgresql.ENUM.create` and
  1388. :meth:`~.postgresql.ENUM.drop` methods can
  1389. be used to emit SQL to a target bind.
  1390. """
  1391. self.create_type = kw.pop("create_type", True)
  1392. super(ENUM, self).__init__(*enums, **kw)
  1393. @classmethod
  1394. def adapt_emulated_to_native(cls, impl, **kw):
  1395. """Produce a PostgreSQL native :class:`_postgresql.ENUM` from plain
  1396. :class:`.Enum`.
  1397. """
  1398. kw.setdefault("validate_strings", impl.validate_strings)
  1399. kw.setdefault("name", impl.name)
  1400. kw.setdefault("schema", impl.schema)
  1401. kw.setdefault("inherit_schema", impl.inherit_schema)
  1402. kw.setdefault("metadata", impl.metadata)
  1403. kw.setdefault("_create_events", False)
  1404. kw.setdefault("values_callable", impl.values_callable)
  1405. kw.setdefault("omit_aliases", impl._omit_aliases)
  1406. return cls(**kw)
  1407. def create(self, bind=None, checkfirst=True):
  1408. """Emit ``CREATE TYPE`` for this
  1409. :class:`_postgresql.ENUM`.
  1410. If the underlying dialect does not support
  1411. PostgreSQL CREATE TYPE, no action is taken.
  1412. :param bind: a connectable :class:`_engine.Engine`,
  1413. :class:`_engine.Connection`, or similar object to emit
  1414. SQL.
  1415. :param checkfirst: if ``True``, a query against
  1416. the PG catalog will be first performed to see
  1417. if the type does not exist already before
  1418. creating.
  1419. """
  1420. if not bind.dialect.supports_native_enum:
  1421. return
  1422. bind._run_ddl_visitor(self.EnumGenerator, self, checkfirst=checkfirst)
  1423. def drop(self, bind=None, checkfirst=True):
  1424. """Emit ``DROP TYPE`` for this
  1425. :class:`_postgresql.ENUM`.
  1426. If the underlying dialect does not support
  1427. PostgreSQL DROP TYPE, no action is taken.
  1428. :param bind: a connectable :class:`_engine.Engine`,
  1429. :class:`_engine.Connection`, or similar object to emit
  1430. SQL.
  1431. :param checkfirst: if ``True``, a query against
  1432. the PG catalog will be first performed to see
  1433. if the type actually exists before dropping.
  1434. """
  1435. if not bind.dialect.supports_native_enum:
  1436. return
  1437. bind._run_ddl_visitor(self.EnumDropper, self, checkfirst=checkfirst)
  1438. class EnumGenerator(DDLBase):
  1439. def __init__(self, dialect, connection, checkfirst=False, **kwargs):
  1440. super(ENUM.EnumGenerator, self).__init__(connection, **kwargs)
  1441. self.checkfirst = checkfirst
  1442. def _can_create_enum(self, enum):
  1443. if not self.checkfirst:
  1444. return True
  1445. effective_schema = self.connection.schema_for_object(enum)
  1446. return not self.connection.dialect.has_type(
  1447. self.connection, enum.name, schema=effective_schema
  1448. )
  1449. def visit_enum(self, enum):
  1450. if not self._can_create_enum(enum):
  1451. return
  1452. self.connection.execute(CreateEnumType(enum))
  1453. class EnumDropper(DDLBase):
  1454. def __init__(self, dialect, connection, checkfirst=False, **kwargs):
  1455. super(ENUM.EnumDropper, self).__init__(connection, **kwargs)
  1456. self.checkfirst = checkfirst
  1457. def _can_drop_enum(self, enum):
  1458. if not self.checkfirst:
  1459. return True
  1460. effective_schema = self.connection.schema_for_object(enum)
  1461. return self.connection.dialect.has_type(
  1462. self.connection, enum.name, schema=effective_schema
  1463. )
  1464. def visit_enum(self, enum):
  1465. if not self._can_drop_enum(enum):
  1466. return
  1467. self.connection.execute(DropEnumType(enum))
  1468. def _check_for_name_in_memos(self, checkfirst, kw):
  1469. """Look in the 'ddl runner' for 'memos', then
  1470. note our name in that collection.
  1471. This to ensure a particular named enum is operated
  1472. upon only once within any kind of create/drop
  1473. sequence without relying upon "checkfirst".
  1474. """
  1475. if not self.create_type:
  1476. return True
  1477. if "_ddl_runner" in kw:
  1478. ddl_runner = kw["_ddl_runner"]
  1479. if "_pg_enums" in ddl_runner.memo:
  1480. pg_enums = ddl_runner.memo["_pg_enums"]
  1481. else:
  1482. pg_enums = ddl_runner.memo["_pg_enums"] = set()
  1483. present = (self.schema, self.name) in pg_enums
  1484. pg_enums.add((self.schema, self.name))
  1485. return present
  1486. else:
  1487. return False
  1488. def _on_table_create(self, target, bind, checkfirst=False, **kw):
  1489. if (
  1490. checkfirst
  1491. or (
  1492. not self.metadata
  1493. and not kw.get("_is_metadata_operation", False)
  1494. )
  1495. ) and not self._check_for_name_in_memos(checkfirst, kw):
  1496. self.create(bind=bind, checkfirst=checkfirst)
  1497. def _on_table_drop(self, target, bind, checkfirst=False, **kw):
  1498. if (
  1499. not self.metadata
  1500. and not kw.get("_is_metadata_operation", False)
  1501. and not self._check_for_name_in_memos(checkfirst, kw)
  1502. ):
  1503. self.drop(bind=bind, checkfirst=checkfirst)
  1504. def _on_metadata_create(self, target, bind, checkfirst=False, **kw):
  1505. if not self._check_for_name_in_memos(checkfirst, kw):
  1506. self.create(bind=bind, checkfirst=checkfirst)
  1507. def _on_metadata_drop(self, target, bind, checkfirst=False, **kw):
  1508. if not self._check_for_name_in_memos(checkfirst, kw):
  1509. self.drop(bind=bind, checkfirst=checkfirst)
  1510. colspecs = {
  1511. sqltypes.ARRAY: _array.ARRAY,
  1512. sqltypes.Interval: INTERVAL,
  1513. sqltypes.Enum: ENUM,
  1514. sqltypes.JSON.JSONPathType: _json.JSONPathType,
  1515. sqltypes.JSON: _json.JSON,
  1516. }
  1517. ischema_names = {
  1518. "_array": _array.ARRAY,
  1519. "hstore": _hstore.HSTORE,
  1520. "json": _json.JSON,
  1521. "jsonb": _json.JSONB,
  1522. "int4range": _ranges.INT4RANGE,
  1523. "int8range": _ranges.INT8RANGE,
  1524. "numrange": _ranges.NUMRANGE,
  1525. "daterange": _ranges.DATERANGE,
  1526. "tsrange": _ranges.TSRANGE,
  1527. "tstzrange": _ranges.TSTZRANGE,
  1528. "integer": INTEGER,
  1529. "bigint": BIGINT,
  1530. "smallint": SMALLINT,
  1531. "character varying": VARCHAR,
  1532. "character": CHAR,
  1533. '"char"': sqltypes.String,
  1534. "name": sqltypes.String,
  1535. "text": TEXT,
  1536. "numeric": NUMERIC,
  1537. "float": FLOAT,
  1538. "real": REAL,
  1539. "inet": INET,
  1540. "cidr": CIDR,
  1541. "uuid": UUID,
  1542. "bit": BIT,
  1543. "bit varying": BIT,
  1544. "macaddr": MACADDR,
  1545. "money": MONEY,
  1546. "oid": OID,
  1547. "regclass": REGCLASS,
  1548. "double precision": DOUBLE_PRECISION,
  1549. "timestamp": TIMESTAMP,
  1550. "timestamp with time zone": TIMESTAMP,
  1551. "timestamp without time zone": TIMESTAMP,
  1552. "time with time zone": TIME,
  1553. "time without time zone": TIME,
  1554. "date": DATE,
  1555. "time": TIME,
  1556. "bytea": BYTEA,
  1557. "boolean": BOOLEAN,
  1558. "interval": INTERVAL,
  1559. "tsvector": TSVECTOR,
  1560. }
  1561. class PGCompiler(compiler.SQLCompiler):
  1562. def visit_array(self, element, **kw):
  1563. return "ARRAY[%s]" % self.visit_clauselist(element, **kw)
  1564. def visit_slice(self, element, **kw):
  1565. return "%s:%s" % (
  1566. self.process(element.start, **kw),
  1567. self.process(element.stop, **kw),
  1568. )
  1569. def visit_json_getitem_op_binary(
  1570. self, binary, operator, _cast_applied=False, **kw
  1571. ):
  1572. if (
  1573. not _cast_applied
  1574. and binary.type._type_affinity is not sqltypes.JSON
  1575. ):
  1576. kw["_cast_applied"] = True
  1577. return self.process(sql.cast(binary, binary.type), **kw)
  1578. kw["eager_grouping"] = True
  1579. return self._generate_generic_binary(
  1580. binary, " -> " if not _cast_applied else " ->> ", **kw
  1581. )
  1582. def visit_json_path_getitem_op_binary(
  1583. self, binary, operator, _cast_applied=False, **kw
  1584. ):
  1585. if (
  1586. not _cast_applied
  1587. and binary.type._type_affinity is not sqltypes.JSON
  1588. ):
  1589. kw["_cast_applied"] = True
  1590. return self.process(sql.cast(binary, binary.type), **kw)
  1591. kw["eager_grouping"] = True
  1592. return self._generate_generic_binary(
  1593. binary, " #> " if not _cast_applied else " #>> ", **kw
  1594. )
  1595. def visit_getitem_binary(self, binary, operator, **kw):
  1596. return "%s[%s]" % (
  1597. self.process(binary.left, **kw),
  1598. self.process(binary.right, **kw),
  1599. )
  1600. def visit_aggregate_order_by(self, element, **kw):
  1601. return "%s ORDER BY %s" % (
  1602. self.process(element.target, **kw),
  1603. self.process(element.order_by, **kw),
  1604. )
  1605. def visit_match_op_binary(self, binary, operator, **kw):
  1606. if "postgresql_regconfig" in binary.modifiers:
  1607. regconfig = self.render_literal_value(
  1608. binary.modifiers["postgresql_regconfig"], sqltypes.STRINGTYPE
  1609. )
  1610. if regconfig:
  1611. return "%s @@ to_tsquery(%s, %s)" % (
  1612. self.process(binary.left, **kw),
  1613. regconfig,
  1614. self.process(binary.right, **kw),
  1615. )
  1616. return "%s @@ to_tsquery(%s)" % (
  1617. self.process(binary.left, **kw),
  1618. self.process(binary.right, **kw),
  1619. )
  1620. def visit_ilike_op_binary(self, binary, operator, **kw):
  1621. escape = binary.modifiers.get("escape", None)
  1622. return "%s ILIKE %s" % (
  1623. self.process(binary.left, **kw),
  1624. self.process(binary.right, **kw),
  1625. ) + (
  1626. " ESCAPE " + self.render_literal_value(escape, sqltypes.STRINGTYPE)
  1627. if escape
  1628. else ""
  1629. )
  1630. def visit_not_ilike_op_binary(self, binary, operator, **kw):
  1631. escape = binary.modifiers.get("escape", None)
  1632. return "%s NOT ILIKE %s" % (
  1633. self.process(binary.left, **kw),
  1634. self.process(binary.right, **kw),
  1635. ) + (
  1636. " ESCAPE " + self.render_literal_value(escape, sqltypes.STRINGTYPE)
  1637. if escape
  1638. else ""
  1639. )
  1640. def _regexp_match(self, base_op, binary, operator, kw):
  1641. flags = binary.modifiers["flags"]
  1642. if flags is None:
  1643. return self._generate_generic_binary(
  1644. binary, " %s " % base_op, **kw
  1645. )
  1646. if isinstance(flags, elements.BindParameter) and flags.value == "i":
  1647. return self._generate_generic_binary(
  1648. binary, " %s* " % base_op, **kw
  1649. )
  1650. flags = self.process(flags, **kw)
  1651. string = self.process(binary.left, **kw)
  1652. pattern = self.process(binary.right, **kw)
  1653. return "%s %s CONCAT('(?', %s, ')', %s)" % (
  1654. string,
  1655. base_op,
  1656. flags,
  1657. pattern,
  1658. )
  1659. def visit_regexp_match_op_binary(self, binary, operator, **kw):
  1660. return self._regexp_match("~", binary, operator, kw)
  1661. def visit_not_regexp_match_op_binary(self, binary, operator, **kw):
  1662. return self._regexp_match("!~", binary, operator, kw)
  1663. def visit_regexp_replace_op_binary(self, binary, operator, **kw):
  1664. string = self.process(binary.left, **kw)
  1665. pattern = self.process(binary.right, **kw)
  1666. flags = binary.modifiers["flags"]
  1667. if flags is not None:
  1668. flags = self.process(flags, **kw)
  1669. replacement = self.process(binary.modifiers["replacement"], **kw)
  1670. if flags is None:
  1671. return "REGEXP_REPLACE(%s, %s, %s)" % (
  1672. string,
  1673. pattern,
  1674. replacement,
  1675. )
  1676. else:
  1677. return "REGEXP_REPLACE(%s, %s, %s, %s)" % (
  1678. string,
  1679. pattern,
  1680. replacement,
  1681. flags,
  1682. )
  1683. def visit_empty_set_expr(self, element_types):
  1684. # cast the empty set to the type we are comparing against. if
  1685. # we are comparing against the null type, pick an arbitrary
  1686. # datatype for the empty set
  1687. return "SELECT %s WHERE 1!=1" % (
  1688. ", ".join(
  1689. "CAST(NULL AS %s)"
  1690. % self.dialect.type_compiler.process(
  1691. INTEGER() if type_._isnull else type_
  1692. )
  1693. for type_ in element_types or [INTEGER()]
  1694. ),
  1695. )
  1696. def render_literal_value(self, value, type_):
  1697. value = super(PGCompiler, self).render_literal_value(value, type_)
  1698. if self.dialect._backslash_escapes:
  1699. value = value.replace("\\", "\\\\")
  1700. return value
  1701. def visit_sequence(self, seq, **kw):
  1702. return "nextval('%s')" % self.preparer.format_sequence(seq)
  1703. def limit_clause(self, select, **kw):
  1704. text = ""
  1705. if select._limit_clause is not None:
  1706. text += " \n LIMIT " + self.process(select._limit_clause, **kw)
  1707. if select._offset_clause is not None:
  1708. if select._limit_clause is None:
  1709. text += "\n LIMIT ALL"
  1710. text += " OFFSET " + self.process(select._offset_clause, **kw)
  1711. return text
  1712. def format_from_hint_text(self, sqltext, table, hint, iscrud):
  1713. if hint.upper() != "ONLY":
  1714. raise exc.CompileError("Unrecognized hint: %r" % hint)
  1715. return "ONLY " + sqltext
  1716. def get_select_precolumns(self, select, **kw):
  1717. # Do not call super().get_select_precolumns because
  1718. # it will warn/raise when distinct on is present
  1719. if select._distinct or select._distinct_on:
  1720. if select._distinct_on:
  1721. return (
  1722. "DISTINCT ON ("
  1723. + ", ".join(
  1724. [
  1725. self.process(col, **kw)
  1726. for col in select._distinct_on
  1727. ]
  1728. )
  1729. + ") "
  1730. )
  1731. else:
  1732. return "DISTINCT "
  1733. else:
  1734. return ""
  1735. def for_update_clause(self, select, **kw):
  1736. if select._for_update_arg.read:
  1737. if select._for_update_arg.key_share:
  1738. tmp = " FOR KEY SHARE"
  1739. else:
  1740. tmp = " FOR SHARE"
  1741. elif select._for_update_arg.key_share:
  1742. tmp = " FOR NO KEY UPDATE"
  1743. else:
  1744. tmp = " FOR UPDATE"
  1745. if select._for_update_arg.of:
  1746. tables = util.OrderedSet()
  1747. for c in select._for_update_arg.of:
  1748. tables.update(sql_util.surface_selectables_only(c))
  1749. tmp += " OF " + ", ".join(
  1750. self.process(table, ashint=True, use_schema=False, **kw)
  1751. for table in tables
  1752. )
  1753. if select._for_update_arg.nowait:
  1754. tmp += " NOWAIT"
  1755. if select._for_update_arg.skip_locked:
  1756. tmp += " SKIP LOCKED"
  1757. return tmp
  1758. def returning_clause(self, stmt, returning_cols):
  1759. columns = [
  1760. self._label_select_column(None, c, True, False, {})
  1761. for c in expression._select_iterables(returning_cols)
  1762. ]
  1763. return "RETURNING " + ", ".join(columns)
  1764. def visit_substring_func(self, func, **kw):
  1765. s = self.process(func.clauses.clauses[0], **kw)
  1766. start = self.process(func.clauses.clauses[1], **kw)
  1767. if len(func.clauses.clauses) > 2:
  1768. length = self.process(func.clauses.clauses[2], **kw)
  1769. return "SUBSTRING(%s FROM %s FOR %s)" % (s, start, length)
  1770. else:
  1771. return "SUBSTRING(%s FROM %s)" % (s, start)
  1772. def _on_conflict_target(self, clause, **kw):
  1773. if clause.constraint_target is not None:
  1774. target_text = "ON CONSTRAINT %s" % clause.constraint_target
  1775. elif clause.inferred_target_elements is not None:
  1776. target_text = "(%s)" % ", ".join(
  1777. (
  1778. self.preparer.quote(c)
  1779. if isinstance(c, util.string_types)
  1780. else self.process(c, include_table=False, use_schema=False)
  1781. )
  1782. for c in clause.inferred_target_elements
  1783. )
  1784. if clause.inferred_target_whereclause is not None:
  1785. target_text += " WHERE %s" % self.process(
  1786. clause.inferred_target_whereclause,
  1787. include_table=False,
  1788. use_schema=False,
  1789. )
  1790. else:
  1791. target_text = ""
  1792. return target_text
  1793. def visit_on_conflict_do_nothing(self, on_conflict, **kw):
  1794. target_text = self._on_conflict_target(on_conflict, **kw)
  1795. if target_text:
  1796. return "ON CONFLICT %s DO NOTHING" % target_text
  1797. else:
  1798. return "ON CONFLICT DO NOTHING"
  1799. def visit_on_conflict_do_update(self, on_conflict, **kw):
  1800. clause = on_conflict
  1801. target_text = self._on_conflict_target(on_conflict, **kw)
  1802. action_set_ops = []
  1803. set_parameters = dict(clause.update_values_to_set)
  1804. # create a list of column assignment clauses as tuples
  1805. insert_statement = self.stack[-1]["selectable"]
  1806. cols = insert_statement.table.c
  1807. for c in cols:
  1808. col_key = c.key
  1809. if col_key in set_parameters:
  1810. value = set_parameters.pop(col_key)
  1811. elif c in set_parameters:
  1812. value = set_parameters.pop(c)
  1813. else:
  1814. continue
  1815. if coercions._is_literal(value):
  1816. value = elements.BindParameter(None, value, type_=c.type)
  1817. else:
  1818. if (
  1819. isinstance(value, elements.BindParameter)
  1820. and value.type._isnull
  1821. ):
  1822. value = value._clone()
  1823. value.type = c.type
  1824. value_text = self.process(value.self_group(), use_schema=False)
  1825. key_text = self.preparer.quote(col_key)
  1826. action_set_ops.append("%s = %s" % (key_text, value_text))
  1827. # check for names that don't match columns
  1828. if set_parameters:
  1829. util.warn(
  1830. "Additional column names not matching "
  1831. "any column keys in table '%s': %s"
  1832. % (
  1833. self.current_executable.table.name,
  1834. (", ".join("'%s'" % c for c in set_parameters)),
  1835. )
  1836. )
  1837. for k, v in set_parameters.items():
  1838. key_text = (
  1839. self.preparer.quote(k)
  1840. if isinstance(k, util.string_types)
  1841. else self.process(k, use_schema=False)
  1842. )
  1843. value_text = self.process(
  1844. coercions.expect(roles.ExpressionElementRole, v),
  1845. use_schema=False,
  1846. )
  1847. action_set_ops.append("%s = %s" % (key_text, value_text))
  1848. action_text = ", ".join(action_set_ops)
  1849. if clause.update_whereclause is not None:
  1850. action_text += " WHERE %s" % self.process(
  1851. clause.update_whereclause, include_table=True, use_schema=False
  1852. )
  1853. return "ON CONFLICT %s DO UPDATE SET %s" % (target_text, action_text)
  1854. def update_from_clause(
  1855. self, update_stmt, from_table, extra_froms, from_hints, **kw
  1856. ):
  1857. kw["asfrom"] = True
  1858. return "FROM " + ", ".join(
  1859. t._compiler_dispatch(self, fromhints=from_hints, **kw)
  1860. for t in extra_froms
  1861. )
  1862. def delete_extra_from_clause(
  1863. self, delete_stmt, from_table, extra_froms, from_hints, **kw
  1864. ):
  1865. """Render the DELETE .. USING clause specific to PostgreSQL."""
  1866. kw["asfrom"] = True
  1867. return "USING " + ", ".join(
  1868. t._compiler_dispatch(self, fromhints=from_hints, **kw)
  1869. for t in extra_froms
  1870. )
  1871. def fetch_clause(self, select, **kw):
  1872. # pg requires parens for non literal clauses. It's also required for
  1873. # bind parameters if a ::type casts is used by the driver (asyncpg),
  1874. # so it's easiest to just always add it
  1875. text = ""
  1876. if select._offset_clause is not None:
  1877. text += "\n OFFSET (%s) ROWS" % self.process(
  1878. select._offset_clause, **kw
  1879. )
  1880. if select._fetch_clause is not None:
  1881. text += "\n FETCH FIRST (%s)%s ROWS %s" % (
  1882. self.process(select._fetch_clause, **kw),
  1883. " PERCENT" if select._fetch_clause_options["percent"] else "",
  1884. "WITH TIES"
  1885. if select._fetch_clause_options["with_ties"]
  1886. else "ONLY",
  1887. )
  1888. return text
  1889. class PGDDLCompiler(compiler.DDLCompiler):
  1890. def get_column_specification(self, column, **kwargs):
  1891. colspec = self.preparer.format_column(column)
  1892. impl_type = column.type.dialect_impl(self.dialect)
  1893. if isinstance(impl_type, sqltypes.TypeDecorator):
  1894. impl_type = impl_type.impl
  1895. has_identity = (
  1896. column.identity is not None
  1897. and self.dialect.supports_identity_columns
  1898. )
  1899. if (
  1900. column.primary_key
  1901. and column is column.table._autoincrement_column
  1902. and (
  1903. self.dialect.supports_smallserial
  1904. or not isinstance(impl_type, sqltypes.SmallInteger)
  1905. )
  1906. and not has_identity
  1907. and (
  1908. column.default is None
  1909. or (
  1910. isinstance(column.default, schema.Sequence)
  1911. and column.default.optional
  1912. )
  1913. )
  1914. ):
  1915. if isinstance(impl_type, sqltypes.BigInteger):
  1916. colspec += " BIGSERIAL"
  1917. elif isinstance(impl_type, sqltypes.SmallInteger):
  1918. colspec += " SMALLSERIAL"
  1919. else:
  1920. colspec += " SERIAL"
  1921. else:
  1922. colspec += " " + self.dialect.type_compiler.process(
  1923. column.type,
  1924. type_expression=column,
  1925. identifier_preparer=self.preparer,
  1926. )
  1927. default = self.get_column_default_string(column)
  1928. if default is not None:
  1929. colspec += " DEFAULT " + default
  1930. if column.computed is not None:
  1931. colspec += " " + self.process(column.computed)
  1932. if has_identity:
  1933. colspec += " " + self.process(column.identity)
  1934. if not column.nullable and not has_identity:
  1935. colspec += " NOT NULL"
  1936. elif column.nullable and has_identity:
  1937. colspec += " NULL"
  1938. return colspec
  1939. def visit_check_constraint(self, constraint):
  1940. if constraint._type_bound:
  1941. typ = list(constraint.columns)[0].type
  1942. if (
  1943. isinstance(typ, sqltypes.ARRAY)
  1944. and isinstance(typ.item_type, sqltypes.Enum)
  1945. and not typ.item_type.native_enum
  1946. ):
  1947. raise exc.CompileError(
  1948. "PostgreSQL dialect cannot produce the CHECK constraint "
  1949. "for ARRAY of non-native ENUM; please specify "
  1950. "create_constraint=False on this Enum datatype."
  1951. )
  1952. return super(PGDDLCompiler, self).visit_check_constraint(constraint)
  1953. def visit_drop_table_comment(self, drop):
  1954. return "COMMENT ON TABLE %s IS NULL" % self.preparer.format_table(
  1955. drop.element
  1956. )
  1957. def visit_create_enum_type(self, create):
  1958. type_ = create.element
  1959. return "CREATE TYPE %s AS ENUM (%s)" % (
  1960. self.preparer.format_type(type_),
  1961. ", ".join(
  1962. self.sql_compiler.process(sql.literal(e), literal_binds=True)
  1963. for e in type_.enums
  1964. ),
  1965. )
  1966. def visit_drop_enum_type(self, drop):
  1967. type_ = drop.element
  1968. return "DROP TYPE %s" % (self.preparer.format_type(type_))
  1969. def visit_create_index(self, create):
  1970. preparer = self.preparer
  1971. index = create.element
  1972. self._verify_index_table(index)
  1973. text = "CREATE "
  1974. if index.unique:
  1975. text += "UNIQUE "
  1976. text += "INDEX "
  1977. if self.dialect._supports_create_index_concurrently:
  1978. concurrently = index.dialect_options["postgresql"]["concurrently"]
  1979. if concurrently:
  1980. text += "CONCURRENTLY "
  1981. if create.if_not_exists:
  1982. text += "IF NOT EXISTS "
  1983. text += "%s ON %s " % (
  1984. self._prepared_index_name(index, include_schema=False),
  1985. preparer.format_table(index.table),
  1986. )
  1987. using = index.dialect_options["postgresql"]["using"]
  1988. if using:
  1989. text += (
  1990. "USING %s "
  1991. % self.preparer.validate_sql_phrase(using, IDX_USING).lower()
  1992. )
  1993. ops = index.dialect_options["postgresql"]["ops"]
  1994. text += "(%s)" % (
  1995. ", ".join(
  1996. [
  1997. self.sql_compiler.process(
  1998. expr.self_group()
  1999. if not isinstance(expr, expression.ColumnClause)
  2000. else expr,
  2001. include_table=False,
  2002. literal_binds=True,
  2003. )
  2004. + (
  2005. (" " + ops[expr.key])
  2006. if hasattr(expr, "key") and expr.key in ops
  2007. else ""
  2008. )
  2009. for expr in index.expressions
  2010. ]
  2011. )
  2012. )
  2013. includeclause = index.dialect_options["postgresql"]["include"]
  2014. if includeclause:
  2015. inclusions = [
  2016. index.table.c[col]
  2017. if isinstance(col, util.string_types)
  2018. else col
  2019. for col in includeclause
  2020. ]
  2021. text += " INCLUDE (%s)" % ", ".join(
  2022. [preparer.quote(c.name) for c in inclusions]
  2023. )
  2024. withclause = index.dialect_options["postgresql"]["with"]
  2025. if withclause:
  2026. text += " WITH (%s)" % (
  2027. ", ".join(
  2028. [
  2029. "%s = %s" % storage_parameter
  2030. for storage_parameter in withclause.items()
  2031. ]
  2032. )
  2033. )
  2034. tablespace_name = index.dialect_options["postgresql"]["tablespace"]
  2035. if tablespace_name:
  2036. text += " TABLESPACE %s" % preparer.quote(tablespace_name)
  2037. whereclause = index.dialect_options["postgresql"]["where"]
  2038. if whereclause is not None:
  2039. whereclause = coercions.expect(
  2040. roles.DDLExpressionRole, whereclause
  2041. )
  2042. where_compiled = self.sql_compiler.process(
  2043. whereclause, include_table=False, literal_binds=True
  2044. )
  2045. text += " WHERE " + where_compiled
  2046. return text
  2047. def visit_drop_index(self, drop):
  2048. index = drop.element
  2049. text = "\nDROP INDEX "
  2050. if self.dialect._supports_drop_index_concurrently:
  2051. concurrently = index.dialect_options["postgresql"]["concurrently"]
  2052. if concurrently:
  2053. text += "CONCURRENTLY "
  2054. if drop.if_exists:
  2055. text += "IF EXISTS "
  2056. text += self._prepared_index_name(index, include_schema=True)
  2057. return text
  2058. def visit_exclude_constraint(self, constraint, **kw):
  2059. text = ""
  2060. if constraint.name is not None:
  2061. text += "CONSTRAINT %s " % self.preparer.format_constraint(
  2062. constraint
  2063. )
  2064. elements = []
  2065. for expr, name, op in constraint._render_exprs:
  2066. kw["include_table"] = False
  2067. exclude_element = self.sql_compiler.process(expr, **kw) + (
  2068. (" " + constraint.ops[expr.key])
  2069. if hasattr(expr, "key") and expr.key in constraint.ops
  2070. else ""
  2071. )
  2072. elements.append("%s WITH %s" % (exclude_element, op))
  2073. text += "EXCLUDE USING %s (%s)" % (
  2074. self.preparer.validate_sql_phrase(
  2075. constraint.using, IDX_USING
  2076. ).lower(),
  2077. ", ".join(elements),
  2078. )
  2079. if constraint.where is not None:
  2080. text += " WHERE (%s)" % self.sql_compiler.process(
  2081. constraint.where, literal_binds=True
  2082. )
  2083. text += self.define_constraint_deferrability(constraint)
  2084. return text
  2085. def post_create_table(self, table):
  2086. table_opts = []
  2087. pg_opts = table.dialect_options["postgresql"]
  2088. inherits = pg_opts.get("inherits")
  2089. if inherits is not None:
  2090. if not isinstance(inherits, (list, tuple)):
  2091. inherits = (inherits,)
  2092. table_opts.append(
  2093. "\n INHERITS ( "
  2094. + ", ".join(self.preparer.quote(name) for name in inherits)
  2095. + " )"
  2096. )
  2097. if pg_opts["partition_by"]:
  2098. table_opts.append("\n PARTITION BY %s" % pg_opts["partition_by"])
  2099. if pg_opts["with_oids"] is True:
  2100. table_opts.append("\n WITH OIDS")
  2101. elif pg_opts["with_oids"] is False:
  2102. table_opts.append("\n WITHOUT OIDS")
  2103. if pg_opts["on_commit"]:
  2104. on_commit_options = pg_opts["on_commit"].replace("_", " ").upper()
  2105. table_opts.append("\n ON COMMIT %s" % on_commit_options)
  2106. if pg_opts["tablespace"]:
  2107. tablespace_name = pg_opts["tablespace"]
  2108. table_opts.append(
  2109. "\n TABLESPACE %s" % self.preparer.quote(tablespace_name)
  2110. )
  2111. return "".join(table_opts)
  2112. def visit_computed_column(self, generated):
  2113. if generated.persisted is False:
  2114. raise exc.CompileError(
  2115. "PostrgreSQL computed columns do not support 'virtual' "
  2116. "persistence; set the 'persisted' flag to None or True for "
  2117. "PostgreSQL support."
  2118. )
  2119. return "GENERATED ALWAYS AS (%s) STORED" % self.sql_compiler.process(
  2120. generated.sqltext, include_table=False, literal_binds=True
  2121. )
  2122. def visit_create_sequence(self, create, **kw):
  2123. prefix = None
  2124. if create.element.data_type is not None:
  2125. prefix = " AS %s" % self.type_compiler.process(
  2126. create.element.data_type
  2127. )
  2128. return super(PGDDLCompiler, self).visit_create_sequence(
  2129. create, prefix=prefix, **kw
  2130. )
  2131. class PGTypeCompiler(compiler.GenericTypeCompiler):
  2132. def visit_TSVECTOR(self, type_, **kw):
  2133. return "TSVECTOR"
  2134. def visit_INET(self, type_, **kw):
  2135. return "INET"
  2136. def visit_CIDR(self, type_, **kw):
  2137. return "CIDR"
  2138. def visit_MACADDR(self, type_, **kw):
  2139. return "MACADDR"
  2140. def visit_MONEY(self, type_, **kw):
  2141. return "MONEY"
  2142. def visit_OID(self, type_, **kw):
  2143. return "OID"
  2144. def visit_REGCLASS(self, type_, **kw):
  2145. return "REGCLASS"
  2146. def visit_FLOAT(self, type_, **kw):
  2147. if not type_.precision:
  2148. return "FLOAT"
  2149. else:
  2150. return "FLOAT(%(precision)s)" % {"precision": type_.precision}
  2151. def visit_DOUBLE_PRECISION(self, type_, **kw):
  2152. return "DOUBLE PRECISION"
  2153. def visit_BIGINT(self, type_, **kw):
  2154. return "BIGINT"
  2155. def visit_HSTORE(self, type_, **kw):
  2156. return "HSTORE"
  2157. def visit_JSON(self, type_, **kw):
  2158. return "JSON"
  2159. def visit_JSONB(self, type_, **kw):
  2160. return "JSONB"
  2161. def visit_INT4RANGE(self, type_, **kw):
  2162. return "INT4RANGE"
  2163. def visit_INT8RANGE(self, type_, **kw):
  2164. return "INT8RANGE"
  2165. def visit_NUMRANGE(self, type_, **kw):
  2166. return "NUMRANGE"
  2167. def visit_DATERANGE(self, type_, **kw):
  2168. return "DATERANGE"
  2169. def visit_TSRANGE(self, type_, **kw):
  2170. return "TSRANGE"
  2171. def visit_TSTZRANGE(self, type_, **kw):
  2172. return "TSTZRANGE"
  2173. def visit_datetime(self, type_, **kw):
  2174. return self.visit_TIMESTAMP(type_, **kw)
  2175. def visit_enum(self, type_, **kw):
  2176. if not type_.native_enum or not self.dialect.supports_native_enum:
  2177. return super(PGTypeCompiler, self).visit_enum(type_, **kw)
  2178. else:
  2179. return self.visit_ENUM(type_, **kw)
  2180. def visit_ENUM(self, type_, identifier_preparer=None, **kw):
  2181. if identifier_preparer is None:
  2182. identifier_preparer = self.dialect.identifier_preparer
  2183. return identifier_preparer.format_type(type_)
  2184. def visit_TIMESTAMP(self, type_, **kw):
  2185. return "TIMESTAMP%s %s" % (
  2186. "(%d)" % type_.precision
  2187. if getattr(type_, "precision", None) is not None
  2188. else "",
  2189. (type_.timezone and "WITH" or "WITHOUT") + " TIME ZONE",
  2190. )
  2191. def visit_TIME(self, type_, **kw):
  2192. return "TIME%s %s" % (
  2193. "(%d)" % type_.precision
  2194. if getattr(type_, "precision", None) is not None
  2195. else "",
  2196. (type_.timezone and "WITH" or "WITHOUT") + " TIME ZONE",
  2197. )
  2198. def visit_INTERVAL(self, type_, **kw):
  2199. text = "INTERVAL"
  2200. if type_.fields is not None:
  2201. text += " " + type_.fields
  2202. if type_.precision is not None:
  2203. text += " (%d)" % type_.precision
  2204. return text
  2205. def visit_BIT(self, type_, **kw):
  2206. if type_.varying:
  2207. compiled = "BIT VARYING"
  2208. if type_.length is not None:
  2209. compiled += "(%d)" % type_.length
  2210. else:
  2211. compiled = "BIT(%d)" % type_.length
  2212. return compiled
  2213. def visit_UUID(self, type_, **kw):
  2214. return "UUID"
  2215. def visit_large_binary(self, type_, **kw):
  2216. return self.visit_BYTEA(type_, **kw)
  2217. def visit_BYTEA(self, type_, **kw):
  2218. return "BYTEA"
  2219. def visit_ARRAY(self, type_, **kw):
  2220. # TODO: pass **kw?
  2221. inner = self.process(type_.item_type)
  2222. return re.sub(
  2223. r"((?: COLLATE.*)?)$",
  2224. (
  2225. r"%s\1"
  2226. % (
  2227. "[]"
  2228. * (type_.dimensions if type_.dimensions is not None else 1)
  2229. )
  2230. ),
  2231. inner,
  2232. count=1,
  2233. )
  2234. class PGIdentifierPreparer(compiler.IdentifierPreparer):
  2235. reserved_words = RESERVED_WORDS
  2236. def _unquote_identifier(self, value):
  2237. if value[0] == self.initial_quote:
  2238. value = value[1:-1].replace(
  2239. self.escape_to_quote, self.escape_quote
  2240. )
  2241. return value
  2242. def format_type(self, type_, use_schema=True):
  2243. if not type_.name:
  2244. raise exc.CompileError("PostgreSQL ENUM type requires a name.")
  2245. name = self.quote(type_.name)
  2246. effective_schema = self.schema_for_object(type_)
  2247. if (
  2248. not self.omit_schema
  2249. and use_schema
  2250. and effective_schema is not None
  2251. ):
  2252. name = self.quote_schema(effective_schema) + "." + name
  2253. return name
  2254. class PGInspector(reflection.Inspector):
  2255. def get_table_oid(self, table_name, schema=None):
  2256. """Return the OID for the given table name."""
  2257. with self._operation_context() as conn:
  2258. return self.dialect.get_table_oid(
  2259. conn, table_name, schema, info_cache=self.info_cache
  2260. )
  2261. def get_enums(self, schema=None):
  2262. """Return a list of ENUM objects.
  2263. Each member is a dictionary containing these fields:
  2264. * name - name of the enum
  2265. * schema - the schema name for the enum.
  2266. * visible - boolean, whether or not this enum is visible
  2267. in the default search path.
  2268. * labels - a list of string labels that apply to the enum.
  2269. :param schema: schema name. If None, the default schema
  2270. (typically 'public') is used. May also be set to '*' to
  2271. indicate load enums for all schemas.
  2272. .. versionadded:: 1.0.0
  2273. """
  2274. schema = schema or self.default_schema_name
  2275. with self._operation_context() as conn:
  2276. return self.dialect._load_enums(conn, schema)
  2277. def get_foreign_table_names(self, schema=None):
  2278. """Return a list of FOREIGN TABLE names.
  2279. Behavior is similar to that of
  2280. :meth:`_reflection.Inspector.get_table_names`,
  2281. except that the list is limited to those tables that report a
  2282. ``relkind`` value of ``f``.
  2283. .. versionadded:: 1.0.0
  2284. """
  2285. schema = schema or self.default_schema_name
  2286. with self._operation_context() as conn:
  2287. return self.dialect._get_foreign_table_names(conn, schema)
  2288. def get_view_names(self, schema=None, include=("plain", "materialized")):
  2289. """Return all view names in `schema`.
  2290. :param schema: Optional, retrieve names from a non-default schema.
  2291. For special quoting, use :class:`.quoted_name`.
  2292. :param include: specify which types of views to return. Passed
  2293. as a string value (for a single type) or a tuple (for any number
  2294. of types). Defaults to ``('plain', 'materialized')``.
  2295. .. versionadded:: 1.1
  2296. """
  2297. with self._operation_context() as conn:
  2298. return self.dialect.get_view_names(
  2299. conn, schema, info_cache=self.info_cache, include=include
  2300. )
  2301. class CreateEnumType(schema._CreateDropBase):
  2302. __visit_name__ = "create_enum_type"
  2303. class DropEnumType(schema._CreateDropBase):
  2304. __visit_name__ = "drop_enum_type"
  2305. class PGExecutionContext(default.DefaultExecutionContext):
  2306. def fire_sequence(self, seq, type_):
  2307. return self._execute_scalar(
  2308. (
  2309. "select nextval('%s')"
  2310. % self.identifier_preparer.format_sequence(seq)
  2311. ),
  2312. type_,
  2313. )
  2314. def get_insert_default(self, column):
  2315. if column.primary_key and column is column.table._autoincrement_column:
  2316. if column.server_default and column.server_default.has_argument:
  2317. # pre-execute passive defaults on primary key columns
  2318. return self._execute_scalar(
  2319. "select %s" % column.server_default.arg, column.type
  2320. )
  2321. elif column.default is None or (
  2322. column.default.is_sequence and column.default.optional
  2323. ):
  2324. # execute the sequence associated with a SERIAL primary
  2325. # key column. for non-primary-key SERIAL, the ID just
  2326. # generates server side.
  2327. try:
  2328. seq_name = column._postgresql_seq_name
  2329. except AttributeError:
  2330. tab = column.table.name
  2331. col = column.name
  2332. tab = tab[0 : 29 + max(0, (29 - len(col)))]
  2333. col = col[0 : 29 + max(0, (29 - len(tab)))]
  2334. name = "%s_%s_seq" % (tab, col)
  2335. column._postgresql_seq_name = seq_name = name
  2336. if column.table is not None:
  2337. effective_schema = self.connection.schema_for_object(
  2338. column.table
  2339. )
  2340. else:
  2341. effective_schema = None
  2342. if effective_schema is not None:
  2343. exc = 'select nextval(\'"%s"."%s"\')' % (
  2344. effective_schema,
  2345. seq_name,
  2346. )
  2347. else:
  2348. exc = "select nextval('\"%s\"')" % (seq_name,)
  2349. return self._execute_scalar(exc, column.type)
  2350. return super(PGExecutionContext, self).get_insert_default(column)
  2351. def should_autocommit_text(self, statement):
  2352. return AUTOCOMMIT_REGEXP.match(statement)
  2353. class PGReadOnlyConnectionCharacteristic(
  2354. characteristics.ConnectionCharacteristic
  2355. ):
  2356. transactional = True
  2357. def reset_characteristic(self, dialect, dbapi_conn):
  2358. dialect.set_readonly(dbapi_conn, False)
  2359. def set_characteristic(self, dialect, dbapi_conn, value):
  2360. dialect.set_readonly(dbapi_conn, value)
  2361. def get_characteristic(self, dialect, dbapi_conn):
  2362. return dialect.get_readonly(dbapi_conn)
  2363. class PGDeferrableConnectionCharacteristic(
  2364. characteristics.ConnectionCharacteristic
  2365. ):
  2366. transactional = True
  2367. def reset_characteristic(self, dialect, dbapi_conn):
  2368. dialect.set_deferrable(dbapi_conn, False)
  2369. def set_characteristic(self, dialect, dbapi_conn, value):
  2370. dialect.set_deferrable(dbapi_conn, value)
  2371. def get_characteristic(self, dialect, dbapi_conn):
  2372. return dialect.get_deferrable(dbapi_conn)
  2373. class PGDialect(default.DefaultDialect):
  2374. name = "postgresql"
  2375. supports_statement_cache = True
  2376. supports_alter = True
  2377. max_identifier_length = 63
  2378. supports_sane_rowcount = True
  2379. supports_native_enum = True
  2380. supports_native_boolean = True
  2381. supports_smallserial = True
  2382. supports_sequences = True
  2383. sequences_optional = True
  2384. preexecute_autoincrement_sequences = True
  2385. postfetch_lastrowid = False
  2386. supports_comments = True
  2387. supports_default_values = True
  2388. supports_default_metavalue = True
  2389. supports_empty_insert = False
  2390. supports_multivalues_insert = True
  2391. supports_identity_columns = True
  2392. default_paramstyle = "pyformat"
  2393. ischema_names = ischema_names
  2394. colspecs = colspecs
  2395. statement_compiler = PGCompiler
  2396. ddl_compiler = PGDDLCompiler
  2397. type_compiler = PGTypeCompiler
  2398. preparer = PGIdentifierPreparer
  2399. execution_ctx_cls = PGExecutionContext
  2400. inspector = PGInspector
  2401. isolation_level = None
  2402. implicit_returning = True
  2403. full_returning = True
  2404. connection_characteristics = (
  2405. default.DefaultDialect.connection_characteristics
  2406. )
  2407. connection_characteristics = connection_characteristics.union(
  2408. {
  2409. "postgresql_readonly": PGReadOnlyConnectionCharacteristic(),
  2410. "postgresql_deferrable": PGDeferrableConnectionCharacteristic(),
  2411. }
  2412. )
  2413. construct_arguments = [
  2414. (
  2415. schema.Index,
  2416. {
  2417. "using": False,
  2418. "include": None,
  2419. "where": None,
  2420. "ops": {},
  2421. "concurrently": False,
  2422. "with": {},
  2423. "tablespace": None,
  2424. },
  2425. ),
  2426. (
  2427. schema.Table,
  2428. {
  2429. "ignore_search_path": False,
  2430. "tablespace": None,
  2431. "partition_by": None,
  2432. "with_oids": None,
  2433. "on_commit": None,
  2434. "inherits": None,
  2435. },
  2436. ),
  2437. ]
  2438. reflection_options = ("postgresql_ignore_search_path",)
  2439. _backslash_escapes = True
  2440. _supports_create_index_concurrently = True
  2441. _supports_drop_index_concurrently = True
  2442. def __init__(
  2443. self,
  2444. isolation_level=None,
  2445. json_serializer=None,
  2446. json_deserializer=None,
  2447. **kwargs
  2448. ):
  2449. default.DefaultDialect.__init__(self, **kwargs)
  2450. # the isolation_level parameter to the PGDialect itself is legacy.
  2451. # still works however the execution_options method is the one that
  2452. # is documented.
  2453. self.isolation_level = isolation_level
  2454. self._json_deserializer = json_deserializer
  2455. self._json_serializer = json_serializer
  2456. def initialize(self, connection):
  2457. super(PGDialect, self).initialize(connection)
  2458. if self.server_version_info <= (8, 2):
  2459. self.full_returning = self.implicit_returning = False
  2460. self.supports_native_enum = self.server_version_info >= (8, 3)
  2461. if not self.supports_native_enum:
  2462. self.colspecs = self.colspecs.copy()
  2463. # pop base Enum type
  2464. self.colspecs.pop(sqltypes.Enum, None)
  2465. # psycopg2, others may have placed ENUM here as well
  2466. self.colspecs.pop(ENUM, None)
  2467. # http://www.postgresql.org/docs/9.3/static/release-9-2.html#AEN116689
  2468. self.supports_smallserial = self.server_version_info >= (9, 2)
  2469. if self.server_version_info < (8, 2):
  2470. self._backslash_escapes = False
  2471. else:
  2472. # ensure this query is not emitted on server version < 8.2
  2473. # as it will fail
  2474. std_string = connection.exec_driver_sql(
  2475. "show standard_conforming_strings"
  2476. ).scalar()
  2477. self._backslash_escapes = std_string == "off"
  2478. self._supports_create_index_concurrently = (
  2479. self.server_version_info >= (8, 2)
  2480. )
  2481. self._supports_drop_index_concurrently = self.server_version_info >= (
  2482. 9,
  2483. 2,
  2484. )
  2485. self.supports_identity_columns = self.server_version_info >= (10,)
  2486. def on_connect(self):
  2487. if self.isolation_level is not None:
  2488. def connect(conn):
  2489. self.set_isolation_level(conn, self.isolation_level)
  2490. return connect
  2491. else:
  2492. return None
  2493. _isolation_lookup = set(
  2494. [
  2495. "SERIALIZABLE",
  2496. "READ UNCOMMITTED",
  2497. "READ COMMITTED",
  2498. "REPEATABLE READ",
  2499. ]
  2500. )
  2501. def set_isolation_level(self, connection, level):
  2502. level = level.replace("_", " ")
  2503. if level not in self._isolation_lookup:
  2504. raise exc.ArgumentError(
  2505. "Invalid value '%s' for isolation_level. "
  2506. "Valid isolation levels for %s are %s"
  2507. % (level, self.name, ", ".join(self._isolation_lookup))
  2508. )
  2509. cursor = connection.cursor()
  2510. cursor.execute(
  2511. "SET SESSION CHARACTERISTICS AS TRANSACTION "
  2512. "ISOLATION LEVEL %s" % level
  2513. )
  2514. cursor.execute("COMMIT")
  2515. cursor.close()
  2516. def get_isolation_level(self, connection):
  2517. cursor = connection.cursor()
  2518. cursor.execute("show transaction isolation level")
  2519. val = cursor.fetchone()[0]
  2520. cursor.close()
  2521. return val.upper()
  2522. def set_readonly(self, connection, value):
  2523. raise NotImplementedError()
  2524. def get_readonly(self, connection):
  2525. raise NotImplementedError()
  2526. def set_deferrable(self, connection, value):
  2527. raise NotImplementedError()
  2528. def get_deferrable(self, connection):
  2529. raise NotImplementedError()
  2530. def do_begin_twophase(self, connection, xid):
  2531. self.do_begin(connection.connection)
  2532. def do_prepare_twophase(self, connection, xid):
  2533. connection.exec_driver_sql("PREPARE TRANSACTION '%s'" % xid)
  2534. def do_rollback_twophase(
  2535. self, connection, xid, is_prepared=True, recover=False
  2536. ):
  2537. if is_prepared:
  2538. if recover:
  2539. # FIXME: ugly hack to get out of transaction
  2540. # context when committing recoverable transactions
  2541. # Must find out a way how to make the dbapi not
  2542. # open a transaction.
  2543. connection.exec_driver_sql("ROLLBACK")
  2544. connection.exec_driver_sql("ROLLBACK PREPARED '%s'" % xid)
  2545. connection.exec_driver_sql("BEGIN")
  2546. self.do_rollback(connection.connection)
  2547. else:
  2548. self.do_rollback(connection.connection)
  2549. def do_commit_twophase(
  2550. self, connection, xid, is_prepared=True, recover=False
  2551. ):
  2552. if is_prepared:
  2553. if recover:
  2554. connection.exec_driver_sql("ROLLBACK")
  2555. connection.exec_driver_sql("COMMIT PREPARED '%s'" % xid)
  2556. connection.exec_driver_sql("BEGIN")
  2557. self.do_rollback(connection.connection)
  2558. else:
  2559. self.do_commit(connection.connection)
  2560. def do_recover_twophase(self, connection):
  2561. resultset = connection.execute(
  2562. sql.text("SELECT gid FROM pg_prepared_xacts")
  2563. )
  2564. return [row[0] for row in resultset]
  2565. def _get_default_schema_name(self, connection):
  2566. return connection.exec_driver_sql("select current_schema()").scalar()
  2567. def has_schema(self, connection, schema):
  2568. query = (
  2569. "select nspname from pg_namespace " "where lower(nspname)=:schema"
  2570. )
  2571. cursor = connection.execute(
  2572. sql.text(query).bindparams(
  2573. sql.bindparam(
  2574. "schema",
  2575. util.text_type(schema.lower()),
  2576. type_=sqltypes.Unicode,
  2577. )
  2578. )
  2579. )
  2580. return bool(cursor.first())
  2581. def has_table(self, connection, table_name, schema=None):
  2582. self._ensure_has_table_connection(connection)
  2583. # seems like case gets folded in pg_class...
  2584. if schema is None:
  2585. cursor = connection.execute(
  2586. sql.text(
  2587. "select relname from pg_class c join pg_namespace n on "
  2588. "n.oid=c.relnamespace where "
  2589. "pg_catalog.pg_table_is_visible(c.oid) "
  2590. "and relname=:name"
  2591. ).bindparams(
  2592. sql.bindparam(
  2593. "name",
  2594. util.text_type(table_name),
  2595. type_=sqltypes.Unicode,
  2596. )
  2597. )
  2598. )
  2599. else:
  2600. cursor = connection.execute(
  2601. sql.text(
  2602. "select relname from pg_class c join pg_namespace n on "
  2603. "n.oid=c.relnamespace where n.nspname=:schema and "
  2604. "relname=:name"
  2605. ).bindparams(
  2606. sql.bindparam(
  2607. "name",
  2608. util.text_type(table_name),
  2609. type_=sqltypes.Unicode,
  2610. ),
  2611. sql.bindparam(
  2612. "schema",
  2613. util.text_type(schema),
  2614. type_=sqltypes.Unicode,
  2615. ),
  2616. )
  2617. )
  2618. return bool(cursor.first())
  2619. def has_sequence(self, connection, sequence_name, schema=None):
  2620. if schema is None:
  2621. schema = self.default_schema_name
  2622. cursor = connection.execute(
  2623. sql.text(
  2624. "SELECT relname FROM pg_class c join pg_namespace n on "
  2625. "n.oid=c.relnamespace where relkind='S' and "
  2626. "n.nspname=:schema and relname=:name"
  2627. ).bindparams(
  2628. sql.bindparam(
  2629. "name",
  2630. util.text_type(sequence_name),
  2631. type_=sqltypes.Unicode,
  2632. ),
  2633. sql.bindparam(
  2634. "schema",
  2635. util.text_type(schema),
  2636. type_=sqltypes.Unicode,
  2637. ),
  2638. )
  2639. )
  2640. return bool(cursor.first())
  2641. def has_type(self, connection, type_name, schema=None):
  2642. if schema is not None:
  2643. query = """
  2644. SELECT EXISTS (
  2645. SELECT * FROM pg_catalog.pg_type t, pg_catalog.pg_namespace n
  2646. WHERE t.typnamespace = n.oid
  2647. AND t.typname = :typname
  2648. AND n.nspname = :nspname
  2649. )
  2650. """
  2651. query = sql.text(query)
  2652. else:
  2653. query = """
  2654. SELECT EXISTS (
  2655. SELECT * FROM pg_catalog.pg_type t
  2656. WHERE t.typname = :typname
  2657. AND pg_type_is_visible(t.oid)
  2658. )
  2659. """
  2660. query = sql.text(query)
  2661. query = query.bindparams(
  2662. sql.bindparam(
  2663. "typname", util.text_type(type_name), type_=sqltypes.Unicode
  2664. )
  2665. )
  2666. if schema is not None:
  2667. query = query.bindparams(
  2668. sql.bindparam(
  2669. "nspname", util.text_type(schema), type_=sqltypes.Unicode
  2670. )
  2671. )
  2672. cursor = connection.execute(query)
  2673. return bool(cursor.scalar())
  2674. def _get_server_version_info(self, connection):
  2675. v = connection.exec_driver_sql("select version()").scalar()
  2676. m = re.match(
  2677. r".*(?:PostgreSQL|EnterpriseDB) "
  2678. r"(\d+)\.?(\d+)?(?:\.(\d+))?(?:\.\d+)?(?:devel|beta)?",
  2679. v,
  2680. )
  2681. if not m:
  2682. raise AssertionError(
  2683. "Could not determine version from string '%s'" % v
  2684. )
  2685. return tuple([int(x) for x in m.group(1, 2, 3) if x is not None])
  2686. @reflection.cache
  2687. def get_table_oid(self, connection, table_name, schema=None, **kw):
  2688. """Fetch the oid for schema.table_name.
  2689. Several reflection methods require the table oid. The idea for using
  2690. this method is that it can be fetched one time and cached for
  2691. subsequent calls.
  2692. """
  2693. table_oid = None
  2694. if schema is not None:
  2695. schema_where_clause = "n.nspname = :schema"
  2696. else:
  2697. schema_where_clause = "pg_catalog.pg_table_is_visible(c.oid)"
  2698. query = (
  2699. """
  2700. SELECT c.oid
  2701. FROM pg_catalog.pg_class c
  2702. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
  2703. WHERE (%s)
  2704. AND c.relname = :table_name AND c.relkind in
  2705. ('r', 'v', 'm', 'f', 'p')
  2706. """
  2707. % schema_where_clause
  2708. )
  2709. # Since we're binding to unicode, table_name and schema_name must be
  2710. # unicode.
  2711. table_name = util.text_type(table_name)
  2712. if schema is not None:
  2713. schema = util.text_type(schema)
  2714. s = sql.text(query).bindparams(table_name=sqltypes.Unicode)
  2715. s = s.columns(oid=sqltypes.Integer)
  2716. if schema:
  2717. s = s.bindparams(sql.bindparam("schema", type_=sqltypes.Unicode))
  2718. c = connection.execute(s, dict(table_name=table_name, schema=schema))
  2719. table_oid = c.scalar()
  2720. if table_oid is None:
  2721. raise exc.NoSuchTableError(table_name)
  2722. return table_oid
  2723. @reflection.cache
  2724. def get_schema_names(self, connection, **kw):
  2725. result = connection.execute(
  2726. sql.text(
  2727. "SELECT nspname FROM pg_namespace "
  2728. "WHERE nspname NOT LIKE 'pg_%' "
  2729. "ORDER BY nspname"
  2730. ).columns(nspname=sqltypes.Unicode)
  2731. )
  2732. return [name for name, in result]
  2733. @reflection.cache
  2734. def get_table_names(self, connection, schema=None, **kw):
  2735. result = connection.execute(
  2736. sql.text(
  2737. "SELECT c.relname FROM pg_class c "
  2738. "JOIN pg_namespace n ON n.oid = c.relnamespace "
  2739. "WHERE n.nspname = :schema AND c.relkind in ('r', 'p')"
  2740. ).columns(relname=sqltypes.Unicode),
  2741. dict(
  2742. schema=schema
  2743. if schema is not None
  2744. else self.default_schema_name
  2745. ),
  2746. )
  2747. return [name for name, in result]
  2748. @reflection.cache
  2749. def _get_foreign_table_names(self, connection, schema=None, **kw):
  2750. result = connection.execute(
  2751. sql.text(
  2752. "SELECT c.relname FROM pg_class c "
  2753. "JOIN pg_namespace n ON n.oid = c.relnamespace "
  2754. "WHERE n.nspname = :schema AND c.relkind = 'f'"
  2755. ).columns(relname=sqltypes.Unicode),
  2756. dict(
  2757. schema=schema
  2758. if schema is not None
  2759. else self.default_schema_name
  2760. ),
  2761. )
  2762. return [name for name, in result]
  2763. @reflection.cache
  2764. def get_view_names(
  2765. self, connection, schema=None, include=("plain", "materialized"), **kw
  2766. ):
  2767. include_kind = {"plain": "v", "materialized": "m"}
  2768. try:
  2769. kinds = [include_kind[i] for i in util.to_list(include)]
  2770. except KeyError:
  2771. raise ValueError(
  2772. "include %r unknown, needs to be a sequence containing "
  2773. "one or both of 'plain' and 'materialized'" % (include,)
  2774. )
  2775. if not kinds:
  2776. raise ValueError(
  2777. "empty include, needs to be a sequence containing "
  2778. "one or both of 'plain' and 'materialized'"
  2779. )
  2780. result = connection.execute(
  2781. sql.text(
  2782. "SELECT c.relname FROM pg_class c "
  2783. "JOIN pg_namespace n ON n.oid = c.relnamespace "
  2784. "WHERE n.nspname = :schema AND c.relkind IN (%s)"
  2785. % (", ".join("'%s'" % elem for elem in kinds))
  2786. ).columns(relname=sqltypes.Unicode),
  2787. dict(
  2788. schema=schema
  2789. if schema is not None
  2790. else self.default_schema_name
  2791. ),
  2792. )
  2793. return [name for name, in result]
  2794. @reflection.cache
  2795. def get_sequence_names(self, connection, schema=None, **kw):
  2796. if not schema:
  2797. schema = self.default_schema_name
  2798. cursor = connection.execute(
  2799. sql.text(
  2800. "SELECT relname FROM pg_class c join pg_namespace n on "
  2801. "n.oid=c.relnamespace where relkind='S' and "
  2802. "n.nspname=:schema"
  2803. ).bindparams(
  2804. sql.bindparam(
  2805. "schema",
  2806. util.text_type(schema),
  2807. type_=sqltypes.Unicode,
  2808. ),
  2809. )
  2810. )
  2811. return [row[0] for row in cursor]
  2812. @reflection.cache
  2813. def get_view_definition(self, connection, view_name, schema=None, **kw):
  2814. view_def = connection.scalar(
  2815. sql.text(
  2816. "SELECT pg_get_viewdef(c.oid) view_def FROM pg_class c "
  2817. "JOIN pg_namespace n ON n.oid = c.relnamespace "
  2818. "WHERE n.nspname = :schema AND c.relname = :view_name "
  2819. "AND c.relkind IN ('v', 'm')"
  2820. ).columns(view_def=sqltypes.Unicode),
  2821. dict(
  2822. schema=schema
  2823. if schema is not None
  2824. else self.default_schema_name,
  2825. view_name=view_name,
  2826. ),
  2827. )
  2828. return view_def
  2829. @reflection.cache
  2830. def get_columns(self, connection, table_name, schema=None, **kw):
  2831. table_oid = self.get_table_oid(
  2832. connection, table_name, schema, info_cache=kw.get("info_cache")
  2833. )
  2834. generated = (
  2835. "a.attgenerated as generated"
  2836. if self.server_version_info >= (12,)
  2837. else "NULL as generated"
  2838. )
  2839. if self.server_version_info >= (10,):
  2840. # a.attidentity != '' is required or it will reflect also
  2841. # serial columns as identity.
  2842. identity = """\
  2843. (SELECT json_build_object(
  2844. 'always', a.attidentity = 'a',
  2845. 'start', s.seqstart,
  2846. 'increment', s.seqincrement,
  2847. 'minvalue', s.seqmin,
  2848. 'maxvalue', s.seqmax,
  2849. 'cache', s.seqcache,
  2850. 'cycle', s.seqcycle)
  2851. FROM pg_catalog.pg_sequence s
  2852. JOIN pg_catalog.pg_class c on s.seqrelid = c."oid"
  2853. WHERE c.relkind = 'S'
  2854. AND a.attidentity != ''
  2855. AND s.seqrelid = pg_catalog.pg_get_serial_sequence(
  2856. a.attrelid::regclass::text, a.attname
  2857. )::regclass::oid
  2858. ) as identity_options\
  2859. """
  2860. else:
  2861. identity = "NULL as identity_options"
  2862. SQL_COLS = """
  2863. SELECT a.attname,
  2864. pg_catalog.format_type(a.atttypid, a.atttypmod),
  2865. (
  2866. SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid)
  2867. FROM pg_catalog.pg_attrdef d
  2868. WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum
  2869. AND a.atthasdef
  2870. ) AS DEFAULT,
  2871. a.attnotnull,
  2872. a.attrelid as table_oid,
  2873. pgd.description as comment,
  2874. %s,
  2875. %s
  2876. FROM pg_catalog.pg_attribute a
  2877. LEFT JOIN pg_catalog.pg_description pgd ON (
  2878. pgd.objoid = a.attrelid AND pgd.objsubid = a.attnum)
  2879. WHERE a.attrelid = :table_oid
  2880. AND a.attnum > 0 AND NOT a.attisdropped
  2881. ORDER BY a.attnum
  2882. """ % (
  2883. generated,
  2884. identity,
  2885. )
  2886. s = (
  2887. sql.text(SQL_COLS)
  2888. .bindparams(sql.bindparam("table_oid", type_=sqltypes.Integer))
  2889. .columns(attname=sqltypes.Unicode, default=sqltypes.Unicode)
  2890. )
  2891. c = connection.execute(s, dict(table_oid=table_oid))
  2892. rows = c.fetchall()
  2893. # dictionary with (name, ) if default search path or (schema, name)
  2894. # as keys
  2895. domains = self._load_domains(connection)
  2896. # dictionary with (name, ) if default search path or (schema, name)
  2897. # as keys
  2898. enums = dict(
  2899. ((rec["name"],), rec)
  2900. if rec["visible"]
  2901. else ((rec["schema"], rec["name"]), rec)
  2902. for rec in self._load_enums(connection, schema="*")
  2903. )
  2904. # format columns
  2905. columns = []
  2906. for (
  2907. name,
  2908. format_type,
  2909. default_,
  2910. notnull,
  2911. table_oid,
  2912. comment,
  2913. generated,
  2914. identity,
  2915. ) in rows:
  2916. column_info = self._get_column_info(
  2917. name,
  2918. format_type,
  2919. default_,
  2920. notnull,
  2921. domains,
  2922. enums,
  2923. schema,
  2924. comment,
  2925. generated,
  2926. identity,
  2927. )
  2928. columns.append(column_info)
  2929. return columns
  2930. def _get_column_info(
  2931. self,
  2932. name,
  2933. format_type,
  2934. default,
  2935. notnull,
  2936. domains,
  2937. enums,
  2938. schema,
  2939. comment,
  2940. generated,
  2941. identity,
  2942. ):
  2943. def _handle_array_type(attype):
  2944. return (
  2945. # strip '[]' from integer[], etc.
  2946. re.sub(r"\[\]$", "", attype),
  2947. attype.endswith("[]"),
  2948. )
  2949. # strip (*) from character varying(5), timestamp(5)
  2950. # with time zone, geometry(POLYGON), etc.
  2951. attype = re.sub(r"\(.*\)", "", format_type)
  2952. # strip '[]' from integer[], etc. and check if an array
  2953. attype, is_array = _handle_array_type(attype)
  2954. # strip quotes from case sensitive enum or domain names
  2955. enum_or_domain_key = tuple(util.quoted_token_parser(attype))
  2956. nullable = not notnull
  2957. charlen = re.search(r"\(([\d,]+)\)", format_type)
  2958. if charlen:
  2959. charlen = charlen.group(1)
  2960. args = re.search(r"\((.*)\)", format_type)
  2961. if args and args.group(1):
  2962. args = tuple(re.split(r"\s*,\s*", args.group(1)))
  2963. else:
  2964. args = ()
  2965. kwargs = {}
  2966. if attype == "numeric":
  2967. if charlen:
  2968. prec, scale = charlen.split(",")
  2969. args = (int(prec), int(scale))
  2970. else:
  2971. args = ()
  2972. elif attype == "double precision":
  2973. args = (53,)
  2974. elif attype == "integer":
  2975. args = ()
  2976. elif attype in ("timestamp with time zone", "time with time zone"):
  2977. kwargs["timezone"] = True
  2978. if charlen:
  2979. kwargs["precision"] = int(charlen)
  2980. args = ()
  2981. elif attype in (
  2982. "timestamp without time zone",
  2983. "time without time zone",
  2984. "time",
  2985. ):
  2986. kwargs["timezone"] = False
  2987. if charlen:
  2988. kwargs["precision"] = int(charlen)
  2989. args = ()
  2990. elif attype == "bit varying":
  2991. kwargs["varying"] = True
  2992. if charlen:
  2993. args = (int(charlen),)
  2994. else:
  2995. args = ()
  2996. elif attype.startswith("interval"):
  2997. field_match = re.match(r"interval (.+)", attype, re.I)
  2998. if charlen:
  2999. kwargs["precision"] = int(charlen)
  3000. if field_match:
  3001. kwargs["fields"] = field_match.group(1)
  3002. attype = "interval"
  3003. args = ()
  3004. elif charlen:
  3005. args = (int(charlen),)
  3006. while True:
  3007. # looping here to suit nested domains
  3008. if attype in self.ischema_names:
  3009. coltype = self.ischema_names[attype]
  3010. break
  3011. elif enum_or_domain_key in enums:
  3012. enum = enums[enum_or_domain_key]
  3013. coltype = ENUM
  3014. kwargs["name"] = enum["name"]
  3015. if not enum["visible"]:
  3016. kwargs["schema"] = enum["schema"]
  3017. args = tuple(enum["labels"])
  3018. break
  3019. elif enum_or_domain_key in domains:
  3020. domain = domains[enum_or_domain_key]
  3021. attype = domain["attype"]
  3022. attype, is_array = _handle_array_type(attype)
  3023. # strip quotes from case sensitive enum or domain names
  3024. enum_or_domain_key = tuple(util.quoted_token_parser(attype))
  3025. # A table can't override a not null on the domain,
  3026. # but can override nullable
  3027. nullable = nullable and domain["nullable"]
  3028. if domain["default"] and not default:
  3029. # It can, however, override the default
  3030. # value, but can't set it to null.
  3031. default = domain["default"]
  3032. continue
  3033. else:
  3034. coltype = None
  3035. break
  3036. if coltype:
  3037. coltype = coltype(*args, **kwargs)
  3038. if is_array:
  3039. coltype = self.ischema_names["_array"](coltype)
  3040. else:
  3041. util.warn(
  3042. "Did not recognize type '%s' of column '%s'" % (attype, name)
  3043. )
  3044. coltype = sqltypes.NULLTYPE
  3045. # If a zero byte or blank string depending on driver (is also absent
  3046. # for older PG versions), then not a generated column. Otherwise, s =
  3047. # stored. (Other values might be added in the future.)
  3048. if generated not in (None, "", b"\x00"):
  3049. computed = dict(
  3050. sqltext=default, persisted=generated in ("s", b"s")
  3051. )
  3052. default = None
  3053. else:
  3054. computed = None
  3055. # adjust the default value
  3056. autoincrement = False
  3057. if default is not None:
  3058. match = re.search(r"""(nextval\(')([^']+)('.*$)""", default)
  3059. if match is not None:
  3060. if issubclass(coltype._type_affinity, sqltypes.Integer):
  3061. autoincrement = True
  3062. # the default is related to a Sequence
  3063. sch = schema
  3064. if "." not in match.group(2) and sch is not None:
  3065. # unconditionally quote the schema name. this could
  3066. # later be enhanced to obey quoting rules /
  3067. # "quote schema"
  3068. default = (
  3069. match.group(1)
  3070. + ('"%s"' % sch)
  3071. + "."
  3072. + match.group(2)
  3073. + match.group(3)
  3074. )
  3075. column_info = dict(
  3076. name=name,
  3077. type=coltype,
  3078. nullable=nullable,
  3079. default=default,
  3080. autoincrement=autoincrement or identity is not None,
  3081. comment=comment,
  3082. )
  3083. if computed is not None:
  3084. column_info["computed"] = computed
  3085. if identity is not None:
  3086. column_info["identity"] = identity
  3087. return column_info
  3088. @reflection.cache
  3089. def get_pk_constraint(self, connection, table_name, schema=None, **kw):
  3090. table_oid = self.get_table_oid(
  3091. connection, table_name, schema, info_cache=kw.get("info_cache")
  3092. )
  3093. if self.server_version_info < (8, 4):
  3094. PK_SQL = """
  3095. SELECT a.attname
  3096. FROM
  3097. pg_class t
  3098. join pg_index ix on t.oid = ix.indrelid
  3099. join pg_attribute a
  3100. on t.oid=a.attrelid AND %s
  3101. WHERE
  3102. t.oid = :table_oid and ix.indisprimary = 't'
  3103. ORDER BY a.attnum
  3104. """ % self._pg_index_any(
  3105. "a.attnum", "ix.indkey"
  3106. )
  3107. else:
  3108. # unnest() and generate_subscripts() both introduced in
  3109. # version 8.4
  3110. PK_SQL = """
  3111. SELECT a.attname
  3112. FROM pg_attribute a JOIN (
  3113. SELECT unnest(ix.indkey) attnum,
  3114. generate_subscripts(ix.indkey, 1) ord
  3115. FROM pg_index ix
  3116. WHERE ix.indrelid = :table_oid AND ix.indisprimary
  3117. ) k ON a.attnum=k.attnum
  3118. WHERE a.attrelid = :table_oid
  3119. ORDER BY k.ord
  3120. """
  3121. t = sql.text(PK_SQL).columns(attname=sqltypes.Unicode)
  3122. c = connection.execute(t, dict(table_oid=table_oid))
  3123. cols = [r[0] for r in c.fetchall()]
  3124. PK_CONS_SQL = """
  3125. SELECT conname
  3126. FROM pg_catalog.pg_constraint r
  3127. WHERE r.conrelid = :table_oid AND r.contype = 'p'
  3128. ORDER BY 1
  3129. """
  3130. t = sql.text(PK_CONS_SQL).columns(conname=sqltypes.Unicode)
  3131. c = connection.execute(t, dict(table_oid=table_oid))
  3132. name = c.scalar()
  3133. return {"constrained_columns": cols, "name": name}
  3134. @reflection.cache
  3135. def get_foreign_keys(
  3136. self,
  3137. connection,
  3138. table_name,
  3139. schema=None,
  3140. postgresql_ignore_search_path=False,
  3141. **kw
  3142. ):
  3143. preparer = self.identifier_preparer
  3144. table_oid = self.get_table_oid(
  3145. connection, table_name, schema, info_cache=kw.get("info_cache")
  3146. )
  3147. FK_SQL = """
  3148. SELECT r.conname,
  3149. pg_catalog.pg_get_constraintdef(r.oid, true) as condef,
  3150. n.nspname as conschema
  3151. FROM pg_catalog.pg_constraint r,
  3152. pg_namespace n,
  3153. pg_class c
  3154. WHERE r.conrelid = :table AND
  3155. r.contype = 'f' AND
  3156. c.oid = confrelid AND
  3157. n.oid = c.relnamespace
  3158. ORDER BY 1
  3159. """
  3160. # http://www.postgresql.org/docs/9.0/static/sql-createtable.html
  3161. FK_REGEX = re.compile(
  3162. r"FOREIGN KEY \((.*?)\) REFERENCES (?:(.*?)\.)?(.*?)\((.*?)\)"
  3163. r"[\s]?(MATCH (FULL|PARTIAL|SIMPLE)+)?"
  3164. r"[\s]?(ON UPDATE "
  3165. r"(CASCADE|RESTRICT|NO ACTION|SET NULL|SET DEFAULT)+)?"
  3166. r"[\s]?(ON DELETE "
  3167. r"(CASCADE|RESTRICT|NO ACTION|SET NULL|SET DEFAULT)+)?"
  3168. r"[\s]?(DEFERRABLE|NOT DEFERRABLE)?"
  3169. r"[\s]?(INITIALLY (DEFERRED|IMMEDIATE)+)?"
  3170. )
  3171. t = sql.text(FK_SQL).columns(
  3172. conname=sqltypes.Unicode, condef=sqltypes.Unicode
  3173. )
  3174. c = connection.execute(t, dict(table=table_oid))
  3175. fkeys = []
  3176. for conname, condef, conschema in c.fetchall():
  3177. m = re.search(FK_REGEX, condef).groups()
  3178. (
  3179. constrained_columns,
  3180. referred_schema,
  3181. referred_table,
  3182. referred_columns,
  3183. _,
  3184. match,
  3185. _,
  3186. onupdate,
  3187. _,
  3188. ondelete,
  3189. deferrable,
  3190. _,
  3191. initially,
  3192. ) = m
  3193. if deferrable is not None:
  3194. deferrable = True if deferrable == "DEFERRABLE" else False
  3195. constrained_columns = [
  3196. preparer._unquote_identifier(x)
  3197. for x in re.split(r"\s*,\s*", constrained_columns)
  3198. ]
  3199. if postgresql_ignore_search_path:
  3200. # when ignoring search path, we use the actual schema
  3201. # provided it isn't the "default" schema
  3202. if conschema != self.default_schema_name:
  3203. referred_schema = conschema
  3204. else:
  3205. referred_schema = schema
  3206. elif referred_schema:
  3207. # referred_schema is the schema that we regexp'ed from
  3208. # pg_get_constraintdef(). If the schema is in the search
  3209. # path, pg_get_constraintdef() will give us None.
  3210. referred_schema = preparer._unquote_identifier(referred_schema)
  3211. elif schema is not None and schema == conschema:
  3212. # If the actual schema matches the schema of the table
  3213. # we're reflecting, then we will use that.
  3214. referred_schema = schema
  3215. referred_table = preparer._unquote_identifier(referred_table)
  3216. referred_columns = [
  3217. preparer._unquote_identifier(x)
  3218. for x in re.split(r"\s*,\s", referred_columns)
  3219. ]
  3220. options = {
  3221. k: v
  3222. for k, v in [
  3223. ("onupdate", onupdate),
  3224. ("ondelete", ondelete),
  3225. ("initially", initially),
  3226. ("deferrable", deferrable),
  3227. ("match", match),
  3228. ]
  3229. if v is not None and v != "NO ACTION"
  3230. }
  3231. fkey_d = {
  3232. "name": conname,
  3233. "constrained_columns": constrained_columns,
  3234. "referred_schema": referred_schema,
  3235. "referred_table": referred_table,
  3236. "referred_columns": referred_columns,
  3237. "options": options,
  3238. }
  3239. fkeys.append(fkey_d)
  3240. return fkeys
  3241. def _pg_index_any(self, col, compare_to):
  3242. if self.server_version_info < (8, 1):
  3243. # http://www.postgresql.org/message-id/10279.1124395722@sss.pgh.pa.us
  3244. # "In CVS tip you could replace this with "attnum = ANY (indkey)".
  3245. # Unfortunately, most array support doesn't work on int2vector in
  3246. # pre-8.1 releases, so I think you're kinda stuck with the above
  3247. # for now.
  3248. # regards, tom lane"
  3249. return "(%s)" % " OR ".join(
  3250. "%s[%d] = %s" % (compare_to, ind, col) for ind in range(0, 10)
  3251. )
  3252. else:
  3253. return "%s = ANY(%s)" % (col, compare_to)
  3254. @reflection.cache
  3255. def get_indexes(self, connection, table_name, schema, **kw):
  3256. table_oid = self.get_table_oid(
  3257. connection, table_name, schema, info_cache=kw.get("info_cache")
  3258. )
  3259. # cast indkey as varchar since it's an int2vector,
  3260. # returned as a list by some drivers such as pypostgresql
  3261. if self.server_version_info < (8, 5):
  3262. IDX_SQL = """
  3263. SELECT
  3264. i.relname as relname,
  3265. ix.indisunique, ix.indexprs, ix.indpred,
  3266. a.attname, a.attnum, NULL, ix.indkey%s,
  3267. %s, %s, am.amname,
  3268. NULL as indnkeyatts
  3269. FROM
  3270. pg_class t
  3271. join pg_index ix on t.oid = ix.indrelid
  3272. join pg_class i on i.oid = ix.indexrelid
  3273. left outer join
  3274. pg_attribute a
  3275. on t.oid = a.attrelid and %s
  3276. left outer join
  3277. pg_am am
  3278. on i.relam = am.oid
  3279. WHERE
  3280. t.relkind IN ('r', 'v', 'f', 'm')
  3281. and t.oid = :table_oid
  3282. and ix.indisprimary = 'f'
  3283. ORDER BY
  3284. t.relname,
  3285. i.relname
  3286. """ % (
  3287. # version 8.3 here was based on observing the
  3288. # cast does not work in PG 8.2.4, does work in 8.3.0.
  3289. # nothing in PG changelogs regarding this.
  3290. "::varchar" if self.server_version_info >= (8, 3) else "",
  3291. "ix.indoption::varchar"
  3292. if self.server_version_info >= (8, 3)
  3293. else "NULL",
  3294. "i.reloptions"
  3295. if self.server_version_info >= (8, 2)
  3296. else "NULL",
  3297. self._pg_index_any("a.attnum", "ix.indkey"),
  3298. )
  3299. else:
  3300. IDX_SQL = """
  3301. SELECT
  3302. i.relname as relname,
  3303. ix.indisunique, ix.indexprs,
  3304. a.attname, a.attnum, c.conrelid, ix.indkey::varchar,
  3305. ix.indoption::varchar, i.reloptions, am.amname,
  3306. pg_get_expr(ix.indpred, ix.indrelid),
  3307. %s as indnkeyatts
  3308. FROM
  3309. pg_class t
  3310. join pg_index ix on t.oid = ix.indrelid
  3311. join pg_class i on i.oid = ix.indexrelid
  3312. left outer join
  3313. pg_attribute a
  3314. on t.oid = a.attrelid and a.attnum = ANY(ix.indkey)
  3315. left outer join
  3316. pg_constraint c
  3317. on (ix.indrelid = c.conrelid and
  3318. ix.indexrelid = c.conindid and
  3319. c.contype in ('p', 'u', 'x'))
  3320. left outer join
  3321. pg_am am
  3322. on i.relam = am.oid
  3323. WHERE
  3324. t.relkind IN ('r', 'v', 'f', 'm', 'p')
  3325. and t.oid = :table_oid
  3326. and ix.indisprimary = 'f'
  3327. ORDER BY
  3328. t.relname,
  3329. i.relname
  3330. """ % (
  3331. "ix.indnkeyatts"
  3332. if self.server_version_info >= (11, 0)
  3333. else "NULL",
  3334. )
  3335. t = sql.text(IDX_SQL).columns(
  3336. relname=sqltypes.Unicode, attname=sqltypes.Unicode
  3337. )
  3338. c = connection.execute(t, dict(table_oid=table_oid))
  3339. indexes = defaultdict(lambda: defaultdict(dict))
  3340. sv_idx_name = None
  3341. for row in c.fetchall():
  3342. (
  3343. idx_name,
  3344. unique,
  3345. expr,
  3346. col,
  3347. col_num,
  3348. conrelid,
  3349. idx_key,
  3350. idx_option,
  3351. options,
  3352. amname,
  3353. filter_definition,
  3354. indnkeyatts,
  3355. ) = row
  3356. if expr:
  3357. if idx_name != sv_idx_name:
  3358. util.warn(
  3359. "Skipped unsupported reflection of "
  3360. "expression-based index %s" % idx_name
  3361. )
  3362. sv_idx_name = idx_name
  3363. continue
  3364. has_idx = idx_name in indexes
  3365. index = indexes[idx_name]
  3366. if col is not None:
  3367. index["cols"][col_num] = col
  3368. if not has_idx:
  3369. idx_keys = idx_key.split()
  3370. # "The number of key columns in the index, not counting any
  3371. # included columns, which are merely stored and do not
  3372. # participate in the index semantics"
  3373. if indnkeyatts and idx_keys[indnkeyatts:]:
  3374. # this is a "covering index" which has INCLUDE columns
  3375. # as well as regular index columns
  3376. inc_keys = idx_keys[indnkeyatts:]
  3377. idx_keys = idx_keys[:indnkeyatts]
  3378. else:
  3379. inc_keys = []
  3380. index["key"] = [int(k.strip()) for k in idx_keys]
  3381. index["inc"] = [int(k.strip()) for k in inc_keys]
  3382. # (new in pg 8.3)
  3383. # "pg_index.indoption" is list of ints, one per column/expr.
  3384. # int acts as bitmask: 0x01=DESC, 0x02=NULLSFIRST
  3385. sorting = {}
  3386. for col_idx, col_flags in enumerate(
  3387. (idx_option or "").split()
  3388. ):
  3389. col_flags = int(col_flags.strip())
  3390. col_sorting = ()
  3391. # try to set flags only if they differ from PG defaults...
  3392. if col_flags & 0x01:
  3393. col_sorting += ("desc",)
  3394. if not (col_flags & 0x02):
  3395. col_sorting += ("nulls_last",)
  3396. else:
  3397. if col_flags & 0x02:
  3398. col_sorting += ("nulls_first",)
  3399. if col_sorting:
  3400. sorting[col_idx] = col_sorting
  3401. if sorting:
  3402. index["sorting"] = sorting
  3403. index["unique"] = unique
  3404. if conrelid is not None:
  3405. index["duplicates_constraint"] = idx_name
  3406. if options:
  3407. index["options"] = dict(
  3408. [option.split("=") for option in options]
  3409. )
  3410. # it *might* be nice to include that this is 'btree' in the
  3411. # reflection info. But we don't want an Index object
  3412. # to have a ``postgresql_using`` in it that is just the
  3413. # default, so for the moment leaving this out.
  3414. if amname and amname != "btree":
  3415. index["amname"] = amname
  3416. if filter_definition:
  3417. index["postgresql_where"] = filter_definition
  3418. result = []
  3419. for name, idx in indexes.items():
  3420. entry = {
  3421. "name": name,
  3422. "unique": idx["unique"],
  3423. "column_names": [idx["cols"][i] for i in idx["key"]],
  3424. }
  3425. if self.server_version_info >= (11, 0):
  3426. entry["include_columns"] = [idx["cols"][i] for i in idx["inc"]]
  3427. if "duplicates_constraint" in idx:
  3428. entry["duplicates_constraint"] = idx["duplicates_constraint"]
  3429. if "sorting" in idx:
  3430. entry["column_sorting"] = dict(
  3431. (idx["cols"][idx["key"][i]], value)
  3432. for i, value in idx["sorting"].items()
  3433. )
  3434. if "options" in idx:
  3435. entry.setdefault("dialect_options", {})[
  3436. "postgresql_with"
  3437. ] = idx["options"]
  3438. if "amname" in idx:
  3439. entry.setdefault("dialect_options", {})[
  3440. "postgresql_using"
  3441. ] = idx["amname"]
  3442. if "postgresql_where" in idx:
  3443. entry.setdefault("dialect_options", {})[
  3444. "postgresql_where"
  3445. ] = idx["postgresql_where"]
  3446. result.append(entry)
  3447. return result
  3448. @reflection.cache
  3449. def get_unique_constraints(
  3450. self, connection, table_name, schema=None, **kw
  3451. ):
  3452. table_oid = self.get_table_oid(
  3453. connection, table_name, schema, info_cache=kw.get("info_cache")
  3454. )
  3455. UNIQUE_SQL = """
  3456. SELECT
  3457. cons.conname as name,
  3458. cons.conkey as key,
  3459. a.attnum as col_num,
  3460. a.attname as col_name
  3461. FROM
  3462. pg_catalog.pg_constraint cons
  3463. join pg_attribute a
  3464. on cons.conrelid = a.attrelid AND
  3465. a.attnum = ANY(cons.conkey)
  3466. WHERE
  3467. cons.conrelid = :table_oid AND
  3468. cons.contype = 'u'
  3469. """
  3470. t = sql.text(UNIQUE_SQL).columns(col_name=sqltypes.Unicode)
  3471. c = connection.execute(t, dict(table_oid=table_oid))
  3472. uniques = defaultdict(lambda: defaultdict(dict))
  3473. for row in c.fetchall():
  3474. uc = uniques[row.name]
  3475. uc["key"] = row.key
  3476. uc["cols"][row.col_num] = row.col_name
  3477. return [
  3478. {"name": name, "column_names": [uc["cols"][i] for i in uc["key"]]}
  3479. for name, uc in uniques.items()
  3480. ]
  3481. @reflection.cache
  3482. def get_table_comment(self, connection, table_name, schema=None, **kw):
  3483. table_oid = self.get_table_oid(
  3484. connection, table_name, schema, info_cache=kw.get("info_cache")
  3485. )
  3486. COMMENT_SQL = """
  3487. SELECT
  3488. pgd.description as table_comment
  3489. FROM
  3490. pg_catalog.pg_description pgd
  3491. WHERE
  3492. pgd.objsubid = 0 AND
  3493. pgd.objoid = :table_oid
  3494. """
  3495. c = connection.execute(
  3496. sql.text(COMMENT_SQL), dict(table_oid=table_oid)
  3497. )
  3498. return {"text": c.scalar()}
  3499. @reflection.cache
  3500. def get_check_constraints(self, connection, table_name, schema=None, **kw):
  3501. table_oid = self.get_table_oid(
  3502. connection, table_name, schema, info_cache=kw.get("info_cache")
  3503. )
  3504. CHECK_SQL = """
  3505. SELECT
  3506. cons.conname as name,
  3507. pg_get_constraintdef(cons.oid) as src
  3508. FROM
  3509. pg_catalog.pg_constraint cons
  3510. WHERE
  3511. cons.conrelid = :table_oid AND
  3512. cons.contype = 'c'
  3513. """
  3514. c = connection.execute(sql.text(CHECK_SQL), dict(table_oid=table_oid))
  3515. ret = []
  3516. for name, src in c:
  3517. # samples:
  3518. # "CHECK (((a > 1) AND (a < 5)))"
  3519. # "CHECK (((a = 1) OR ((a > 2) AND (a < 5))))"
  3520. # "CHECK (((a > 1) AND (a < 5))) NOT VALID"
  3521. # "CHECK (some_boolean_function(a))"
  3522. # "CHECK (((a\n < 1)\n OR\n (a\n >= 5))\n)"
  3523. m = re.match(
  3524. r"^CHECK *\((.+)\)( NOT VALID)?$", src, flags=re.DOTALL
  3525. )
  3526. if not m:
  3527. util.warn("Could not parse CHECK constraint text: %r" % src)
  3528. sqltext = ""
  3529. else:
  3530. sqltext = re.compile(
  3531. r"^[\s\n]*\((.+)\)[\s\n]*$", flags=re.DOTALL
  3532. ).sub(r"\1", m.group(1))
  3533. entry = {"name": name, "sqltext": sqltext}
  3534. if m and m.group(2):
  3535. entry["dialect_options"] = {"not_valid": True}
  3536. ret.append(entry)
  3537. return ret
  3538. def _load_enums(self, connection, schema=None):
  3539. schema = schema or self.default_schema_name
  3540. if not self.supports_native_enum:
  3541. return {}
  3542. # Load data types for enums:
  3543. SQL_ENUMS = """
  3544. SELECT t.typname as "name",
  3545. -- no enum defaults in 8.4 at least
  3546. -- t.typdefault as "default",
  3547. pg_catalog.pg_type_is_visible(t.oid) as "visible",
  3548. n.nspname as "schema",
  3549. e.enumlabel as "label"
  3550. FROM pg_catalog.pg_type t
  3551. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
  3552. LEFT JOIN pg_catalog.pg_enum e ON t.oid = e.enumtypid
  3553. WHERE t.typtype = 'e'
  3554. """
  3555. if schema != "*":
  3556. SQL_ENUMS += "AND n.nspname = :schema "
  3557. # e.oid gives us label order within an enum
  3558. SQL_ENUMS += 'ORDER BY "schema", "name", e.oid'
  3559. s = sql.text(SQL_ENUMS).columns(
  3560. attname=sqltypes.Unicode, label=sqltypes.Unicode
  3561. )
  3562. if schema != "*":
  3563. s = s.bindparams(schema=schema)
  3564. c = connection.execute(s)
  3565. enums = []
  3566. enum_by_name = {}
  3567. for enum in c.fetchall():
  3568. key = (enum.schema, enum.name)
  3569. if key in enum_by_name:
  3570. enum_by_name[key]["labels"].append(enum.label)
  3571. else:
  3572. enum_by_name[key] = enum_rec = {
  3573. "name": enum.name,
  3574. "schema": enum.schema,
  3575. "visible": enum.visible,
  3576. "labels": [],
  3577. }
  3578. if enum.label is not None:
  3579. enum_rec["labels"].append(enum.label)
  3580. enums.append(enum_rec)
  3581. return enums
  3582. def _load_domains(self, connection):
  3583. # Load data types for domains:
  3584. SQL_DOMAINS = """
  3585. SELECT t.typname as "name",
  3586. pg_catalog.format_type(t.typbasetype, t.typtypmod) as "attype",
  3587. not t.typnotnull as "nullable",
  3588. t.typdefault as "default",
  3589. pg_catalog.pg_type_is_visible(t.oid) as "visible",
  3590. n.nspname as "schema"
  3591. FROM pg_catalog.pg_type t
  3592. LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
  3593. WHERE t.typtype = 'd'
  3594. """
  3595. s = sql.text(SQL_DOMAINS)
  3596. c = connection.execution_options(future_result=True).execute(s)
  3597. domains = {}
  3598. for domain in c.mappings():
  3599. domain = domain
  3600. # strip (30) from character varying(30)
  3601. attype = re.search(r"([^\(]+)", domain["attype"]).group(1)
  3602. # 'visible' just means whether or not the domain is in a
  3603. # schema that's on the search path -- or not overridden by
  3604. # a schema with higher precedence. If it's not visible,
  3605. # it will be prefixed with the schema-name when it's used.
  3606. if domain["visible"]:
  3607. key = (domain["name"],)
  3608. else:
  3609. key = (domain["schema"], domain["name"])
  3610. domains[key] = {
  3611. "attype": attype,
  3612. "nullable": domain["nullable"],
  3613. "default": domain["default"],
  3614. }
  3615. return domains