q3c
Overview
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 1540 | q3c | No | Yes | No | Yes | No | Yes | - |
| Related | pg_sphere earthdistance postgis h3 pointcloud pgrouting pg_geohash mobilitydb qdgc pg_eviltransform |
|---|
Version
Build
You can build the RPM / DEB packages for q3c using pig build:
Install
You can install q3c directly. First, make sure the PGDG repository is added and enabled:
Install the extension using pig or apt/yum/dnf:
Create Extension:
Usage
Sources:
Q3C (Quad Tree Cube) is a PostgreSQL extension for fast sky-indexing of astronomical catalogues. It enables efficient spatial queries on spherical coordinates (right ascension and declination), including cone searches, ellipse searches, polygon queries, positional cross-matches, and nearest-neighbor lookups.
All angles (ra, dec, distances) are in degrees, proper motions in mas/year, and epochs in years (e.g. 2000.5, 2010.5). All Q3C function names start with the q3c_ prefix.
Table Preparation
To use Q3C, create a spatial index on your table with ra and dec columns (in degrees):
Optionally cluster the table by the index to ensure faster queries on large datasets:
Alternatively, reorder the table before indexing:
After indexing, analyze the table:
Functions
q3c_ang2ipix(ra, dec)– returns the ipix value (64-bit integer pixel identifier) for given ra and decq3c_dist(ra1, dec1, ra2, dec2)– returns the distance in degrees between two pointsq3c_dist_pm(ra1, dec1, pmra1, pmdec1, cosdec_flag, epoch1, ra2, dec2, epoch2)– returns distance in degrees between two points, taking proper motion into account. Thecosdec_flag(0 or 1) indicates whether the proper motion includes the cos(dec) term (1) or not (0).q3c_join(ra1, dec1, ra2, dec2, radius)– returns true if (ra1, dec1) is withinradiusspherical distance of (ra2, dec2). Use when the index onq3c_ang2ipix(ra2, dec2)is created.q3c_join_pm(ra1, dec1, pmra1, pmdec1, cosdec_flag, epoch1, ra2, dec2, epoch2, max_delta_epoch, radius)– likeq3c_joinbut takes proper motion into account.max_delta_epochis the maximum epoch difference possible between two tables.q3c_ellipse_join(ra1, dec1, ra2, dec2, major, ratio, pa)– likeq3c_join, except (ra1, dec1) must be within an ellipse with semi-major axismajor, axis ratioratio, and position anglepa(from north through east)q3c_radial_query(ra, dec, center_ra, center_dec, radius)– returns true if (ra, dec) is withinradiusdegrees of (center_ra, center_dec). Main function for cone searches. Requires index onq3c_ang2ipix(ra, dec).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), specified by semi-major axis, axis ratio, and positional angle.q3c_poly_query(ra, dec, poly)– returns true if (ra, dec) is within the spherical polygon specified as an array of RA/DEC values or a PostgreSQL polygon type. Uses the index.q3c_ipix2ang(ipix)– returns a two-element array of (ra, dec) corresponding to a given ipixq3c_pixarea(ipix, bits)– returns the spherical area corresponding to a given ipix at the pixelisation level given bybits(1 is smallest, 30 is the cube face)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. Does NOT use the q3c index.q3c_version()– returns the installed version of Q3C
Examples
Cone Search
Query all objects within 0.1 degrees of (ra, dec) = (11, 12):
The column names of the table must come first, and the search location after, otherwise the index will not be used.
Alternative cone search using q3c_join (can be faster for small tables):
Ellipse Search
Search for objects within an ellipse centered at (10, 20) with semi-major axis 1 degree, axis ratio 0.5, and PA of 10 degrees:
Polygon Search
Query objects inside a spherical polygon with vertices (0,0), (2,0), (2,1), (0,1):
Using PostgreSQL polygon type:
Positional Cross-Match
Cross-match table1 and table2 within 0.001 degrees. The index must exist on q3c_ang2ipix(ra, dec) of table2:
The ra/dec columns from the indexed table must be the 3rd and 4th arguments. This returns all pairs within the matching distance, not just nearest neighbors.
With per-object error radius:
Ellipse Cross-Match
Cross-match using elliptical error areas (e.g., matching within galaxy elliptical bodies):
Cross-Match with Proper Motion
Cross-match with proper motion correction. Assumes table1 has pmra, pmdec (mas/yr) and epoch columns, pmra includes cos(dec) factor, and max epoch difference is 30 years:
Nearest Neighbour (with NULLs for unmatched)
Returns the nearest neighbour for each row, with NULLs if no match exists within 1 arcsecond:
Nearest Neighbour (matched only)
Returns only objects that have neighbours:
Nearest Neighbour (CTE variant)
Uses a CTE with an object ID column (requires an index on the ID column):
Density Estimation
Estimate object density using pixelation depth of 25:
Note: Q3C does not have uniform pixel areas (unlike HEALPIX).
Limitations
- Querying very large polygons with diameter greater than ~25 degrees is not supported
- Polygons with more than 100 vertices are not supported
Performance Tips
- Ensure correct argument order in Q3C functions (e.g.,
q3c_radial_query(ra, dec, 120, 3, 1)notq3c_radial_query(120, 3, ra, dec, 1)) - Use
EXPLAINto verify the query plan uses bitmap scans on the Q3C index - If the planner chooses a bad plan, try:
SET enable_mergejoin TO off; SET enable_seqscan TO off; SET enable_hashjoin TO off; - Cluster the table using the Q3C index for best performance
- When combining
q3c_join()with additional filter clauses, use CTEs withMATERIALIZEDto avoid plan issues:
2.0.6 Correctness Fixes
Version 2.0.6 fixes memory errors, missed sub-milliarcsecond cone matches, polygon-cache reuse across different rows, and radial/ellipse queries near cube corners. Non-finite coordinates generally return no match; q3c_in_poly returns NULL for a non-finite point. Recheck application handling of invalid coordinates after updating the library and SQL extension.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)