Skip to content

Lazily load composite types from pg_type #1126

Closed

Description

@walkindude

Hello, I noticed an issue when I'm opening the first connection to a Postgres cluster on a remote machine. This has nothing to do with issue #268 or #313 (at least I think).
With the help of a small network monitor I happened to notice a significant amount of data being downloaded on the first call to Open(). Subsequent calls are fast, which hints at some initialization step that needs to be done.

The bulk of the data seems to be made of the results of this query.

I tried running the query directly on the remote server and it's fast (around 0.350 s), but for my database it returns 49224 rows, and downloading those is slow, taking a few tens of seconds. This many types probably come from the fact that the database I'm connecting to contains a few tens of schemas, each one with a few tens of tables. I know nothing about what Postgres considers a type, but querying pg_types shows names of both tables and the sequences tied to their autoincrement columns.

I nailed it down to that part of code because at one time the connection failed to open because of a timeout, and I think the stacktrace pointed here.

Actually, to make the thing work I had to increase the timeout from the connection string, which by the way looks like this:

Server=example.com;Port=5432;Database=mydb;UserId=u;Password=shhhh;CommandTimeout=60;Timeout=60;

Am I doing something obviously wrong?

The server is a 9.3.5 on Windows server 2008 R2. I stumbled upon this issue with Npgsql 3.1.2, but I had a copy of 3.0.5 and the issue was still there.

Activity

  1. roji commented on May 25, 2016

    @roji
    Member

    @walkindude, you understand correctly - the first time Npgsql connects to a database, it downloads the type definitions so that it knows how to interpret results that are fetched later. Since tables are also considered types in PostgreSQL (i.e. composite), these are downloaded as well. I admit I'm not sure how you got to nearly 50000 - a few tens of schemas with a few tens of tables should definitely not get you that high.

    Any way you can dig a bit deeper and see what the bulk of the rows correspond to? Are they really just actual tables, or is there something else going on?

    I can take a look at reducing what is fetched at connection time - we could filter out all tables/composites and then load them only on-demand as composite mapping occurs, but I'd like to be sure that's where things are coming from.

    In the meantime setting Timeout=60 is a good workaround (CommandTimeout shouldn't be needed here though).

  2. walkindude commented on May 25, 2016

    @walkindude
    Author

    Hello and thanks for your quick reply.

    If that could help, tomorrow when I get back to work I could execute a raw select * from pg_types and post the result somewhere (or send it directly to you, if that's possible). I can't access the database from home due to restrictions in pg_hba.conf. When I ran that query earlier today, the result set was maybe 20 or 30 rows bigger than the one I got when I ran the query I pointed to. As I remember, most of the records were related to tables and the sequences they own (each table has a serial column), and the bulk of them came from our app's schemas (we adopted a one-schema-per-customer strategy, so the figure is only bound to increase).
    I could also run the actual query that Npgsql is running.

    If you can think of better ways I could help you sort this out, just let me know.

    For a bit of context, the database is the main storage for an ASP MVC app. For this app, we came up with a migrations system whereby users can dynamically create tables and add/drop columns, and for that we're exploiting Entity Framework's own migrations mechanism. It may be that EF is doing something funky behind our backs. I doubt it really has to do with what basically boils down to just a bunch of create table statements, but hey.
    That's really our main usage of EF, because with such a dynamic database structure, we have to resort to Dapper and dynamically generate queries to show the actual data to the user.

    What prompted me to open this issue is that today I was writing this desktop app for internal use and it took forever to connect to the remote db, making testing very time-consuming.
    One funny thing that I should really look into is that another desktop app that connects to the same database doesn't seem to show such a degradation in performance. Both apps use Dapper, and the only difference is that on this other app I'm using Dapper's async API (and I await on ADO's OpenAsync()), and in this new one I don't. Maybe that has something to do with it.

    Apart from bumping up the Timeout on the connection string, I solved by moving the app to the remote machine and testing it directly from there. Still painful, but much quicker.

    Thanks again.

  3. roji commented on May 25, 2016

    @roji
    Member

    Such dynamic and per-user schemas seem like a pretty extreme scenario for a relational database... PostgreSQL actually provides great "pockets" of non-strictness (i.e. jsonb) but treating the entire schema as a per-user and dynamic thing is bound to create some issues. But if it works for you then why not.

    The best thing for me would be to execute Npgsql's own query and send me link to the results somewhere.

    One funny thing that I should really look into is that another desktop app that connects to the same database doesn't seem to show such a degradation in performance. Both apps use Dapper, and the only difference is that on this other app I'm using Dapper's async API (and I await on ADO's OpenAsync()), and in this new one I don't. Maybe that has something to do with it.

    That's definitely worth looking into. The Npgsql sync vs. async APIs shouldn't have any bearing on how much a query takes on PostgreSQL. However, if you're performing operations in parallel in some way then async could be making things faster (in a non-interesting way). Let me know what you find.

  4. walkindude commented on May 25, 2016

    @walkindude
    Author

    Such dynamic and per-user schemas seem like a pretty extreme scenario for a relational database... PostgreSQL actually provides great "pockets" of non-strictness (i.e. jsonb) but treating the entire schema as a per-user and dynamic thing is bound to create some issues. But if it works for you then why not.

    It was either that or one-database-per-user, and a fair amount of googling seemed to indicate that having multiple schemas in a single database was way better than one db per user. There were not many alternatives, at least as far as I know. This particular product was meant to be really customizable, and even though many tables look the same across customers, there are many of them who absolutely need custom columns to store additional data. Which could have well been jsonb columns, but back then there was barely any support for json in Postgres, and switching over now would require a major refactoring that would never be allowed by management. That, and the boss really wanted each customer's data to be isolated from the other's. You know, you can try to explain things to non-technical people only so many times. If it was for me, there would only be one schema with the core tables common for all users, and additional username-prefixed tables to store other data in jsonb columns or some other thing.

    Anyhow, here's a link to the results of the query, I managed to run it. It's a CSV dump, let me know if it suits you. I sorted it by nspname to make it easier to look at.

  5. roji commented on May 25, 2016

    @roji
    Member

    I'm definitely not going to go into your application and database design :) If it works for you that's enough. I'll take a look at your file tomorrow.

  6. walkindude commented on May 25, 2016

    @walkindude
    Author

    I'm definitely not going to go into your application and database design :)

    Me neither :D

    Thank you for your help.

  7. roji commented on May 27, 2016

    @roji
    Member

    Ouch...

    One first observation about your data is that around 33321 of your 49224 types are composites. It would make sense to load composites lazily (when they are first mapped/used), especially since actual composite use in Npgsql is a really rare user feature.

    But another strange thing I can see in your data is that there are 15833 array types. From a small random sample I took most of these arrays actually have an element type which Npgsql filters out because it can't handle them: the element OID column contains OIDs which don't appear in the OID column. I wonder about these, could you please do SELECT * FROM pg_type WHERE oid=191668 (and maybe a few other element OIDs which don't correspond?

  8. roji commented on May 27, 2016

    @roji
    Member

    Oops, my bad. Those "uncorresponding arrays" from above are actually simply arrays of composite types (had a filter on in Excel).

    So lazy loading of of composite types would make your list go down to almost nothing. I'll look into this, although there's a good chance this will be a 3.2 feature (depending on how invasive/risky it is).

  9. added this to the 3.2 milestone on May 27, 2016
  10. self-assigned this
    on May 27, 2016
  11. changed the title [-]Performance issue while first connecting to remote server[/-] [+]Lazily load composite types from pg_type[/+] on May 27, 2016
  12. roji commented on May 29, 2016

    @roji
    Member

    Note to self: we could also load enums lazily, but that would break the ability to read unmapped enums as strings. Enums are also much less likely to cause an performance issue, so am dropping it.

  13. added a commit that references this issue on May 29, 2016
    ec7b79d
  14. modified the milestones: 3.1.3, 3.2 on May 29, 2016
  15. roji commented on May 29, 2016

    @roji
    Member

    @walkindude, I've made this change and backported it to 3.1.3, so Npgsql will no longer load all composite type definitions on first connect (it will do so the first time a composite type is mapped). The build is in progress, in a few minutes you should see a nuget for 3.1.3-ci-183 appearing in the Npgsql stable feed, it would be great if you can test and confirm everything is good.

  16. roji commented on May 29, 2016

    @roji
    Member

    Confirm v3.1.3-ci-185 is available in the stable feed for testing.

  17. walkindude commented on May 29, 2016

    @walkindude
    Author

    Hey, thanks. Sorry for the late reply!
    Wonderful, I'll get to it as soon as possible. It might take a few days, though.

    Thank you so much, man.

    Just out of curiosity: from what you saw in the file I sent you, is there anything that looks suspicious in the database I'm connecting to?

  18. walkindude commented on May 30, 2016

    @walkindude
    Author

    @roji, can confirm the time just went down from tens of seconds to 0.668 s. Awesome, thank you so much, you just saved me a few hours of frustration. From the couple of tests I ran, nothing seems to be broken.

    At some point I'll take a look at the other app (the one that uses async) and let you know what I find if you think it's still relevant.

    Thank you again, you've been super kind.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions