sql

All things SQL and JDBC...
dharrigan 2020-02-28T14:38:01.017600Z

Anyone here used a psql function with a parameter that is also used inside an inner (nested) select?

ben 2020-02-28T14:43:16.017700Z

I’m not sure what exactly you mean. Do you have an example?

dharrigan 2020-02-28T14:54:37.017900Z

Well, the question is more leading 🙂 If I define a function with a paramter called foo, I can't seem to refer to that parameter within a nested select. On the outer select, yes, no problem, I can see it, but not within the inner 🙂

dharrigan 2020-02-28T15:02:32.018100Z

Here's a very convoluted example

dharrigan 2020-02-28T15:02:33.018300Z

CREATE OR REPLACE FUNCTION foobar(foo INTEGER DEFAULT 1)
AS
$$
BEGIN
    RETURN QUERY
        SELECT a.b
        FROM (SELECT a.c FROM a WHERE a.datetime BETWEEN current_date - INTERVAL foo AND current_date) a;
        --WHERE foo = 1;
END;
$$
    LANGUAGE plpgsql;

dharrigan 2020-02-28T15:03:04.018500Z

the variable foo cannot be accessed within the inner select, yet it can be accessed in the where clause

dharrigan 2020-02-28T15:03:48.018700Z

The query isn't functional (ha!), it's mearly to illustrate the issue that I face 🙂

ben 2020-02-28T15:16:34.018900Z

I see. I’ve no idea I’m afraid. Is there any chance using a CTE instead of a nested select would help?

dharrigan 2020-02-28T15:18:41.019100Z

Do'h fixed. I was using the wrong datatype

dharrigan 2020-02-28T15:18:50.019300Z

so naturally it couldn't be used

2020-02-28T17:45:58.020500Z

I am looking at a database optimization that my predecessor has done that is trying to cache a count() value that runs a bit like this

INSERT INTO cached_count (id, countedval)
VALUES (1, (SELECT count(id) from biggish_table where condition IS NULL AND id = 1))
ON CONFLICT (id) DO
    UPDATE
    SET countedval = cached_count.countedval -1
RETURNING countedval;

parameme 2020-03-02T11:40:24.041Z

My understanding is that ROW_COUNT would only help if you processed / touched all the rows you are wanting to count on that cursor. This really needs some thought / design to balance the need for performance improvement and read vs write frequency (and cache invalidation). An index on biggish_table (id, <any condition columns>) could help significantly. Same index with a WHERE condition IS NULL could also help. And specific index types might help. But if it really has to be optimal in I/O though then aggregate maintenance on change really requires either app code to support or triggers to maintain.

1👍
2020-03-02T12:19:55.041300Z

yeah I'm concluding that the extra complexity makes it really not worth the trouble

2020-02-28T17:46:53.020900Z

my objection to this is that it always runs the count(id) meaning that it doesn't optimize anything, it just creates overhead

2020-02-28T17:47:18.021100Z

I only want that to run if the row for id is missing

2020-02-28T17:47:48.021300Z

what I really want is an

UPDATE ... ON MISSING
construct

2020-02-28T17:47:51.021500Z

or near offer

2020-02-28T17:48:02.021700Z

This is in Postgres BTW

2020-02-28T17:50:15.022100Z

I wonder if I can abuse COALESCE into helping...

2020-02-28T17:56:32.022700Z

ooh dear that sounds ouchy

seancorfield 2020-02-28T17:56:36.022900Z

(sorry, cat on keyboard)

2🐈
2020-02-28T17:59:53.023300Z

if anyone has a pet way to UPDATE ON MISSING , I am agog to hear it

2020-02-28T18:00:19.023500Z

otherwise I guess I'm best off stripping out this 'optimisation` - its got a noticeable overhead but no benefit

isak 2020-02-28T18:52:59.024Z

You could use ROW_COUNT, I think: https://stackoverflow.com/a/36325731

grzm 2020-02-28T19:24:33.024300Z

nits: psql is the PostgreSQL command line tool. The language you're using for the function is PL/pgSQL, and the project as a whole is PostgreSQL, postgres, or pg, if you're into the whole brevity thing.

seancorfield 2020-02-28T19:32:12.026100Z

I just released seancorfield/next.jdbc {:mvn/version "1.0.384"} -- https://github.com/seancorfield/next-jdbc -- It's mostly documentation updates based on feedback here and via issues, but also a few minor performance tweaks for result set builders, and a bug fix to how column name keywords are constructed when the table name is not available. The latter also means you can now use :qualifier-fn (constantly "prefix") even when the table name is not available for a given column in the results, which provides an easier migration from clojure.java.jdbc.

seancorfield 2020-02-28T19:32:57.027Z

The documentation updates include examples of datafiable-row and more explanation of what it is about.

seancorfield 2020-02-28T19:33:38.027900Z

Thanks to everyone here who has provided feedback on the docs (and to other folks who've provided feedback via GitHub issues)!

seancorfield 2020-02-28T19:34:24.028700Z

(and, yes, just like many of the Contrib libraries, I've decided to switch to major.minor.commits for versioning)

valtteri 2020-02-28T21:08:27.029600Z

Wow! That was quick