cpostgresqlplpgsqlnotifylisten

Making postgres do a blocking wait


I thought it might be interesting (and useful) to create a blocking wait() that could be used from a plpgsql function. I got it working, but I am not certain that it is well conceived. One interesting issue occurs when using datagrip. If I call my pg_wait() from a function:

select util.wait_test_func() into var   --this calls my pg_wait()

The function will properly block until I issue a NOTIFY in a different query console window. In fact, if the function, wait_test_function() calls my pg_wait() three times, it will correctly block three times, and I will have to call NOTIFY three times to allow the function to complete. However, once the function completes, if I call it again, it returns immediately, without blocking. It's almost as if the NOTIFY is still in the queue, but I'm not really convinced that is the problem. If I close the datagrip query console, open a new one, and reissue the function call, it again works as expected, properly blocking. I can repeat this consistently. The first call in a new query console window always properly blocks, but every subsequent call always returns immediately. This is my first C function in postgres, so I am wondering if I am just doing something fundamentally wrong. Thank you for any help. My code follows.

The C function:

#ifdef WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>

#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif

#include "libpq-fe.h"
#include "postgres.h"
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include "fmgr.h"
#include "utils/palloc.h"
#include "utils/elog.h"
#include "storage/bufpage.h"
#include "utils/builtins.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

PG_FUNCTION_INFO_V1( pg_wait );

Datum pg_wait( PG_FUNCTION_ARGS );

Datum pg_wait( PG_FUNCTION_ARGS )
{
    PGconn     *conn;
    PGresult   *res;
    PGnotify   *notify;
    int         nnotifies;
    int         sock;
    fd_set      input_mask;
    
    char* conninfo = text_to_cstring(PG_GETARG_TEXT_PP(0));
    char* channel_name = text_to_cstring(PG_GETARG_TEXT_PP(1));
    
    char strlisten[50];
    strcpy(strlisten,  "LISTEN ");
    strcat(strlisten, channel_name);

    conn = PQconnectdb(conninfo);
    res = PQexec(conn, strlisten);
    PQclear(res);

    sock = PQsocket(conn);
    FD_ZERO(&input_mask);
    FD_SET(sock, &input_mask);
    select(sock + 1, &input_mask, NULL, NULL, NULL);
    PQconsumeInput(conn);
    if ((notify = PQnotifies(conn)) != NULL)
    {
       PQfreemem(notify);
       PQconsumeInput(conn);
    }
    PQfinish(conn);

    PG_RETURN_TEXT_P( PG_GETARG_TEXT_PP(1) );
}

The util.wait_test_func()

create or replace function util.wait_test_func() returns integer
    parallel safe
    language plpgsql
as $$
DECLARE
    signal_name TEXT;
BEGIN
    RAISE NOTICE 'Before wait1';
    SELECT pg_wait('dbname=edw port=5432','CHANNEL1') INTO signal_name;
    RAISE NOTICE 'After wait1: %', signal_name;

    RAISE NOTICE 'Before wait2';
    SELECT pg_wait('dbname=edw port=5432','CHANNEL1') INTO signal_name;
    RAISE NOTICE 'After wait2: %', signal_name;

    RAISE NOTICE 'Before wait3';
    SELECT pg_wait('dbname=edw port=5432','CHANNEL1') INTO signal_name;
    RAISE NOTICE 'After wait3: %', signal_name;

    RETURN 0;
END
$$;

Thank you again for any help you can provide.


Solution

  • Did you try declaring your function VOLATILE