]> git.ipfire.org Git - thirdparty/psycopg.git/log
thirdparty/psycopg.git
3 years agofix: fix typo: ingeger
Daniele Varrazzo [Mon, 16 May 2022 00:58:46 +0000 (02:58 +0200)] 
fix: fix typo: ingeger

3 years agochore: bump version to release 3.0.14
Daniele Varrazzo [Sun, 15 May 2022 15:46:00 +0000 (17:46 +0200)] 
chore: bump version to release 3.0.14

3 years agofix: don't barf on errors with blank sqlstate
Daniele Varrazzo [Sat, 14 May 2022 08:22:44 +0000 (10:22 +0200)] 
fix: don't barf on errors with blank sqlstate

Such messages are not entirely valid (sqlstate is documented to be
always present), however we receive them after a SHOW HELP in the
PgBouncer admin database.

The SHOW HELP actually does generate a sqlstate `00000` but the message
is somehow parsed incorrectly by the libpq, which goes on to report an
error:

    message contents do not agree with length in message type "N"

See #303. PgBouncer issue reported upstream in
https://github.com/pgbouncer/pgbouncer/issues/718

3 years agodocs: drop stray reference to psycopg3
Daniele Varrazzo [Sun, 15 May 2022 10:00:00 +0000 (12:00 +0200)] 
docs: drop stray reference to psycopg3

3 years agoMerge branch 'client-side-cursor'
Daniele Varrazzo [Sun, 15 May 2022 09:59:43 +0000 (11:59 +0200)] 
Merge branch 'client-side-cursor'

3 years agodocs: Subtly suggest to use the psycopg.sql module rather than ClientCursor 302/head
Daniele Varrazzo [Sun, 15 May 2022 09:57:22 +0000 (11:57 +0200)] 
docs: Subtly suggest to use the psycopg.sql module rather than ClientCursor

3 years agodocs: add documentation for client-side binding cursors
Daniele Varrazzo [Fri, 13 May 2022 01:13:05 +0000 (03:13 +0200)] 
docs: add documentation for client-side binding cursors

3 years agotest: test multi-statement parametric queries with client-side binding
Daniele Varrazzo [Fri, 13 May 2022 00:29:38 +0000 (02:29 +0200)] 
test: test multi-statement parametric queries with client-side binding

3 years agofix: set ClientCursor class module to psycopg
Daniele Varrazzo [Fri, 13 May 2022 00:21:10 +0000 (02:21 +0200)] 
fix: set ClientCursor class module to psycopg

3 years agofeat: add params to Cursor.copy()
Daniele Varrazzo [Thu, 12 May 2022 23:18:59 +0000 (01:18 +0200)] 
feat: add params to Cursor.copy()

3 years agotest: disable tests throwing copy to executemany in client-side cursors
Daniele Varrazzo [Thu, 12 May 2022 22:16:13 +0000 (00:16 +0200)] 
test: disable tests throwing copy to executemany in client-side cursors

The async one sometimes gets in an allocation loop. On my laptop it gets
OOM'd quickly. On Github Actions it might be the cause of test taking an
unusually long time.

3 years agofix: don't forbid all PQexec queries in client-side cursors
Daniele Varrazzo [Thu, 12 May 2022 21:47:46 +0000 (23:47 +0200)] 
fix: don't forbid all PQexec queries in client-side cursors

We can still use PQexecParams with an empty list of parameters. This has
advantages, such as the possibility of implementing stream(), and less
faffing with differences between Cursor and ClientCursor.

3 years agofeat: add ClientCursor.mogrify()
Daniele Varrazzo [Thu, 12 May 2022 14:46:28 +0000 (16:46 +0200)] 
feat: add ClientCursor.mogrify()

3 years agofeat: add ClientCursor and AsyncClientCursor classes
Daniele Varrazzo [Thu, 12 May 2022 02:25:51 +0000 (04:25 +0200)] 
feat: add ClientCursor and AsyncClientCursor classes

3 years agotest: skip testing random multirange arrays with empty last elements
Daniele Varrazzo [Sat, 14 May 2022 22:24:17 +0000 (00:24 +0200)] 
test: skip testing random multirange arrays with empty last elements

Previously we were skipping the ones with an empty first element,
because of a known shortcoming in finding the right OID. Now that we
scan the whole array to find all the elements' classes, it's the last
entry which might break dumping.

3 years agoMerge branch 'fix-301'
Daniele Varrazzo [Sat, 14 May 2022 00:02:30 +0000 (02:02 +0200)] 
Merge branch 'fix-301'

3 years agofix: allow dumping mixed types list, as long as they use the same dumper
Daniele Varrazzo [Fri, 13 May 2022 23:29:53 +0000 (01:29 +0200)] 
fix: allow dumping mixed types list, as long as they use the same dumper

Necessary to dump a mix of ipv4/ipv6 addresses, which result in the same
Postgres type, so it's a legit array.

3 years agofix: Raise DataError dumping lists of mixed types
Daniele Varrazzo [Fri, 13 May 2022 22:37:48 +0000 (00:37 +0200)] 
fix: Raise DataError dumping lists of mixed types

This requires more checking dumping lists, but the alternative is to
guard every single dumper, which is painful as well, and more brittle.

3 years agofix: raise DataError if IntDumper tries to dump another type
Daniele Varrazzo [Fri, 13 May 2022 14:54:54 +0000 (16:54 +0200)] 
fix: raise DataError if IntDumper tries to dump another type

Allow to dump int subclasses, but not other numeric type, which can lead
to truncation. As seen in #301, the Python implementation truncates; the
c implementation is reporting failing with a TypeError, but on Python
3.8 it raises a deprecation warning instead.

The condition largely happens dumping array of mixed types. However we
should probably test with array of mixed class in a more generic way.

Close #301.

3 years agotest: fix number of columns in generated faker schema
Daniele Varrazzo [Fri, 13 May 2022 22:41:14 +0000 (00:41 +0200)] 
test: fix number of columns in generated faker schema

3 years agotest: fix formats testing in number wrapper array test
Daniele Varrazzo [Fri, 13 May 2022 15:02:15 +0000 (17:02 +0200)] 
test: fix formats testing in number wrapper array test

3 years agodocs: Python enum labels -> members
Daniele Varrazzo [Thu, 12 May 2022 19:55:44 +0000 (21:55 +0200)] 
docs: Python enum labels -> members

3 years agodocs: drop docsting for AsyncPipeline.sync
Daniele Varrazzo [Thu, 12 May 2022 19:46:30 +0000 (21:46 +0200)] 
docs: drop docsting for AsyncPipeline.sync

It was out of date and, for Sync/Async objects, Async docs don't
reiterate what's on the Sync side, they only show the different
signature.

3 years agofeat: add cursor_factory parameter to `connect()`.
Daniele Varrazzo [Thu, 12 May 2022 12:48:09 +0000 (14:48 +0200)] 
feat: add cursor_factory parameter to `connect()`.

This makes easier to use ClientCursor and port more easily code running
on psycopg2 dong more DDL operations and caring less about performance.

3 years agorefactor: implement ServerCursor with a mixin rather than an helper
Daniele Varrazzo [Wed, 11 May 2022 12:38:37 +0000 (14:38 +0200)] 
refactor: implement ServerCursor with a mixin rather than an helper

This was the original intention, but some older Mypy limitation was
stopping us from doing so. Or I wasn't good enough at it.

3 years agofeat: make Cursor constructor public
Daniele Varrazzo [Wed, 4 May 2022 18:09:31 +0000 (20:09 +0200)] 
feat: make Cursor constructor public

This allows a more natural creation of cursors subclasses, without the
need of tweaking the connection's cursor_factory and allowing to pass
arbitrary init arguments to the cursor.

3 years agoMerge branch 'typed-literal'
Daniele Varrazzo [Thu, 12 May 2022 13:34:46 +0000 (15:34 +0200)] 
Merge branch 'typed-literal'

3 years agofix(sql): prefer names without space to cast builtins 264/head
Daniele Varrazzo [Sun, 20 Mar 2022 22:31:50 +0000 (23:31 +0100)] 
fix(sql): prefer names without space to cast builtins

Certain types have a regtype with spaces, such as "timestamp with time
zone". Prefer the type short name, in this case ("timestamptz").

3 years agofeat: add `Transformer.as_literal()` to convert literals to sql
Daniele Varrazzo [Sun, 20 Mar 2022 18:24:53 +0000 (19:24 +0100)] 
feat: add `Transformer.as_literal()` to convert literals to sql

This allows to keep a cache oid -> representation that will be
invalidated automatically if encoding or oid mapping changes, as well as
optimising the function in C.

3 years agofix(sql): represent array literals correctly
Daniele Varrazzo [Sun, 20 Mar 2022 18:22:47 +0000 (19:22 +0100)] 
fix(sql): represent array literals correctly

3 years agorefactor: `TypeInfo.alt_name` renamed to `regtype`
Daniele Varrazzo [Sun, 20 Mar 2022 15:21:40 +0000 (16:21 +0100)] 
refactor: `TypeInfo.alt_name` renamed to `regtype`

This has now acquired a precise meaning: it is the name of the type as
SQL snippet (it may contain invalid names, which get quoted, or a schema
name). It can be included literally in a query to make a valid cast.

The previous name wasn't documented and wasn't part of any public API so
we can probably rename it without much problem.

3 years agotest(cursor): make sure Column.name is right for invalid names too
Daniele Varrazzo [Sun, 20 Mar 2022 15:10:03 +0000 (16:10 +0100)] 
test(cursor): make sure Column.name is right for invalid names too

3 years agofix(sql): fetch correct type names in TypeInfo subclasses
Daniele Varrazzo [Sun, 20 Mar 2022 15:07:56 +0000 (16:07 +0100)] 
fix(sql): fetch correct type names in TypeInfo subclasses

Needed to fix `Literal.as_string()` of types using a `TypeInfo` subclass
to fetch info (composite, range, multirange).

3 years agofix(sql): fix `sql.Literal` with invalid type names
Daniele Varrazzo [Sun, 20 Mar 2022 15:04:54 +0000 (16:04 +0100)] 
fix(sql): fix `sql.Literal` with invalid type names

Make sure that `TypeInfo.alt_name` is always populated, even if with a
copy of `name`.

3 years agofix(composite): fix fetching composite info with invalid name or field names
Daniele Varrazzo [Sun, 20 Mar 2022 15:02:12 +0000 (16:02 +0100)] 
fix(composite): fix fetching composite info with invalid name or field names

3 years agorefactor: add _as_python_identifier() function
Daniele Varrazzo [Sun, 20 Mar 2022 14:54:44 +0000 (15:54 +0100)] 
refactor: add _as_python_identifier() function

The function takes a PostgreSQL "invalid" name (such as `a-b` or '0' or an
unicode string) and makes a valid Python name.

Currently used to make valid namedtuples from `nametuple_row()` but can
be used to make other valid Python names.

3 years agoperf: use `b"'"[0]` as const value to represent `ord("'")`
Daniele Varrazzo [Sun, 20 Mar 2022 00:44:07 +0000 (01:44 +0100)] 
perf: use `b"'"[0]` as const value to represent `ord("'")`

The value is expanded at compile time::

    >>> dis.dis('b"\'"[0]')
      1           0 LOAD_CONST               0 (39)
                  2 RETURN_VALUE

3 years agofeat(sql): add explicit type cast to Literal output
Daniele Varrazzo [Sun, 13 Mar 2022 02:35:41 +0000 (03:35 +0100)] 
feat(sql): add explicit type cast to Literal output

3 years agofeat: add `Transformer.encoding` attribute
Daniele Varrazzo [Sun, 13 Mar 2022 02:16:21 +0000 (03:16 +0100)] 
feat: add `Transformer.encoding` attribute

3 years agofeat: add Transformer.from_context() method
Daniele Varrazzo [Sat, 8 Jan 2022 23:01:56 +0000 (00:01 +0100)] 
feat: add Transformer.from_context() method

Allow to reuse the same transformer in nested context, instead of
creating new ones. Even if transformer copies are shallow, we may end up
creating several in certain places.

3 years agoMerge branch 'pipeline-more-tests'
Daniele Varrazzo [Tue, 10 May 2022 17:11:20 +0000 (19:11 +0200)] 
Merge branch 'pipeline-more-tests'

3 years agofix: make transaction status check account for pipeline mode 299/head
Denis Laxalde [Tue, 10 May 2022 10:11:21 +0000 (12:11 +0200)] 
fix: make transaction status check account for pipeline mode

We turn _check_intrans() into a generator _check_intrans_gen() in order
to call _pipeline._sync_gen() if the connection is in pipeline mode so
as to retrieve an accurate connection status.

This makes the safety guard about 'autocommit' when inside a transaction
work in pipeline mode, thus removing the xfail in transaction tests.

In test_autocommit_unknown, we now catch OperationalError which is
raised by conn.wait() rather than ProgrammingError previously which is
no longer reached.

3 years agofix: wrap transaction in an outer pipeline if the connection has one
Denis Laxalde [Tue, 10 May 2022 07:10:06 +0000 (09:10 +0200)] 
fix: wrap transaction in an outer pipeline if the connection has one

Before entering a transaction on a connection in pipeline mode, we open an
outer pipeline to ensure that a Sync is after the transaction exists;
similarly to the inner pipeline, this is to ensure that the connection
state is restored at transaction exit similarly to the non-pipeline
case. The inner pipeline is not enough because we need to account for
the exit transaction statement (COMMIT or ROLLBACK) and then sync its
result.

This fixes transactions tests failing in previous commit.

3 years agotest: parametrize transaction tests with pipeline on/off
Denis Laxalde [Tue, 10 May 2022 07:01:07 +0000 (09:01 +0200)] 
test: parametrize transaction tests with pipeline on/off

The 'conn' fixture is overloaded in transaction tests, pulling the
'pipeline' fixture to have them run with or without a pipeline.

When checking for connection's transaction state, just after entering
the transaction() block (e.g. in test_basic), we need to sync the
pipeline in order to get the state right. This is just for tests as the
exact transaction state, in pipeline mode, is not deterministic since
query results are not fetched directly.

The following tests fail:

  tests/test_transaction.py::test_basic[pipeline=on]
  tests/test_transaction.py::test_begins_on_enter[pipeline=on]
  tests/test_transaction.py::test_commit_on_successful_exit[pipeline=on]
  tests/test_transaction.py::test_rollback_on_exception_exit[pipeline=on]
  tests/test_transaction.py::test_autocommit_off_but_no_tx_started_successful_exit[pipeline=on]
  tests/test_transaction.py::test_autocommit_off_but_no_tx_started_exception_exit[pipeline=on]
  tests/test_transaction.py::test_nested_all_changes_persisted_on_successful_exit[pipeline=on]
  tests/test_transaction.py::test_nested_all_changes_discarded_on_outer_exception[pipeline=on]
  tests/test_transaction.py::test_nested_all_changes_discarded_on_inner_exception[pipeline=on]
  tests/test_transaction.py::test_nested_inner_scope_exception_handled_in_outer_scope[pipeline=on]
  tests/test_transaction.py::test_nested_three_levels_successful_exit[pipeline=on]
  tests/test_transaction.py::test_named_savepoints_successful_exit[pipeline=on]
  tests/test_transaction.py::test_explicit_rollback_of_enclosing_tx_outer_tx_unaffected[pipeline=on]
  tests/test_transaction.py::test_str[pipeline=on]

This is because the connection state is not correct at the end of
transaction, we'll fix this in next commit.

Some others were adjusted as follows:

* when entering the transaction() block, e.g. in test_begins_on_enter,
  we eventually call pipeline.sync() in order to get the state right
  (this is just for tests as the exact transaction state, in pipeline
  mode, is not deterministic since query results are not fetched
  directly);

* test_context_inerror_rollback_no_clobber[pipeline=on] is skipped, as
  explained in comment inline;

* test_prohibits_use_of_commit_rollback_autocommit[pipeline=on] is
  xfail, because Connection._check_intrans() does not check for pipeline
  mode (see TODO inline about a possible solution);

* test_str now checks for [<txstatus>, pipeline=ON] in str(Transaction).

3 years agotest: add fixtures to toggle pipeline mode
Denis Laxalde [Thu, 9 Dec 2021 15:37:50 +0000 (16:37 +0100)] 
test: add fixtures to toggle pipeline mode

Also add a pytest mark so that one can run 'pytest -m pipeline' to
select all pipeline-mode tests.

3 years agofeat: display the pipeline status in the objects repr
Daniele Varrazzo [Tue, 10 May 2022 10:10:14 +0000 (12:10 +0200)] 
feat: display the pipeline status in the objects repr

3 years agochore: bump version to release 3.0.13
Daniele Varrazzo [Tue, 10 May 2022 09:20:03 +0000 (11:20 +0200)] 
chore: bump version to release 3.0.13

3 years agoMerge branch 'fix-296'
Daniele Varrazzo [Tue, 10 May 2022 08:14:29 +0000 (10:14 +0200)] 
Merge branch 'fix-296'

3 years agodocs: suggest to use commit to maintain atomic sanity in pipeline mode 297/head
Daniele Varrazzo [Mon, 9 May 2022 23:21:47 +0000 (01:21 +0200)] 
docs: suggest to use commit to maintain atomic sanity in pipeline mode

3 years agofix: fix integration between pipelines and nested transaction
Daniele Varrazzo [Mon, 9 May 2022 23:03:13 +0000 (01:03 +0200)] 
fix: fix integration between pipelines and nested transaction

Refactor to remove all the knowledge about pipelines from the
transaction object. Replaced by just syncing before entering the
transaction.

Had to drop the pipeline exit, which will now fetch results whatever the
level. If not doing so, a final sync should have been forced to exit
with a clean state from an inner transaction block.

3 years agotest: fix test of pipeline mode and nested transaction
Daniele Varrazzo [Mon, 9 May 2022 22:57:16 +0000 (00:57 +0200)] 
test: fix test of pipeline mode and nested transaction

This test fails in the current implementation.

3 years agotest: drop TODO points and emergency rollbacks from pipeline tests
Daniele Varrazzo [Mon, 9 May 2022 21:46:47 +0000 (23:46 +0200)] 
test: drop TODO points and emergency rollbacks from pipeline tests

The previous changeset restored some sanity. The behaviour in
`test_errors_raised_on_commit` is not inconsistent with the non-pipeline
mode because the two are not comparable: in non-pipeline mode the
exception is raised by `execute()`, not by `commit()`. If an exception
is raised in `commit()` itself, it is already handled consistently, as
`test_error_on_commit` shows.

3 years agofix: wrap transaction in pipelines if the connection has one 298/head
Denis Laxalde [Mon, 9 May 2022 19:14:59 +0000 (21:14 +0200)] 
fix: wrap transaction in pipelines if the connection has one

When entering a transaction on a connection in pipeline mode, we open an
inner pipeline to ensure that a Sync is emitted at the end of
transaction thus restoring the connection in its expected state (i.e.
the same as in non-pipeline mode).

3 years agotest: add test to verify the behaviour of failed commits
Daniele Varrazzo [Mon, 9 May 2022 13:46:30 +0000 (15:46 +0200)] 
test: add test to verify the behaviour of failed commits

The exception is raised where expected; the transaction is left in IDLE
state. This is unlike what happens in pipeline mode.

3 years agofix: fix Transaction commit in pipeline mode
Daniele Varrazzo [Mon, 9 May 2022 13:36:19 +0000 (15:36 +0200)] 
fix: fix Transaction commit in pipeline mode

Make sure we receive the commit message, so we can raise an immediate
exception, as we do for connection-handled transactions.

Highlight, in the test, a behaviour difference between commit in
pipeline and non-pipeline mode. In the latter, after a failed commit,
the connection is left IDLE; in pipeline mode it is left INERROR so we
need a further rollback to keep on using it. Will look if this can be
made consistent.

3 years agofix: fix nested transaction entering in pipeline mode
Daniele Varrazzo [Mon, 9 May 2022 13:23:51 +0000 (15:23 +0200)] 
fix: fix nested transaction entering in pipeline mode

If the connection is idle, it might be that we are missing results.
Receive them to align the connection state to what the transaction
object expects, otherwise the assert in _push_savepoint() will fail.

3 years agofix: sync the pipeline on commit()
Daniele Varrazzo [Mon, 9 May 2022 00:57:05 +0000 (02:57 +0200)] 
fix: sync the pipeline on commit()

This maintains the expectation that, after commit(), the operation is
really persisted. As per postgres pipeline docs:

    The client must not assume that work is committed when it sends a
    COMMIT â€” only when the corresponding result is received to confirm the
    commit is complete.

With this change we are effectively receiving the result of the commit,
eventually throwing an exception if it happened.

Close #296

3 years agofix: fetch results on Pipeline.sync().
Daniele Varrazzo [Mon, 9 May 2022 00:52:39 +0000 (02:52 +0200)] 
fix: fetch results on Pipeline.sync().

This allows a sync() call to establish an high level synchronization
point between the application and the server. If there were errors in
the processing so far, sync will throw the exception.

See #296.

3 years agodocs: add warning about error messages raised randomly in pipeline mode
Daniele Varrazzo [Sun, 8 May 2022 23:36:40 +0000 (01:36 +0200)] 
docs: add warning about error messages raised randomly in pipeline mode

Close #266

3 years agofix: raise a clean error on pipeline() on a closed connection
Daniele Varrazzo [Sun, 8 May 2022 20:26:28 +0000 (22:26 +0200)] 
fix: raise a clean error on pipeline() on a closed connection

3 years agochore: update cibuildwheel to 2.5.0
Daniele Varrazzo [Mon, 2 May 2022 01:01:11 +0000 (03:01 +0200)] 
chore: update cibuildwheel to 2.5.0

3 years agoMerge branch 'fix-293'
Daniele Varrazzo [Sun, 8 May 2022 20:14:16 +0000 (22:14 +0200)] 
Merge branch 'fix-293'

3 years agoFix array oid dumping lists of integer
Daniele Varrazzo [Sun, 8 May 2022 14:29:32 +0000 (16:29 +0200)] 
Fix array oid dumping lists of integer

The problem was only in the list text dumper, which was trying to do more
than what needed. The list binary dumper worked alright.

Drop mixed array test, which don't really represent anything Postgres
can work with.

Close #293

3 years agotest: add test to verify the wrong array oid with numbers
Daniele Varrazzo [Thu, 5 May 2022 21:04:10 +0000 (23:04 +0200)] 
test: add test to verify the wrong array oid with numbers

The array binary dumper does the right thing; the text one picks
numeric[] unconditionally. It was clearly made on purpose, but #293
shows that it's a bad idea.

3 years agodocs: wordsmith pipeline docs
Daniele Varrazzo [Sun, 8 May 2022 14:24:17 +0000 (16:24 +0200)] 
docs: wordsmith pipeline docs

3 years agodocs: drop repeated sentence
Daniele Varrazzo [Sat, 7 May 2022 18:21:43 +0000 (20:21 +0200)] 
docs: drop repeated sentence

3 years agoMerge branch 'pipeline-docs'
Daniele Varrazzo [Sat, 7 May 2022 18:08:23 +0000 (20:08 +0200)] 
Merge branch 'pipeline-docs'

3 years agodocs: typo fixed 263/head
Daniele Varrazzo [Sat, 7 May 2022 17:59:49 +0000 (19:59 +0200)] 
docs: typo fixed

Thank you @dlax!

3 years agodocs: add examples and messages details in pipeline mode
Daniele Varrazzo [Sat, 7 May 2022 17:57:50 +0000 (19:57 +0200)] 
docs: add examples and messages details in pipeline mode

3 years agodocs: mention that conn.pipeline() yields a Pipeline object 295/head
Denis Laxalde [Sat, 7 May 2022 15:49:43 +0000 (17:49 +0200)] 
docs: mention that conn.pipeline() yields a Pipeline object

While reading the section, it is otherwise not obvious to understand
where to get the Pipeline object from.

3 years agodocs: add note about implicit transactions and sync in pipeline mode
Daniele Varrazzo [Sat, 7 May 2022 15:29:25 +0000 (17:29 +0200)] 
docs: add note about implicit transactions and sync in pipeline mode

3 years agodocs: split/reshuffle pipeline docs
Daniele Varrazzo [Sat, 7 May 2022 14:54:51 +0000 (16:54 +0200)] 
docs: split/reshuffle pipeline docs

- Postgres concept
- Psycopg usage
- detail about sync points
- disclaimer - don't call the semver police.

3 years agodocs: document that now rollback issues a sync point
Daniele Varrazzo [Sat, 7 May 2022 14:53:34 +0000 (16:53 +0200)] 
docs: document that now rollback issues a sync point

3 years agodocs: add warning disclaimer about pipeline mode
Daniele Varrazzo [Sat, 7 May 2022 14:21:07 +0000 (16:21 +0200)] 
docs: add warning disclaimer about pipeline mode

3 years agodocs: clarify flush and sync pipeline operations
Denis Laxalde [Sun, 3 Apr 2022 19:59:55 +0000 (21:59 +0200)] 
docs: clarify flush and sync pipeline operations

3 years agodocs: fix pipeline docs typos
Daniele Varrazzo [Sun, 3 Apr 2022 18:37:44 +0000 (20:37 +0200)] 
docs: fix pipeline docs typos

3 years agodocs: add note about features not supported in pipeline mode
Daniele Varrazzo [Sun, 3 Apr 2022 00:25:55 +0000 (02:25 +0200)] 
docs: add note about features not supported in pipeline mode

3 years agodocs: add first cut of pipeline mode docs
Daniele Varrazzo [Sat, 2 Apr 2022 22:55:18 +0000 (00:55 +0200)] 
docs: add first cut of pipeline mode docs

3 years agofix: sync pipeline state on rollback 267/head
Daniele Varrazzo [Mon, 4 Apr 2022 01:41:49 +0000 (03:41 +0200)] 
fix: sync pipeline state on rollback

3 years agoMerge branch 'pipeline-sync-no-fetch'
Daniele Varrazzo [Sat, 7 May 2022 13:12:56 +0000 (15:12 +0200)] 
Merge branch 'pipeline-sync-no-fetch'

3 years agofeat: only Sync nested pipelines at exit 269/head
Denis Laxalde [Wed, 6 Apr 2022 18:01:53 +0000 (20:01 +0200)] 
feat: only Sync nested pipelines at exit

It's not needed to force a results fetch when exiting a nested pipeline
as this would naturally happen either later in the outer pipeline or
eventually at the end of the outermost one. Rather, just do a Sync, as
it allows outer pipeline to resume normal execution in case the inner
one got into aborted state.

3 years agofix: only request a pipeline flush in returning executemany()
Denis Laxalde [Mon, 4 Apr 2022 15:19:55 +0000 (17:19 +0200)] 
fix: only request a pipeline flush in returning executemany()

A full Sync is not needed as we're only interested in getting results
when returning=True.

3 years agofix: do not fetch in Pipeline.sync()
Denis Laxalde [Mon, 4 Apr 2022 09:41:06 +0000 (11:41 +0200)] 
fix: do not fetch in Pipeline.sync()

During previous refactorings, we made Pipeline.sync() also fetch results
from the server. But this somehow breaks the semantics of the
synchronization point as defined by Postgres because the user might be
interested in emitting Sync message as a way to solely close the current
series of queries in the pipeline: i.e., flush queries from client to
server and reset the pipeline error state. In this respect, the 'fetch'
step should be explicit.

BasePipeline._sync_gen() is changed to only emit a Sync and a new
_exit_gen() method is introduced doing what _sync_gen() previously did.
Accordingly, the warning emitted when calling this _exit_gen() at
pipeline exit is adjusted to say "terminating" instead of "syncing".

3 years agofix: shorten tracebacks on pipeline sync and exit
Daniele Varrazzo [Mon, 4 Apr 2022 00:54:41 +0000 (02:54 +0200)] 
fix: shorten tracebacks on pipeline sync and exit

The details shown are only the communication bowels. So we go from:

    Traceback (most recent call last):
      File "trace.py", line 301, in <module>
        conn.execute("select pg_sleep(0.2)")
      File "/usr/lib/python3.8/contextlib.py", line 120, in __exit__
        next(self.gen)
      File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 881, in pipeline
        yield pipeline
      File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 197, in __exit__
        self.sync()
      File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 183, in sync
        self._conn.wait(self._sync_gen())
      File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 896, in wait
        return waiting.wait(gen, self.pgconn.socket, timeout=timeout)
      File "/home/piro/dev/psycopg3/psycopg/psycopg/waiting.py", line 237, in wait_epoll
        s = gen.send(ready)
      File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 92, in _sync_gen
        yield from self._communicate_gen()
      File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 105, in _communicate_gen
        self._process_results(queued, results)
      File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 149, in _process_results
        raise e.error_from_result(result, encoding=pgconn_encoding(self.pgconn))
    psycopg.errors.UndefinedColumn: column "foo" does not exist
    LINE 1: select foo
                   ^

to:

    Traceback (most recent call last):
      File "trace.py", line 301, in <module>
        conn.execute("select pg_sleep(0.2)")
      File "/usr/lib/python3.8/contextlib.py", line 120, in __exit__
        next(self.gen)
      File "/home/piro/dev/psycopg3/psycopg/psycopg/connection.py", line 881, in pipeline
        yield pipeline
      File "/home/piro/dev/psycopg3/psycopg/psycopg/_pipeline.py", line 205, in __exit__
        raise exc2.with_traceback(None)
    psycopg.errors.UndefinedColumn: column "foo" does not exist
    LINE 1: select foo
                   ^

which shows much more clearly that the traceback was raised at pipeline
block exit.

3 years agofix: allow to use cancel() on a closed connection
Daniele Varrazzo [Mon, 2 May 2022 00:45:21 +0000 (02:45 +0200)] 
fix: allow to use cancel() on a closed connection

3 years agodocs: add hint about using add_signal_handler() to interrupt async queries
Daniele Varrazzo [Mon, 2 May 2022 00:37:00 +0000 (02:37 +0200)] 
docs: add hint about using add_signal_handler() to interrupt async queries

Courtesy @gjcarneiro, thank you!

3 years agoMerge branch 'fix-286'
Daniele Varrazzo [Mon, 2 May 2022 00:53:32 +0000 (02:53 +0200)] 
Merge branch 'fix-286'

3 years agoperf: avoid unnecessary recvfrom() in cursor.stream()
Daniele Varrazzo [Sun, 1 May 2022 22:56:42 +0000 (00:56 +0200)] 
perf: avoid unnecessary recvfrom() in cursor.stream()

Call PQisBusy() before PQconsumeInput() on fetching results. If not
busy, don't call PQconsumeInput() at all but just go to fetching results
and notifications.

This is especially useful in single-row mode because most of the times
the libpq can produce several results after a single network fetch.
Previously we were calling PQconsumeInput() also when results were
already on the client and there was nothing new to read, which forced
the libpq to run a select() to tell apart a lack of data from an EOF,
see `the grumble`_, and caused the overhead reported in #286.

Close #286.

.. _the grumble: https://github.com/postgres/postgres/blob/ed57cac84d1c5642737dab1e4c4b8cb4f0c4305f/src/interfaces/libpq/fe-misc.c#L681

3 years agotest: add cursor.stream() test for empty result sets
Daniele Varrazzo [Sun, 1 May 2022 20:45:24 +0000 (22:45 +0200)] 
test: add cursor.stream() test for empty result sets

Also add a missing async test for results with no column.

3 years agorefactor: replace an assert with a type: ignore
Daniele Varrazzo [Sun, 1 May 2022 20:44:37 +0000 (22:44 +0200)] 
refactor: replace an assert with a type: ignore

It's not like we give away opcodes for free here.

3 years agorefactor: send only once per stream
Daniele Varrazzo [Sun, 1 May 2022 12:35:02 +0000 (14:35 +0200)] 
refactor: send only once per stream

No need to do it at every row, probably entirely wrong. However this is
not the cause of the slowness reported in #286.

3 years agorefactor: only clean up the wait() timeout if needed
Daniele Varrazzo [Sun, 1 May 2022 12:30:20 +0000 (14:30 +0200)] 
refactor: only clean up the wait() timeout if needed

Often we terminate the iteration at the first item, so no need to select
at all.

3 years agoRe-export from psycopg._cmodule
hauntsaninja [Sun, 1 May 2022 05:44:04 +0000 (23:44 -0600)] 
Re-export from psycopg._cmodule

3 years agotest: fix variable scope in ctrl-c test
Daniele Varrazzo [Sat, 30 Apr 2022 13:18:14 +0000 (15:18 +0200)] 
test: fix variable scope in ctrl-c test

3 years agotest: verify ctrl-c in async mode using get_event_loop
Daniele Varrazzo [Fri, 29 Apr 2022 23:16:02 +0000 (01:16 +0200)] 
test: verify ctrl-c in async mode using get_event_loop

3 years agotest: drop test guard for unsupported Python version
Daniele Varrazzo [Fri, 29 Apr 2022 23:54:04 +0000 (01:54 +0200)] 
test: drop test guard for unsupported Python version

3 years agodocs: document psycopg 3.0.12 released
Daniele Varrazzo [Fri, 29 Apr 2022 22:42:19 +0000 (00:42 +0200)] 
docs: document psycopg 3.0.12 released

3 years agodocs: improvements to the copy docs
Daniele Varrazzo [Fri, 29 Apr 2022 22:28:07 +0000 (00:28 +0200)] 
docs: improvements to the copy docs

3 years agodocs: add example of table copy across server
Daniele Varrazzo [Fri, 29 Apr 2022 22:07:47 +0000 (00:07 +0200)] 
docs: add example of table copy across server

See #285 for a conversation.

3 years agodocs: document that ticket #254 is fixed in 3.0.12
Daniele Varrazzo [Fri, 29 Apr 2022 21:42:20 +0000 (23:42 +0200)] 
docs: document that ticket #254 is fixed in 3.0.12

We can consider this just a bug, not a change of interface. It is
particularly bad because it doesn't allow to pipe a COPY FROM into a
COPY TO.

See https://github.com/psycopg/psycopg/discussions/285#discussioncomment-2663192
for details.