GithubHelp home page GithubHelp logo

segasai / q3c Goto Github PK

View Code? Open in Web Editor NEW
74.0 12.0 27.0 1.42 MB

PostgreSQL extension for spatial indexing on a sphere

License: GNU General Public License v2.0

Makefile 3.39% C 92.60% Python 2.27% Perl 1.48% Shell 0.25%
postgresql sphere astronomy nearest-neighbors query coordinates

q3c's Introduction

Build Status Coverage Status

Q3C

Author: Sergey Koposov, University of Edinburgh

Copyright (c) 2004-2023 Sergey Koposov

Email: skoposov AT ed DOT ac DOT uk

Fresh versions of the software could be obtained here: https://github.com/segasai/q3c

To read more about the Q3C indexing, you can check out the paper published in ADASS conference proceedings http://adsabs.harvard.edu/abs/2006ASPC..351..735K The citation is "Koposov, S., & Bartunov, O. 2006, Astronomical Society of the Pacific Conference Series, 351, 735". Q3C is also registered in the ASCL library https://ascl.net/1905.008 . If you use Q3C, you are kindly asked to cite the 2006 paper. I am also always happy to hear about any usage of Q3C.

Prerequisites

In order to use Q3C you need to have a PostgreSQL database installed (version 9.1 or later). If you have PostgreSQL version lower than 9.1, you will need to use an older version of Q3C (1.4.x).

To successfully compile Q3C you must have pg_config in your PATH (that means that you may need to install the -devel versions of PostgreSQL packages)

Installation

  • make
  • make install
  • Execute "CREATE EXTENSION q3c" in the PostgreSQL client(psql) for the database where you plan to use q3c

After the installation you will have several new functions in PostgreSQL. All names of these functions start with the "q3c_" prefix.

Updating

If you are updating from previous version of q3c, you still need to do the make, make install steps, but after that you need to do

  • ALTER EXTENSION q3c UPDATE TO 'A.B.C';

instead of 'CREATE EXTENSION'. Here A.B.C is the placeholder for the version, i.e. '2.0.0'; You also may want to check what version of q3c is installed by either of following commands:

  • select q3c_version();
  • SELECT * FROM pg_available_extension_versions WHERE name ='q3c';

Table preparation for Q3C

To begin use Q3C for searches and cross-matches you should create the indexes on your tables.

In this demonstration we'll assume that you have the table called "mytable" with "ra" and "dec" columns (right ascension and declination in degrees).

First, you will need to create the spatial index, using the command:

my_db# CREATE INDEX ON mytable (q3c_ang2ipix(ra, dec));

The next procedure is optional but strongly recommended: cluster the table using newly created index. The clustering procedure is the procedure of ordering the data on the disk according to the Q3C spatial index values, which will ensure faster queries if your table is very large. If the data have been ingested in the database in ordered fashion (i.e. along some spherical zones), the clustering step can be omitted (although still recommended). The clustering step may take a while (hours) if your dataset is large.

my_db# CLUSTER mytable_q3c_ang2ipix_idx ON mytable;

Alternatively, instead of CLUSTER, you can also just reorder your table yourself before indexing (can be faster) my_db# create table mytable1 as select * from mytable order by q3c_ang2ipix(ra,dec);

The last step is analyzing your table:

my_db# ANALYZE mytable;

Now you should be able to use q3c queries.

Q3C functions

IMPORTANT Throughout q3c it is assumed that all the angles (ra, dec and distances) are in units of angular degrees, the proper motions are in mas/year, and that the units for the epochs are years, i.e. 2000.5, 2010.5.

The functions installed by Q3C are:

  • q3c_ang2ipix(ra, dec) -- returns the ipix value at ra and dec

  • q3c_dist(ra1, dec1, ra2, dec2) -- returns the distance in degrees between two points (ra1,dec1) and (ra2,dec2)

  • q3c_dist_pm(ra1, dec1, pmra1, pmdec1, cosdec_flag, epoch1, ra2, dec2, epoch2) -- returns the distance in degrees between two points (ra1,dec1) and (ra2,dec2) at the epoch epoch2 while taking the proper motion into account. IMPORTANT The cosdec flag (0 or 1) indicates whether the provided proper motion includes the cos(dec) term (1) or not (0) . The previous versions (q3c 1.8) did not have that parameter and assumed pmra without cos(dec))

  • q3c_join(ra1, dec1, ra2, dec2, radius) -- returns true if (ra1, dec1) is within radius spherical distance of (ra2, dec2). It should be used when the index on q3c_ang2ipix(ra2, dec2) is created. See below for examples.

  • q3c_join_pm(ra1, dec1, pmra1, pmdec1, cosdec_flag, epoch1, ra2, dec2, epoch2, max_delta_epoch, radius) -- returns true if (ra1, dec1) is within radius spherical distance of (ra2, dec2). It takes into account the proper motion of the source pmra1, pmdec1 (in mas/yr) and epochs of the source coordinates epoch1, and epoch2 (in years). max_delta_epoch is the maximum epoch difference possible between two tables (i.e. if the oldest epoch in catalog1 is 1970 and the newest epoch in catalog2 is 2015, then the max_delta_epoch should be 45). You should use this function if the index on q3c_ang2ipix(ra2,dec2) was created. IMPORTANT The cosdec flag (0 or 1) indicates whether the provided proper motion includes the cos(dec) term (1) or not (0) . The previous versions (q3c 1.8) did not have that parameter and assumed pmra without cos(dec))

  • q3c_ellipse_join(ra1, dec1, ra2, dec2, major, ratio, pa) -- like q3c_join, except (ra1, dec1) have to be within an ellipse with semi-major axis major, the axis ratio ratio and the position angle pa (from north through east)

  • q3c_radial_query(ra, dec, center_ra, center_dec, radius) -- returns true if ra, dec is within radius degrees of center_ra, center_dec. This is the main function for cone searches. This function should be used when the index on q3c_ang2ipix(ra,dec) is created.

  • q3c_ellipse_query(ra, dec, center_ra, center_dec, maj_ax, axis_ratio, PA ) -- returns true if ra, dec is within the ellipse from center_ra, center_dec. The ellipse is specified by semi-major axis, axis ratio and positional angle. This function should be used if when the index on q3c_ang2ipix(ra,dec) is created.

  • q3c_poly_query(ra, dec, poly) -- returns true if ra, dec is within the spherical polygon specified as an array of right ascensions and declinations Alternatively poly can be an PostgreSQL polygon type. This function uses the index for faster queries, assuming the index on q3c_ang2ipix(ra,dec) was created.

  • q3c_ipix2ang(ipix) -- returns a two-element array of (ra,dec) corresponding to a given ipix.

  • q3c_pixarea(ipix, bits) -- returns the area corresponding to a given ipix at the pixelisation level given by bits (1 is smallest, 30 is the cube face) in steradians.

  • q3c_ipixcenter(ra, dec, bits) -- returns the ipix value of the pixel center at certain pixel depth covering the specified (ra,dec)

  • q3c_in_poly(ra, dec, poly) -- returns true/false if point is inside a polygon. This function will not use the index.

  • q3c_version() -- returns the version of Q3C that is installed

Query examples

  • The cone search (the query of all objects within the circular region of the sky): For example to query all objects within radius of 0.1 deg from (ra,dec) = (11,12)deg in the table mytable you would do:
my_db# SELECT * FROM mytable WHERE q3c_radial_query(ra, dec, 11, 12, 0.1);

The order of arguments is important, so that the column names of the table should come first, and the location where you search after, otherwise the index won't be used.

There is also an alternative way of doing cone searches which could be a bit faster if the table that you are working with that table that is small. In that case q3c_radial_query may be too CPU heavy. So you may want to query the table:

  my_db# SELECT * FROM mytable WHERE q3c_join(11, 12, ra, dec, 0.1);
  • The ellipse search: search for objects within the ellipse from a given point:
my_db=# select * from mytable WHERE
	q3c_ellipse_query(ra, dec, 10, 20, 1, 0.5 ,10);

returns the objects which are within the ellipse with the center at (ra,dec)=(10,20) semi-major axis of 1 degree, axis ratio of 0.5 and positional angle of 10 degrees.

  • The polygonal query, i.e. the query of the objects which lie inside the region bounded by the polygon on the sphere. To query the objects in the polygon ((0,0),(2,0),(2,1),(0,1)) ) (this is the spherical polygon with following vertices: (ra=0, dec=0) ; (ra=2, dec=0); (ra=2, dec=1); (ra=0, dec=1)):
my_db# SELECT * FROM mytable WHERE
		q3c_poly_query(ra, dec, ARRAY[0, 0, 2, 0, 2, 1, 0, 1]);
  • The polygonal query using PostgreSQL polygon type
my_db# SELECT * FROM mytable WHERE
		q3c_poly_query(ra, dec, '((0, 0), (2, 0), (2, 1), (0, 1))'::polygon);
  • The positional cross-match of the tables: In this example we will assume that we have a huge table "table2" with ra and dec columns and an already created index on q3c_ang2ipix(ra,dec) and a smaller table "table1" with ra and dec columns.

    Now, if we want to cross-match the tables "table1" and "table2" by position with the crossmatch radius of 0.001 degrees, we would do it with the following query:

my_db# SELECT * FROM table1 AS a, table2 AS b WHERE
		q3c_join(a.ra, a.dec, b.ra, b.dec, 0.001);

The order of arguments is important again, because it determines whether an index is going to be used or not. The ra,dec columns from the table with the index should go after the ra,dec columns from the table without the index.

It is important that the query will return ALL the pairs within the matching distance, rather than just nearest neighbors. See the nearest neighbors queries below.

If every object in table1 have his own error circle ( we'll assume that the radius of that circle in degrees is stored in the column "err"), then you should run the query:

my_db# SELECT * FROM table1 AS a, table2 AS b WHERE
		q3c_join(a.ra, a.dec, b.ra, b.dec, a.err);
  • The positional cross-match of the tables with the ellipse error-area: (for example if you want to find all the objects from one catalogue which lies inside the elliptical bodies of the galaxies from the second catalogue)

It is possible to do the join when the error area of each record of the catalogue is an ellipse. Then you can do the query like this

my_db# SELECT * FROM table1 AS a, table2 AS b WHERE
		q3c_ellipse_join(a.ra, a.dec, b.ra, b.dec, a.maj_ax
		a.axis_ratio, a.PA);

where axis_ratio is the column with axis ratio of the ellipses and PA is the column with the positional angles of them, and maj_ax is the column with semi-major axes of those ellipses.

  • The positional cross-match of the tables with proper motions taken into account In this example we will assume that we have a huge table "table2" with ra and dec columns and an already created index on q3c_ang2ipix(ra,dec) and a smaller table "table1" with ra and dec columns. We will also assume that this table1 has an epoch column (in year units) as well as pmra, pmdec columns (in units of mas/yr), while the table2 only has the epoch column. We will also assume that the pmra columns has the cos(dec) factor. and that we know the upper bound on the epoch difference between the two catalogs is say 30 years. (it doesn't have to be precise, but it is important that the true largest epoch difference is not larger than the specified number).

    Now, if we want to cross-match the tables "table1" and "table2" by position with the crossmatch radius of 0.001 degrees, we would do it with the following query:

my_db# SELECT * FROM table1 AS a, table2 AS b WHERE
		q3c_join_pm (a.ra, a.dec, a.pmra, a.pmdec, 1,
		a.epoch, b.ra, b.dec, b.epoch, 30, 0.001);
  • The density estimation of your objects using pixelation depth of 25:
my_db# SELECT (q3c_ipix2ang(i))[1] as ra ,(q3c_ipix2ang(i))[2] as dec ,c,
				q3c_pixarea(i,25) as area from 
					(select q3c_ipixcenter(ra,dec, 25) as i, count(*) as c from
						mytable group by i) as x;

returns the list of ra,dec of the Q3C pixel center, number of objects within a given pixel, and pixel area. If you use that query you should keep in mind that Q3C doesn't have the property of uniform pixel areas (as opposed to HEALPIX).

  • Nearest neighbour queries: This query selects the only nearest neighbour for each row in your table. If there is no neighbor, the columns are filled with nulls.
my_db# SELECT  t.*, ss.* FROM mytable AS t
       LEFT JOIN LATERAL (
               SELECT s.* 
                    FROM 
                        sdssdr9.phototag AS s
                    WHERE
                        q3c_join(t.ra, t.dec, s.ra, s.dec, 1./3600)
                    ORDER BY
                        q3c_dist(t.ra,t.dec,s.ra,s.dec)
                    ASC LIMIT 1
               ) as ss ON true;

The idea behind the query is that for every row of your table LATERAL() executes the subquery, that returns all the neighbours within the aperture and then orders them by distance takes the top one.

If you want only the objects that have the neighbours then the query will look like that

my_db# SELECT  t.*, ss.* FROM mytable AS t,
       LATERAL (
               SELECT s.* 
                    FROM 
                        sdssdr9.phototag AS s
                    WHERE
                        q3c_join(t.ra, t.dec, s.ra, s.dec, 1./3600)
                    ORDER BY
                        q3c_dist(t.ra,t.dec,s.ra,s.dec)
                    ASC LIMIT 1
               ) as ss ;
  • Nearest neighbour 2

This query selects the only nearest neighbour for each row in your table. If there are no neighbours, the columns are filled with nulls. This query requires presence of some object id column with the index on the table.

my_db# WITH x AS MATERIALIZED (
      SELECT *, ( SELECT objid FROM sdssdr9.phototag AS p WHERE q3c_join(m.ra, m.dec, p.ra, p.dec, 1./3600)
                  ORDER BY q3c_dist(m.ra, m.dec, p.ra, p.dec) ASC LIMIT 1) AS match_objid  FROM mytable AS m 
          )
    SELECT * FROM x, sdssdr9.phototag AS s WHERE x.match_objid=s.objid;

Limitations

  • Querying of very large polygons occupying area with the diameter > 25 degrees or so is not supported
  • Polygons with more than 100 vertices are not supported

Performance issues/Slow queries

If you experience slow q3c queries, the following list may suggest possible solutions.

  • Check that you are using the correct order of arguments in the q3c functions. I.e. q3c_radial_query(120,3,ra,dec,1) instead of q3c_radial_query(ra,dec,120,3,1)
  • Verify the plan of the query using 'EXPLAIN ...' command. That will tell you how PG tries to execute it. If you see something involving merge_join, or just seq scans (instead of bitmap scans using the q3c index), likely the plan is wrong and you have to fix it
  • Force Postgresql to use the q3c_index by disabling seq scans or merge and hash joins by setting 'set enable_mergejoin to off; set enable_seqscan to off; set enable hashjoin to off;'
  • Cluster your table using q3c index to sort your table by position.
  • Check if you are using q3c_join() query together with additional clauses. I.e. the query select * from t1, t2 where q3c_join(t1.ra,t1.dec,t2.ra,t2.dec,1./3600) and t1.mag<1 and t2.mag>33 likely will NOT execute properly, you will likely need to rewrite it as
WITH x AS MATERIALIZED (select * from t1 where t1.mag<1) 
   y as (select *, t2.mag as t2mag from x, t2 where q3c_join(t1.ra,t1.dec,t2.ra,t2.dec,1./3600) )
   select * from y where t2mag>33

q3c's People

Contributors

df7cb avatar esabol avatar segasai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

q3c's Issues

Getting a 'polygon is too large' error.

Hi,

I have the following query using a polygon generated as the boundary of a region (from a FITS header):

SELECT * FROM my_catalog WHERE q3c_poly_query(ra, dec, '((255.3121984665786, 18.92312700639222), (255.3143170639072, 18.9386188738895), (255.3164420501668, 18.95411450130444), (255.3185734189125, 18.9696136942468), (255.3207111629718, 18.98511625949686), (255.3228552744448, 19.00062200500532), (255.3250057447032, 19.01613073989319), (255.3271625643902, 19.03164227445179), (255.3293257234205, 19.04715642014253), (255.3314952109791, 19.06267298959681), (255.3336710155218, 19.07819179661602), (255.335853124774, 19.09371265617135), (255.3380415257311, 19.10923538440376), (255.3402362046577, 19.12475979862382), (255.3424371470873, 19.14028571731165), (255.3446443378217, 19.15581296011687), (255.3468577609312, 19.17134134785842), (255.3490773997538, 19.18687070252457), (255.3513032368947, 19.20240084727275), (255.3535352542262, 19.21793160642952), (255.3557734328875, 19.23346280549046), (255.3580177532839, 19.24899427112011), (255.3602681950864, 19.26452583115187), (255.3625247372319, 19.28005731458791), (255.3647873579223, 19.29558855159914), (255.3670560346244, 19.31111937352509), (255.3693307440691, 19.32664961287385), (255.3716114622518, 19.34217910332202), (255.3738981644311, 19.3577076797146), (255.3761908251292, 19.37323517806494), (255.3784894181313, 19.38876143555468), (255.3807939164848, 19.4042862905337), (255.3831042924995, 19.41980958252001), (255.3854205177471, 19.43533115219972), (255.3877425630606, 19.45085084142696), (255.390070398534, 19.46636849322387), (255.3924039935222, 19.48188395178047), (255.3947433166403, 19.49739706245466), (255.3970883357634, 19.51290767177215), (255.3994390180261, 19.5284156274264), (255.4017953298224, 19.54392077827858), (255.4041572368051, 19.5594229743575), (255.4065247038853, 19.5749220668596), (255.4088976952324, 19.59041790814889), (255.4112761742737, 19.60591035175687), (255.4136601036936, 19.62139925238256), (255.4160494454337, 19.63688446589239), (255.4184441606922, 19.65236584932019), (255.4208442099237, 19.66784326086715), (255.4232495528387, 19.68331655990185), (255.4256601484031, 19.69878560696005), (255.4092289742708, 19.7009057247165), (255.392798380428, 19.70302314707616), (255.376368427323, 19.70513794574351), (255.3599391705132, 19.70725019083826), (255.343510660665, 19.70935995089326), (255.3270829435549, 19.71146729285232), (255.3106560600697, 19.71357228206791), (255.2942300462075, 19.71567498229921), (255.2778049330776, 19.71777545570963), (255.2613807469016, 19.71987376286484), (255.2449575090137, 19.72196996273055), (255.2285352358611, 19.72406411267019), (255.212113939005, 19.72615626844291), (255.1956936251207, 19.72824648420131), (255.1792742959982, 19.73033481248926), (255.162855948543, 19.7324213042397), (255.1464385747763, 19.73450600877251), (255.1300221618355, 19.73658897379226), (255.1136066919751, 19.73867024538615), (255.0971921425669, 19.74074986802171), (255.0807784861005, 19.74282788454463), (255.0643656901839, 19.74490433617666), (255.0479537175441, 19.7469792625134), (255.0315425260274, 19.74905270152208), (255.0151320685998, 19.75112468953943), (254.998722293348, 19.75319526126952), (254.9823131434793, 19.75526444978149), (254.9659045573223, 19.75733228650748), (254.9494964683277, 19.75939880124043), (254.9330888050682, 19.76146402213181), (254.9166814912392, 19.76352797568963), (254.9002744456597, 19.76559068677609), (254.8838675822719, 19.76765217860547), (254.8674608101425, 19.76971247274198), (254.8510540334626, 19.77177158909756), (254.8346471515484, 19.7738295459297), (254.8182400588416, 19.77588635983929), (254.8018326449098, 19.77794204576836), (254.785424794447, 19.77999661699806), (254.769016387274, 19.7820500851463), (254.7526072983387, 19.78410246016575), (254.7361973977169, 19.78615375034146), (254.7197865506125, 19.78820396228892), (254.7033746173576, 19.79025310095163), (254.6869614534134, 19.7923011695991), (254.6705469093705, 19.7943481698246), (254.6541308309492, 19.79639410154296), (254.6377130589998, 19.79843896298841), (254.6212934295035, 19.80048275071235), (254.6048717735721, 19.80252545958125), (254.6025497945791, 19.78709200433929), (254.6002326510131, 19.77165530716474), (254.5979203780645, 19.75621546518594), (254.5956130101798, 19.7407725766955), (254.593310581062, 19.7253267411503), (254.5910131236713, 19.70987805917137), (254.5887206702246, 19.69442663254415), (254.586433252197, 19.6789725642182), (254.5841509003213, 19.66351595830749), (254.5818736445888, 19.64805692009019), (254.5796015142492, 19.63259555600884), (254.5773345378118, 19.61713197367024), (254.5750727430448, 19.60166628184558), (254.5728161569763, 19.58619859047039), (254.5705648058949, 19.57072901064459), (254.5683187153492, 19.55525765463247), (254.5660779101492, 19.53978463586278), (254.5638424143656, 19.52431006892868), (254.5616122513312, 19.50883406958784), (254.5593874436402, 19.49335675476241), (254.5571680131497, 19.47787824253908), (254.5549539809791, 19.46239865216912), (254.5527453675111, 19.44691810406836), (254.5505421923913, 19.43143671981731), (254.5483444745297, 19.41595462216112), (254.5461522321001, 19.40047193500964), (254.5439654825408, 19.38498878343748), (254.5417842425552, 19.36950529368406), (254.5396085281115, 19.35402159315358), (254.5374383544438, 19.33853781041515), (254.5352737360521, 19.3230540752028), (254.5331146867027, 19.30757051841551), (254.5309612194283, 19.29208727211729), (254.5288133465292, 19.27660446953724), (254.5266710795726, 19.26112224506953), (254.5245344293936, 19.24564073427355), (254.5224034060955, 19.23016007387389), (254.5202780190501, 19.21468040176045), (254.5181582768979, 19.19920185698847), (254.5160441875487, 19.18372457977859), (254.513935758182, 19.16824871151692), (254.5118329952468, 19.1527743947551), (254.5097359044629, 19.13730177321036), (254.5076444908206, 19.12183099176564), (254.5055587585809, 19.10636219646954), (254.5034787112767, 19.0908955345365), (254.501404351712, 19.07543115434682), (254.4993356819635, 19.05996920544674), (254.4972727033801, 19.04450983854854), (254.4952154165833, 19.02905320553058), (254.5116030592, 19.02697688253724), (254.5279844649522, 19.02489841535069), (254.5443599581396, 19.02281785058198), (254.560729858222, 19.02073523309787), (254.5770944798202, 19.0186506060232), (254.5934541327146, 19.0165640107432), (254.6098091218452, 19.01447548690559), (254.6261597473117, 19.01238507242291), (254.6425063043725, 19.01029280347468), (254.6588490834452, 19.00819871450985), (254.6751883701054, 19.00610283824868), (254.6915244450871, 19.00400520568525), (254.707857584282, 19.00190584608953), (254.7241880587393, 18.9998047870096), (254.7405161346652, 18.99770205427386), (254.7568420734229, 18.99559767199324), (254.773166131532, 18.9934916625634), (254.7894885606684, 18.99138404666685), (254.8058096076635, 18.98927484327523), (254.8221295145044, 18.98716406965136), (254.8384485183333, 18.98505174135158), (254.8547668514472, 18.98293787222778), (254.8710847412977, 18.98082247442959), (254.88740241049, 18.97870555840663), (254.9037200767835, 18.97658713291062), (254.9200379530909, 18.97446720499746), (254.9363562474778, 18.97234578002953), (254.9526751631624, 18.97022286167776), (254.9689948985156, 18.96809845192376), (254.9853156470596, 18.96597255106203), (255.0016375974687, 18.96384515770214), (255.017960933568, 18.96171626877076), (255.0342858343337, 18.95958587951385), (255.0506124738921, 18.95745398349891), (255.0669410215196, 18.95532057261696), (255.0832716416424, 18.95318563708483), (255.0996044938357, 18.95104916544713), (255.1159397328236, 18.94891114457863), (255.1322775084786, 18.94677155968623), (255.1486179658213, 18.94463039431107), (255.1649612450199, 18.94248763033083), (255.1813074813896, 18.94034324796172), (255.1976568053925, 18.93819722576072), (255.214009342637, 18.93604954062771), (255.2303652138774, 18.93390016780755), (255.2467245350136, 18.93174908089231), (255.2630874170904, 18.92959625182332), (255.2794539662971, 18.92744165089341), (255.2958242839675, 18.92528524674899))'::polygon);

This is a plot of the polygon in question:

ploygon_plot

The error I am getting is:

ERROR:  The polygon is too large. Polygons having diameter >~23 degrees are unsupported
CONTEXT:  SQL function "q3c_poly_query" during inlining

I have code where the function does work. Can you suggest what might be the problem?

pg_repack q3c index

I'm dealing with some huge tables for which clustering after creating the q3c index can take several days, during which the tables are locked (I cannot even create indexes for other tables). A non-locking alternative seems to be pg_repack. Do you have experience with it and know if it will work with q3c indixes?

Moving Q3C to a different schema?

I am trying to move the Q3C extension to a different schema than where it was originally installed, using the following ALTER EXTENSION call:

ALTER EXTENSION q3c SET SCHEMA utils;

But that results in the following error message:

ERROR:  extension "q3c" does not support SET SCHEMA

Turns out that Q3C is installed with relocatable=False, meaning that I can't move it. I could, in principle drop the extension and install it again, but what would require me to drop all the dependencies as well, which I REALLY don't want to do! (billions of rows and a long down-time!)

Is there a reason for Q3C not to be relocatable? If not, should something be changed in the "installer"? Is there a way to move it to a different schema, without dropping the extension?

Add instructions for upgrading to README.md?

May I suggesting adding some instructions to README.md regarding how to upgrade an existing installation? It's really simple, but I think it's worth mentioning.

ALTER EXTENSION q3c UPDATE;

Or to a specific version:

ALTER EXTENSION q3c UPDATE TO '2.0.0';

This query is also useful:

SELECT * FROM pg_available_extension_versions WHERE name ='q3c';

Or maybe there there could be a make upgrade target in the Makefile which does make install and then does the ALTER EXTENSION q3c UPDATE; automatically?

q3c_pixarea minor bug

The depth claimed to need to be less than 30, but 30 works. Minor fence posting error.

gsh=# select q3c_pixarea(0,31);
ERROR: Invalid depth. It should be less than 30.
gsh=# select q3c_pixarea(0,30);
q3c_pixarea

2.09439510239319
(1 row)
gsh=#

Creating an index xrashes the instance

What steps will reproduce the problem?
1. Deploy q3c on 9.0.1 instance with 16kb block size
2. try to create an index whichi crashes the instance,, i.e.:
2011-04-21 14:29:56.811 
CEST,"nienarto","surveys",24251,"::1:37146",4db0232f.5ebb,1,"CREATE 
INDEX",2011-04-21 14:29:35 CEST,5/6,0,ERROR,42883,"function q3c_ang2ipix(double 
precision, double precision) does not exist",,"No function matches the given 
name and argument types. You might need to add explicit type casts.",,,,"CREATE 
INDEX q3c_sources_part_hip_ogle_sdss_cu5_bord_idx ON 
sources_part_hip_ogle_sdss_cu5_bord (q3c_ang2ipix(alpha, delta)); 
",98,"ParseFuncOrColumn, parse_func.c:310","pgAdmin III - Query Tool"
2011-04-21 14:30:09.562 CEST,,,14758,,4d459119.39a6,11,,2011-01-30 17:26:01 
CET,,0,LOG,00000,"server process (PID 24251) was terminated by signal 11: 
Segmentation fault",,,,,,,,"LogChildExit, postmaster.c:2845",""
2011-04-21 14:30:09.562 CEST,,,14758,,4d459119.39a6,12,,2011-01-30 17:26:01 
CET,,0,LOG,00000,"terminating any other active server 
processes",,,,,,,,"HandleChildCrash, postmaster.c:2659",""
2011-04-21 14:30:09.562 
CEST,"oglehip","surveys",23832,"::1:37145",4db022dd.5d18,5,"idle",2011-04-21 
14:28:13 CEST,4/0,0,WARNING,57P02,"terminating connection because of crash of 
another server process","The postmaster has commanded this server process to 
roll back the current transaction and exit, because another server process 
exited abnormally and possibly corrupted shared memory.","In a moment you 
should be able to reconnect to the database and repeat your 
command.",,,,,,"quickdie, postgres.c:2626","pgAdmin III - Query Tool"
2011-04-21 14:30:09.562 
CEST,"oglehip","surveys",23827,"::1:37144",4db022da.5d13,1,"idle",2011-04-21 
14:28:10 CEST,3/0,0,WARNING,57P02,"terminating connection because of crash of 
another server process","The postmaster has commanded this server process to 
roll back the current transaction and exit, because another server process 
exited abnormally and possibly corrupted shared memory.","In a moment you 
should be able to reconnect to the database and repeat your 
command.",,,,,,"quickdie, postgres.c:2626","pgAdmin III - Browser"
2011-04-21 14:30:09.562 CEST,,,23419,,4db0228b.5b7b,2,,2011-04-21 14:26:51 
CEST,1/0,0,WARNING,57P02,"terminating connection because of crash of another 
server process","The postmaster has commanded this server process to roll back 
the current transaction and exit, because another server process exited 
abnormally and possibly corrupted shared memory.","In a moment you should be 
able to reconnect to the database and repeat your command.",,,,,,"quickdie, 
postgres.c:2626",""

What is the expected output? What do you see instead?
well, index created

What version of the product are you using? On what operating system?
in-house compiled PG: 9.0.1, ubuntu 10.10 x64 2.6.32-26-generic #48-Ubuntu SMP 
Wed Nov 24 10:14:11 UTC 2010 x86_64 GNU/Linux

Cheers,
Krzysztof


Original issue reported on code.google.com by [email protected] on 21 Apr 2011 at 12:10

distance near pole

I've test two points' distance near pole using sentence
select degrees(q3c_dist(radians(0),radians(90),radians(180),radians(89.9)))
the distance should be 0.1 degree, but it returns "179.932445023645"
even
select degrees(q3c_dist(radians(0),radians(90),radians(180),radians(90)))
returns "179.932342092551"

Objects near polygon boundary not selected by q3c_poly_query()

What steps will reproduce the problem?

1. Create a simple two column table with two double precision fields: ra and 
dec. Populate the table with uniformly spaced values of ra and dec, centered at 
(ra = 45.0, dec = 90.0), and a total width of 2 degrees on a side. Create a q3c 
index on the table with q3c_ang2ipix.

2. Define a four-sided polygon whose vertices lie just a few arcseconds outside 
the range of ra and dec values in the table, e.g. 
{88.999,43.999,88.999,46.001,91.001,46.001,91.001,43.999}.  

3. Select objects from the table using q3c_poly_query and the polygon defined 
in step 2.

What is the expected output? What do you see instead?

I expect the query to return all the rows in the table. However, a few rows 
near one boundary are not selected, as shown in Fig_1.png. The blue dots show 
the coordinates of selected objects; the white dots show ones that should have 
been selected, but were not. 


What version of the product are you using? On what operating system?

I'm using:

* q3c-1.4.17
* Postgresql v9.2.1
* Linux (2.6.18-308.4.1.el5)


Please provide any additional information below.

It appears that this problem is independent of RA, but strongly dependent on 
the absolute value of declination.  The number of objects 'missed' by 
poly_query increases as you move away from 0.0, but starts to decrease again as 
you move to the poles (above ~80 degrees).  The 'missing' objects always appear 
only on the boundary closest to dec = 0.0. 

Original issue reported on code.google.com by [email protected] on 18 Dec 2012 at 3:18

Attachments:

"make test" should "drop database if exists q3c_test" before "createdb q3c_test"?

Minor thing: I just upgraded our installation from q3c 1.5.0 to 2.0.0, and I mistakenly executed make test before make install. Well, the test failed (because of the proper motion syntax changes in 2.0.0), and it left the q3c_test database intact. Subsequent make test attempts failed because it doesn't expect the q3c_test database to exist.

> env PGUSER=postgres make test
createdb q3c_test
createdb: database creation failed: ERROR:  database "q3c_test" already exists
gmake: *** [test] Error 1

Also, wow, "make test" takes a lot longer to run now, like 5 x longer than with 1.5.0. Just a lot more testing going on, I guess?

Execution of make test fails.

The execution of

$ make test

fails:

gcc gen_data.c -lm -DQ3C_VERSION='"v1.4.24"' -O3 -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -I. -I./ -I/usr/include/postgresql/9.5/server -I/usr/include/postgresql/internal -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2 -I/usr/include/tcl8.6 -o gen_data
createdb q3c_test
psql q3c_test -c "CREATE TABLE test (ra double precision, dec double precision)"
CREATE TABLE
psql q3c_test -c "CREATE TABLE test1 (ra double precision, dec double precision)"
CREATE TABLE
psql q3c_test -c "CREATE TABLE test_small (ra double precision, dec double precision)"
CREATE TABLE
./gen_data 1 1000000 | psql q3c_test -c "COPY test FROM STDIN WITH DELIMITER ' '"
COPY 1000000
./gen_data 2 1000000 | psql q3c_test -c "COPY test1 FROM STDIN WITH DELIMITER ' '"
COPY 1000000
./gen_data 3 100000 | psql q3c_test -c "COPY test_small FROM STDIN WITH DELIMITER ' '"
COPY 100000
psql q3c_test -c '\i q3c.sql'
BEGIN
SET
psql:q3c.sql:8: ERROR: could not access file "$libdir/q3c": No such file or directory

This variables is created during the execution of the the command $make

sed 's,MODULE_PATHNAME,$libdir/q3c,g' q3c.sql.in >q3c.sql

But it looks like $libdir is not defined.

I am using

q3c-1.4.24
ubuntu 14.04
postgresql 9.5.2
and pg_config is defined in the path.

q3c regression test failures on ppc64el, and i386/trusty

Hi,
q3c is failing regression tests for apt.postgresql.org in all Debian and Ubuntu releases on ppc64el. All PG versions from 9.2..9.6 are affected. The diff is always:

00:02:25.466 diff results/ellipse.out expected/ellipse.expected
00:02:25.466 2838c2838
00:02:25.466 <    804
00:02:25.466 ---
00:02:25.466 >    803
00:02:25.466 Makefile:37: recipe for target 'test' failed

Additionally, it is failing on i386, Ubuntu trusty only (again, all PG versions affected):

00:06:32.113 diff results/ellipse.out expected/ellipse.expected
00:06:32.114 2838c2838
00:06:32.114 <    804
00:06:32.114 ---
00:06:32.114 >    803
00:06:32.114 make: *** [test] Error 1

Btw, diff -u would make the output more easy to inspect. Or maybe even better, did you consider using pg_regress instead? It would take care of all the run-and-check-diff logic. Using it should be as easy as adding this to Makefile:

REGRESS=ang2ipix area cone ellipse join poly version

Thanks!

Compilation error against PostgreSQL 12rc1

Hello,

I am trying to compile Q3C against PostgreSQL 12RC1 and I get this output:

gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC  -O3 -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -DQ3C_VERSION='"'1.8.0'"' -I. -I./ -I/usr/custom/postgresql-12rc1/include/server -I/usr/custom/postgresql-12rc1/include/internal  -D_GNU_SOURCE   -c -o prepare.o prepare.c
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC  -O3 -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -DQ3C_VERSION='"'1.8.0'"' -I. -I./ -I/usr/custom/postgresql-12rc1/include/server -I/usr/custom/postgresql-12rc1/include/internal  -D_GNU_SOURCE   -c -o q3cube.o q3cube.c
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2 -fPIC  -O3 -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -DQ3C_VERSION='"'1.8.0'"' -I. -I./ -I/usr/custom/postgresql-12rc1/include/server -I/usr/custom/postgresql-12rc1/include/internal  -D_GNU_SOURCE   -c -o q3c_poly.o q3c_poly.c
gcc -std=gnu99 prepare.o q3cube.o q3c_poly.o -lm -o prepare
q3cube.o: In function `q3c_dump_prm':
q3cube.c:(.text+0x4a72): undefined reference to `pg_fprintf'
q3cube.c:(.text+0x4a88): undefined reference to `pg_fprintf'
q3cube.c:(.text+0x4a99): undefined reference to `pg_fprintf'
q3cube.c:(.text+0x4ac0): undefined reference to `pg_fprintf'
q3cube.c:(.text+0x4ad5): undefined reference to `pg_fprintf'
q3cube.o:q3cube.c:(.text+0x4af5): more undefined references to `pg_fprintf' follow
collect2: error: ld returned 1 exit status
make: *** [prepare] Error 1

Is it possible that something changed somewhere in v12? I do see a change in the definition of fprint between 11.5 and 12.

11.5 (from installed directory)

% grep -r pg_fprintf
include/internal/port.h:167:extern int	pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2, 3);
include/internal/port.h:180:#define fprintf(...)	pg_fprintf(__VA_ARGS__)
include/internal/port.h:187:#define fprintf			pg_fprintf
include/server/port.h:167:extern int	pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2, 3);
include/server/port.h:180:#define fprintf(...)	pg_fprintf(__VA_ARGS__)
include/server/port.h:187:#define fprintf			pg_fprintf

12RC1:

include/internal/port.h:179:extern int	pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2, 3);
include/internal/port.h:196:#define fprintf			pg_fprintf
include/server/port.h:179:extern int	pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2, 3);
include/server/port.h:196:#define fprintf			pg_fprintf
include/server/plpython.h:161:#define fprintf			pg_fprintf

And this may or may not be helpful.

This is on CentOS Linux with gcc v. 4.8.5. The output on macOS 10.14:

gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -Wno-unused-command-line-argument -O2   -O3 -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -DQ3C_VERSION='"'1.8.0'"' -I. -I./ -I/usr/local/postgresql-12rc1/include/server -I/usr/local/postgresql-12rc1/include/internal  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk   -I/usr/local/readline-8.0/include  -c -o prepare.o prepare.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -Wno-unused-command-line-argument -O2   -O3 -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -DQ3C_VERSION='"'1.8.0'"' -I. -I./ -I/usr/local/postgresql-12rc1/include/server -I/usr/local/postgresql-12rc1/include/internal  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk   -I/usr/local/readline-8.0/include  -c -o q3cube.o q3cube.c
q3cube.c:1328:44: warning: format specifies type 'long long' but the argument has type 'q3c_ipix_t' (aka 'long') [-Wformat]
                fprintf(fp, "Q3C_CONST("Q3C_IPIX_FMT")", xbits[i]);
                                        ~~~~~~~~~~~~     ^~~~~~~~
q3cube.c:1341:44: warning: format specifies type 'long long' but the argument has type 'q3c_ipix_t' (aka 'long') [-Wformat]
                fprintf(fp, "Q3C_CONST("Q3C_IPIX_FMT")", ybits[i]);
                                        ~~~~~~~~~~~~     ^~~~~~~~
q3cube.c:1354:44: warning: format specifies type 'long long' but the argument has type 'q3c_ipix_t' (aka 'long') [-Wformat]
                fprintf(fp, "Q3C_CONST("Q3C_IPIX_FMT")", xbits1[i]);
                                        ~~~~~~~~~~~~     ^~~~~~~~~
q3cube.c:1367:44: warning: format specifies type 'long long' but the argument has type 'q3c_ipix_t' (aka 'long') [-Wformat]
                fprintf(fp, "Q3C_CONST("Q3C_IPIX_FMT")", ybits1[i]);
                                        ~~~~~~~~~~~~     ^~~~~~~~~
q3cube.c:1372:64: warning: format specifies type 'long long' but the argument has type 'q3c_ipix_t' (aka 'long') [-Wformat]
        Q3C_IPIX_FMT",____xbits,____ybits,____xbits1,____ybits1};\n", hprm->nside);
        ~~~~~~~~~~~~                                                  ^~~~~~~~~~~
5 warnings generated.
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -Wno-unused-command-line-argument -O2   -O3 -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -DQ3C_VERSION='"'1.8.0'"' -I. -I./ -I/usr/local/postgresql-12rc1/include/server -I/usr/local/postgresql-12rc1/include/internal  -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk   -I/usr/local/readline-8.0/include  -c -o q3c_poly.o q3c_poly.c
gcc prepare.o q3cube.o q3c_poly.o -lm -o prepare
Undefined symbols for architecture x86_64:
  "_pg_fprintf", referenced from:
      _q3c_dump_prm in q3cube.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [prepare] Error 1

q3c_poly_query using postgresql polygon type

When using the q3c_poly_query function it has to be called using an string-array like shown in the examples:

SELECT * FROM mytable WHERE q3c_poly_query(ra, dec, '{0, 0, 2, 0, 2, 1, 0, 1}');

However, if I have a table of polygons (in column named "footprint", stores as PostgreSQL polygon objects (https://www.postgresql.org/docs/10/datatype-geometric.html) I would like to check which polygons the point is in:

SELECT * FROM mytable WHERE q3c_poly_query(69.57945923, -57.20383833, footprint);

This fails because q3c_poly_query does not accept polygons as input.

Wouldn't it make sense to have q3c_poly_query accept actual polygons as input?

Crash when q3c_join used for cone search

What steps will reproduce the problem?
1.  Create table, pos_large, of HEASARC observations with large areas or 
positional uncertainties.  This is a table of about 400K rows and has four 
columns: the original table the data is from, the ra, dec, and the default 
search radius (dsr) for that row.  Note that dsr can have values as large as 40 
degrees.
2. Using psql run a query of the form
  select count(*) 
     from pos_big 
     where q3c_join(99., 18., ra, dec, dsr)
3. Get an error message:
server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or after processing the request.

The log includes the following:
LOG:  server process (PID 19413) was terminated by signal 11: Segmentation fault
DETAIL:  Failed process was running: select count(*) from master_table.pos_big
         where q3c_join(  99,  18, ra , dec , dsr) ;
LOG:  terminating any other active server processes
WARNING:  terminating connection because of crash of another server process
DETAIL:  The postmaster has commanded this server process to roll back the 
current transaction and exit, because another server process exited abnormally 
and possibly corrupted shared memory.
--------------------
What is the expected output? What do you see instead?

If I run this query with 0.1*dsr rather than above it works fine.  As I 
increase the coefficient the query seems to crash when I'm getting about 7,000 
hits.

The count() doesn't make any difference.  A
   select * ...
fails too.

----------------------
What version of the product are you using? On what operating system?

Postgres 9.2.1
Q3C 1.4.13
Linux 2.6.18-308-13.1.el5PAE


--------------------------
Please provide any additional information below.

The q3c_radial_query function does work here, but it is a factor of 30 or more 
slower than q3c_join and not faster than more standard approaches.  In 
particular q3c_join is much,much faster when we do a cone search of our 
pos_small table (which has ~33 million rows).  The query takes less than a 
second so long as we put the constant values first and the row values second in 
the q3c_join function.  Q3c_radial_query takes about 30 seconds when querying 
our 33 million row table, about the same as a query using just a simple dec 
based index.

A workaround may be to use q3c_join for the pos_small table and not use q3c for 
the pos_big table, but this is inelegant, and it may simply be luck that we 
haven't seen the same problem in queries of pos_small (and indeed I just 
confirmed that it is possible to
set up queries that will fail there too if I specify a large search radius.  
The behavior suggests the problems is with the maximum radius, not the number 
of rows returned).

Original issue reported on code.google.com by [email protected] on 9 Oct 2012 at 6:50

Add selectivity estimate?

I'm still regularly struggling with abysimal query plans when q3c functions are in queries; disabling seqscans helps a bit of course, but it certainly is not an suboptimal and has unwelcome side effects in general.

Now the support functions Laurenz Albe reports on on https://www.cybertec-postgresql.com/en/optimizer-support-functions/ would seem to be helpful here; I would guess that giving q3c_join a selectivity of (roughly) match_radius**2/4 pi (so: circle area over sphere area) would already greatly improve the query plans (even acknowledging that star density varies by orders of magnitude over the sky).

Do you have plans to add support functions? Do you see major obstacles to at least a very rough implementation that would assume evenly distributed objects?

If I update the q3c version must we remake the indexes?

Hi,

we have recently migrated q3c from version 1.5 to version 2.0 (following the instruction included in the main page). Must we make the indexes from scratch (remove and make them again) or the already made indices are valid?

Cheers,

J.

Slow return when using q3c with EXIST

Hi there,

We're trying to carry out a query that seems to be taking much longer than we'd expect. It seems to boil down to:

SELECT t1.objectId FROM table1 AS t1 WHERE EXISTS (SELECT 1 FROM table2 AS t2 WHERE q3c_join(t2.ra_query, t2.dec_query, t1.ra, t1.dec, 0.0008333));

To give an idea of numbers: in our tests there are around 30 rows in t2, and around 10^8 rows in t1; t1 is indexed on (ra,dec) with q3c, t2 is not indexed. There should be 30 matches.

Oddly, the subquery is very quick when ran separately, and if we manually enter the results from the separately-run subquery into the main query, then it is also very quick. However, when put together, they are very slow.

Also oddly, when we replace (t2.ra_query, t2.dec_query) with scalar values, the whole query runs very quickly.

We were wondering if there's something specific that will cause q3c_join to work slowly when combined with an EXISTS or IN statement (we've also tried the latter with the same results).

Thanks for your help, and for q3c in general!

q3c_poly_query does not select objects inside a reasonably sized polygon

What steps will reproduce the problem?

1. Create a simple two column table with two double precision fields: ra and 
dec. Populate the table with uniformly spaced values of ra and dec, centered at 
(ra = 45.0, dec = 35.5), and a total width of 1 degree on a side. Create a q3c 
index on the table with q3c_ang2ipix.

2. Define a four-sided polygon whose vertices lie outside the range of ra and 
dec values in the table, e.g. {40,30,40,40,50,40,50,30}.  

3. Select objects from the table using q3c_poly_query and the polygon defined 
in step 2.


What is the expected output? What do you see instead?

I expect the query will select all rows in the table. However, the query only 
select a subset of the rows. 

The attached .png files illustrate the problem. 

Fig1.png shows the ra (x-axis) and dec (y-axis) of the rows returned by a 
'SELECT *' from the table. 

Fig2.png shows the ra and dec of the rows returned when filtering with 
q3c_poly_query, e.g. "SELECT * FROM table WHERE 
q3c_poly_query(ra,dec,'{40.0,30,40,40,50,40,50,30}');". 

Comparing the two figures, you can see that the polygon query is missing  rows 
that are inside the polygon.

The missing data problem persists if I change the vertices to be larger or 
smaller. 

After playing around with the vertices, the problem seems to occur whenever the 
point (45.0, 35.26) lies inside the polygon.  The problem goes away if the 
polygon does not contain that point.

Curiously, this same problem appears if either of two other points are inside 
the specified polygon: (135.0, 35.26) and (225.0, 35.26) (and provided the 
table has rows inside the polygon). 

I'd appreciate it if you have look into this.

What version of the product are you using? On what operating system?

I'm using:

* q3c-1.4.12-rev-ac0bf1cf8393
* Postgresql v9.2.1
* Linux (2.6.18-308.4.1.el5)


 - Greg Madsen


Original issue reported on code.google.com by [email protected] on 14 Dec 2012 at 3:28

Attachments:

Unexpected 'too large' error with q3c_poly_query()

What steps will reproduce the problem?

I have a table (usnob1) and three fields: ra (double), dec (double) and mag 
(real, NULL is allowed).  I've created a q3c index on the table with 
q3c_ang2ipix, and clustered the table on the index.  Up until now, I've used 
many q3c functions on this table with no problems. The usnob1 table has about 
300M rows and takes up ~60GB of disk space.

The crux of the problem is performing a query with q3c_poly_query() in a WHERE 
clause along with another filter on the 'mag' field.  The polygon is quite 
small (~0.25 deg on a side), but I am getting an error:

"ERROR:  The polygon is too large. Polygons having diameter >~23 degrees are 
unsupported"

An example query that causes the problem is:

SELECT ra, dec, mag
FROM usnob1 
WHERE 
q3c_poly_query(
ra, dec, 
{'{49.875,11.875,50.125,11.875,50.125,12.125,49.875,12.125}'}
)
AND 
mag < 20.0
;

If I change the coordinates of the polygon just a little bit (and preserve the 
area of the polygon), the problem goes away (sometimes). The problem seems to 
depend on the properties of the 'mag' field.  For example, if I keep the 
polygon vertices as above, but change the 'mag < 20.0' clause to 'mag > 10.0 , 
then I get rows with no error.  I have not been able to identify the full set 
of conditions on 'mag' that give the 'too large' error. 

If I use a brute force approach by putting constraints on the ra and dec fields 
(e.g. WHERE ra BETWEEN 49.875 AND 50.125, etc.), I get sensible results (but 
the query takes forever).  


What is the expected output? What do you see instead?

I expect to get a list of (~1000) objects from the table that satisfy the WHERE 
clause. Instead, I get the error message:

ERROR:  The polygon is too large. Polygons having diameter >~23 degrees are 
unsupported


********** Error **********

ERROR: The polygon is too large. Polygons having diameter >~23 degrees are 
unsupported
SQL state: XX000



What version of the product are you using? On what operating system?

Postgresql: 8.4.10
q3c: 1.4.12
OS: MacOS 10.6.8

Please provide any additional information below.


Original issue reported on code.google.com by [email protected] on 20 Apr 2012 at 5:40

Build failure on recent Debian and Ubuntu

q3c-1.4.19 can't be compiled on recent versions of Debian (since 7.0) and most 
probably Ubuntu, if you use stock packages for postgresql (9.1, 9.2, 9.3).

> make
gcc -DQ3C_VERSION='"release-1.4.18-1-g74a4237"'  -O3 -D_GNU_SOURCE 
-D__STDC_FORMAT_MACROS -I. -I./ -I/usr/include/postgresql/9.3/server 
-I/usr/include/postgresql/internal -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE 
-I/usr/include/libxml2  -I/usr/include/tcl8.5 -g -O2 -fstack-protector 
--param=ssp-buffer-size=4 -Wformat -Werror=format-security -fPIC -pie 
-I/usr/include/mit-krb5 -DLINUX_OOM_ADJ=0 -Wall -Wmissing-prototypes 
-Wpointer-arith -Wdeclaration-after-statement -Wendif-labels 
-Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv 
-fexcess-precision=standard -g -fpic prepare.o q3cube.o q3c_poly.o  -L/usr/lib 
-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -L/usr/lib/mit-krb5 
-L/usr/lib/x86_64-linux-gnu/mit-krb5  -Wl,--as-needed -lpgport -lpgcommon 
-lxslt -lxml2 -lpam -lssl -lcrypto -lkrb5 -lcom_err -lgssapi_krb5 -lz -ledit 
-lcrypt -ldl -lm  -o prepare
/usr/bin/ld: cannot find -lxslt
/usr/bin/ld: cannot find -lxml2
/usr/bin/ld: cannot find -lpam
/usr/bin/ld: cannot find -ledit
collect2: error: ld returned 1 exit status
make: *** [prepare] Error 1

The problem is due to multiarch - most of libraries (including these causing 
the errors) have been moved from /usr/lib to e.g. /usr/lib/x86_64-linux-gnu

The problem may be fixed with the following patch:
--- q3c-1.4.19/Makefile 2013-09-29 19:12:46.000000000 +0400
+++ q3c-1.4.19.fixed/Makefile   2014-01-26 04:06:17.068028941 +0400
@@ -43,7 +43,7 @@
    ./prepare

 prepare: prepare.o q3cube.o q3c_poly.o
-   $(CC) $(CPPFLAGS) $(CFLAGS) prepare.o q3cube.o q3c_poly.o $(PG_LIBS) 
$(LDFLAGS) $(LIBS) -o $@
+   $(CC) prepare.o q3cube.o q3c_poly.o -lm -o $@

 oldclean: 
    rm -f *~ tests/*~

Original issue reported on code.google.com by [email protected] on 26 Jan 2014 at 12:19

Compliation error with q3cube.c

What steps will reproduce the problem?

1. When making q3c-1.4.16, I get a couple of errors when compiling q3cube.c 

What is the expected output? What do you see instead?

I expect it to compile without errors. However, I get these messages:

> make
which: no git in 
(/export/tesla1/madsen/postgresql/bin:/usr/lib64/qt-3.3/bin:/usr/NX/bin:/usr/ker
beros/bin:/usr/local/bin:/bin:/usr/bin:/opt/dell/srvadmin/bin)
sed 's,MODULE_PATHNAME,$libdir/q3c,g' q3c.sql.in >q3c.sql
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith 
-Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute 
-Wformat-security -fno-strict-aliasing -fwrapv -fpic 
-DQ3C_VERSION="release-1.4.16"  -O3 -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -I. 
-I. -I/export/tesla1/madsen/postgresql/include/server 
-I/export/tesla1/madsen/postgresql/include/internal -D_GNU_SOURCE   -c -o q3c.o 
q3c.c
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith 
-Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute 
-Wformat-security -fno-strict-aliasing -fwrapv -fpic 
-DQ3C_VERSION="release-1.4.16"  -O3 -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -I. 
-I. -I/export/tesla1/madsen/postgresql/include/server 
-I/export/tesla1/madsen/postgresql/include/internal -D_GNU_SOURCE   -c -o 
prepare.o prepare.c
gcc -O2 -Wall -Wmissing-prototypes -Wpointer-arith 
-Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute 
-Wformat-security -fno-strict-aliasing -fwrapv -fpic 
-DQ3C_VERSION="release-1.4.16"  -O3 -D_GNU_SOURCE -D__STDC_FORMAT_MACROS -I. 
-I. -I/export/tesla1/madsen/postgresql/include/server 
-I/export/tesla1/madsen/postgresql/include/internal -D_GNU_SOURCE   -c -o 
q3cube.o q3cube.c
q3cube.c:32: error: ‘release’ undeclared here (not in a function)
q3cube.c:32:29: error: too many decimal points in number
make: *** [q3cube.o] Error 1

It appears as if my gcc doesn't like assigning Q3C_VERSION to a static char []. 



What version of the product are you using? On what operating system?

* q3c-1.4.16
* gcc version 4.1.2 20080704 (Red Hat 4.1.2-52)
* bash version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
* Linux  2.6.18-308.4.1.el5
* Postgres 9.2.1

Please provide any additional information below.


If I manually replace line 32 in q3cube.c with:

static char __q3c_version[] = "release-1.4.16";

then I have no compilation problems.

Original issue reported on code.google.com by [email protected] on 18 Dec 2012 at 2:27

support for PARALLEL SAFE

Would it make sense to add PARALLEL SAFE to the SQL definitions for the q3c functions?
The Postgresql Documentation states:

Functions should be labeled parallel unsafe if they modify any database state, or if they make changes to the transaction such as using sub-transactions, or if they access sequences or attempt to make persistent changes to settings (e.g., setval). They should be labeled as parallel restricted if they access temporary tables, client connection state, cursors, prepared statements, or miscellaneous backend-local state which the system cannot synchronize in parallel mode (e.g., setseed cannot be executed other than by the group leader because a change made by another process would not be reflected in the leader). In general, if a function is labeled as being safe when it is restricted or unsafe, or if it is labeled as being restricted when it is in fact unsafe, it may throw errors or produce wrong answers when used in a parallel query. C-language functions could in theory exhibit totally undefined behavior if mislabeled, since there is no way for the system to protect itself against arbitrary C code, but in most likely cases the result will be no worse than for any other function. If in doubt, functions should be labeled as UNSAFE, which is the default.

From my admittedly limited understanding of the guts of q3c, it doesn't try to do anything that would prevent it from being PARALLEL SAFE, so is this a feasible change?

gcc gen_data.c -lm -o gen_data -- > fatal error: postgres.h: No such file or directory

The execution of

$make test

throws the error fatal error: postgres.h: No such file or directory.

I have checked that the issue can be fixed making the following change in the Make file:

$(CC) $&lt; $(MYBINLIBS) $(CPPFLAGS) -o $@

I am using

q3c-1.4.24
ubuntu 14.04
postgresql 9.5.2
and pg_config is defined in the path.

Does this solution has sense for you?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.