@@ -135,7 +135,6 @@ static ssize_t socket_write(Channel* chan, char const* buf, size_t size);
135
135
*/
136
136
#define ELOG (severity ,fmt ,...)
137
137
138
-
139
138
static Proxy * proxy ;
140
139
int MyProxyId ;
141
140
pgsocket MyProxySocket ;
@@ -594,9 +593,16 @@ channel_write(Channel* chan, bool synchronous)
594
593
while (peer -> tx_pos < peer -> tx_size ) /* has something to write */
595
594
{
596
595
ssize_t rc = socket_write (chan , peer -> buf + peer -> tx_pos , peer -> tx_size - peer -> tx_pos );
596
+
597
597
ELOG (LOG , "%p: write %d tx_pos=%d, tx_size=%d: %m" , chan , (int )rc , peer -> tx_pos , peer -> tx_size );
598
598
if (rc <= 0 )
599
599
return false;
600
+
601
+ if (!chan -> client_port )
602
+ ELOG (LOG , "Send command %c from client %d to backend %d (%p:ready=%d)" , peer -> buf [peer -> tx_pos ], peer -> client_port -> sock , chan -> backend_pid , chan , chan -> backend_is_ready );
603
+ else
604
+ ELOG (LOG , "Send reply %c to client %d from backend %d (%p:ready=%d)" , peer -> buf [peer -> tx_pos ], chan -> client_port -> sock , peer -> backend_pid , peer , peer -> backend_is_ready );
605
+
600
606
if (chan -> client_port )
601
607
chan -> proxy -> state -> tx_bytes += rc ;
602
608
else
@@ -665,6 +671,7 @@ channel_read(Channel* chan)
665
671
while (chan -> tx_size == 0 ) /* there is no pending write op */
666
672
{
667
673
ssize_t rc ;
674
+ bool handshake = false;
668
675
#ifdef USE_SSL
669
676
int waitfor = 0 ;
670
677
if (chan -> client_port && chan -> client_port -> ssl_in_use )
@@ -674,8 +681,8 @@ channel_read(Channel* chan)
674
681
rc = chan -> client_port
675
682
? secure_raw_read (chan -> client_port , chan -> buf + chan -> rx_pos , chan -> buf_size - chan -> rx_pos )
676
683
: recv (chan -> backend_socket , chan -> buf + chan -> rx_pos , chan -> buf_size - chan -> rx_pos , 0 );
677
-
678
684
ELOG (LOG , "%p: read %d: %m" , chan , (int )rc );
685
+
679
686
if (rc <= 0 )
680
687
{
681
688
if (rc == 0 || (errno != EAGAIN && errno != EWOULDBLOCK ))
@@ -688,6 +695,12 @@ channel_read(Channel* chan)
688
695
ModifyWaitEvent (chan -> proxy -> wait_events , chan -> event_pos , WL_SOCKET_READABLE |WL_SOCKET_WRITEABLE |WL_SOCKET_EDGE , NULL );
689
696
chan -> edge_triggered = false;
690
697
}
698
+
699
+ if (!chan -> client_port )
700
+ ELOG (LOG , "Receive reply %c %d bytes from backend %d (%p:ready=%d) to client %d" , chan -> buf [0 ] ? chan -> buf [0 ] : '?' , (int )rc + chan -> rx_pos , chan -> backend_pid , chan , chan -> backend_is_ready , chan -> peer ? chan -> peer -> client_port -> sock : -1 );
701
+ else
702
+ ELOG (LOG , "Receive command %c %d bytes from client %d to backend %d (%p:ready=%d)" , chan -> buf [0 ] ? chan -> buf [0 ] : '?' , (int )rc + chan -> rx_pos , chan -> client_port -> sock , chan -> peer ? chan -> peer -> backend_pid : -1 , chan -> peer , chan -> peer ? chan -> peer -> backend_is_ready : -1 );
703
+
691
704
chan -> rx_pos += rc ;
692
705
msg_start = 0 ;
693
706
@@ -696,7 +709,6 @@ channel_read(Channel* chan)
696
709
{
697
710
int msg_len ;
698
711
uint32 new_msg_len ;
699
- bool handshake = false;
700
712
if (chan -> pool == NULL ) /* process startup packet */
701
713
{
702
714
Assert (msg_start == 0 );
@@ -718,7 +730,6 @@ channel_read(Channel* chan)
718
730
}
719
731
if (chan -> rx_pos - msg_start >= msg_len ) /* Message is completely fetched */
720
732
{
721
- int response_size = msg_start + msg_len ;
722
733
if (chan -> pool == NULL ) /* receive startup packet */
723
734
{
724
735
Assert (chan -> client_port );
@@ -811,15 +822,22 @@ channel_read(Channel* chan)
811
822
else if (chan -> gucs && is_transactional_statement (stmt ))
812
823
{
813
824
size_t gucs_len = strlen (chan -> gucs );
814
- if (chan -> rx_pos + gucs_len > chan -> buf_size )
825
+ if (chan -> rx_pos + gucs_len + 1 > chan -> buf_size )
815
826
{
816
827
/* Reallocate buffer to fit concatenated GUCs */
817
- chan -> buf_size = chan -> rx_pos + gucs_len ;
828
+ chan -> buf_size = chan -> rx_pos + gucs_len + 1 ;
818
829
chan -> buf = repalloc (chan -> buf , chan -> buf_size );
819
830
}
820
831
if (is_transaction_start (stmt ))
821
832
{
822
833
/* Append GUCs after BEGIN command to include them in transaction body */
834
+ Assert (chan -> buf [chan -> rx_pos - 1 ] == '\0' );
835
+ if (chan -> buf [chan -> rx_pos - 2 ] != ';' )
836
+ {
837
+ chan -> buf [chan -> rx_pos - 1 ] = ';' ;
838
+ chan -> rx_pos += 1 ;
839
+ msg_len += 1 ;
840
+ }
823
841
memcpy (& chan -> buf [chan -> rx_pos - 1 ], chan -> gucs , gucs_len + 1 );
824
842
chan -> in_transaction = true;
825
843
}
@@ -838,49 +856,52 @@ channel_read(Channel* chan)
838
856
chan -> in_transaction = true;
839
857
}
840
858
}
841
- if (chan -> peer == NULL ) /* client is not yet connected to backend */
859
+ msg_start += msg_len ;
860
+ }
861
+ else break ; /* Incomplete message. */
862
+ }
863
+ elog (LOG , "Message size %d" , msg_start );
864
+ if (msg_start != 0 )
865
+ {
866
+ /* Has some complete messages to send to peer */
867
+ if (chan -> peer == NULL ) /* client is not yet connected to backend */
868
+ {
869
+ if (!chan -> client_port )
870
+ {
871
+ /* We are not expecting messages from idle backend. Assume that it some error or shutdown. */
872
+ channel_hangout (chan , "idle" );
873
+ return false;
874
+ }
875
+ client_attach (chan );
876
+ if (handshake ) /* Send handshake response to the client */
842
877
{
843
- if (!chan -> client_port )
844
- {
845
- /* We are not expecting messages from idle backend. Assume that it some error or shutdown. */
846
- channel_hangout (chan , "idle" );
847
- return false;
848
- }
849
- client_attach (chan );
850
- if (handshake ) /* Send handshake response to the client */
878
+ /* If we attach new client to the existed backend, then we need to send handshake response to the client */
879
+ Channel * backend = chan -> peer ;
880
+ chan -> rx_pos = 0 ; /* Skip startup packet */
881
+ if (backend != NULL ) /* Backend was assigned */
851
882
{
852
- /* If we attach new client to the existed backend, then we need to send handshake response to the client */
853
- Channel * backend = chan -> peer ;
854
- Assert (chan -> rx_pos == msg_len && msg_start == 0 );
855
- chan -> rx_pos = 0 ; /* Skip startup packet */
856
- if (backend != NULL ) /* Backend was assigned */
857
- {
858
- Assert (backend -> handshake_response != NULL ); /* backend has already sent handshake responses */
859
- Assert (backend -> handshake_response_size < backend -> buf_size );
860
- memcpy (backend -> buf , backend -> handshake_response , backend -> handshake_response_size );
861
- backend -> rx_pos = backend -> tx_size = backend -> handshake_response_size ;
862
- backend -> backend_is_ready = true;
863
- return channel_write (chan , false);
864
- }
865
- else
866
- {
867
- /* Handshake response will be send to client later when backend is assigned */
868
- return false;
869
- }
883
+ Assert (backend -> handshake_response != NULL ); /* backend has already sent handshake responses */
884
+ Assert (backend -> handshake_response_size < backend -> buf_size );
885
+ memcpy (backend -> buf , backend -> handshake_response , backend -> handshake_response_size );
886
+ backend -> rx_pos = backend -> tx_size = backend -> handshake_response_size ;
887
+ backend -> backend_is_ready = true;
888
+ elog (LOG , "Send handshake response to the client" );
889
+ return channel_write (chan , false);
870
890
}
871
- else if ( chan -> peer == NULL ) /* Backend was not assigned */
891
+ else
872
892
{
873
- chan -> tx_size = response_size ; /* query will be send later once backend is assigned */
893
+ /* Handshake response will be send to client later when backend is assigned */
894
+ elog (LOG , "Handshake response will be sent to the client later when backed is assigned" );
874
895
return false;
875
896
}
876
897
}
877
- msg_start += msg_len ;
898
+ else if (chan -> peer == NULL ) /* Backend was not assigned */
899
+ {
900
+ chan -> tx_size = msg_start ; /* query will be send later once backend is assigned */
901
+ elog (LOG , "Query will be sent to this client later when backed is assigned" );
902
+ return false;
903
+ }
878
904
}
879
- else break ; /* Incomplete message. */
880
- }
881
- if (msg_start != 0 )
882
- {
883
- /* Has some complete messages to send to peer */
884
905
Assert (chan -> tx_pos == 0 );
885
906
Assert (chan -> rx_pos >= msg_start );
886
907
chan -> tx_size = msg_start ;
0 commit comments