@@ -116,27 +116,37 @@ confirm_query_canceled_impl(int line, PGconn *conn)
116116
117117/*
118118 * Using monitorConn, query pg_stat_activity to see that the connection with
119- * the given PID is in the given state. We never stop until it does.
119+ * the given PID is either in the given state, or waiting on the given event
120+ * (only one of them can be given).
120121 */
121122static void
122- wait_for_connection_state (int line , PGconn * monitorConn , int procpid , char * state )
123+ wait_for_connection_state (int line , PGconn * monitorConn , int procpid ,
124+ char * state , char * event )
123125{
124126 const Oid paramTypes [] = {INT4OID , TEXTOID };
125127 const char * paramValues [2 ];
126128 char * pidstr = psprintf ("%d" , procpid );
127129
130+ Assert ((state == NULL ) ^ (event == NULL ));
131+
128132 paramValues [0 ] = pidstr ;
129- paramValues [1 ] = state ;
133+ paramValues [1 ] = state ? state : event ;
130134
131135 while (true)
132136 {
133137 PGresult * res ;
134138 char * value ;
135139
136- res = PQexecParams (monitorConn ,
137- "SELECT count(*) FROM pg_stat_activity WHERE "
138- "pid = $1 AND state = $2" ,
139- 2 , paramTypes , paramValues , NULL , NULL , 1 );
140+ if (state != NULL )
141+ res = PQexecParams (monitorConn ,
142+ "SELECT count(*) FROM pg_stat_activity WHERE "
143+ "pid = $1 AND state = $2" ,
144+ 2 , paramTypes , paramValues , NULL , NULL , 0 );
145+ else
146+ res = PQexecParams (monitorConn ,
147+ "SELECT count(*) FROM pg_stat_activity WHERE "
148+ "pid = $1 AND wait_event = $2" ,
149+ 2 , paramTypes , paramValues , NULL , NULL , 0 );
140150
141151 if (PQresultStatus (res ) != PGRES_TUPLES_OK )
142152 pg_fatal_impl (line , "could not query pg_stat_activity: %s" , PQerrorMessage (monitorConn ));
@@ -145,7 +155,7 @@ wait_for_connection_state(int line, PGconn *monitorConn, int procpid, char *stat
145155 if (PQnfields (res ) != 1 )
146156 pg_fatal_impl (line , "unexpected number of columns received: %d" , PQnfields (res ));
147157 value = PQgetvalue (res , 0 , 0 );
148- if (value [ 0 ] != '0' )
158+ if (strcmp ( value , "0" ) != 0 )
149159 {
150160 PQclear (res );
151161 break ;
@@ -172,7 +182,7 @@ send_cancellable_query_impl(int line, PGconn *conn, PGconn *monitorConn)
172182 * connection below is reliable, instead of possibly seeing an outdated
173183 * state.
174184 */
175- wait_for_connection_state (line , monitorConn , PQbackendPID (conn ), "idle" );
185+ wait_for_connection_state (line , monitorConn , PQbackendPID (conn ), "idle" , NULL );
176186
177187 env_wait = getenv ("PG_TEST_TIMEOUT_DEFAULT" );
178188 if (env_wait == NULL )
@@ -183,10 +193,10 @@ send_cancellable_query_impl(int line, PGconn *conn, PGconn *monitorConn)
183193 pg_fatal_impl (line , "failed to send query: %s" , PQerrorMessage (conn ));
184194
185195 /*
186- * Wait for the query to start , because if the query is not running yet
187- * the cancel request that we send won't have any effect.
196+ * Wait for the sleep to be active , because if the query is not running
197+ * yet, the cancel request that we send won't have any effect.
188198 */
189- wait_for_connection_state (line , monitorConn , PQbackendPID (conn ), "active " );
199+ wait_for_connection_state (line , monitorConn , PQbackendPID (conn ), NULL , "PgSleep " );
190200}
191201
192202/*
@@ -2098,10 +2108,7 @@ usage(const char *progname)
20982108static void
20992109print_test_list (void )
21002110{
2101- #if 0
2102- /* Commented out until further stabilized */
21032111 printf ("cancel\n" );
2104- #endif
21052112 printf ("disallowed_in_pipeline\n" );
21062113 printf ("multi_pipelines\n" );
21072114 printf ("nosync\n" );
0 commit comments