import re


def get_query_desc_ptr(backtrace):
    """
    >>> get_query_desc_ptr(["#21 standard_ExecutorRun (queryDesc=0x562aad0b46f8, direction=<optimized out>,"])
    '0x562aad0b46f8'
    >>> get_query_desc_ptr(["#1  pgss_ExecutorRun (queryDesc=0x562aad0b46f8, direction=<optimized out>,"])
    '0x562aad0b46f8'
    """
    query_desc_ptr_regexp = re.compile(r"ExecutorRun \(queryDesc=(0x[0-9a-f]+)[,\)]")
    # ExecutorRun can appear more than once in backtrace when running PL/pgSQL code. Return the deepest one.
    for frame in backtrace:
        m = query_desc_ptr_regexp.search(frame)
        if m:
            return m.group(1)
    return None