delete from product_categories where category IN (select distinct category from product_categories PC LEFT JOIN categories C on PC.category = C.code WHERE code is NULL);
delete PC from product_categories PC left join products P on PC.sku = P.sku where P.sku is NULL;
delete C from categories C left join product_categories PC on C.code = PC.category where C.parent > 0 and PC.category is NULL;
Drupal module authors appear to be oblivious of PostgreSQL as they are using MySQL specific functions all over the place. Most of these problems can be solved by mimic MySQL functions by adding custom PostgreSQL functions. Drupal itself supplies a bunch of routines through the database installation routine.