Skip to content

Conversation

@manandre
Copy link
Collaborator

@manandre manandre commented Mar 1, 2023

Closes #4950

@manandre manandre force-pushed the 4950-pg-service-conf branch from 445bfdf to 21e195f Compare March 5, 2023 20:03

static string? GetDefaultSystemPostgresDir() => ExecuteCommandSync("pg_config", "--sysconfdir");

static string? ExecuteCommandSync(string command, string arguments)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this already contains 'Sync' and uses APIs that can block I guess that you also plan an Async variant (also for GetConfigFromIniFile()) - no?
The internal part could use a boolean async flag like we do it all the time in Npgsql (e. g. see KerberosUsernameProvider for how to do this for Process)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.
I have shared the code between both usages.

@manandre manandre force-pushed the 4950-pg-service-conf branch from 4a98532 to 5492bb9 Compare March 7, 2023 21:49
@manandre
Copy link
Collaborator Author

manandre commented Mar 8, 2023

The CI checks seem blocked in an infinite wait, even after a new commit on the PR branch...
Can someone help me to solve this ?

@vonzshik
Copy link
Contributor

vonzshik commented Mar 8, 2023

The CI checks seem blocked in an infinite wait, even after a new commit on the PR branch... Can someone help me to solve this ?

It's probably because of merge conflict.

@manandre manandre force-pushed the 4950-pg-service-conf branch from 61ddce6 to 0607b3e Compare March 8, 2023 18:49
@manandre
Copy link
Collaborator Author

manandre commented Mar 8, 2023

The CI checks seem blocked in an infinite wait, even after a new commit on the PR branch... Can someone help me to solve this ?

It's probably because of merge conflict.

Yes, my local git repo was corrupted, so I was not able to pull from upstream and so see the conflict...
Thanks for your help.

@manandre manandre marked this pull request as ready for review March 8, 2023 22:26
@manandre manandre requested a review from roji as a code owner March 8, 2023 22:26
@manandre manandre requested review from Brar and vonzshik and removed request for Brar, roji and vonzshik March 9, 2023 18:57
@manandre
Copy link
Collaborator Author

@vonzshik @Brar @roji
The PR is now ready for (final) review.
Your comments are welcome.

@vonzshik
Copy link
Contributor

@manandre Great, I'll take a look tomorrow.

UseShellExecute = false
};
return includeRealm ? _principalWithRealm : _principalWithoutRealm;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe leave the part above here as sync function and move the part below to a local async function (as it was before).
That way access to the cached values works without async state machine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Contributor

@vonzshik vonzshik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks quite good.
@roji whenever you have a time.

@manandre manandre force-pushed the 4950-pg-service-conf branch from 6c78361 to 4b6a2a4 Compare August 5, 2023 13:10
@manandre
Copy link
Collaborator Author

manandre commented Aug 5, 2023

@roji Can you please tell me whether something prevents this PR from being merged?

@manandre manandre force-pushed the 4950-pg-service-conf branch from 4b6a2a4 to adafb88 Compare October 1, 2023 13:32
@roji
Copy link
Member

roji commented Nov 7, 2023

@NinoFloris to verify that there are no size regressions for NativeAOT.

@NinoFloris NinoFloris closed this Nov 7, 2023
@NinoFloris NinoFloris reopened this Nov 7, 2023
Copy link
Member

@NinoFloris NinoFloris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we're pulling in FileStream again with this PR.

Could you copy the workaround from #5234 to break the internal dependency on FileStream that the convenience File apis like File.ReadAllLines have? (FileStream itself references System.Uri again and so on. It's quite wasteful)

You know it's gone by looking at the NativeAOT github action summary and opening our Size by Namespace panel. System.IO should be around 30kb.

Alternatively you can use sizoscope https://github.com/MichalStrehovsky/sizoscope and load in the mstat file attached to the action run.

I'd be happy to help if you run into any issues.

@manandre manandre force-pushed the 4950-pg-service-conf branch from adafb88 to 2c3f2fe Compare November 7, 2023 23:05
@manandre
Copy link
Collaborator Author

manandre commented Nov 7, 2023

@NinoFloris I have applied the same workaround, please tell me whether it solves the issue here. I am not sure how to read the sizoscopeX output...

@NinoFloris
Copy link
Member

NinoFloris commented Nov 9, 2023

@manandre to get the dependency analysis going you should also download the npgsql.scan.dgml.xml. Then in sizoscope -> open you select both the mstat and the xml.

Once you're in double clicking a type should pop up another window allowing you to traverse to roots keeping the type alive.

If you want to know what this PR added compared to baseline you can take an mstat & .xml from an actions run on main, then in sizoscope(X) load it via the tools -> diff option in the menu.

In either case I took a look and it seems we're now always rooting System.Diagnostics.Process, which itself brings in a lot, including filestream it seems. This PR is adding about 400kb to the minimal binary just to read pg_config --sysconfdir, that's too much for too little added value IMO.

Would we be able to get that info some other way, or be able to make reading it via pg_config optional?

@manandre
Copy link
Collaborator Author

manandre commented Nov 9, 2023

@NinoFloris Thanks for the tutorial ;)
I have removed the dependency to System.Linq as a desperate attempt to reduce binary size.
For System.Diagnostics.Process I do not understand as it is already used in the KerberosUsernameProvider class, it should not appear as new, should it?

@NinoFloris
Copy link
Member

We're only conditionally referencing it via this mechanism

=> KerberosUsernameProvider.GetUsername(async, includeRealm, connectionLogger, cancellationToken);

So as long as a project using the NpgsqlSlimDataSourceBuilder doesn't call EnableIntegratedSecurity() we don't reference the code that ends up using Process.

@manandre
Copy link
Collaborator Author

manandre commented Nov 12, 2023

Oh! I was not aware that NativeAOT is able to optimize such paths.
Here, the sysconfdir is only required when the Service field is set in the connection string or if the PGSERVICE environment variable is set. Why does NativeAOT is not able to optimize this path?

@NinoFloris
Copy link
Member

So the idea is that the NativeAOT scanner figures out whether these derived types are actually constructed. As long as there is no reachable path to a concrete constructor call of the derived type it can trim its code and its dependencies as well.

Here, the sysconfdir is only required when the Service field is set in the connection string or if the PGSERVICE environment variable is set. Why does NativeAOT is not able to optimize this path?

These things can only be reasoned about at runtime, at which point the code has to be included, just in case.

@NinoFloris
Copy link
Member

We're aiming to release Npgsql and EFCore.Pg tomorrow so I'm going to move it out to 9.0.

We'll have a bit more time to think about workarounds. Though I hope we see more size improvements in .NET 9.0, ideally when System.Diagnostics.Process is pulled in as well (at least we'll have time to raise the issue as well).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for pg_service.conf

5 participants