Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing a DuckDBInstance? #122

Open
Tracked by #769
nshiab opened this issue Jan 27, 2025 · 5 comments
Open
Tracked by #769

Closing a DuckDBInstance? #122

nshiab opened this issue Jan 27, 2025 · 5 comments
Assignees
Labels
enhancement New feature or request question Further information is requested
Milestone

Comments

@nshiab
Copy link

nshiab commented Jan 27, 2025

Hi!

With the previous DuckDB node bindings, we could .close() a Database instance to free up memory.

Is there a way to do the same thing with this version?

Thank you!

@jraymakers
Copy link
Contributor

There isn't currently a way to explicitly close an instance. However, it will be closed automatically a short time after the reference goes out of scope (and thus gets garbage collected). This typically happens in under a second, but does depend on the GC behavior so can't be strictly guaranteed.

It wouldn't be hard to expose explicit close for instances - I did this already for connections - but I'm curious about the use case. It's not recommended in general to create more than one instance per process, either at the same time or over time. In which case would you call an explicit close, if it existed?

@jraymakers jraymakers self-assigned this Jan 27, 2025
@nshiab
Copy link
Author

nshiab commented Jan 27, 2025

Thanks for the answer, @jraymakers !

I’m currently updating my library, simple-data-analysis, with duckdb-node-neo

I just noticed that I had a method for closing an instance with the previous bindings, which is why I’m asking.

But if it’s closed automatically, I guess it’s all good!

Thank you for all your hard work. This is a fantastic improvement! 😊

@jraymakers jraymakers added the enhancement New feature or request label Jan 27, 2025
@jraymakers
Copy link
Contributor

I might add it at some point if I get enough requests. Let me know if you find a need for it. I'll leave this open in the meantime.

Glad you're enjoying the new library!

@jraymakers jraymakers added the question Further information is requested label Feb 1, 2025
@albertog
Copy link

albertog commented Feb 6, 2025

It is recomendable hold connection open vs. open and close when needed?

@jraymakers
Copy link
Contributor

It is recomendable hold connection open vs. open and close when needed?

Connections in DuckDB (and Node Neo) are fairly lightweight compared to other DBMSs. They're just in-memory objects. I'd usually recommend keeping them around. The only reason I can think of to close one is to release any memory associated with it. This is usually small, but it can get large if you create (large) temporary objects, because these are associated with the connection.

Other semantic properties of connections might affect your decision of how many to use:

  • Different connections can run SQL in parallel. Each connection can only have a single active query (and transaction).
  • Each connection has its own context for things like the current database & schema, variables, and named prepared statements, in addition to temporary objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants