@@ -521,6 +521,25 @@ conn_setup_cancel(connectionObject *self, PGconn *pgconn)
521521 return 0 ;
522522}
523523
524+ /* Return 1 if the "replication" keyword is set in the DSN, 0 otherwise */
525+ static int
526+ dsn_has_replication (char * pgdsn )
527+ {
528+ int ret = 0 ;
529+ PQconninfoOption * connopts , * ptr ;
530+
531+ connopts = PQconninfoParse (pgdsn , NULL );
532+
533+ for (ptr = connopts ; ptr -> keyword != NULL ; ptr ++ ) {
534+ if (strcmp (ptr -> keyword , "replication" ) == 0 && ptr -> val != NULL )
535+ ret = 1 ;
536+ }
537+
538+ PQconninfoFree (connopts );
539+
540+ return ret ;
541+ }
542+
524543
525544/* Return 1 if the server datestyle allows us to work without problems,
526545 0 if it needs to be set to something better, e.g. ISO. */
@@ -549,47 +568,53 @@ conn_setup(connectionObject *self, PGconn *pgconn)
549568{
550569 PGresult * pgres = NULL ;
551570 char * error = NULL ;
571+ int rv = -1 ;
552572
553573 self -> equote = conn_get_standard_conforming_strings (pgconn );
554574 self -> server_version = conn_get_server_version (pgconn );
555575 self -> protocol = conn_get_protocol_version (self -> pgconn );
556576 if (3 != self -> protocol ) {
557577 PyErr_SetString (InterfaceError , "only protocol 3 supported" );
558- return -1 ;
578+ goto exit ;
559579 }
560580
561581 if (0 > conn_read_encoding (self , pgconn )) {
562- return -1 ;
582+ goto exit ;
563583 }
564584
565585 if (0 > conn_setup_cancel (self , pgconn )) {
566- return -1 ;
586+ goto exit ;
567587 }
568588
569589 Py_BEGIN_ALLOW_THREADS ;
570590 pthread_mutex_lock (& self -> lock );
571591 Py_BLOCK_THREADS ;
572592
573- if (!conn_is_datestyle_ok (self -> pgconn )) {
593+ if (!dsn_has_replication ( self -> dsn ) && ! conn_is_datestyle_ok (self -> pgconn )) {
574594 int res ;
575595 Py_UNBLOCK_THREADS ;
576596 res = pq_set_guc_locked (self , "datestyle" , "ISO" ,
577597 & pgres , & error , & _save );
578598 Py_BLOCK_THREADS ;
579599 if (res < 0 ) {
580600 pq_complete_error (self , & pgres , & error );
581- return -1 ;
601+ goto unlock ;
582602 }
583603 }
584604
585605 /* for reset */
586606 self -> autocommit = 0 ;
587607
608+ /* success */
609+ rv = 0 ;
610+
611+ unlock :
588612 Py_UNBLOCK_THREADS ;
589613 pthread_mutex_unlock (& self -> lock );
590614 Py_END_ALLOW_THREADS ;
591615
592- return 0 ;
616+ exit :
617+ return rv ;
593618}
594619
595620/* conn_connect - execute a connection to the database */
@@ -886,8 +911,11 @@ _conn_poll_setup_async(connectionObject *self)
886911 self -> autocommit = 1 ;
887912
888913 /* If the datestyle is ISO or anything else good,
889- * we can skip the CONN_STATUS_DATESTYLE step. */
890- if (!conn_is_datestyle_ok (self -> pgconn )) {
914+ * we can skip the CONN_STATUS_DATESTYLE step.
915+ * Note that we cannot change the datestyle on a replication
916+ * connection.
917+ */
918+ if (!dsn_has_replication (self -> dsn ) && !conn_is_datestyle_ok (self -> pgconn )) {
891919 Dprintf ("conn_poll: status -> CONN_STATUS_DATESTYLE" );
892920 self -> status = CONN_STATUS_DATESTYLE ;
893921 if (0 == pq_send_query (self , psyco_datestyle )) {
0 commit comments