xo / usql

Universal command-line interface for SQL databases
MIT License
8.88k stars 347 forks source link

Edge case: metadata reader, postgres with installed debugger, pg_class has entries with relkind = 'c' #356

Closed Allam76 closed 2 years ago

Allam76 commented 2 years ago

Hello and thanks for a great piece of software!

I have an edge case for the metadata reader for postgres.

When the debugger is installed, postgres adds some entries in the pg_class table with relkind = 'c'. This is not supported in the metadata table reader query as the type becomes null whereas the scan expects string. This of course only happens when the filter for type is empty.

Workaround is to set the type filter.

Probably a good idea to add a where c.reltype <> 'c' to the query.

nineinchnick commented 2 years ago

Thanks for the report! Can you include a little bit more info on how to reproduce this? Here are a couple questions:

  1. How to install that debugger? Can you link any references to it?
  2. Which commands you run in usql that fail?
  3. Do you get an error in usql or does it silently fail? Can you attach the errors?
  4. If you checked that the query fails in the database logs, can you attach these?

When working on that metadata reader I was copying the queries from psql but I don't remember if I skipped any conditions. We'll probably add what you suggested but I need to be able to reproduce the issue to verify any fixes.

Allam76 commented 2 years ago

1) How to install the debugger: https://qtibia.com/how-to-install-the-pl-pgsql-debugger-and-use-it-in-pgadmin/

2) Programatically, note no type filter: image

3) Error in scan of result set: sql: Scan error on column index 2, name "Type": converting NULL to string is unsupported This is rather obvious since the database table looks like: image and case has no handling of c:

    WHEN 'r' THEN 'table' 
    WHEN 'v' THEN 'view' 
    WHEN 'm' THEN 'materialized view' 
    WHEN 'i' THEN 'index' 
    WHEN 'S' THEN 'sequence' 
    WHEN 's' THEN 'special' 
    WHEN 'f' THEN 'foreign table' 
    WHEN 'p' THEN 'partitioned table' 
    WHEN 'I' THEN 'partitioned index' 
END as "Type"

This could be fixed with either relkind <> 'c' in where or "Type::text" type cast.

4) The database is fine, the error is in the usql api.

Cheers Martin