CREATE OR REPLACE FUNCTION is_sql(sql text) returns boolean
language plpgsql
AS
$$
DECLARE
exception_message text;
exception_context text;
BEGIN
BEGIN
EXECUTE E'DO $IS_SQL$ BEGIN
RETURN;
' || trim(trailing E';
' from sql) || E';
END; $IS_SQL$;';
EXCEPTION WHEN syntax_error THEN
GET STACKED DIAGNOSTICS
exception_message = MESSAGE_TEXT,
exception_context = PG_EXCEPTION_CONTEXT;
RAISE NOTICE '%', exception_context;
RAISE NOTICE '%', exception_message;
RETURN FALSE;
END;
RETURN TRUE;
END
$$;
-- TEST
SELECT is_sql('SELECTx'), is_sql('SELECT x');