Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partition queries broken #47

Closed
rustprooflabs opened this issue Nov 24, 2023 · 2 comments
Closed

Partition queries broken #47

rustprooflabs opened this issue Nov 24, 2023 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@rustprooflabs
Copy link
Owner

Details

The dd.partition_parents and dd.partition_children views are broken when partitions exist.

What I did

cargo pgrx run

Create extension & query view (default empty)

(localhost 🐘) ryanlambert@pgdd=# create extension pgdd;
CREATE EXTENSION
Time: 63.543 ms
(localhost 🐘) ryanlambert@pgdd=# select * from dd.partition_parents;
(0 rows)

Load test partition data

(localhost 🐘) ryanlambert@pgdd=# \i ~/git/pgdd/tests/test-partition-data.sql 
psql:/home/ryanlambert/git/pgdd/tests/test-partition-data.sql:5: NOTICE:  schema "pgdd_test" does not exist, skipping
DROP SCHEMA
Time: 1.053 ms
CREATE SCHEMA
Time: 2.004 ms
CREATE TABLE
Time: 2.069 ms
CREATE TABLE
Time: 5.403 ms
CREATE TABLE
Time: 5.205 ms
CREATE TABLE
Time: 5.409 ms
INSERT 0 15
Time: 2.895 ms

Then tried query against partition view again. It fails with a type error.

select * from dd.partition_parents;
ERROR:  Datum error: Postgres type bigint oid={#20, builtin: INT8OID} is not compatible with the Rust type pgrx_pg_sys::submodules::oids::Oid oid={#26, builtin: OIDOID}

Findings so far...

But... it is oid

The code behind the dd.partition_parent() function is pretty straight foward and should be returning an OID. After all, it's pulling directly from pg_catalog.pg_class.oid. While a CTE isn't helpful at all there, that also shouldn't impact the data type. I double checked the type returned via pg_typeof() to be paranoid.

SELECT pg_typeof(c.oid), c.oid,
        n.nspname::TEXT AS s_name,
        c.relname::TEXT AS t_name,
        CASE WHEN pt.partrelid IS NOT NULL THEN 'declarative'
            WHEN c.relkind = 'r' THEN 'inheritance'
            ELSE 'unknown' END AS partition_type,
        COUNT(i.inhrelid) AS partitions
    FROM pg_catalog.pg_class c
    INNER JOIN pg_catalog.pg_inherits i ON c.oid = i.inhparent
    LEFT JOIN pg_catalog.pg_partitioned_table pt
        ON c.oid = pt.partrelid 
    LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
    WHERE c.relkind != 'I' -- Exclude partitioned indexes
    GROUP BY c.relkind, c.oid, pt.partrelid, n.nspname::TEXT, c.relname::TEXT
;
┌───────────┬───────┬───────────┬────────┬────────────────┬────────────┐
│ pg_typeof │  oid  │  s_name   │ t_name │ partition_type │ partitions │
╞═══════════╪═══════╪═══════════╪════════╪════════════════╪════════════╡
│ oid       │ 42225 │ pgdd_test │ parent │ declarative    │          3 │
└───────────┴───────┴───────────┴────────┴────────────────┴────────────┘

Rust code

I don't see any obvious differences in the Rust code used regarding how we are handling the OID. Example, no issue here.

SELECT oid, pg_typeof(oid), * FROM dd.tables;

Comparing what is done for the tables function vs the partition functions, I don't see any obvious differences.
Functional tables code does this (here and here):

name!(oid, Result<Option<pg_sys::Oid>, pgrx::spi::Error>)
...
let oid = row["oid"].value::<pg_sys::Oid>();

The broken partition_parents version does the same things (here and here)

name!(oid, Result<Option<pg_sys::Oid>, pgrx::spi::Error>),
...
let oid = row["oid"].value::<pg_sys::Oid>();
@rustprooflabs
Copy link
Owner Author

Ah, the casting was happening in the partition_children query... See #48.

@rustprooflabs rustprooflabs added this to the 0.5.2 milestone Nov 24, 2023
@rustprooflabs
Copy link
Owner Author

Verified this fixes the problem on Pg15 where it was detected. Will tag and release new version 0.5.2 including new installers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant