Tuesday, July 3, 2012

Postgresql 9.x to 8.x restore issue with bytea field contains hex values

Error:
  • Warning: Invalid argument supplied for foreach() in list_themes() (line 780 of /var/www/html/drupal/includes/theme.inc).
  • Warning: Invalid argument supplied for foreach() in system_region_list() (line 2709 of /var/www/html/drupal/modules/system/system.module).
  • PHP Notice:  unserialize(): Error at offset 0 of 1009 bytes in /var/www/html/drupal/includes/cache.inc on line 447
  • PHP Notice:  unserialize(): Error at offset 0 of 296805 bytes in /var/www/html/drupal/includes/cache.inc on line 447
  • PHP Warning:  Invalid argument supplied for foreach() in /var/www/html/drupal/includes/module.inc on line 185
  • PHP Warning:  array_keys() expects parameter 1 to be array, null given in /var/www/html/drupal/includes/module.inc on line 89
  • PHP Notice:  unserialize(): Error at offset 0 of 296805 bytes in /var/www/html/drupal/includes/cache.inc on line 447
  • PHP Warning:  Invalid argument supplied for foreach() in /var/www/html/drupal/includes/module.inc on line 185
  • PHP Warning:  array_keys() expects parameter 1 to be array, null given in /var/www/html/drupal/includes/module.inc on line 89
  • PHP Fatal error:  Call to undefined function system_run_automated_cron() in /var/www/html/drupal/includes/common.inc on line 2614
Reason:
This will occurs when we try to host our project in a new location.  In that time, we take backup of code and database to relocate. This makes the problem.

In the case of PostgreSQL, version 9.x has two output formats called 'hex' and 'escape'. This is the setting to decide the output format of our raw data's stored in database. In case of bytea, the values are in binary format and that should be exported properly to understand by others (either it may be PHP or PostgreSQL server).

Restoring in same PostgreSQL version is not a problem. If it is lower version compared to source, then it makes issue. Because the 'hex' format used by 9.x can't understand by lower versions some time. So these should be exported properly.

Solution:
Note : Do this in server machine from where you going to take PostgreSQL backup.


The output format can be set by editing "postgresql.conf" file located in PostgreSQL installation directory. In that, add the line like,
bytea_output = 'escape'
 and restart the PostgreSQL service once.

This will give us the escaped output of bytea data's while backing up as well as querying from programming languages like PHP.

Now take the backup and restore it in lower version as follows

For plain backup use console import using the command '\i' and 
for tar backup user higher version of PG ADMIN to restore it.

For Drupal applications,
Clear the cache tables to remove the existing cached values by executing the following,
truncate cache;
truncate cache_block;
truncate cache_bootstrap;
truncate cache_field;
truncate cache_filter;
truncate cache_form;
truncate cache_image;
truncate cache_menu;
truncate cache_page;
truncate cache_path;
truncate cache_update;
truncate cache_views;
truncate cache_views_data;
Additional workaround:
Even if it is not working then do the following. This is optional only not mandatory one.
Edit the sites/default/settings.php and make the setting as 
$update_free_access = TRUE;
and then run following the commands in console like
$ cd /var/www/drupal/
$ php update.php
$ php cron.php
and run the application again. Still error persist, then repeat the steps from scratch once.

Note: This is tested under Ubuntu 10.04 and Cent OS 6.

No comments:

Post a Comment