Skip to content

Conversation

@jzunigax2
Copy link
Contributor

  • Fixes zero-size files that incorrectly have a file_id by setting it to NULL
  • These files were created before the check_fileid_null_only_for_zero_size constraint was added
  • Unblocks users who can't empty trash due to constraint violation on update

Context

The constraint was added with NOT VALID, which skipped existing rows but still validates on any UPDATE. When trash deletion updates these files (setting status = DELETED), the constraint fires and fails.

@jzunigax2 jzunigax2 added the bug Something isn't working label Feb 3, 2026
@jzunigax2 jzunigax2 self-assigned this Feb 3, 2026
@jzunigax2 jzunigax2 requested a review from sg-gs February 3, 2026 22:30
sg-gs
sg-gs previously approved these changes Feb 4, 2026
Copy link
Member

@sg-gs sg-gs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running migration

Copy link
Member

@sg-gs sg-gs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current proposal is a bit better although the cost-per-chunk is still high:

Limit  (cost=1000.00..17632.09 rows=1000 width=4) (actual time=1.081..22.435 rows=1000 loops=1)
  ->  Gather  (cost=1000.00..50191485.23 rows=3017689 width=4) (actual time=1.080..22.346 rows=1000 loops=1)
        Workers Planned: 2
        Workers Launched: 0
        ->  Parallel Seq Scan on files  (cost=0.00..49888716.33 rows=1257370 width=4) (actual time=0.749..21.785 rows=1000 loops=1)
              Filter: ((file_id IS NOT NULL) AND (size = 0))
              Rows Removed by Filter: 3155
Planning Time: 0.167 ms
Execution Time: 22.543 ms

Let's work with:

...
WHERE size = 0 AND file_id IS NOT null AND status != 'DELETED'
...

Which will make the things almost 10x faster in the worst case:

Limit  (cost=0.43..794.94 rows=1000 width=4) (actual time=0.015..0.660 rows=1000 loops=1)
  ->  Index Scan using idx_zero_size_files_per_user on files  (cost=0.43..1506699.21 rows=1896396 width=4) (actual time=0.015..0.602 rows=1000 loops=1)
        Filter: (file_id IS NOT NULL)
Planning Time: 0.176 ms
Execution Time: 0.700 ms

@jzunigax2 jzunigax2 requested a review from sg-gs February 5, 2026 13:47
@jzunigax2 jzunigax2 force-pushed the fix/delete-fileids-from-empty-files branch from ed82765 to 8a27120 Compare February 5, 2026 14:24
@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 5, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready-for-preview

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants