DBRepair development

Here we go. This is what it looks like.

Anything obvious before I share the TAR file for folks to kick it around?

root@lizum:/usb/plex/PMS-save/Plug-in Support/Databases/databases.2025.05.17# ./DBRepair.sh --sqlite /usr/lib/plexmediaserver/ --databases "$(pwd)" deflate auto exit
 
 
 
      Database Repair Utility for Plex Media Server  (User Defined)
                       Version v1.12.00
 
[2025-10-04 13.29.10]       PlexSQLite = '/usr/lib/plexmediaserver//Plex SQLite'
[2025-10-04 13.29.10]       Databases  = '/usb/plex/PMS-save/Plug-in Support/Databases/databases.2025.05.17'
 
[2025-10-04 13.29.10] Check and Deflate started.
[2025-10-04 13.29.10] 
[2025-10-04 13.29.10] Checking the PMS databases
[2025-10-04 13.38.46] Check complete.  PMS main database is OK.
[2025-10-04 13.38.46] Check complete.  PMS blobs database is OK.
[2025-10-04 13.38.46] 
[2025-10-04 13.38.46] Backup current databases with '-BACKUP-2025-10-04_13.38.46' timestamp.
[2025-10-04 13.38.59] Starting Deflate.  (There will be no output until complete.)
[2025-10-04 13.40.08] PMS main database successfully repaired.
[2025-10-04 13.40.08] Reducing main database size.
[2025-10-04 13.40.09] PMS main database size reduced.
[2025-10-04 13.40.09] Verifying PMS main database.
[2025-10-04 13.40.10] Verification complete.  PMS main database is OK.
[2025-10-04 13.40.10] PMS main database reduced from 31586 MB to 206 MB (153 %)
[2025-10-04 13.40.10] PMS main database deflate completed.
[2025-10-04 13.40.10] Saving current main database with '-BLOATED-2025-10-04_13.38.46'
[2025-10-04 13.40.10] Making deflated database active
[2025-10-04 13.40.10] Deflate complete. Please check your library settings and contents for completeness.
[2025-10-04 13.40.10] Deflate successful.
[2025-10-04 13.40.10] Recommend running Auto next to complete optimization of new database.
 
[2025-10-04 13.40.10] Automatic Check,Repair,Index started.
[2025-10-04 13.40.10] 
[2025-10-04 13.40.10] Checking the PMS databases
[2025-10-04 13.40.11] Check complete.  PMS main database is OK.
[2025-10-04 13.40.11] Check complete.  PMS blobs database is OK.
[2025-10-04 13.40.11] 
[2025-10-04 13.40.11] Exporting current databases using timestamp: 2025-10-04_13.40.10
[2025-10-04 13.40.11] Exporting Main DB
[2025-10-04 13.40.14] Exporting Blobs DB
[2025-10-04 13.40.32] Successfully exported the main and blobs databases.
[2025-10-04 13.40.32] Start importing into new databases.
[2025-10-04 13.40.32] Importing Main DB.
[2025-10-04 13.40.38] Importing Blobs DB.
[2025-10-04 13.40.40] Successfully imported databases.
[2025-10-04 13.40.40] Verifying databases integrity after importing.
[2025-10-04 13.40.41] Verification complete.  PMS main database is OK.
[2025-10-04 13.40.41] Verification complete.  PMS blobs database is OK.
[2025-10-04 13.40.41] Saving current databases with '-BACKUP-2025-10-04_13.40.10'
[2025-10-04 13.40.41] Making repaired databases active
[2025-10-04 13.40.41] Repair complete. Please check your library settings and contents for completeness.
[2025-10-04 13.40.41] Recommend:  Scan Files and Refresh all metadata for each library section.
[2025-10-04 13.40.41] 
[2025-10-04 13.40.41] Backing up of databases
[2025-10-04 13.40.41] Backup current databases with '-BACKUP-2025-10-04_13.40.41' timestamp.
[2025-10-04 13.40.41] Reindexing main database
[2025-10-04 13.40.43] Reindexing main database successful.
[2025-10-04 13.40.43] Reindexing blobs database
[2025-10-04 13.40.43] Reindexing blobs database successful.
[2025-10-04 13.40.43] Reindex complete.
[2025-10-04 13.40.43] Automatic Check, Repair/optimize, & Index successful.
 
root@lizum:/usb/plex/PMS-save/Plug-in Support/Databases/databases.2025.05.17# 

PS: bug found. ā€œ153 %ā€ should be ā€œ15300 %ā€ (shell math error)

1 Like

Or you could switch from percent to multiples.

PMS main database reduced from 31586 MB to 206 MB (153x)

Or maybe it’s enough to just give the before and after size, lol :slight_smile:

All:

For your consideration , DBRepair v1.12.00 ā€œalpha 1ā€

Deflate works but would like to build more confidence.
– Alpha removed–

Would appreciate feedback. I can provide a 31GB sample DB if needed

ALL:

Below please find DBRepair v1.12.00

Specific to v1.12.00

  1. Deflate capability - Deflate a bloated PMS main database and retain bandwidth statistics.
  • Recommended usage: ā€œDeflateā€ followed by ā€œAutoā€ to optimize the DB after reduction.

  • Method changed to reconstruction of bandwidth table versus selective copy.
    This results in correct operation of bandwidth statistics functionality
    as well as significantly improved performance.

  1. Purge/Prune merged - Purge and Prune commands have been merged. This was done because most folks will run both when cleaning up. This merge simplifies operation.
  • Internally, the code will silently support both commands as one for a few more releases.

As always, your comments greatly appreciated.

DBRepair-v1.12.00-beta.tar (170 KB)

1 Like

No issues on M1 macOS 15.7.1 or QNAP x64 QTS-4.5.4.2957.
Thanks!

@nibbles

Did you try the deflate function?

Does it still show bandwidth data as it should?

Yes I did use deflate on both. Both showed no errors. The QNAP run reported this:

Check and Deflate started.

Checking the PMS databases
Check complete.  PMS main database is OK.
Check complete.  PMS blobs database is OK.

Backup current databases with '-BACKUP-2025-10-18_18.56.40' timestamp.
Starting Deflate.  (There will be no output until complete.)
PMS main database successfully repaired.
Reducing main database size.
PMS main database size reduced.
Verifying PMS main database.
Verification complete.  PMS main database is OK.
PMS main database reduced from 122 MB to 120 MB
PMS main database deflate completed.
Saving current main database with '-BLOATED-2025-10-18_18.56.40'
Making deflated database active
Deflate complete. Please check your library settings and contents for completeness.
Deflate successful.
Recommend running Auto next to complete optimization of new database.

The Mac reported this:

Check and Deflate started.

Checking the PMS databases
Check complete.  PMS main database is OK.
Check complete.  PMS blobs database is OK.

Backup current databases with '-BACKUP-2025-10-18_18.48.59' timestamp.
Starting Deflate.  (There will be no output until complete.)
PMS main database successfully repaired.
Reducing main database size.
PMS main database size reduced.
Verifying PMS main database.
Verification complete.  PMS main database is OK.
PMS main database reduced from 1 MB to 1 MB
PMS main database deflate completed.
Saving current main database with '-BLOATED-2025-10-18_18.48.59'
Making deflated database active
Deflate complete. Please check your library settings and contents for completeness.
Deflate successful.
Recommend running Auto next to complete optimization of new database.

Reading the source code I see it would have output an error in case of bandwidth issues,
Output "Error: Could not correct statistics_bandwidth table (error $Result)".
which neither OS did. So I figure this means success. :+1:

No problems on QNAP TS-664 QTS 5.2.7.3256

Enter command # -or- command name (4 char min) : 23
 
Check and Deflate started.

Checking the PMS databases
Check complete.  PMS main database is OK.
Check complete.  PMS blobs database is OK.

Backup current databases with '-BACKUP-2025-10-19_10.15.40' timestamp.
Starting Deflate.  (There will be no output until complete.)
PMS main database successfully repaired.
Reducing main database size.
PMS main database size reduced.
Verifying PMS main database.
Verification complete.  PMS main database is OK.
PMS main database reduced from 200 MB to 200 MB
PMS main database deflate completed.
Saving current main database with '-BLOATED-2025-10-19_10.15.40'
Making deflated database active
Deflate complete. Please check your library settings and contents for completeness.
Deflate successful.
Recommend running Auto next to complete optimization of new database.

btw, my DB was not inflated to begin with.

If the database isn’t bloated (i.e. the previous post reported PMS main database reduced from 200 MB to 200 MB) shouldn’t the process be skipped, even on a marginally bloated database?

How would it know to skip without going through the process (genuine question)?

Maybe copy the database to a temp dir and run the process on the temp database and report similar to the prune report (Counting how many files can be removed. OK to prune 906 files? (Y/N) ? )

I’m not following your logic; the user has already asked for it to be done (by selecting option 23), so why ask again adding unnecessary complexity?

[edited for typos]

How do I know if my database is bloated? What’s the point of running the process if it isn’t?

I didn’t know if mine was bloated or not, so I ran the option.

My logic is, if you were able to know beforehand that it wasn’t bloated, you wouldn’t need to run the option :grinning_face_with_smiling_eyes:

All,
This is a good point. So the question is: When should ā€œAutoā€ run ā€œDeflateā€ on its own?

In my experience with this problem, I’ve seen:

  1. Some users have a lot of media with 600-700 MB databases
  2. Bloated databases usually starting over 1GB up through the 1TB range but there are some smaller media library users with bloat at 500 MB.

Would it therefore make sense for ā€œautoā€ to perform a deflate pass by default if the DB size is greater than 500 MB as a precaution ?

-or-

Would it make sense for ā€œautoā€ to perform a deflate pass if the number of media items is low (as compared to the DB size). By this I mean 25,000 media items and 200 MB are normal. If the DB size and the number of media items in the DB isn’t within that range, then automatically perform the deflate ?

I’ve been struggling with how to incorporate Deflate into Auto since I started this work.

In one of my test DBs,

  1. 49091 - media items
  2. 31 GB - original DB size
  3. 207 MB - final DB size

Ratios: PMS normally has 4K bytes per media item

I think the logic should be (but VERY MUCH would like input for this)

  1. If DB size > 2GB – do deflate
  2. If number of media items * 4K < Current size – do deflate

Everyone’s thoughts?
(these are just example / possible thresholds. Whatever works best will be used)

I appreciate all the due consideration being given here. Thanks!

Personally I like #2 as logic for an auto function. Having some aspect of a ratio calculation rather than using a hard threshold seems like the way to go and the #2 option feels like a good way to check against a ā€œnormalā€ expectation for a DB.

@Insomnic_1

I like using the ratio as well.

I want to include the hard threshold because when we see a 300+ GB database, we already know there’s a bloat problem so it makes no sense to see what the ratio is.

Here’s the logic I have so far. This will be part of ā€œAutoā€ :slight_smile:

        fi

        # See if Deflate needed
        DBSize="$(stat $STATFMT $STATBYTES "${DBDIR}/$CPPL.db")"
        DBSize="$(($DBSize / 1000000))"  # Size in MB

        DeflateNeeded=0
        # If size > 1 GB, or ratio not 4K:1 then force the check
        if [ $DBSize -lt 1000 ]; then
         Items="$("$PLEX_SQLITE" "$CPPL.db" 'select count(*) from media_parts;')"
         [ $(($Items \* 4000)) -lt $DBSize ] && DeflateNeeded=1

        else
          # DBSize is large ( > 2 GB )
          [ $DBSize -gt 2000 ] && DeflateNeeded=1
        fi

        # Is deflate needed

The only value to be determined is that hard size threshold
Is 2GB a good number ?

1 Like

I also like the ratio but, if 4k the average, perhaps trigger at 6k?

Triggering at 6KB is fine too.

It’s definitely something easily adjusted.

I did just restructure the trigger logic to be more legible
This block shows:

  1. If the DB > 1.5 GB then force the deflate pass
  2. If the DB size is more than 6K:1 then force the deflate pass

This seem to make more sense with automatic ?


        # See if Deflate needed
        DBSize="$(stat $STATFMT $STATBYTES "${DBDIR}/$CPPL.db")"
        DBSize="$(($DBSize / 1000000))"  # Size in MB
        NeedDeflate=0

        # If size > 1.5 GB, or ratio not 6K:1 then force the check
        if [ $DBSize -gt 1500 ]; then
          NeedDeflate=1

        else
         Items="$("$PLEX_SQLITE" "$CPPL.db" 'select count(*) from media_parts;')"
        [ $DBSize -ge $(($Items * 6000)) ] && NeedDeflate=1

        fi

        # Is deflate needed
        if [$NeedDeflate -eq 1 ]; then
        
2 Likes