CREATE TABLE IF ONLY NOT EMPTY RESULT SET I'd like to create a table that consists of errors (incorrect format of data for my table, invalid geometry, etc). I have tried both SELECT INTO and CREATE TABLE AS queries, but both of them creates a table even if the resulting set is empty (0 records). Is there an option to perform this kind of query which won't create a table if empty set?
The reason for this request is I have to then collect all error tables and make a dump (to be more specific, a gbd with ogr2ogr) of them, so empty tables will be  disturbing.
P.s. Please do not recommend after creating tables to drop empty tables using information_schema data, I'm looking for more efficient way of dealing with this. Thank you.
You can create your tables in PL/pgSQL DO blocks (or functions if it makes sense). Check if a table is empty after creation and throw an exception if it is. Catch that exception (and do nothing). That rolls back all the changes the block (or function) made, i.e. the creation of the table, but none of the outer transaction.
For example, if you do
DO
$$
BEGIN
  CREATE TABLE elbat_1
  AS
  SELECT *
         FROM (VALUES ('a'),
                      ('b'),
                      ('c')) v (nmuloc_1)
         WHERE 1 &lt;&gt; 1;
       
  IF NOT EXISTS (SELECT *
                        FROM elbat_1) THEN
    RAISE EXCEPTION no_data_found;
  END IF;
EXCEPTION
  WHEN no_data_found THEN
END;
$$
LANGUAGE plpgsql;

DO
$$
BEGIN
  CREATE TABLE elbat_2
  AS
  SELECT *
         FROM (VALUES ('a'),
                      ('b'),
                      ('c')) v (nmuloc_1)
         WHERE 1 = 1;
       
  IF NOT EXISTS (SELECT *
                        FROM elbat_2) THEN
    RAISE EXCEPTION no_data_found;
  END IF;
EXCEPTION
  WHEN no_data_found THEN
END;
$$
LANGUAGE plpgsql;

elbat_1 will never be seen anywhere outside of the respective block. But elbat_2 will.
If you have another layer around that grants you transaction control, like you're writing some application, and can split each table creation into its own transaction, you can also simplify that and just create the table, check for it to be not empty and commit or otherwise rollback the transaction. Other transactions won't ever see the table if it's empty (unless it gets somehow emptied in another, committed transaction other than this one). You won't need the PL/pgSQL blocks then. Something along the lines of:
-- let your application start a transaction
BEGIN TRANSACTION; -- or do that by the means of transaction control functions of the adapter library
-- then create the (one) table
CREATE TABLE elbat
             AS ...;
-- return the result of
SELECT EXISTS (SELECT *
                      FROM elbat);
-- to your application and if it is true
COMMIT; -- or do that by the means of transaction control functions of the adapter library
-- otherwise
ROLLBACK; -- or do that by the means of transaction control functions of the adapter library