<div dir="ltr"><br><br>On Wed, Jan 30, 2019 at 9:42 AM Markus Metz <<a href="mailto:markus.metz.giswork@gmail.com">markus.metz.giswork@gmail.com</a>> wrote:<br>><br>><br>><br>> On Tue, Jan 29, 2019 at 11:37 PM Huidae Cho <<a href="mailto:grass4u@gmail.com">grass4u@gmail.com</a>> wrote:<br>> ><br>> > Just for our records, actually, PostgreSQL 9.5+ supports CREATE INDEX IF NOT EXISTS, which the grass sqlite driver already does (this is why we didn't have this warning with sqlite before). Not sure if we want to enforce the minimum version for a specific database though.<br>><br><div>> We can use "if not exists" for PostgreSQL 9.5+, otherwise not, please try trunk r74042.</div><div><br></div><div>trunk r74043<br></div>><br>> Markus M<br>> ><br>> > Best,<br>> > Huidae<br>> ><br>> > On Tue, Jan 29, 2019 at 5:16 PM Huidae Cho <<a href="mailto:grass4u@gmail.com">grass4u@gmail.com</a>> wrote:<br>> >><br>> >> I just did a quick search and found that these modules (i.segment.stats, r.mwprecip, v.in.geopaparazzi, v.link.precip, v.out.gps, v.ply.rectify, v.unpack) create/copy new tables themselves without the index and invoke v.db.connect. For now, we cannot move index creation to v.db.addtable.<br>> >><br>> >> Huidae<br>> >><br>> >> On Tue, Jan 29, 2019 at 4:12 PM Huidae Cho <<a href="mailto:grass4u@gmail.com">grass4u@gmail.com</a>> wrote:<br>> >>><br>> >>> Yes. Like you said, v.db.connect followed by v.db.connect -d gives me the same pg errors and grass warning. I agree with you that the preferred solution would be to create the index in v.db.addtable, not in v.db.connect "if" no modules rely on v.db.connect for creating the unique index.<br>> >>><br>> >>> Huidae<br>> >>><br>> >>><br>> >>><br>> >>><br>> >>> On Tue, Jan 29, 2019 at 3:51 PM Markus Metz <<a href="mailto:markus.metz.giswork@gmail.com">markus.metz.giswork@gmail.com</a>> wrote:<br>> >>>><br>> >>>><br>> >>>><br>> >>>> On Tue, Jan 29, 2019 at 4:12 PM Huidae Cho <<a href="mailto:grass4u@gmail.com">grass4u@gmail.com</a>> wrote:<br>> >>>> ><br>> >>>> > You're right. It was a warning with PostgresSQL error messages (attached below), not a fatal error. But I don't think there should be any warning (or PostgreSQL errors) because the user didn't do anything wrong here (v.edit map tool=create; v.db.addtable).<br>> >>>><br>> >>>> Hmm yes I agree. At least for v.db.addtable the index can be created by v.db.connect when the new table is actually linked to the vector map.<br>> >>>><br>> >>>> You might still get these PG errors when you try to connect an already existing table, but this is probably not happening very often, and v.db.connect finishes anyway.<br>> >>>><br>> >>>> Markus M<br>> >>>><br>> >>>> ><br>> >>>> > DBMI-PostgreSQL driver error:<br>> >>>> > Unable to create index: create unique index tmp2_cat on tmp2 ( cat )<br>> >>>> > ERROR:  relation "tmp2_cat" already exists<br>> >>>> ><br>> >>>> ><br>> >>>> > DBMI-PostgreSQL driver error:<br>> >>>> > Unable to create index: create unique index tmp2_cat on tmp2 ( cat )<br>> >>>> > ERROR:  relation "tmp2_cat" already exists<br>> >>>> ><br>> >>>> ><br>> >>>> > WARNING: Cannot create index<br>> >>>> > here<br>> >>>> > WARNING: Values in column <cat> will be overwritten<br>> >>>> > Reading features...<br>> >>>> >  100%<br>> >>>> > Updating database...<br>> >>>> >  100%<br>> >>>> ><br>> >>>> > Huidae<br>> >>>> ><br>> >>>> > On Tue, Jan 29, 2019 at 9:37 AM Markus Metz <<a href="mailto:markus.metz.giswork@gmail.com">markus.metz.giswork@gmail.com</a>> wrote:<br>> >>>> >><br>> >>>> >><br>> >>>> >><br>> >>>> >> On Sun, Jan 27, 2019 at 11:21 AM Markus Metz <<a href="mailto:markus.metz.giswork@gmail.com">markus.metz.giswork@gmail.com</a>> wrote:<br>> >>>> >> ><br>> >>>> >> ><br>> >>>> >> ><br>> >>>> >> > On Sat, Jan 26, 2019 at 2:50 PM Huidae Cho <<a href="mailto:grass4u@gmail.com">grass4u@gmail.com</a>> wrote:<br>> >>>> >> > ><br>> >>>> >> > > Markus,<br>> >>>> >> > ><br>> >>>> >> > > If there is a linked table, v.db.addtable stops in line 106. If not, this script doesn't create a unique index and calls v.db.connect. v.db.connect adds a db link in line 317 and creates a unique index (db_create_index2) in line 334 if linking was successful. SQLite didn't complain when both modules created the same unique index, but PostgreSQL failed in v.db.connect (2nd time creating the same unique index).<br>> >>>> >><br>> >>>> >> What do you mean with "failed"? v.db.connect will issue a warning if it can not create an index but finish successfully.<br>> >>>> >><br>> >>>> >> Markus M<br>> >>>> >><br>> >>>> >> >> Not sure which code was added first/later. I think it's more of how we design both modules. v.db.connect will fail if we try to link a table with a unique index. Isn't v.db.connect supposed to "just" connect a table to a layer (without creating any database objects like index)? Which module should be responsible for creating unique indices?<br>> >>>> >> > ><br>> >>>> >> > > Before this commit:<br>> >>>> >> > > 1. v.db.addtable creates unique index<br>> >>>> >> > > 2. v.db.connect tries to create unique index again ==> fatal error<br>> >>>> >> > ><br>> >>>> >> > > After this commit:<br>> >>>> >> > > 1. v.db.addtable doesn't create unique index<br>> >>>> >> > > 2. v.db.connect creates unique index<br>> >>>> >> > ><br>> >>>> >> > > Probably, it should be:<br>> >>>> >> > > 1. v.db.addtable should create unique index<br>> >>>> >> > > 2. v.db.connect shouldn't try to create unique index? Just "connect"...<br>> >>>> >> ><br>> >>>> >> > Yes, this is the preferred solution. Consider v.db.connect -d followed by v.db.connect, i.e. disconnecting a table and then reconnecting the same table: in this case v.db.connect should also fail with PostgreSQL. IMHO, a unique index should only be created when the table is created.<br>> >>>> >> ><br>> >>>> >> > Markus M<br>> >>>> >> ><br>> >>>> >> > ><br>> >>>> >> > > Regards,<br>> >>>> >> > > Huidae<br>> >>>> >> > ><br>> >>>> >> > > On Sat, Jan 26, 2019 at 7:39 AM Markus Neteler <<a href="mailto:neteler@osgeo.org">neteler@osgeo.org</a>> wrote:<br>> >>>> >> > >><br>> >>>> >> > >> Hi,<br>> >>>> >> > >><br>> >>>> >> > >> On Tue, Jan 22, 2019 at 3:51 AM <<a href="mailto:svn_grass@osgeo.org">svn_grass@osgeo.org</a>> wrote:<br>> >>>> >> > >> ><br>> >>>> >> > >> > Author: hcho<br>> >>>> >> > >> > Date: 2019-01-21 18:51:33 -0800 (Mon, 21 Jan 2019)<br>> >>>> >> > >> > New Revision: 73995<br>> >>>> >> > >> ><br>> >>>> >> > >> > Modified:<br>> >>>> >> > >> >    grass/trunk/scripts/v.db.addtable/<a href="http://v.db.addtable.py">v.db.addtable.py</a><br>> >>>> >> > >> > Log:<br>> >>>> >> > >> > v.db.addtable: Do not create unique index from this script; v.db.connect will try to create it again causing some drivers to fail (PostgreSQL)<br>> >>>> >> > >> ><br>> >>>> >> > >> > Modified: grass/trunk/scripts/v.db.addtable/<a href="http://v.db.addtable.py">v.db.addtable.py</a><br>> >>>> >> > >> > ===================================================================<br>> >>>> >> > >> > --- grass/trunk/scripts/v.db.addtable/<a href="http://v.db.addtable.py">v.db.addtable.py</a>  2019-01-21 22:37:59 UTC (rev 73994)<br>> >>>> >> > >> > +++ grass/trunk/scripts/v.db.addtable/<a href="http://v.db.addtable.py">v.db.addtable.py</a>  2019-01-22 02:51:33 UTC (rev 73995)<br>> >>>> >> > >> > @@ -139,16 +139,6 @@<br>> >>>> >> > >> >          except CalledModuleError:<br>> >>>> >> > >> >              grass.fatal(_("Unable to create table <%s>") % table)<br>> >>>> >> > >> ><br>> >>>> >> > >> > -        # create index, see db/driver/*/index.c<br>> >>>> >> > >> > -        if driver != "dbf":<br>> >>>> >> > >> > -            sql = "CREATE UNIQUE INDEX %s_%s ON %s (%s)" % (table, key, table, key)<br>> >>>> >> > >> > -            try:<br>> >>>> >> > >> > -                grass.run_command('db.execute',<br>> >>>> >> > >> > -                                  database=database, driver=driver, sql=sql)<br>> >>>> >> > >> > -            except:<br>> >>>> >> > >> > -                grass.warning(_("Unable to create index on table <%s>") % table)<br>> >>>> >> > >> > -                pass<br>> >>>> >> > >> > -<br>> >>>> >> > >> >      # connect the map to the DB:<br>> >>>> >> > >> >      if schema:<br>> >>>> >> > >> >          table = '{schema}.{table}'.format(schema=schema, table=table)<br>> >>>> >> > >><br>> >>>> >> > >> just a conceptual question:<br>> >>>> >> > >> ... are we sure that this index creation removal never leads to a<br>> >>>> >> > >> table without index?<br>> >>>> >> > >><br>> >>>> >> > >> Markus<br>> >>>> >> > ><br>> >>>> >> > ><br>> >>>> >> > ><br>> >>>> >> > > --<br>> >>>> >> > > Huidae Cho, Ph.D., GISP, PE (MD), CFM, M.ASCE<br>> >>>> >> > > Open Source GIS Developer, GRASS GIS Development Team<br>> >>>> >> > > _______________________________________________<br>> >>>> >> > > grass-dev mailing list<br>> >>>> >> > > <a href="mailto:grass-dev@lists.osgeo.org">grass-dev@lists.osgeo.org</a><br>> >>>> >> > > <a href="https://lists.osgeo.org/mailman/listinfo/grass-dev">https://lists.osgeo.org/mailman/listinfo/grass-dev</a><br>> >>>> ><br>> >>>> ><br>> >>>> ><br>> >>>> > --<br>> >>>> > Huidae Cho, Ph.D., GISP, PE (MD), CFM, M.ASCE<br>> >>>> > Open Source GIS Developer, GRASS GIS Development Team<br>> >>><br>> >>><br>> >>><br>> >>> --<br>> >>> Huidae Cho, Ph.D., GISP, PE (MD), CFM, M.ASCE<br>> >>> Open Source GIS Developer, GRASS GIS Development Team<br>> >><br>> >><br>> >><br>> >> --<br>> >> Huidae Cho, Ph.D., GISP, PE (MD), CFM, M.ASCE<br>> >> Open Source GIS Developer, GRASS GIS Development Team<br>> ><br>> ><br>> ><br>> > --<br>> > Huidae Cho, Ph.D., GISP, PE (MD), CFM, M.ASCE<br>> > Open Source GIS Developer, GRASS GIS Development Team</div>