vscode-postgres icon indicating copy to clipboard operation
vscode-postgres copied to clipboard

Error annoyingly keeps popping out the output panel

Open Logerfo opened this issue 5 years ago • 7 comments

[Error - 15:26:34] Request textDocument/completion failed. Message: Request textDocument/completion failed with message: Cannot read property 'attname' of null Code: -32603

Logerfo avatar May 10 '19 18:05 Logerfo

Interesting...

The only places in code completion where attname is accessed as a property is within Array.prototype.forEach loops specifically the looping of columns on a table.

The fact that it's entering these loops, suggests somehow that the column array is non-empty - yet contains a null value, yet the table columns are the result of a json_agg on a join "table" (sub-select really).

Does your database user have limited permissions on tables/fields (as compared to the rest of the database/tables on the same server)? If root or admin user, is it even possible for a table or view to have 0 columns (I have my doubts)?

Borvik avatar May 11 '19 10:05 Borvik

I'll check for everything you asked on Monday. In the meanwhile, can you point me to the SQL that's returning this attname column, so I can test it in my database?

Logerfo avatar May 11 '19 13:05 Logerfo

Certainly. The query gets all the tables and views (permission checked) and joins it to the list of columns for the linked table and view and returns everything in one go.

        SELECT
          tbl.schemaname,
          tbl.tablename,
          tbl.quoted_name,
          tbl.is_table,
          json_agg(a) as columns
        FROM
          (
            SELECT
              schemaname,
              tablename,
              (quote_ident(schemaname) || '.' || quote_ident(tablename)) as quoted_name,
              true as is_table
            FROM
              pg_tables
            WHERE
              schemaname not in ('information_schema', 'pg_catalog', 'pg_toast')
              AND schemaname not like 'pg_temp_%'
              AND schemaname not like 'pg_toast_temp_%'
              AND has_schema_privilege(quote_ident(schemaname), 'CREATE, USAGE') = true
              AND has_table_privilege(quote_ident(schemaname) || '.' || quote_ident(tablename), 'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER') = true
            union all
            SELECT
              schemaname,
              viewname as tablename,
              (quote_ident(schemaname) || '.' || quote_ident(viewname)) as quoted_name,
              false as is_table
            FROM pg_views
            WHERE
              schemaname not in ('information_schema', 'pg_catalog', 'pg_toast')
              AND schemaname not like 'pg_temp_%'
              AND schemaname not like 'pg_toast_temp_%'
              AND has_schema_privilege(quote_ident(schemaname), 'CREATE, USAGE') = true
              AND has_table_privilege(quote_ident(schemaname) || '.' || quote_ident(viewname), 'SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES, TRIGGER') = true
          ) as tbl
          LEFT JOIN (
            SELECT
              attrelid,
              attname,
              format_type(atttypid, atttypmod) as data_type,
              attnum,
              attisdropped
            FROM
              pg_attribute
          ) as a ON (
            a.attrelid = tbl.quoted_name::regclass
            AND a.attnum > 0
            AND NOT a.attisdropped
            AND has_column_privilege(tbl.quoted_name, a.attname, 'SELECT, INSERT, UPDATE, REFERENCES')
          )
        GROUP BY schemaname, tablename, quoted_name, is_table;

Borvik avatar May 11 '19 13:05 Borvik

So I tested everything and there was in fact some tables with zero columns. So yes, it's possible. I guess that probably this should be predicted in the extension code, so one point for you. Also, my tables should actually have some columns, that's not right, so one point for me.

Note that the value of the columns column for those tables is null. That's the default behavior for the function json_agg when the argument is empty. If you remove the a.attnum > 0 condition, it will not return null anymore, but only system attributes will be present in the json.

Perhaps you should discard tables (or views, for that matter) with zero columns.

Logerfo avatar May 13 '19 15:05 Logerfo

Your right we can probably discard table/views with zero columns in the language server - they don't need to be around for intellisense - but I do think they should still be visible in the explorer tree.

Now to build out a test case for this.

Borvik avatar May 15 '19 10:05 Borvik

Your right we can probably discard table/views with zero columns in the language server - they don't need to be around for intellisense - but I do think they should still be visible in the explorer tree.

Now to build out a test case for this.

so,is here any choice to switch output error off ?

Mrzyxing avatar Mar 19 '20 08:03 Mrzyxing

I'm running into this error as well when using postgres inside the timescaledb-ha docker container.

When running the query @Borvik (https://github.com/Borvik/vscode-postgres/issues/103#issuecomment-491512405) I do see three tables that appear to be metadata:

  • _timescaledb_cache.cache_inval_bgw_job
  • _timescaledb_cache.cache_inval_extension
  • _timescaledb_cache.cache_inval_hypertable

Is it possible to have Intellisense ignore those tables as suggested in https://github.com/Borvik/vscode-postgres/issues/103#issuecomment-492596267?

jaredlander avatar Mar 29 '22 14:03 jaredlander

Any workaround on this? It always happens on timescaled postgres dbs

polmonso avatar Oct 29 '22 20:10 polmonso

Will the PR be accepted? That would help a lot.

jhonathas avatar Apr 04 '23 20:04 jhonathas

I doubt it. Haven't heard from @Borvik unfortunately. We should probably fork this and publish another extension. Feel free to do so 🚀 and tag me

polmonso avatar Apr 05 '23 08:04 polmonso

Sorry - this has kinda been hobby programming for me in my spare time, which I don't seem to have a lot of. I will try to review this over the weekend.

Borvik avatar Apr 07 '23 10:04 Borvik