Can Flex styles run arbitrary SQL against the database? #1415
Replies: 2 comments 4 replies
-
What I'd do here is to load two different tables and create a view that combines them. Have osm2pgsql create and update the underlying tables and have your application use the combined view. |
Beta Was this translation helpful? Give feedback.
-
There is a Lua module out there for connecting to databases (look for lua-dbi). You should be able to use that to connect to the database and do anything you like. You can also run SQL commands before or after running osm2pgsql using the But the larger issue here is something else: osm2pgsql does things with the database and the user doesn't always know what they are. It might change settings, create indexes, do things in transactions or not, etc. So running your code while osm2pgsql does its thing always runs the risk of "disturbing" osm2pgsql or even breaking the data in some subtle way. And even if something works today, internals of osm2pgsql might change in the next version and then your solution breaks. So this is the problem with "allowing" users to run arbitrary SQL commands. You can do it, but if something breaks, you can't complain about osm2pgsql. So if you are doing something like this I suggest you do it before running osm2pgsql or after, but not during. (You can still use the normal Lua interpreter with the lua-dbi module to do this so that you can share Lua code with the Lua run from osm2pgsql if you wish to do so.) Okay, so this is why there is no "officially sanctioned" and easy way to run arbitrary SQL from osm2pgsql. I realize that this blocks users from doing many interesting and clever things and I'd like to see some solution here, but I am not sure what this can be. Allowing the user more flexibility here would mean restricting the flexibility that osm2pgsql has. At the moment osm2pgsql can, for instance, build an index sooner or later in the processing. It doesn't matter, because as long as it always done when osm2pgsql is done, the user will always see the index at the end of the processing. But if we "allow" access while osm2pgsql is running, users will rely on certain behaviour of osm2pgsql (like at what point an index is available or not) which we can't change any more then. More specifically to your question: I believe you want to change the tables after they have been created by osm2pgsql but before they have been filled. But that doesn't work (even if you did that from Lua after the table creation commands and before the callbacks are called) because osm2pgsql normally creates tables twice: It creates a temporary table, adds the data, then creates the final table and copies the sorted data over. This is exactly one of those cases where you need to have very specific knowledge of the inner workings of osm2pgsql to do something. |
Beta Was this translation helpful? Give feedback.
-
With the flex output is it possible to run arbitrary SQL queries from the Lua styles? I have wondered this a few times already and previously looked around through the docs and did not find anything obvious. This week I started experimenting with partitioning schemes with the data and think this ability would be very helpful for this idea as well.
The planned partitioning scheme is to use the the region of the data (e.g.
north-america--us-west
) and the date the data is from. In order to use Postgres partitions (Pg10+) those values need to be in the table and in a primary key. In a non-partitioned scheme having the date and region on every row would just waste a bunch of space for no real benefit.Normal (no partitioning) would create the simple PK.
When the partitioning switch was enabled it would run different SQL to add necessary columns, comments and the more complicated PK. This sets the stage to load new data and only have to script basic
ATTACH PARTITION
commands.Other ideas for running arbitrary SQL based on logic within Lua is to run appropriate post-import commands to create generated columns, comments, etc.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions