> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/tposs/6.19/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/tposs/6.19/troubleshooting/database.md).

# Database issues

Database issues may occur during operations on the TPOSS cluster since it extensively uses MySQL. This document provides common tips for troubleshooting known issues.

## MySQL `1406` error - data too long for column

Most of the table columns used by TPOSS to store string-like data are of the [varchar(256)](https://dev.mysql.com/doc/refman/8.0/en/char.html) data type. If you provide too long data, the column size may not be enough. In such a case, the MySQL `1406` error occurs. To solve the issue, increase the size of a column with the following command:

```sql
ALTER TABLE {TABLE-NAME} MODIFY {COLUMN-NAME} VARCHAR( { NEW-SIZE } );
```

## Repair partitions in the CDR table

TPOSS relies on a set of MySQL stored procedures to automatically manage [partition](https://docs.cennso.com/components/tposs/6.19/configuration/charging.md#database-backend) rotation.

Partitions older than the period defined by **cdr.controller.database.partitions_duration** must be dropped, while new partitions should be created in advance so that data can continue to be written without interruption.

Under normal conditions, the database ensures that at least two partitions exist:

- Currently active partition
- Special partition named `future`, based on which new partitions are created

The process of partition rotation can be broken for various reasons. If you notice that new partitions stop being created and only a single active partition named `future` remains, the database will write data into this partition without any further deletion. Over time, this may lead to the database running out of disk space.

To check the current state of the CDR table partitions, run the following SQL command:

```sql
show create table cdr \G
```

Under normal conditions, the output should include multiple time-based partitions followed by the `future` partition, for example:

```sql
CREATE TABLE `cdrs_wrong_partitions_backup_20251210_123039` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  ...
  ...
  ...
  `rating_group` int DEFAULT NULL,
  PRIMARY KEY (`id`,`inserted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3
/*!50500 PARTITION BY RANGE (to_seconds(`inserted_at`))
(PARTITION p20251210133029 VALUES LESS THAN (63932592629) ENGINE = InnoDB,
 PARTITION p20251210143029 VALUES LESS THAN (63932596229) ENGINE = InnoDB,
 ...
 ...
 ...
 PARTITION p20251211123029 VALUES LESS THAN (63932675429) ENGINE = InnoDB,
 PARTITION future VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */ |
```

If the only partition present is `future` (the last one in the example above), the partition layout is broken and must be repaired.

To repair partitions, follow the steps below:

1. Switch to the TPOSS database where you have to fix the partitions:

    ```sql
    use {DATABASE_NAME};
    ```

2. Execute the SQL commands below, replacing `INTERVAL 1 day` in **lines 3 and 4** with the value defined by **cdr.controller.database.partitions_duration**:

    ```sql
    SELECT utc_timestamp() INTO @currtime;

    SET @partition_time := TO_SECONDS(@currtime + INTERVAL 1 day);
    SET @partition_name := REPLACE(@currtime + INTERVAL 1 day, ' ', '');
    SET @partition_name := REPLACE(@partition_name, '-', '');
    SET @partition_name := REPLACE(@partition_name, ':', '');
    ```

    For example, if **cdr.controller.database.partitions_duration** is set to `12-hours`, replace `INTERVAL 1 day` with `INTERVAL 12 hour`. For a complete list of supported interval formats, refer to the official MySQL documentation on [Temporal Intervals](https://dev.mysql.com/doc/refman/8.4/en/expressions.html#temporal-intervals).

3. Execute the SQL commands listed below one by one to create a new `cdr` table with partitions.

    These commands perform the next actions:

    - Create an empty copy of the existing `cdr` table with wrong partitions
    - Build proper partitions on this table
    - Rename the original `cdr` table to `cdrs_wrong_partitions_backup_{UTC_DATE_TIME}` and the copy to `cdr`

    Create a delimiter for a new procedure:

    ```sql
    DELIMITER $$
    ```

    Create a new procedure:

    ```sql
    DROP PROCEDURE IF EXISTS backup_partitioned_cdr_table $$

    CREATE PROCEDURE backup_partitioned_cdr_table(name varchar(255))
    BEGIN
      SET @sql1 = CONCAT('CREATE TABLE IF NOT EXISTS `', name, '` LIKE `cdr`');
      PREPARE stmt1 FROM @sql1;
      EXECUTE stmt1;
      DEALLOCATE PREPARE stmt1;
     
      SET @sql2 = CONCAT('ALTER TABLE `', name, '` REMOVE PARTITIONING');
      PREPARE stmt2 FROM @sql2;
      EXECUTE stmt2;
      DEALLOCATE PREPARE stmt2;
    END $$
    ```

    Switch back to the default delimiter:

    ```sql
    DELIMITER ;
    ```

    Reorganize partitions:

    ```sql
    BEGIN;
    
    CALL backup_partitioned_cdr_table('fresh_cdr_table');

    SET @sql = CONCAT(
      'ALTER TABLE fresh_cdr_table ',
      'PARTITION BY RANGE (TO_SECONDS(inserted_at)) (',
      'PARTITION p', @partition_name, ' VALUES LESS THAN (', @partition_time, '), ',
      'PARTITION future VALUES LESS THAN MAXVALUE )'
    );

    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

    SET @backup_table := CONCAT('cdrs_wrong_partitions_backup_', REPLACE(DATE_FORMAT(@currtime, '%Y%m%d_%H%i%s'), ' ', '_'));
    SET @sql := CONCAT('RENAME TABLE cdr TO ', @backup_table, ', fresh_cdr_table TO cdr');

    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

    COMMIT;
    ```

After completing these steps, the new `cdr` table should contain exactly two partitions:

- Currently active partition
- The `future` partition

All additional partitions should be created automatically by the database within the next few minutes.

> [!IMPORTANT]
> You should verify within the next hour that all expected partitions have been created correctly. If partitions are still missing, manually execute: 
>
> ```sql
> CALL reorganize_partitions(NUMBER_OF_NEW_PARTITIONS, 'time-unit', 'time-value');
> ```
>
> Use the `time-unit` and `time-value` from **cdr.controller.database.partitions_duration**. If you are not sure about the parameters, you can take them from the output of this query:
>
> ```sql
> SELECT EVENT_DEFINITION FROM information_schema.events WHERE EVENT_NAME = 'reorganize_partitions';
> ```
>
> If the procedure fails, submit a support ticket and include the full error output from this call and steps you executed.

You should delete the original table, named `cdrs_wrong_partitions_backup_{UTC_DATE_TIME}`, once its data-retention period has expired. You can do it using the next command:

```sql
DROP TABLE cdrs_wrong_partitions_backup_{UTC_DATE_TIME};
```
