How to add foreign key in a table where column value can come from two different tables? I have three tables 

create table main_comments (
   id serial primary key,
   comment text not null 
);

create table sub_comments (
   id serial primary key,
   main_id integer not null,
   comment text not null ,
   foreign key (main_id) references main_comments(id) on delete cascade 
); 

create table report (
  id serial primary key ,
  comment_id integer not null ,
  table_type bool ,
  report text not null 
);


in report table comment_id column's value can come from main_comments or sub_comments. so avoiding the confusion i add table_type column which tell from where comment_id value comes from like main or sub table. But now i want to add foreign key to report table.
I would suggest you structure your comments like this,

CREATE TABLE comments (
   comment_id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
   parent_id  int REFERENCES comments,
   comment text NOT NULL
);


Then you three advantages,


you don't have the problem about having to reference sub-comments AND comments
you can go more than one layer deep
the schema between subcomments and comments is in sync


For more information see the postgresql and hierarchy badges.