Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "extern/rkwebutil"]
path = extern/rkwebutil
url = https://github.com/rknop/rkwebutil.git
[submodule "extern/pg_healpix"]
path = extern/pg_healpix
url = https://github.com/segasai/pg_healpix.git
7 changes: 7 additions & 0 deletions db/2025-07-19_npix_order13.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE EXTENSION IF NOT EXISTS pg_healpix;

ALTER TABLE diaobject ADD COLUMN npix_order13 BIGINT;

UPDATE diaobject SET npix_order13 = healpix_ang2ipix_nest(8192, ra, dec);

CREATE INDEX idx_diaobject_npix13 ON diaobject USING btree(npix_order13);
26 changes: 26 additions & 0 deletions db/2025-07-19_skymap_helpers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Helper functions for skymap cross-matching

-- Unpack a NUNIQ value into (order_m, ipix_coarse)
DROP FUNCTION IF EXISTS decode_uniq(BIGINT);
CREATE FUNCTION decode_uniq(nuniq BIGINT)
RETURNS TABLE(order_m INT, ipix_coarse BIGINT)
LANGUAGE plpgsql IMMUTABLE STRICT AS $$
DECLARE
m INT;
BEGIN
-- order_m is floor(log4(nuniq/4)) = floor(log2(nuniq/4)/2)
m := FLOOR(LOG(4.0, nuniq::numeric / 4));
order_m := m;
-- ipix_coarse is the remainder once the order prefix is removed
ipix_coarse := nuniq - (4 * CAST(POWER(2, m * 2) AS BIGINT));
RETURN NEXT;
END;
$$;

-- Check if a fine pixel belongs to the coarse pixel defined by order and ipix
DROP FUNCTION IF EXISTS is_within(BIGINT, INT, BIGINT);
CREATE FUNCTION is_within(npix BIGINT, order_m INT, ipix_coarse BIGINT)
RETURNS BOOLEAN
LANGUAGE SQL IMMUTABLE STRICT AS $$
SELECT (npix >> (2 * (13 - order_m))) = ipix_coarse;
$$;
9 changes: 9 additions & 0 deletions docker/postgres/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ RUN curl -L https://github.com/ossc-db/pg_hint_plan/archive/refs/tags/REL15_1_5_
&& make \
&& make install

RUN git clone https://github.com/segasai/pg_healpix.git /build/pg_healpix \
&& cd /build/pg_healpix \
&& make USE_PGXS=1 \
&& make USE_PGXS=1 install \
&& rm -rf /build/pg_healpix

# ======================================================================
FROM base AS postgres

Expand All @@ -70,6 +76,9 @@ COPY --from=build /usr/lib/postgresql/15/lib/pg_hint_plan.so usr/lib/postgresql/
COPY --from=build /usr/lib/postgresql/15/lib/bitcode/pg_hint_plan /usr/share/postgresql/lib/bitcode/pg_hint_plan
COPY --from=build /usr/lib/postgresql/15/lib/bitcode/pg_hint_plan.index.bc /usr/share/postgresql/lib/bitcode/pg_hint_plan.index.bc
COPY --from=build /usr/share/postgresql/15/extension/pg_hint_plan* /usr/share/postgresql/15/extension/
# pg_healpix extension
COPY --from=build /usr/lib/postgresql/15/lib/pg_healpix.so /usr/lib/postgresql/15/lib/pg_healpix.so
COPY --from=build /usr/share/postgresql/15/extension/pg_healpix* /usr/share/postgresql/15/extension/

# Make sure this matches what is in the config file (created just above)
# (There is some futzing about here to make sure the right permissions are
Expand Down
1 change: 1 addition & 0 deletions docker/postgres/run_postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ if [ ! -f $POSTGRES_DATA_DIR/PG_VERSION ]; then
psql --command "CREATE EXTENSION q3c" fastdb
psql --command "CREATE EXTENSION pgcrypto" fastdb
psql --command "CREATE EXTENSION pg_hint_plan" fastdb
psql --command "CREATE EXTENSION pg_healpix" fastdb
ropasswd=`cat /secrets/postgres_ro_password`
psql --command "CREATE USER postgres_ro PASSWORD '${ropasswd}'"
psql --command "GRANT CONNECT ON DATABASE fastdb TO postgres_ro"
Expand Down
4 changes: 3 additions & 1 deletion docker/webserver/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ RUN source /venv/bin/activate && \
pytz==2025.1 \
remote-pdb==2.1.0 \
requests==2.32.3 \
simplejson==3.20.1
simplejson==3.20.1 \
ligo.skymap==2.4.0


# ======================================================================
# This is for the test webserver. It installs crappy keys so you
Expand Down
7 changes: 7 additions & 0 deletions docs/developers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ If later you pull a new revision, ``git status`` may show your submodule as modi

to get the current version of all submodules.

The ``pg_healpix`` submodule contains a PostgreSQL extension written in
C for HEALPix indexing. After checking out the code you may build and
install the extension with ``make`` and ``make install`` (``pg_config``
must be in your ``PATH``). Then load it in your database with::

CREATE EXTENSION pg_healpix;


.. _installing-the-code:

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ FASTDB — DESC's time-domain database
overview
usage
developers
pg_healpix


Indices and tables
Expand Down
45 changes: 45 additions & 0 deletions lvk/db/2025-07-19_001_alert.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Table used to store Ligo Virgo KAGRA (LVK) alerts
CREATE TABLE lvk(
alert_type text NOT NULL,
time_created timestamp [ (p) ] with time zone NOT NULL,
superevent_id text PRIMARY KEY NOT NULL
);

CREATE TABLE event(
superevent_id text PRIMARY KEY REFERENCES lvk(superevent_id),
"time" timestamp [ (p) ] with time zone NOT NULL,
far double precision NOT NULL,
significant boolean NOT NULL,
instruments text[] NOT NULL,
search text NOT NULL,
group text NOT NULL,
pipeline text NOT NULL,
duration double precision,
central_frequency double precision
);

CREATE TABLE properties(
superevent_id text PRIMARY KEY REFERENCES lvk(superevent_id),
HasNS double precision,
HasRemnant double precision,
HasMassGap double precision
);

CREATE TABLE classification(
superevent_id text PRIMARY KEY REFERENCES lvk(superevent_id),
BNS double precision,
NSBH double precision,
BBH double precision,
Terrestrial double precision
);

CREATE TABLE external_coincidence(
superevent_id text PRIMARY KEY REFERENCES lvk(superevent_id),
gcn_notice_id bigint NOT NULL,
ivorn text NOT NULL,
observatory text NOT NULL,
search text NOT NULL,
time_difference double precision NOT NULL,
time_coincidence_far double precision NOT NULL,
time_sky_position_coincidence_far double precision NOT NULL
);
1 change: 1 addition & 0 deletions src/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ class DiaObject( DBBase ):
__tablename__ = "diaobject"
_tablemeta = None
_pk = [ 'diaobjectid', 'processing_version' ]
npix_order29 = None


# ======================================================================
Expand Down
Loading