The following code generates the error "Stack Overflow: try tail recursion?" 

The code reads in bibliographic data using OAI-PMH and updates a database for each chunk of data. With OAI-PMH, only part of the data is available for each request, so the server returns a resumption token if there are more data available. 

The xquery function making the queries is implemented recursively preceded by a database update request (see the last two lines) for each call. Is it db:add() that causes the stack overflow? The recursion cannot be placed further towards the end!

declare %updating function local:getResumption($token) {
     if (empty($token)) then 
        ()
     else
        let $http-request := http:send-request($http-option, $URL || $token)
        let $result := 
            if ($http-request instance of node()) then
             $http-request
           else
             <http-err>{$http-request}</http-err>
        
        let $resume := $result//oai:resumptionToken/text()
        return  (
            db:add($database,element chunk {$result//oai:metadata}, $path) , 
            local:getResumption($resume)
        )
 };

Best,
Lars