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.
This commit is contained in:
Daniele Varrazzo 2010-11-16 18:10:58 +00:00
parent 15bba2966f
commit e1a04bec0e
3 changed files with 58 additions and 25 deletions

View File

@ -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

View File

@ -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.

View File

@ -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: