Repository navigation
Add extension for view logging - #434
MutableFire wants to merge 2 commits into
Conversation
d9165fa to
f5c37b9
Compare
| static void | ||
| view_log_post_parse_analyze_hook(ParseState *pstate, Query *query) | ||
| { | ||
| log_views_from_rtable(query); |
There was a problem hiding this comment.
What about using a standard walker function from PostgreSQL core?
There was a problem hiding this comment.
query_tree_walker crashes with unrecognized node type: 705 when it encounters specific fields inside the Query struct. The core walker in nodeFuncs.c does not know how to descend into these specific node types and aborts the query with an error, which breaks CREATE VIEW and every subsequent test. Therefore we cannot use query_tree_walker here. We have to enumerate the relevant standard fields manually (targetList, jointree, havingQual, limitCount, limitOffset, cteList, rtable) and walk each of them with expression_tree_walker, which only touches standard expression nodes and never hits the specific ones.
9542127 to
9d96e05
Compare
| } | ||
| ReleaseSysCache(tp); | ||
| } | ||
| if (query->targetList) |
There was a problem hiding this comment.
You should also check cteList, JoinExpr conditions, limitCount, and other fields. For example both queries below execute without logging the reference to v:
WITH c AS (SELECT * FROM v) SELECT * FROM c;
SELECT 1 LIMIT (SELECT id FROM v);
parse_sub_analyze() does not invoke the hook separately, maybe you should use query_tree_walker() to cover the relevant fields instead of enumerating them manually?
There was a problem hiding this comment.
query_tree_walker crashes with unrecognized node type: 705 when it encounters specific fields inside the Query struct. The core walker in nodeFuncs.c does not know how to descend into these specific node types and aborts the query with an error, which breaks CREATE VIEW and every subsequent test. Therefore we cannot use query_tree_walker here. We have to enumerate the relevant standard fields manually (targetList, jointree, havingQual, limitCount, limitOffset, cteList, rtable) and walk each of them with expression_tree_walker, which only touches standard expression nodes and never hits the specific ones.
| if (node == NULL) | ||
| return false; | ||
|
|
||
| if (IsA(node, SubLink)) |
There was a problem hiding this comment.
what about this case?
SELECT (SELECT id FROM v) IN (SELECT 1);
SubLink handling skips its testexpr. The walker returns immediately after traversing subselect, skipping the left-hand expression of IN/ANY/ALL.
| nspName = nspNameBuf; | ||
| } | ||
|
|
||
| strlcat(buf, nspName, sizeof(buf)); |
There was a problem hiding this comment.
What about this case?
postgres=# CREATE OR REPLACE VIEW "comma,view" AS SELECT 1;
CREATE VIEW
postgres=# SELECT * FROM "comma,view";
?column?
----------
1
(1 row)
A valid view name such as "comma,view" produces ...,public.comma,view, resulting in five fields instead of four.
c5e0f20 to
83f7505
Compare
| errmsg("could not open file " LOG_FILE_NAME ": %m"))); | ||
| return; | ||
| } | ||
| write(f, str, strlen(str)); |
There was a problem hiding this comment.
Why will happen if write isn't success?
https://pubs.opengroup.org/onlinepubs/9799919799/functions/write.html
| continue; | ||
| } | ||
|
|
||
| if (rte->rtekind != RTE_RELATION) |
There was a problem hiding this comment.
Did you consider all variants subqueries?
For example what happen if subqueries in functions?
| -- start_ignore | ||
| SELECT pg_file_unlink('pg_log/view.log'); | ||
| -- end_ignore |
There was a problem hiding this comment.
Can we remove ignore?
| -- start_ignore | |
| SELECT pg_file_unlink('pg_log/view.log'); | |
| -- end_ignore | |
| SELECT pg_file_unlink('pg_log/view.log'); |
There was a problem hiding this comment.
Do we need this code?
| -- start_ignore | |
| SELECT pg_file_unlink('pg_log/view.log'); | |
| -- end_ignore |
There was a problem hiding this comment.
Yes, we do. At the end of a test we should bring DB in the same state as before the test. So the file should be deleted to avoid impact on other tests
There was a problem hiding this comment.
The file deleted in last block used: https://github.com/open-gpdb/gpdb/pull/434/changes/BASE..83f75054d7f12293468c49bc0b7a8161e18e97a3#diff-40354da305a822f20aa639b54810286931f86085e0d444f147d867e65fe2d441R108
And the file deleted each run test: https://github.com/open-gpdb/gpdb/pull/434/changes/BASE..83f75054d7f12293468c49bc0b7a8161e18e97a3#diff-40354da305a822f20aa639b54810286931f86085e0d444f147d867e65fe2d441R4
88d5dd7 to
d80a20e
Compare
Add
view_logextension that logs all references to database views duringquery analysis. It hooks into
post_parse_analyze_hookto inspect the rangetable and subqueries, writing a CSV entry with timestamp, user, session ID,
and schema-qualified view name to
pg_log/view.logon the master node.Logging is presence-based (before view expansion) and does not guarantee
data was actually fetched.