summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-11-16 18:10:58 +0000
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>2010-11-16 18:15:00 +0000
commite1a04bec0e9ca0fe15da400465ed449dbae6d2a1 (patch)
treeaf4f67d48044e1caa690c4aa59fa6dd462675f14
parent15bba2966f6404a90b9e4e50c27842cfc78f5431 (diff)
downloadpsycopg2-e1a04bec0e9ca0fe15da400465ed449dbae6d2a1.tar.gz
Don't inconditionately run a query at connection to set datestyle to ISO.
Run it only if PQparameterStatus() reports the datestyle is set to something different.
-rw-r--r--ChangeLog5
-rw-r--r--NEWS-2.34
-rw-r--r--psycopg/connection_int.c74
3 files changed, 58 insertions, 25 deletions
diff --git a/ChangeLog b/ChangeLog
index 8d377b7..9c9b3b4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -6,9 +6,12 @@
* psycopg/connection_int.c: dropped support for protocol 2 at compile
time and protocol 2-specific code.
- * psycopg/connection_int.c: don't run a query at connection to detect
+ * psycopg/connection_int.c: don't run a query at every connection to detect
client encoding: use PQparameterStatus() instead.
+ * psycopg/connection_int.c: don't run a query at every connection to set
+ the datestyle to ISO if PQparameterStatus() reports it already is.
+
2010-11-11 Daniele Varrazzo <daniele.varrazzo@gmail.com>
* lib/extras.py: build the namedtuple only once per execution, not once
diff --git a/NEWS-2.3 b/NEWS-2.3
index ead9064..02d0aaf 100644
--- a/NEWS-2.3
+++ b/NEWS-2.3
@@ -15,7 +15,9 @@ psycopg 2.3 aims to expose some new features introduced in PostgreSQL 9.0.
- Dropped support for protocol 2: Psycopg 2.3 can only connect to PostgreSQL
servers with version at least 7.4.
- - don't issue a query at every connection to detect the client encoding.
+ - don't issue a query at every connection to detect the client encoding
+ and to set the datestyle to ISO if it is already compatible with what
+ expected.
- `mogrify()` now supports unicode queries.
- subclasses of a type that can be adapted are adapted as the superclass.
- `errorcodes` knows a couple of new codes introduced in PostgreSQL 9.0.
diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c
index ba923e0..8516b2b 100644
--- a/psycopg/connection_int.c
+++ b/psycopg/connection_int.c
@@ -292,6 +292,22 @@ conn_get_server_version(PGconn *pgconn)
}
+/* Return 1 if the server datestyle allows us to work without problems,
+ 0 if it needs to be set to something better, e.g. ISO. */
+static int
+conn_is_datestyle_ok(PGconn *pgconn)
+{
+ const char *ds;
+
+ ds = PQparameterStatus(pgconn, "DateStyle");
+ Dprintf("conn_connect: DateStyle %s", ds);
+
+ /* Return true if ds starts with "ISO"
+ * e.g. "ISO, DMY" is fine, "German" not. */
+ return (ds[0] == 'I' && ds[1] == 'S' && ds[2] == 'O');
+}
+
+
/* conn_setup - setup and read basic information about the connection */
int
@@ -323,23 +339,26 @@ conn_setup(connectionObject *self, PGconn *pgconn)
return -1;
}
- if (!green) {
- Py_UNBLOCK_THREADS;
- pgres = PQexec(pgconn, psyco_datestyle);
- Py_BLOCK_THREADS;
- } else {
- pgres = psyco_exec_green(self, psyco_datestyle);
- }
+ if (!conn_is_datestyle_ok(self->pgconn)) {
+ if (!green) {
+ Py_UNBLOCK_THREADS;
+ Dprintf("conn_connect: exec query \"%s\";", psyco_datestyle);
+ pgres = PQexec(pgconn, psyco_datestyle);
+ Py_BLOCK_THREADS;
+ } else {
+ pgres = psyco_exec_green(self, psyco_datestyle);
+ }
- if (pgres == NULL || PQresultStatus(pgres) != PGRES_COMMAND_OK ) {
- PyErr_SetString(OperationalError, "can't set datestyle to ISO");
- IFCLEARPGRES(pgres);
- Py_UNBLOCK_THREADS;
- pthread_mutex_unlock(&self->lock);
- Py_BLOCK_THREADS;
- return -1;
+ if (pgres == NULL || PQresultStatus(pgres) != PGRES_COMMAND_OK ) {
+ PyErr_SetString(OperationalError, "can't set datestyle to ISO");
+ IFCLEARPGRES(pgres);
+ Py_UNBLOCK_THREADS;
+ pthread_mutex_unlock(&self->lock);
+ Py_BLOCK_THREADS;
+ return -1;
+ }
+ CLEARPGRES(pgres);
}
- CLEARPGRES(pgres);
if (!green) {
Py_UNBLOCK_THREADS;
@@ -642,15 +661,24 @@ _conn_poll_setup_async(connectionObject *self)
*/
self->isolation_level = 0;
- Dprintf("conn_poll: status -> CONN_STATUS_DATESTYLE");
- self->status = CONN_STATUS_DATESTYLE;
- if (0 == pq_send_query(self, psyco_datestyle)) {
- PyErr_SetString(OperationalError, PQerrorMessage(self->pgconn));
- break;
+ /* If the datestyle is ISO or anything else good,
+ * we can skip the CONN_STATUS_DATESTYLE step. */
+ if (!conn_is_datestyle_ok(self->pgconn)) {
+ Dprintf("conn_poll: status -> CONN_STATUS_DATESTYLE");
+ self->status = CONN_STATUS_DATESTYLE;
+ if (0 == pq_send_query(self, psyco_datestyle)) {
+ PyErr_SetString(OperationalError, PQerrorMessage(self->pgconn));
+ break;
+ }
+ Dprintf("conn_poll: async_status -> ASYNC_WRITE");
+ self->async_status = ASYNC_WRITE;
+ res = PSYCO_POLL_WRITE;
+ }
+ else {
+ Dprintf("conn_poll: status -> CONN_STATUS_READY");
+ self->status = CONN_STATUS_READY;
+ res = PSYCO_POLL_OK;
}
- Dprintf("conn_poll: async_status -> ASYNC_WRITE");
- self->async_status = ASYNC_WRITE;
- res = PSYCO_POLL_WRITE;
break;
case CONN_STATUS_DATESTYLE: