Occ files:scan -all produces PHP Fatal error ends with segmentation error

Nextcloud version (eg, 18.0.2): 19.0.3
Operating system and version (eg, Ubuntu 20.04):Linux 4.14
Apache or nginx version (eg, Apache 2.4.25): Apache 2.4.39
PHP version (eg, 7.1): 7.2.26

The issue you are facing:
Unable to complete occ files:scan -all it produces the following error

(full log here https://pastebin.pl/view/e2c7fc10)

Starting scan for user 1 out of 15 (a*)
Starting scan for user 2 out of 15 (a*)
Starting scan for user 3 out of 15 (a*)
Starting scan for user 4 out of 15 (A*)
Starting scan for user 5 out of 15 (c*)
PHP Fatal error:  Method Doctrine\DBAL\Query\Expression\CompositeExpression::__toString() must not throw an exception, caught Error: Maximum function nesting level of '256' reached, aborting! in /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/DB/QueryBuilder/CompositeExpression.php on line 0
PHP Stack trace:
PHP   1. {main}() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/occ:0
PHP   2. require_once() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/occ:11
PHP   3. OC\Console\Application->run() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/console.php:100
PHP   4. Symfony\Component\Console\Application->run() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Console/Application.php:215
PHP   5. Symfony\Component\Console\Application->doRun() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/symfony/console/Application.php:148
PHP   6. Symfony\Component\Console\Application->doRunCommand() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/symfony/console/Application.php:272
PHP   7. OCA\Files\Command\Scan->run() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/symfony/console/Application.php:1012
PHP   8. OCA\Files\Command\Scan->run() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/core/Command/Base.php:169
PHP   9. OCA\Files\Command\Scan->execute() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/symfony/console/Command/Command.php:255
PHP  10. OCA\Files\Command\Scan->scanFiles() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/apps/files/lib/Command/Scan.php:207
PHP  11. OC\Files\Utils\Scanner->scan() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/apps/files/lib/Command/Scan.php:151
PHP  12. OC\Files\Cache\Scanner->scan() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Utils/Scanner.php:260
PHP  13. OC\Files\Cache\Scanner->scanChildren() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Scanner.php:340
PHP  14. OC\Files\Cache\Scanner->scanChildren() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Scanner.php:391
PHP  15. OC\Files\Cache\Scanner->scanChildren() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Scanner.php:391
PHP  16. OC\Files\Cache\Scanner->scanChildren() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Scanner.php:391
PHP  17. OC\Files\Cache\Scanner->scanChildren() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Scanner.php:391
.
.
(message repeats all the way until below)
.
.
PHP 239. OC\Files\Cache\Scanner->scanChildren() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Scanner.php:391
PHP 240. OC\Files\Cache\Scanner->scanChildren() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Scanner.php:391
PHP 241. OC\Files\Cache\Scanner->handleChildren() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Scanner.php:388
PHP 242. OC\Files\Cache\Scanner->scanFile() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Scanner.php:423
PHP 243. OC\Files\Cache\Scanner->addToCache() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Scanner.php:224
PHP 244. OC\Files\Cache\HomeCache->insert() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Scanner.php:294
PHP 245. OC\Files\Cache\HomeCache->update() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Cache.php:321
PHP 246. OC\Files\Cache\CacheQueryBuilder->execute() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/Files/Cache/Cache.php:363
PHP 247. Doctrine\DBAL\Query\QueryBuilder->execute() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/DB/QueryBuilder/QueryBuilder.php:216
PHP 248. Doctrine\DBAL\Query\QueryBuilder->getSQL() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Query/QueryBuilder.php:203
PHP 249. Doctrine\DBAL\Query\QueryBuilder->getSQLForUpdate() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Query/QueryBuilder.php:233
PHP 250. Doctrine\DBAL\Query\Expression\CompositeExpression->__toString() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Query/QueryBuilder.php:1193
PHP 251. implode() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php:108
PHP 252. OC\DB\QueryBuilder\CompositeExpression->__toString() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php:108
PHP 253. Doctrine\DBAL\Query\Expression\CompositeExpression->__toString() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/lib/private/DB/QueryBuilder/CompositeExpression.php:91
PHP 254. implode() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php:108
PHP 255. OC\DB\QueryBuilder\CompositeExpression->__toString() /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/doctrine/dbal/lib/Doctrine/DBAL/Query/Expression/CompositeExpression.php:108
Segmentation fault

Is this the first time you’ve seen this error? (Y/N): Y

Steps to replicate it:
inside terminal run ./occ files:scan -all

The output of your Nextcloud log in Admin > Logging:

Error	PHP	Cannot modify header information - headers already sent by (output started at /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/sabre/http/lib/Sapi.php:132) at /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/apps/dav/lib/Connector/Sabre/File.php#683		2020-09-11T15:48:19+0800
Error	PHP	Cannot modify header information - headers already sent by (output started at /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/sabre/http/lib/Sapi.php:132) at /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/apps/dav/lib/Connector/Sabre/File.php#683		2020-09-11T15:48:19+0800
Error	PHP	Cannot modify header information - headers already sent by (output started at /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/sabre/http/lib/Sapi.php:132) at /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/apps/dav/lib/Connector/Sabre/File.php#683		2020-09-11T15:48:19+0800
Error	PHP	Cannot modify header information - headers already sent by (output started at /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/3rdparty/sabre/http/lib/Sapi.php:132) at /share/CACHEDEV2_DATA/.qpkg/NextCloud/nextcloud/apps/dav/lib/Connector/Sabre/File.php#683		2020-09-11T15:47:25+0800
.
.
.
keeps going on...

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

    <?php
    $CONFIG = array (
      'instanceid' => '******',
      'passwordsalt' => '****',
      'secret' => '****',
      'trusted_domains' =>
      array (
        0 => '****',
        1 => '****',
        2 => '***',
        3 => '****',
        4 => '***',
      ),
      'trusted_proxies' =>
      array (
        0 => '***8',
        1 => '***8',
      ),
      'forwarded_for_headers' =>
      array (
        0 => 'HTTP_X_FORWARDED_FOR',
      ),
      'datadirectory' => '/*******',
      'overwrite.cli.url' => 'https://****/',
      'htaccess.RewriteBase' => '/',
      'dbtype' => 'pgsql',
      'version' => '19.0.3.1',
      'dbname' => 'nextcloud',
      'dbhost' => 'localhost',
      'dbport' => '',
      'dbtableprefix' => 'oc_',
      'dbuser' => '****',
      'dbpassword' => '****',
      'logtimezone' => 'Asia/Singapore',
      'installed' => true,
      'filelocking.enabled' => true,
      'memcache.local' => '\\OC\\Memcache\\APCu',
      'memcache.locking' => '\\OC\\Memcache\\Redis',
      'redis' =>
      array (
        'host' => '127.0.0.1',
        'port' => 6379,
        'timeout' => 0.0,
      ),
      'mail_from_address' => 'server',
      'mail_smtpmode' => 'smtp',
      'mail_domain' => '****om',
      'theme' => '',
      'log_type' => 'owncloud',
      'loglevel' => 3,
      'logfile' => '/opt/NextCloud/nextcloud/data/nextcloud.log',
      'maintenance' => false,
      'mail_smtphost' => '****.com',
      'mail_smtpport' => '465',
      'updater.release.channel' => 'stable',
      'app.mail.imap.timeout' => 60,
      'app.mail.smtp.timeout' => 60,
      'app.mail.smtplog.enabled' => true,
      'mail_smtpauth' => 1,
      'mail_smtpname' => '****.com',
      'mail_smtppassword' => '****',
      'onlyoffice' =>
      array (
        'verify_peer_off' => true,
      ),
      'app_install_overwrite' =>
      array (
        0 => 'activitylog',
        1 => 'sharerenamer',
      ),
      'mail_sendmailmode' => 'smtp',
      'updater.secret' => '****',
      'mail_smtpsecure' => 'ssl',
      'mail_smtpauthtype' => '****',
    );

Has the user in question a folder with many subfolders?

Is the xdebug extension for PHP loaded? Mind to turn it off and try again?

yes many, plenty sub folders.

turning off xdebug did the trick. I didn’t know it was on by default!

For the others, i turned it off by commenting out
;zend_extension=xdebug.so

inside php.ini