Skip to content
Merged
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
6 changes: 3 additions & 3 deletions gpcontrib/access_log/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# access_log

The extension logs when and which user initializes Seq Scan on which partition
or table. The log file name is pg_log/access.log. To activate the extension you
can use the shared_preload_libraries GUC or the LOAD command.
The extension logs when, which user and in which session initializes Seq Scan on
which partition or table. The log file name is pg_log/access.log. To activate
the extension you can use the shared_preload_libraries GUC or the LOAD command.

If you want to register Seq Scans on segments only you should load access_log
on segments and don't load on master:
Expand Down
5 changes: 4 additions & 1 deletion gpcontrib/access_log/access_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <unistd.h>

#include "catalog/pg_namespace.h"
#include "cdb/cdbvars.h"
#include "executor/nodeSeqscan.h"
#include "libpq/auth.h"
#include "utils/syscache.h"
Expand Down Expand Up @@ -45,6 +46,7 @@ access_log_init_scan_hook(Relation currentRelation)
HeapTuple tp;
struct timeval tv;
pg_time_t stamp_time;
size_t len;

gettimeofday(&tv, NULL);
stamp_time = (pg_time_t) tv.tv_sec;
Expand All @@ -59,7 +61,8 @@ access_log_init_scan_hook(Relation currentRelation)

if (MyProcPort != NULL && MyProcPort->user_name != NULL)
strlcat(buf, MyProcPort->user_name, sizeof(buf));
strlcat(buf, ",", sizeof(buf));
len = strlen(buf);
snprintf(buf + len, sizeof(buf) - len, ",con%d,", gp_session_id);

tp = SearchSysCache1(NAMESPACEOID,
ObjectIdGetDatum(currentRelation->rd_rel->relnamespace));
Expand Down
215 changes: 108 additions & 107 deletions gpcontrib/access_log/expected/access_log.out
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ begin
end loop;

execute 'create external table access_log
(logtime timestamp with time zone, loguser text, tbl text)
(logtime timestamp with time zone, loguser text, logsession text, tbl text)
location (' || locations || ') format ''csv''';
end $$;
-- Start logging
load '$libdir/access_log.so';
create or replace function show_log(before_query text)
returns table(gp_segment_id int, date_ok bool, user_ok bool, tbl text)
returns table(gp_segment_id int, date_ok bool, user_ok bool, sess_ok bool, tbl text)
as $$
select gp_segment_id,
logtime between before_query::timestamp with time zone and now() date_ok,
loguser=current_user user_ok,
logsession = 'con' || current_setting('gp_session_id') sess_ok,
tbl
from access_log;
$$ language sql;
Expand All @@ -55,20 +56,20 @@ select * from t_heap_part;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+------------------------------------
2 | t | t | public.t_heap_part_1_prt_1_2_prt_1
2 | t | t | public.t_heap_part_1_prt_1_2_prt_2
2 | t | t | public.t_heap_part_1_prt_2_2_prt_1
2 | t | t | public.t_heap_part_1_prt_2_2_prt_2
0 | t | t | public.t_heap_part_1_prt_1_2_prt_1
0 | t | t | public.t_heap_part_1_prt_1_2_prt_2
0 | t | t | public.t_heap_part_1_prt_2_2_prt_1
0 | t | t | public.t_heap_part_1_prt_2_2_prt_2
1 | t | t | public.t_heap_part_1_prt_1_2_prt_1
1 | t | t | public.t_heap_part_1_prt_1_2_prt_2
1 | t | t | public.t_heap_part_1_prt_2_2_prt_1
1 | t | t | public.t_heap_part_1_prt_2_2_prt_2
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+------------------------------------
2 | t | t | t | public.t_heap_part_1_prt_1_2_prt_1
2 | t | t | t | public.t_heap_part_1_prt_1_2_prt_2
2 | t | t | t | public.t_heap_part_1_prt_2_2_prt_1
2 | t | t | t | public.t_heap_part_1_prt_2_2_prt_2
0 | t | t | t | public.t_heap_part_1_prt_1_2_prt_1
0 | t | t | t | public.t_heap_part_1_prt_1_2_prt_2
0 | t | t | t | public.t_heap_part_1_prt_2_2_prt_1
0 | t | t | t | public.t_heap_part_1_prt_2_2_prt_2
1 | t | t | t | public.t_heap_part_1_prt_1_2_prt_1
1 | t | t | t | public.t_heap_part_1_prt_1_2_prt_2
1 | t | t | t | public.t_heap_part_1_prt_2_2_prt_1
1 | t | t | t | public.t_heap_part_1_prt_2_2_prt_2
(12 rows)

select pg_file_unlink('pg_log/access.log') from gp_dist_random('gp_id');
Expand All @@ -87,14 +88,14 @@ select * from t_heap_part where c = 40;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+------------------------------------
0 | t | t | public.t_heap_part_1_prt_1_2_prt_1
0 | t | t | public.t_heap_part_1_prt_2_2_prt_1
1 | t | t | public.t_heap_part_1_prt_1_2_prt_1
1 | t | t | public.t_heap_part_1_prt_2_2_prt_1
2 | t | t | public.t_heap_part_1_prt_1_2_prt_1
2 | t | t | public.t_heap_part_1_prt_2_2_prt_1
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+------------------------------------
0 | t | t | t | public.t_heap_part_1_prt_1_2_prt_1
0 | t | t | t | public.t_heap_part_1_prt_2_2_prt_1
1 | t | t | t | public.t_heap_part_1_prt_1_2_prt_1
1 | t | t | t | public.t_heap_part_1_prt_2_2_prt_1
2 | t | t | t | public.t_heap_part_1_prt_1_2_prt_1
2 | t | t | t | public.t_heap_part_1_prt_2_2_prt_1
(6 rows)

select pg_file_unlink('pg_log/access.log') from gp_dist_random('gp_id');
Expand All @@ -112,14 +113,14 @@ select * from t_heap_part where b = 0;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+------------------------------------
0 | t | t | public.t_heap_part_1_prt_1_2_prt_1
0 | t | t | public.t_heap_part_1_prt_1_2_prt_2
1 | t | t | public.t_heap_part_1_prt_1_2_prt_1
1 | t | t | public.t_heap_part_1_prt_1_2_prt_2
2 | t | t | public.t_heap_part_1_prt_1_2_prt_1
2 | t | t | public.t_heap_part_1_prt_1_2_prt_2
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+------------------------------------
0 | t | t | t | public.t_heap_part_1_prt_1_2_prt_1
0 | t | t | t | public.t_heap_part_1_prt_1_2_prt_2
1 | t | t | t | public.t_heap_part_1_prt_1_2_prt_1
1 | t | t | t | public.t_heap_part_1_prt_1_2_prt_2
2 | t | t | t | public.t_heap_part_1_prt_1_2_prt_1
2 | t | t | t | public.t_heap_part_1_prt_1_2_prt_2
(6 rows)

-- Don't delete files, because the next select will lead to adding log records
Expand Down Expand Up @@ -158,11 +159,11 @@ select * from t_heap;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+---------------
2 | t | t | public.t_heap
0 | t | t | public.t_heap
1 | t | t | public.t_heap
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+---------------
2 | t | t | t | public.t_heap
0 | t | t | t | public.t_heap
1 | t | t | t | public.t_heap
(3 rows)

select pg_file_unlink('pg_log/access.log') from gp_dist_random('gp_id');
Expand Down Expand Up @@ -197,20 +198,20 @@ select * from t_ao_part;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+----------------------------------
0 | t | t | public.t_ao_part_1_prt_1_2_prt_1
0 | t | t | public.t_ao_part_1_prt_2_2_prt_1
0 | t | t | public.t_ao_part_1_prt_1_2_prt_2
0 | t | t | public.t_ao_part_1_prt_2_2_prt_2
1 | t | t | public.t_ao_part_1_prt_1_2_prt_1
1 | t | t | public.t_ao_part_1_prt_2_2_prt_1
1 | t | t | public.t_ao_part_1_prt_1_2_prt_2
1 | t | t | public.t_ao_part_1_prt_2_2_prt_2
2 | t | t | public.t_ao_part_1_prt_1_2_prt_1
2 | t | t | public.t_ao_part_1_prt_2_2_prt_1
2 | t | t | public.t_ao_part_1_prt_1_2_prt_2
2 | t | t | public.t_ao_part_1_prt_2_2_prt_2
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+----------------------------------
0 | t | t | t | public.t_ao_part_1_prt_1_2_prt_1
0 | t | t | t | public.t_ao_part_1_prt_2_2_prt_1
0 | t | t | t | public.t_ao_part_1_prt_1_2_prt_2
0 | t | t | t | public.t_ao_part_1_prt_2_2_prt_2
1 | t | t | t | public.t_ao_part_1_prt_1_2_prt_1
1 | t | t | t | public.t_ao_part_1_prt_2_2_prt_1
1 | t | t | t | public.t_ao_part_1_prt_1_2_prt_2
1 | t | t | t | public.t_ao_part_1_prt_2_2_prt_2
2 | t | t | t | public.t_ao_part_1_prt_1_2_prt_1
2 | t | t | t | public.t_ao_part_1_prt_2_2_prt_1
2 | t | t | t | public.t_ao_part_1_prt_1_2_prt_2
2 | t | t | t | public.t_ao_part_1_prt_2_2_prt_2
(12 rows)

select pg_file_unlink('pg_log/access.log') from gp_dist_random('gp_id');
Expand All @@ -229,14 +230,14 @@ select * from t_ao_part where c = 40;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+----------------------------------
1 | t | t | public.t_ao_part_1_prt_1_2_prt_1
1 | t | t | public.t_ao_part_1_prt_2_2_prt_1
2 | t | t | public.t_ao_part_1_prt_1_2_prt_1
2 | t | t | public.t_ao_part_1_prt_2_2_prt_1
0 | t | t | public.t_ao_part_1_prt_1_2_prt_1
0 | t | t | public.t_ao_part_1_prt_2_2_prt_1
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+----------------------------------
1 | t | t | t | public.t_ao_part_1_prt_1_2_prt_1
1 | t | t | t | public.t_ao_part_1_prt_2_2_prt_1
2 | t | t | t | public.t_ao_part_1_prt_1_2_prt_1
2 | t | t | t | public.t_ao_part_1_prt_2_2_prt_1
0 | t | t | t | public.t_ao_part_1_prt_1_2_prt_1
0 | t | t | t | public.t_ao_part_1_prt_2_2_prt_1
(6 rows)

select pg_file_unlink('pg_log/access.log') from gp_dist_random('gp_id');
Expand All @@ -254,14 +255,14 @@ select * from t_ao_part where b = 0;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+----------------------------------
0 | t | t | public.t_ao_part_1_prt_1_2_prt_1
0 | t | t | public.t_ao_part_1_prt_1_2_prt_2
1 | t | t | public.t_ao_part_1_prt_1_2_prt_1
1 | t | t | public.t_ao_part_1_prt_1_2_prt_2
2 | t | t | public.t_ao_part_1_prt_1_2_prt_1
2 | t | t | public.t_ao_part_1_prt_1_2_prt_2
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+----------------------------------
0 | t | t | t | public.t_ao_part_1_prt_1_2_prt_1
0 | t | t | t | public.t_ao_part_1_prt_1_2_prt_2
1 | t | t | t | public.t_ao_part_1_prt_1_2_prt_1
1 | t | t | t | public.t_ao_part_1_prt_1_2_prt_2
2 | t | t | t | public.t_ao_part_1_prt_1_2_prt_1
2 | t | t | t | public.t_ao_part_1_prt_1_2_prt_2
(6 rows)

-- One segment
Expand Down Expand Up @@ -297,11 +298,11 @@ select * from t_ao;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+-------------
2 | t | t | public.t_ao
1 | t | t | public.t_ao
0 | t | t | public.t_ao
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+-------------
2 | t | t | t | public.t_ao
1 | t | t | t | public.t_ao
0 | t | t | t | public.t_ao
(3 rows)

select pg_file_unlink('pg_log/access.log') from gp_dist_random('gp_id');
Expand Down Expand Up @@ -336,20 +337,20 @@ select * from t_aoco_part;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+------------------------------------
1 | t | t | public.t_aoco_part_1_prt_1_2_prt_1
1 | t | t | public.t_aoco_part_1_prt_2_2_prt_1
1 | t | t | public.t_aoco_part_1_prt_2_2_prt_2
1 | t | t | public.t_aoco_part_1_prt_1_2_prt_2
2 | t | t | public.t_aoco_part_1_prt_1_2_prt_1
2 | t | t | public.t_aoco_part_1_prt_2_2_prt_1
2 | t | t | public.t_aoco_part_1_prt_2_2_prt_2
2 | t | t | public.t_aoco_part_1_prt_1_2_prt_2
0 | t | t | public.t_aoco_part_1_prt_1_2_prt_1
0 | t | t | public.t_aoco_part_1_prt_2_2_prt_1
0 | t | t | public.t_aoco_part_1_prt_2_2_prt_2
0 | t | t | public.t_aoco_part_1_prt_1_2_prt_2
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+------------------------------------
1 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_1
1 | t | t | t | public.t_aoco_part_1_prt_2_2_prt_1
1 | t | t | t | public.t_aoco_part_1_prt_2_2_prt_2
1 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_2
2 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_1
2 | t | t | t | public.t_aoco_part_1_prt_2_2_prt_1
2 | t | t | t | public.t_aoco_part_1_prt_2_2_prt_2
2 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_2
0 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_1
0 | t | t | t | public.t_aoco_part_1_prt_2_2_prt_1
0 | t | t | t | public.t_aoco_part_1_prt_2_2_prt_2
0 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_2
(12 rows)

select pg_file_unlink('pg_log/access.log') from gp_dist_random('gp_id');
Expand All @@ -368,14 +369,14 @@ select * from t_aoco_part where c = 40;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+------------------------------------
1 | t | t | public.t_aoco_part_1_prt_1_2_prt_1
1 | t | t | public.t_aoco_part_1_prt_2_2_prt_1
2 | t | t | public.t_aoco_part_1_prt_1_2_prt_1
2 | t | t | public.t_aoco_part_1_prt_2_2_prt_1
0 | t | t | public.t_aoco_part_1_prt_1_2_prt_1
0 | t | t | public.t_aoco_part_1_prt_2_2_prt_1
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+------------------------------------
1 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_1
1 | t | t | t | public.t_aoco_part_1_prt_2_2_prt_1
2 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_1
2 | t | t | t | public.t_aoco_part_1_prt_2_2_prt_1
0 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_1
0 | t | t | t | public.t_aoco_part_1_prt_2_2_prt_1
(6 rows)

select pg_file_unlink('pg_log/access.log') from gp_dist_random('gp_id');
Expand All @@ -393,14 +394,14 @@ select * from t_aoco_part where b = 0;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+------------------------------------
0 | t | t | public.t_aoco_part_1_prt_1_2_prt_1
0 | t | t | public.t_aoco_part_1_prt_1_2_prt_2
1 | t | t | public.t_aoco_part_1_prt_1_2_prt_1
1 | t | t | public.t_aoco_part_1_prt_1_2_prt_2
2 | t | t | public.t_aoco_part_1_prt_1_2_prt_1
2 | t | t | public.t_aoco_part_1_prt_1_2_prt_2
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+------------------------------------
0 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_1
0 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_2
1 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_1
1 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_2
2 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_1
2 | t | t | t | public.t_aoco_part_1_prt_1_2_prt_2
(6 rows)

-- One segment
Expand Down Expand Up @@ -436,11 +437,11 @@ select * from t_aoco;
(0 rows)

select * from show_log(:'before_query');
gp_segment_id | date_ok | user_ok | tbl
---------------+---------+---------+---------------
1 | t | t | public.t_aoco
2 | t | t | public.t_aoco
0 | t | t | public.t_aoco
gp_segment_id | date_ok | user_ok | sess_ok | tbl
---------------+---------+---------+---------+---------------
1 | t | t | t | public.t_aoco
2 | t | t | t | public.t_aoco
0 | t | t | t | public.t_aoco
(3 rows)

select pg_file_unlink('pg_log/access.log') from gp_dist_random('gp_id');
Expand Down
Loading
Loading