Quantcast
Channel: Question and Answer » postgresql
Viewing all 1138 articles
Browse latest View live

PostGIS: ST_Equals false when ST_Intersection=100% of geometry?

$
0
0

I have 2 datasets consisting of cadastral parcel data – roughly 125,000 rows each. The geometry column is WKB polygons representing parcel boundaries; all data are geometrically valid (the polygons are closed etc).

Some recent data arrived in a different projection to the base data being used for a comparison job – so I reprojected the newer one (base was 4326; the other was WGA94 that got brought into PostGIS as 900914… I reprojected it to 4326).

The first stage of the analysis was to find and store non-matching parcels; part of that is to identify and store parcels with identical geometry.

So I ran a very standard query (the code block below abstracts away schema details etc):

create table matchdata as
  select  a.*
  from gg2014 a, gg2013 b
  where ST_Equals(a.g1,b.g1)

ZERO results.

“Odd…” I thought. “Perhaps there have been tiny vertex shifts caused by the reprojection: that would be annoying, and really shouldn’t happen.”

Fortunately there is abundant aspatial data (5 identifier columns) that enable me to establish parcels that should be spatially identical: those with the same identifier, whose change-date in the 2014 table was before the max change-date in the 2013 data. That amounted to 120,086 distinct rows.

I stored the identifiers and geometries in a separate table (match_id), and ran the following query:

select apid, 
       bpid, 
       ST_Area(ag::geometry) as aa, 
       ST_Area(bg::geometry) as ab,
       ST_Area(ST_Intersection(ag,bg)::geometry)/ST_Area(ag::geometry) as inta,
       ST_Area(ST_Intersection(ag,bg)::geometry)/ST_Area(ag::geometry) as intb
from match_id
order by inta

The first 16 values for inta and intb were identically zero, the next 456 were 0.99999999-ish (min 0.99999999999994, max 0.999999999999999), and rows 473 onwards were 1 – until row 120050, when the area of the intersection was greater than either geometry (the greatest value for inta and intb was 1.00000000000029, but still).

So here’s my conundrum: if two geometries intersect spatially by between 99.999999999994% and 100.000000000029% of their respective areas, I would like “ST_Equals” to say “Yep…. I’ll give you that one. Close enough”.

After all, it’s equivalent to being out by about 1 part in 16 trillion… i.e., as if the US national debt was off by less than 93 cents.

In the context of the circumference of the Earth (at ~40,000km), it’s like being off by 0.0000000025km, tops (since to result in an area difference that small, any vertex shift must be even smaller).

According to TFD (which I have R’d) the tolerance for ST_Intersects() is notionally 0.00001m (1mm), so the implied changes in the vertices (which I confess I have not checked: I will ST_Dump() them and do so) would seem to be smaller than the tolerance. (I realise that ST_Intersects !== ST_Intersection(), but it’s the only tolerance mentioned).

I have not been able to find out the corresponding tolerance for the vertex comparison undertaken by ST_Equals()… but it seems really odd that at least 120,000 of my rows ought to pass any sensible assessment of spatial identity, but don’t.

(Note: I also did the same exercise using ::geography – with results that had more variability, but still more than 110,000 entries with a nice clean ’1′).

Is there a way to loosen the tolerance of ST_Equals, that doesn’t require digging into the interstices of the code? I am not keen on doing that.

If not, is there a kludge that anyone is aware of?

Note: it would be good if the ‘kludge’ wasn’t doing a bilateral comparison like

where ST_within(g1, ST_Buffer(g2, 0.0000001))
  and ST_within(g2, ST_Buffer(g1, 0.0000001))


   - I've done that: sure, it works... but it's a gigantic documentation PITA).

I can work around this, but writing the 20 pages to document the workaround – which will only ever come up again if we get dodgy data – is a PITA that I would rather not have to do given that it’s likely to be a one-off.

(Versions: Postgresql 9.3.5; PostGIS 2.1.3)


php script with lat and long for postgresql

$
0
0

I would like to create a postgresql geodatabase able to display, with OL3, data entered through a php script. In the php script i indicated the two fields latitude and longitude data as numeric fields.
Does anyone have any suggestions on how to make it clear to postgresql that the fields are the latitude and longitude coordinate fields, or I have to implement a conversion to the format recognized by postgresql?
In this case how can I do?
Thank You

Use another schema than public in geoserver monitoring plugin

$
0
0

We want to use the Geoserver Monitoring Plugin, based on a PostgreSQL 9.3 database.

It works fine except that the plugin create the tables request and request_resources in the public schema, while we want them to be separated in a schema called monitoring.

What we tried:

  • Add the searchpath to the jdbc url in the file monitoring/db.properties (See: http://stackoverflow.com/a/4820666/291541)

    url=jdbc:postgresql://localhost:5432/mydatabase?searchpath=monitoring

  • Add a line in the hibernate.properties file to force the schema:

    hibernate.default_schema=monitoring

  • Create a user dedicated to that role and set his default schema to monitoring (See http://stackoverflow.com/a/4168794/291541)

    set search_path to 'monitoring'

Unfortunately nothing worked. The tables are not created in the monitoring schema. An error appeared in geoserver.log with that last solution:

ERROR [util.JDBCExceptionReporter] – ERROR: relation
“hibernate_sequence” does not exist

Any idea?

Postgresql – Stuck building a plpgsql function [closed]

$
0
0

I am a beginner in plpgsql and i am stuck coding a function.
I need a function that does the following:

Giving a table, a id_field in that table and another field in that table it does:

ORIGINAL TABLE

id_field     field_traspose
---------    --------------
    1              A
    1              B
    1              C
    2              A
    3              F
    3              X

RESULT

id_field     field_traspose
---------    --------------
    1              A, B, C
    2              A
    3              F, X

My attempt:

CREATE OR REPLACE FUNCTION traspose(mytable character varying, id_field character varying, field_traspose character varying)
  RETURNS setof RECORD AS
$BODY$ 
DECLARE
    r record;
    result record;
BEGIN

FOR r IN EXECUTE 'SELECT '||id_field||','||field_traspose||'  from '||mytable LOOP

-- Here should go the logic that joins every field_traspose for a same id_field and
--returns the record as one of the returning records (can be many different id_fields)   

RETURN NEXT result;
END LOOP;

RETURN;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

I’m stuck at this point.
Regards,

Centos 7, Postgresql Failed to Read Permission Data Directory in mounted Partition

$
0
0

I’ve a problem with start PostgreSQL 9.4 on Centos 7 Server. Because the partition /var/lib/pgsql/9.4/data/ (which contains PostgreSQL data) is full so I want to change to another partition.

/dev/sdb1               154685904  322500 146482752   1% /run/media/root/Data

I’ve used this command to copy all the old data to new data folder:

cp -a /var/lib/pgsql/9.4/data/ /run/media/root/Data/pgsql_data

Then I changed the directory to new data directory in postgresql.conf (/var/lib/pgsql/9.4/data)

data_directory = '/run/media/root/Data/pgsql_data/'     # use data in another directory

And then I try to restart PostgreSQL, here are errors:

postgresql-9.4.service - PostgreSQL 9.4 database server    
Loaded: loaded (/usr/lib/systemd/system/postgresql-9.4.service; enabled)    
Active: failed (Result: exit-code) since Wed 2015-03-11 09:17:21 ICT; 4s ago   
Process: 54975 ExecStop=/usr/pgsql-9.4/bin/pg_ctl stop -D ${PGDATA} -s -m fast (code=exited, status=0/SUCCESS)   
Process: 56133 ExecStart=/usr/pgsql-9.4/bin/pg_ctl start -D ${PGDATA} -s -w -t 300 (code=exited, status=1/FAILURE)   
Process: 56127 ExecStartPre=/usr/pgsql-9.4/bin/postgresql94-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)  
Main PID: 54580 (code=exited, status=0/SUCCESS)

Mar 11 09:17:16 localhost.localdomain pg_ctl[56133]: LOG:  skipping missing configuration file "/run/media/root/Data/pgsql_data/postgresql.auto.conf" 
Mar 11 09:17:16 localhost.localdomain pg_ctl[56133]: < 2015-03-10 22:17:16.187 EDT FATAL:  could not read permissions of directory "/run/media/root/Data/pgsql_data": Permission denied 
Mar 11 09:17:21 localhost.localdomain pg_ctl[56133]: pg_ctl: could not start server 
Mar 11 09:17:21 localhost.localdomain pg_ctl[56133]: Examine the log output. 
Mar 11 09:17:21 localhost.localdomain systemd[1]: postgresql-9.4.service: control process exited, code=exited status=1 
Mar 11 09:17:21 localhost.localdomain systemd[1]: Failed to start PostgreSQL 9.4 database server. 
Mar 11 09:17:21 localhost.localdomain systemd[1]: Unit postgresql-9.4.service entered failed state.

Please help!

Multilingual Article/ Category database design

$
0
0

I wasn’t getting any answers over at Programmers and was advised to go over here instead…


I wanted to make a design for a multilingual database and after having drawn it up have arrived at 5 tables, some of them very short.

The idea is that is used for an ‘About Us’ sort of area and consists of articles sorted into categories, with tracking for changes.

Categories
    id              # Unique ID
    parent_id       # To create heirarchy in categories.

Category_Language
    id              # Unique ID
    category_key    # Link to Category table
    language        # ISO Language code 
    name            # Name of category in that language 

Article
    id              # Unique ID
    category_key    # foreign key to link to the category.

Article_Language
    id              # Unique ID
    article_key     # foreign key to link to the article.
    name            # name of the article in that language.
    content         # html content in that language.

Article_Revisions
    id              # Unique ID
    article_key     # Foreign key to link to article_language
    content         # markup of content in language.
    date_created    # date created
    revision_number # increments for every change.
    revision_user   # who changed it?
    revision_date   # when was it changed?
    size            # size of the revision

So now I’m wondering, could I have done this simpler? Would it be better to create a big table than two small ones from a computational perspective?

  • Categories with an additional language column and skip Category_Language table?
  • Article with an additional language column and skip Article_Language table?

I could even have squished together the list of articles and categories into one table with languages and type column to query which one I want, but that does not seem efficient and hardly pedagogical.

Geoserver – How to make a psql-backed timestamp that uses timezones

$
0
0

I’m working on an application that extends geoserver and am frustrated as I’m trying to define a SimpleFeature that has timestamps that include timezones.

I’ve tried declaring my timestamp field using:

getBuilder().add(fieldName, Date.class);

As well as:

getBuilder().add(fieldName, Timestamp.class);

In both cases, the associated fields that are created in my postgres database have the type timestamp without time zone

Ultimately, I’m just trying to get that psql type to read as timestamp with time zone, there’s a good chance I’m missing a key concept. I’m quite new to this.

Any help would be great.

How to put the results of a query into a variable?

$
0
0

I have written a Postgres anonymous block. In which I have written join query on multiple tables like:

select a.portal.*, a.p_fb_config.*, a.p_gplus_config  
from a.portal 
   left outer join a.p_fb_config on a.portal.pid = a.p_fb_config.pid 
   left outer join a.p_gplus_config on a.portal.pid = a.p_gplus_config.pid;

Now I want to catch this value in variable. So how should I declare that variable?

I need something like:

         portal_row a.portal%ROWTYPE;//for 3 tables

PostgreSQL 9.3.5 database restore error : plpgsql already exists

$
0
0

I have a .net program to restore a PostgreSQL database backup. What it is doing is

1) Delete the existing database.
2) Create it again.
3) Initialize some variables/properties.
4) Restore the database.

My problem is I am trying to restore the PostgreSQL 8.3 database backup to PostgreSQL 9.3.
While restoring i am getting the following error : plpgsql already exists so my program returns FAILURE as there is error.

If i do a DROP EXTENSION 'plpgsql' query after i create the new DB, restore is very successful. But from my program, i can’t do it or not willing to do it.

I know this is ignorable error but need to make my program correct. How can i do this?

Thanks,

Extracting specific geometry point from MultiPoint geometry using PostGIS?

$
0
0

I am working with multipoint geometries in postgis and would like to extract the geometry point associated with the greatest y value of the multipoint geometry. How may I accomplish such a task?

For example, if my multipoint geometry is defined as follows:

‘MULTIPOINT(2 5, 3 1, 4 0)’

Then how may I gather the geometry point associated with the greatest y-value?

The following query will tell me that the greatest y-value is 5:
“SELECT ST_ASTEXT(MP), ST_YMAX(MP) FROM
(SELECT ST_GeomFromText(‘MULTIPOINT(2 5, 3 0.9, 4 1.2)’) as MP) as foo;”

However, I would like to create a query that returns ‘POINT(2 5)’ rather than the YMAX value of 5.

Any thoughts?

Migrate MySQL 3.23 Data

$
0
0

I have .FRM, .MYD and .MYI files from a MySQL (version 3.23 I think) database from back in the early 2000′s. I managed to get the files into a Windows installation of 3.23 and then dump it, however I can’t get it into a version 4.0 installation. I’ve been trying to migrate it from one version to another to another, however every step causes another issue, whether with the installation of an old version, or changes in reserved words, etc.

Is there anything out there to simplify this process? Any thoughts on how I can bring the data files to current compatibility? My end goal is to get it all into a PostgreSQL database.

To reiterate, all I have are the data files and only MYD, MYI, FRM. I don’t actually have a working installation from which to run queries. Even the Windows install I managed to get up and running seemed to pose challenges.

Schemas with the same name in a Redshift cluster?

$
0
0

Is it harmful in some way to have multiple schemas with the same name in a database cluster, specifically Redshift?

I want to create multiple databases, and then create a schema “tables” or “data” or something for each database. Is there any potential problem with that?

How compiled are prepared statements?

$
0
0

I found this explanation of how prepared statements are compiled, but I’m still unsure.

What exactly is “compiled” in the context of a prepared statement? Is a C compiler executed to create a custom executable?

If so, is that limited by prepareThreshold?

If not, what exactly is it compiled into, and are there any projects for converting prepared statements into executables?

OpenLayers WFS-T tinyows insert transaction: error GID value

$
0
0

I try to implement insertion of new point using Openlayers v2.13 saveStrategy calling tinyows which implements the WFS-T transaction; I use the example from http://mapserver.org/tinyows/openlayershowto.html – the code is exactly the same except for the layer.

I have two questions:

1) adding a new point implements a transaction which uses “MULTIPOINT” but my postgis layer is of type POINT – is there a way to force “POINT” ? My solution up to now is to use a TRIGGER in postgresql to do an INSTEAD OF to change MULTIPOINT TO POINT.

2) I get an error because the insertion creates a “gid” value which should not be included, as it is added by default by the counter. The TINYOWS log give me the following transaction and error:

SQL

INSERT INTO “arch_geo_dbt”.”p030104_v” (“gid”,”the_geom”) VALUES (’042159162153000033′,’010400002031BF0D00010000000101000000B69CF08B3069314130B8664669AD5541′)

ERROR:

ERROR: value “042159162153000033″ is out of range for type integer

Below my XML from the REQUEST POST

<wfs:Transaction xmlns:wfs="http://www.opengis.net/wfs" service="WFS" version="1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd"><wfs:Insert><feature:dbtcivici xmlns:feature="http://www.tinyows.org/"><feature:the_geom><gml:MultiPoint xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:900913"><gml:pointMember><gml:Point><gml:pos>1141040.5466402 5682597.1000195</gml:pos></gml:Point></gml:pointMember></gml:MultiPoint></feature:the_geom></feature:dbtcivici></wfs:Insert></wfs:Transaction>

Does anyone know how to solve this problem?
Thanks.
Francesco

How to know if two geometric figures are approximately equal?

$
0
0

I would like to know how to determine if two geometric figures (the_geom) are approximately equals.

By the way, I’m working with Python, PostgreSQL and Postgis

I have the following geometries:

0101000020110F0000FE546AD2413E5EC16D56617C93C74FC1

0101000020110F00009D7E6384413E5EC10185DD7F93C74FC1

When I see both figures on the map, the variation is imperceptible.

Any idea?


PostgreSQL pg_upgrade –link mode performance issue after upgrade

$
0
0

Trying to do PostgreSQL Upgrade from 9.0 to 9.3 latest version. Using pg_uprade –link mode, since the downtime is very minimal compare to other options like pg_dumpall and pg_upgrade with normal mode.

After upgrading executing a query to check time taking for execution. It is taking much time nearly 30 mins for 1st time and for 2nd time onwards it is taking 15 mins. The Same query when executed in 9.0 is not even taking max of 45 seconds

No of Rows : 150000 records
Using same postgresql.conf file
vaccumdb all : done after upgrade

want to know is there any thing else need to configure, To improve the performance.

No where i can see in documentation using pg_upgrade –link will degrade the performance.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows on the new cluster                        ok
Deleting files from new pg_clog                             ok
Copying old pg_clog to new server                           ok
Setting next transaction ID for new cluster                 ok
Deleting files from new pg_multixact/offsets                ok
Setting oldest multixact ID on new cluster                  ok
Resetting WAL archives                                      ok
Setting frozenxid and minmxid counters in new cluster       ok
Restoring global objects in the new cluster                 ok
Adding support functions to new cluster                     ok
Restoring database schemas in the new cluster

mydatabase

Setting minmxid counter in new cluster                      ok
Removing support functions from new cluster                 ok
Adding ".old" suffix to old global/pg_control               ok

If you want to start the old cluster, you will need to remove
the ".old" suffix from /opt/postgres/9.0/data/global/pg_control.old.
Because "link" mode was used, the old cluster cannot be safely
started once the new cluster has been started.

Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to analyze new cluster                      ok
Creating script to delete old cluster                       ok

Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade so,
once you start the new server, consider running:
    analyze_new_cluster.sh

Running this script will delete the old cluster's data files:
    delete_old_cluster.sh

i got message like above but i couldn’t find such files in postgres folder to execute.

I am using postgresql9.3.5 from openscg community

**postgresql.conf changes**

shared_buffers = 4GB
work_mem = 128MB
maintenance_work_mem = 256MB
effective_cache_size = 12GB

Compare timestamps [on hold]

$
0
0

I am using psql 9.4.1 amd am looking for a way to select from a table if a timestamp1 is smaller than another timestamp2. I’ve tried

select * from timestamps where timestamp1 < timestamp2;

but this query doesn’t return any results.

The query

select * from timestamps where timestamp1 != timestamp2;

returns results but I will actually need to know when timestamp1 is smaller than timestamp2.

The table timestamps looks somewhat like this:

+-------+---------------------------+---------------------------+
|id     |timestamp1                 |timestamp2                 |
+-------+---------------------------+---------------------------+
|1      |2015-05-01 01:02:03.456789 |2015-04-01 01:02:03.456789 |
+-------+---------------------------+---------------------------+
|2      |2015-02-21 01:02:03.456789 |2015-04-05 01:02:03.456789 |
+-------+---------------------------+---------------------------+
|3      |2015-01-21 01:02:03.456789 |2015-03-05 01:02:03.456789 |

Thanks!

Unique Index much bigger than the table itself

$
0
0

Pg 9.3 on debian 7.

I have a very huge table in rows, about 178 million (will turn 250 million in a week and then grow 500 thousand a day), but not that huge in size, wich is 20GB, it’s a small varchar, some timestamps and integers. I need to prevent a group of 5 columns to be inserted repeated, so I created an unique index on them.
The problem is that the index is 34GB now. My questions are: How can an unique index of 5 columns in a table of 9 be bigger than the full amount of data itself? Will this index ever be used, cause I guess it’s cheaper for the DBMS to scan the entire table instead. And is there any way to turn this index smaller or maybe have another solution to implement an unique constraint?

id_process_publication bigint
search_token character varying(25)
publication_date date
publication_page integer
id_publication_site integer
id_publication_book integer
insert_time timestamp
integration_consume_time timestamp
elastic_consume_time timestamp

Indexes:
"process_publication_pkey" PRIMARY KEY, btree (id_process_publication)
"idx_uniq_process_publication" UNIQUE, btree (search_token, publication_date, publication_page, id_publication_site, id_publication_book)
"idx_uniq_process_publication_where_elastic_consume_time_null" UNIQUE, btree (id_process_publication) WHERE elastic_consume_time IS NULL
"idx_uniq_process_publication_where_integration_consume_time_nul" UNIQUE, btree (id_process_publication) WHERE integration_consume_time IS NULL

Is there a way to improve this query further?

$
0
0

I’ve got a query that over time I’ve been improving. Originally it simply selected * from the records table, and the data was formatted on the webapp side. But that was slow and I wanted to make things faster. I found that I could format the output into the correct json in postgres very quickly but the query was UGLY as sin. Then postgres 9.4 came out and brought json_object_agg which made the query nice and simple, and even faster than before, and all I had to do was change the data type of the data column to be json (it had been storing json-formatted data anyway).

The current version of the query is:

select json_object_agg(day,data order by day) as jsobj from records

The records table is:

d records
                         Table "public.records"
 Column |  Type   |                      Modifiers                       
--------+---------+------------------------------------------------------
 id     | integer | not null default nextval('records_id_seq'::regclass)
 day    | date    | not null
 data   | json    | not null
Indexes:
    "records_pkey" PRIMARY KEY, btree (id)
    "records_day_key" UNIQUE CONSTRAINT, btree (day)

What I’m wondering is this: is there a way to improve the query further? Either making it more readable or more performant?

I think that because I never look inside the value of data and simply operate on it as a whole (simply inserting it into more json which can be done with blind inlining), that the json datatype is still faster.

The syntax for ordering within an aggregate function is weird, but helpful.

Log large queries with PGbouncer

$
0
0

Is there a way to ask pgbouncer to log any query that generates a result set larger than, say, 5MB?

Viewing all 1138 articles
Browse latest View live


Latest Images