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

Migration - update schema -removing indexes #189

Open
suchym opened this issue Aug 25, 2021 · 13 comments
Open

Migration - update schema -removing indexes #189

suchym opened this issue Aug 25, 2021 · 13 comments

Comments

@suchym
Copy link

suchym commented Aug 25, 2021

In 'EntityModule.RegisterMigrations' I create several GIST indexes (working with PostgreSQL) at the end of migration (AddPostUpgradeAction) by running an SQL script.
When I create database and apply a schema update, everything works as expected, but when migration is reapplied to the database, the GIST indexes are removed.
Upgrade options are set to 'DbUpgradeOptions.Default' only.
I use version 3.0.2 that VS package manager has shown me as latest stable.
So question is: How to avoid removing GIST indexes during a schema update?

Thank you in advance.

@rivantsov
Copy link
Owner

well, as of now, there's no easy way, unfortunately. Give me a couple of days to come up with something usable

@rivantsov
Copy link
Owner

I have some ideas to try, I will push smth shortly

@rivantsov
Copy link
Owner

rivantsov commented Sep 10, 2021

pushed v 3.4.0, Look at test SchemaUpdate test, it creates extra index in migration; you just add the index name to ignore list - this is new thing, list of Db objects to ignore in schema upgrade; you can do it right in the RegisterMigr method where you add the script for the index.

@suchym
Copy link
Author

suchym commented Sep 17, 2021

I upgraded VITa to 3.4.0 version, created new database and tried to update schema.
I filled up the DbSettings.UpgradeSettings.IgnoreDbObjects list to test this feature when applying the update the second time.

I got an exception on the first run:
'42704: index "PK_UniqueStructure" for table "UniqueStructure" does not exist'
when VITA tried to execute command
ALTER TABLE common."UniqueStructure" CLUSTER ON "PK_UniqueStructure";

The order in which the commands are executed appears to be incorrect because
command:
ALTER TABLE common."UniqueStructure" ADD CONSTRAINT "PK_UniqueStructure" PRIMARY KEY ("Id")
appears after : 'ALTER TABLE common."UniqueStructure" CLUSTER ON "PK_UniqueStructure";' command in DbUpgradeEventArgs.UpdateInfo.AllScripts list.

I believe that DbUpgrading event displays an ordered list of commands in the DbUpgradeEventArgs.UpdateInfo.AllScripts list.

@suchym
Copy link
Author

suchym commented Sep 17, 2021

To the previous entry: When I applied the upgrade schema to an existing database I got exception but now related to the materialized view. The order in which the commands were executed was also incorrect.

Here is order:

  1. {Drop Key: IXC_SpectraStatistics_PostprocessingPol(UniqueClusteredIndex)}
  2. {ViewDrop:Drop View: spectrum."SpectraStatistics"}
  3. {ColumnSetupComplete:Add View: spectrum."SpectraStatistics"}
  4. {PrimaryKeyAdd:Add Key: IXC_SpectraStatistics_PostprocessingPol(UniqueClusteredIndex)}
    (ALTER TABLE spectrum."SpectraStatistics" CLUSTER ON "IXC_SpectraStatistics_PostprocessingPol";)
  5. {IndexAdd:Add Key: IXC_SpectraStatistics_PostprocessingPol(UniqueClusteredIndex)}

The question is, why is the materialized view always re-created?
Creation command omits the WITH [NO] DATA clause
This can lead to incorrect application behavior .
PostgreSQL Documentation says:
WITH [ NO ] DATA
This clause specifies whether or not the materialized view should be populated at creation time. If not, the materialized view will be flagged as unscannable and cannot be queried until REFRESH MATERIALIZED VIEW is used.

@rivantsov
Copy link
Owner

looking into this

@rivantsov
Copy link
Owner

Looking at pg docs, trying to find this 'WITH NO DATA' clause, cannot find any:
https://www.postgresql.org/docs/13/rules-materializedviews.html
what docs you use?

@suchym
Copy link
Author

suchym commented Sep 21, 2021 via email

@rivantsov
Copy link
Owner

ah, I see. But anyway, this With-Data clause is optional. I need no-data anyway because I need to wait for completion of all actions (just in case there are more keys and indexes, and tables must be finished etc) - before filling the data.
For wrong order error - found a cause, surprised it never showed up before, but I guess it's not so frequent combination. Will fix it.
As for Mat view rebuilt - I have no good answer. The problem with mat views is that
a) the PG people do not treat them as views and do not even return them in View list in info-schema queries so I had to hack my own workaround:

// Postgres does not return materialized views in information_schema.views - they say it's intentional (and stupid!)

b) I do not have the 'old' view definition in db, notice I put empty string in the query for View_Definition field, so I have no way to compare the new vs old, so I assume I need to rebuild the views always.

So the question is - do you really need that stuff? It seems gains/conveniences are marginal. If you do, the only way I see is create it and then add it to 'ignore' list

@rivantsov
Copy link
Owner

Pushed new version; problem with indexes (wrong order) should be fixed.
For mat views, trying a workaround. Saving view SQL hashes in DbVersion records (in DbInfoModule). Comparing hashes to figure out if views changed or not.
Let me know if it works for you

@suchym
Copy link
Author

suchym commented Sep 22, 2021 via email

@suchym
Copy link
Author

suchym commented Sep 22, 2021 via email

@rivantsov
Copy link
Owner

will look into this index issue

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

No branches or pull requests

2 participants