Commit Graph

74 Commits (a57bb8caeab831f71b25d77c9dde5d430226cf49)

Author SHA1 Message Date
maksymnevajdev 319c3172f2 fix panic in prepared sql 2023-12-01 18:34:41 -06:00
Ville Skyttä 24ed0e4257 Make use of strings.Cut 2023-10-04 20:41:55 +03:00
Jack Christensen c08cc72306 Improve QueryExecModeCacheDescribe and clarify documentation
QueryExecModeCacheDescribe actually is safe even when the schema or
search_path is modified. It may return an error on the first execution
but it should never silently encode or decode a value incorrectly. Add a
test to demonstrate and ensure this behavior.

Update documentation of QueryExecModeCacheDescribe to remove warning of
undetected result decoding errors.

Update documentation of QueryExecModeCacheStatement and
QueryExecModeCacheDescribe to indicate that the first execution of an
invalidated statement may fail.
2023-09-23 10:35:42 -05:00
Jack Christensen 7de53a958b stmtcache: Use deterministic, stable statement names
Statement names are now a function of the SQL. This may make database
diagnostics, monitoring, and profiling easier.
2023-09-23 09:55:05 -05:00
Jack Christensen 4410fc0a65 Remove nbconn
The non-blocking IO system was designed to solve three problems:

1. Deadlock that can occur when both sides of a connection are blocked
   writing because all buffers between are full.
2. The inability to use a write deadline with a TLS.Conn without killing
   the connection.
3. Efficiently check if a connection has been closed before writing.
   This reduces the cases where the application doesn't know if a query
   that does a INSERT/UPDATE/DELETE was actually sent to the server or
   not.

However, the nbconn package is extraordinarily complex, has been a
source of very tricky bugs, and has OS specific code paths. It also does
not work at all with underlying net.Conn implementations that do not
have platform specific non-blocking IO syscall support and do not
properly implement deadlines. In particular, this is the case with
golang.org/x/crypto/ssh.

I believe the deadlock problem can be solved with a combination of a
goroutine for CopyFrom like v4 used and a watchdog for regular queries
that uses time.AfterFunc.

The write deadline problem actually should be ignorable. We check for
context cancellation before sending a query and the actual Write should
be almost instant as long as the underlying connection is not blocked.
(We should only have to wait until it is accepted by the OS, not until
it is fully sent.)

Efficiently checking if a connection has been closed is probably the
hardest to solve without non-blocking reads. However, the existing code
only solves part of the problem. It can detect a closed or broken
connection the OS knows about, but it won't actually detect other types
of broken connections such as a network interruption. This is currently
implemented in CheckConn and called automatically when checking a
connection out of the pool that has been idle for over one second. I
think that changing CheckConn to a very short deadline read and changing
the pool to do an actual Ping would be an acceptable solution.

Remove nbconn and non-blocking code. This does not leave the system in
an entirely working state. In particular, CopyFrom is broken, deadlocks
can occur for extremely large queries or batches, and PgConn.CheckConn
is now a `select 1` ping. These will be resolved in subsequent commits.
2023-06-12 09:39:26 -05:00
Nicola Murino 33d4fa0fa6 TLS with Fake Non-blocking IO test is expected to fail on Windows 2023-06-03 06:45:28 -05:00
Nicola Murino b0fa429fd0 add a comment explaining that nbOperMu and nbOperCnt are used on Windows 2023-06-03 06:45:28 -05:00
Nicola Murino 32c7858e61 Revert "Remove unused fields"
This reverts commit 2c1973de46.
2023-06-03 06:45:28 -05:00
Jack Christensen 2c1973de46 Remove unused fields 2023-05-27 08:18:47 -05:00
Dmitry K e9d64ec29d Use `time.Equal` instead of direct comparison 2023-03-24 17:51:34 -05:00
Dmitry K 2f1bba09c4 Guard deadline readings by mutex 2023-03-24 17:51:34 -05:00
Dmitry K d829073b2f Improve deadline simulation 2023-03-24 17:51:34 -05:00
Dmitry K 48da6435a5 Add deadline simulation 2023-03-24 17:51:34 -05:00
Dmitry K 34e3013153 Remove commented out atomic calls 2023-03-24 17:51:34 -05:00
Dmitry K 009a377028 Use mutex to guard entire `SetBlockingMode` call 2023-03-24 17:51:34 -05:00
Dmitry K e05abb83ec Better error messages 2023-03-24 17:51:34 -05:00
Dmitry K 89475c4c91 use `atomic.Int32` instead of `int + atomic calls` 2023-03-24 17:51:34 -05:00
Dmitry K c3d62c8783 Small comment update 2 2023-03-24 17:51:34 -05:00
Dmitry K 1298a835bc Small comment update 2023-03-24 17:51:34 -05:00
Dmitry K b2b4fbcf57 Set socket to non-blocking mode in `Read`, `Flush` and `BufferReadUntilBlock` operations 2023-03-24 17:51:34 -05:00
Dmitry K a83faa67f5 Small improvements 2023-03-24 17:51:34 -05:00
Dmitry K 8b5e8d9d89 Fix Windows non-blocking I/O for CopyFrom
Created based on discussion here: https://github.com/jackc/pgx/pull/1525#pullrequestreview-1344511991

Fixes https://github.com/jackc/pgx/issues/1552
2023-03-24 17:51:34 -05:00
Nicola Murino 19039e6dd1 fix build on 32-bit Windows 2023-03-07 17:09:03 -06:00
Dmitry K 0dbb0a52ab Fix `realNonblockingRead`, set `realNonblockingRead` call error to `nonblockReadErr` 2023-03-04 09:25:36 -06:00
Dmitry K 087b8b2ba8 Try to make windows non-blocking I/O 2023-03-04 09:25:36 -06:00
Jack Christensen c09ddaf440 Add Windows non-blocking IO 2023-03-04 09:25:36 -06:00
Jack Christensen 6105ca5073 Fix TestInternalNonBlockingWriteWithDeadline(t
The test was relying on sending so big a message that the write blocked.
However, it appears that on Windows the TCP connections over localhost
have an very large or infinite sized buffer. Change the test to simply
set the deadline to the current time before triggering the write.
2023-02-25 17:02:55 -06:00
Jack Christensen 8f46c75e73 Fix: fake non-blocking read adaptive wait time
If the time reached the minimum time before the 5 tries were up it
would get stuck reading 1 byte at a time indefinitely.
2023-02-25 16:45:34 -06:00
Jack Christensen b707faea8f Fix flickering test TestBufferNonBlockingRead 2023-02-10 19:40:31 -06:00
Jack Christensen 9963c32d4f Only count when bytes actually read 2023-01-31 20:35:44 -06:00
Jack Christensen 6bc327b3ce Find fastest possible read time for fakeNonblockingReadWaitDuration
The first 5 fake non-blocking reads are limited to 1 byte. This should
ensure that there is a measurement of a read where bytes are already
waiting in Go or the OS's read buffer.
2023-01-31 20:25:57 -06:00
Jack Christensen f46d35610e Only set c.fakeNonblockingReadWaitDuration when it will be decreased 2023-01-31 20:25:17 -06:00
Jack Christensen cf78472ce5 Use unix build tag
With Go 1.19 available we can use a simpler build tag.
2023-01-31 20:10:34 -06:00
Jack Christensen 898891a6ee Fake non-blocking read adapts its max wait time
The reason for a high max wait time was to ensure that reads aren't
cancelled when there is data waiting for it in Go or the OS's receive
buffer. Unfortunately, there is no way to know ahead of time how long
this should take.

This new code uses 2x the fastest successful read time as the max read
time. This allows the code to adapt to whatever host it is running on.

https://github.com/jackc/pgx/issues/1481
2023-01-28 09:35:52 -06:00
Jack Christensen 7019ed1edf Fix tests for iobufpool optimization 2023-01-28 09:30:12 -06:00
Jack Christensen eee854fb06 iobufpool uses *[]byte instead of []byte to reduce allocations 2023-01-28 08:02:49 -06:00
Jack Christensen bc754291c1 Save memory on non blocking read path
Only create RawConn.Read callback once and have it use NetConn fields.
Avoids the closure and some allocations.

https://github.com/jackc/pgx/issues/1481
2023-01-27 20:53:30 -06:00
Jack Christensen 2c7d86a543 Only create RawConn.Write callback once
This saves an allocation on every call.

https://github.com/jackc/pgx/issues/1481
2023-01-27 20:34:21 -06:00
Jack Christensen 7941518809 BufferReadUntilBlock should release buf when no bytes read
This was causing allocations every time there was a non-blocking read
with nothing to read.

https://github.com/jackc/pgx/issues/1481
2023-01-27 18:03:38 -06:00
Alexey Palazhchenko f839d501a7 Apply `gofmt -s`
And add CI check for that.
2023-01-24 07:55:00 -06:00
Mark Chambers 516300aabf spelling: successfully, compatibility 2023-01-16 20:06:01 -06:00
Jack Christensen ba4bbf92af Fix query sanitizer
...when query text has contains Unicode replacement character.
uft8.RuneError actually is a valid character.
2022-11-14 18:32:26 -06:00
Jack Christensen d7c7ddc594 Fix Windows 386 atomic usage
https://github.com/jackc/pgx/issues/1307
2022-09-24 09:23:36 -05:00
Jack Christensen 906f709e0c Fix typo in Windows code
https://github.com/jackc/pgx/issues/1274
2022-08-11 20:59:37 -05:00
Jack Christensen 33b782a96d Potential fix for Windows
https://github.com/jackc/pgx/issues/1274
2022-08-11 20:55:50 -05:00
Jack Christensen a5b4f888c2 Fix flickering test on CI
Ensure the conn reads everything expected before closing.
2022-07-16 18:16:19 -05:00
Jack Christensen ca41a6a222 Update docs 2022-07-10 14:32:08 -05:00
Jack Christensen e7aa76ccf9 SendBatch now uses pipeline mode to prepare and describe statements
Previously, a batch with 10 unique parameterized statements executed
100 times would entail 11 network round trips. 1 for each prepare /
describe and 1 for executing them all. Now pipeline mode is used to
prepare / describe all statements in a single network round trip. So it
would only take 2 round trips.
2022-07-09 09:32:36 -05:00
Jack Christensen ed3e9f1dd4 Check for more specific error 2022-07-01 15:33:12 -05:00
Jack Christensen 9afd320b9e Fix flickering test in CI
While this test always worked on my machine, it flickered in CI. And to
be fair the test can't guarantee the condition it is testing. Work
around this by trying many times before admitting failure.
2022-06-25 16:05:20 -05:00