Skip to content

completion_refresh thread crashes with KeyError #1590

Closed

Description

@shgol

Description

I get the following Exception a few seconds after connecting to the db

Traceback (most recent call last):
  File "/opt/homebrew/Cellar/python@3.14/3.14.4_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/threading.py", line 1082, in _bootstrap_inner
    self._context.run(self.run)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.14/3.14.4_1/Frameworks/Python.framework/Versions/3.14/lib/python3.14/threading.py", line 1024, in run
    self._target(*self._args, **self._kwargs)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/pgcli/4.4.0_1/libexec/lib/python3.14/site-packages/pgcli/completion_refresher.py", line 63, in _bg_refresh
    refresher(completer, executor)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/pgcli/4.4.0_1/libexec/lib/python3.14/site-packages/pgcli/completion_refresher.py", line 111, in refresh_tables
    completer.extend_columns(executor.table_columns(), kind="tables")
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/pgcli/4.4.0_1/libexec/lib/python3.14/site-packages/pgcli/pgcompleter.py", line 232, in extend_columns
    metadata[schema][relname][colname] = column
    ~~~~~~~~~~~~~~~~^^^^^^^^^
KeyError: 'some_table_name'

You can reproduce this by creating a table in between the calls to extend_relations and extend_columns in completion_refresher.py. The dict keyed on relname doesn't get created, and then after the table is created extend_columns looks up the new table and gets KeyError.
I repro this consistently due to another process programmatically creating tables often.

repro'd on pgcli 4.4.0

Activity

  1. shgol commented on Jun 26, 2026

    @shgol
    ContributorAuthor

    Sure, PR is ready again. Maintainers, feel free to merge when you'd like.

  2. DiegoDAF commented on Jun 29, 2026

    @DiegoDAF
    Contributor

    I can confirm this on my side. Same KeyError in the background completion_refresh thread, triggered when a relation is created in the window between extend_relations and extend_columns (we have external processes creating tables frequently, so it reproduces consistently).

    The fix in #1591 resolves it by guarding the writes in extend_columns with setdefault instead of assuming the relation key already exists:

    metadata.setdefault(schema, {})
    metadata[schema].setdefault(relname, OrderedDict())
    metadata[schema][relname][colname] = column

    I've been running this patch in my fork (DiegoDAF/pgcli.daf) for a few weeks across daily use and the crash is gone, with no side effects on completion behavior.

    The only thing blocking it is that #1591 currently shows merge conflicts. @shgol, would you be able to rebase it on current main? Happy to help get it over the line if useful. It would be great to land this, since the crash is easy to hit on busy databases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions