I have used pg_stat_operations to know when a table or view is created or altered,
I want to know when a function or sequence is created or modified.
(Function list is availed at pg_proc and information_schema.routines but there is no option for creation or modification date)
Is there any way to find it out???
Related
I feel the need to get the column names and data types of the table returned by any function that has a 'record' return data type, because...
A key process in an existing SQL Server-based system makes use of a stored procedure that takes a user-defined function as a parameter. An initial step gets the column names and types of the table returned by the function that was passed as a parameter.
In Postgres 13 I can use pg_proc.prorettype and the corresponding pg_type to find functions that return record types...that's a start. I can also use pg_get_function_result() to get the string containing the information I need. But, it's a string, and while I ultimately will have to assemble a very similar string, this is just one application of the info. Is there a tabular equivalent containing (column_name, data_type, ordinal_position), or do I need to do that myself?
Is there access to a composite data type the system may have created when such a function is created?
One option that I think will work for me, but I think it's a little weird, is to:
> create temp table t as select * from function() limit 0;
then look that table up in info_schema.columns, assemble what I need and drop the temp table...putting all of this into a function.
You can query the catalog table pg_proc, which contains all the required information:
SELECT coalesce(p.na, 'column' || p.i),
p.ty::regtype,
p.i
FROM pg_proc AS f
CROSS JOIN LATERAL unnest(
coalesce(f.proallargtypes, ARRAY[f.prorettype]),
f.proargmodes,
f.proargnames
)
WITH ORDINALITY AS p(ty,mo,na,i)
WHERE f.proname = 'interval_ok'
AND coalesce(p.mo, 'o') IN ('o', 't')
ORDER BY p.i;
I have a Timescale database that someone else created.
How do I determine if create_hypertable was called on a table ?
For now I use this, but there must be a better way:
SELECT * FROM hypertable_relation_size('public.data');
And if create_hypertable was called on a table, which parameters (including chunk_time_interval) were used when calling create_hypertable ?
(In some cases there is a from_date and to_date)
TimescaleDB maintains metadata about hypertables and provides views to query for the metadata. Views are located in schema timescaledb_information and information about hypertables can be retrieved from timescaledb_information.hypertable.
For example:
SELECT * FROM timescaledb_information.hypertable WHERE table_name = 'data';
This API doc contains more information and examples.
Note that the time chunk interval can be changed over time, so the view doesn't provide information about it. So it is necessary to inspect every chunk to see its interval. This can be done by calling function chunk_relation_size_pretty described in the doc here. For example:
SELECT chunk_table, partitioning_columns, ranges
FROM chunk_relation_size_pretty('data');
If you are in another schema, then it is necessary to specify fully qualified name of the hypertable as it expects an identifier:
SET SCHEMA 'test';
SELECT chunk_table, partitioning_columns, ranges
FROM public.chunk_relation_size_pretty('public.data');
Updated syntax (version>2.0) to get metadata about hypertables :
SELECT * FROM timescaledb_information.hypertables WHERE hypertable_name = 'data';
See Can't display hypertable information: timescaledb_information.hypertable does not exist
I receive an excel file with following info:
Source_column_name,Source_table_name,Destination_column,Destination_table,Where_clause_Condition,Join_condition,Procedure_name,Created_date,Modified_date
[ColumnS1], [TableS1], [ColumnD1], [TableD1], [Where
TableS1.Condition1 = ‘XYZ’] ,[INNER JOIN TableS0 ON TableS1.X1=
TableS0.X2], [Proc_XYZ], [today's date]
[ColumnS2], [TableS2], [ColumnD2], [TableD2], [Where
TableS2.Condition2 = ‘XYZ’], [INNER JOIN TableS0 ON TableS2.X1=
TableS0.X2], [Proc_ABC], [Old date], [Today’s date]
If created date is today's date then source column should be added to table and also proc should be altered ie add column is select statement in proc and add table name along with join and where clause condition.
If modified date is today's date then only proc needs to be altered with where clause condition wherever the table is used (TableS2 is the above case).
Can anyone please help with any approach.
Don't alter the proc dynamically.
Instead put conditional logic in the proc, and pass parameters to it that tell it what to do based on the incoming data.
Since you haven't shared any code, sample data or desired results, I can't be any more specific than that, unfortunately.
Is it possible to join a view with another table in SQL? If so, how?
I have a query on Oracle db which has specific fields. I need to re create the same query on PostgreSQL but some of the data in the PostgreSQL query are coming from a view... And that view has missing information. It's a pretty complex view, so I don't want to NOT use it for now.
For example, in Oracle I do this:
SELECT
d.dos_id,
trunc(d.dos_creation, 'MM') as Cohorte,
sum(v.ver_etude + v.ver_direct) as encaissé
from t_dossier d
left outer join v_versement v
on v.dos_id = d.dos_id
In the Postgres one, I'm using a view. But the view does not return "dos_id" so I cannot explicitly join v_versement with the view.
Is there a way to force a view to return specific fields at runtime which weren't there when creating the view?
You can't force it
to return specific fields at runtime which weren't there when creating
the view
You can create or replace it with limitation:
https://www.postgresql.org/docs/current/static/sql-createview.html
CREATE OR REPLACE VIEW is similar, but if a view of the same name
already exists, it is replaced. The new query must generate the same
columns that were generated by the existing view query (that is, the
same column names in the same order and with the same data types), but
it may add additional columns to the end of the list. The calculations
giving rise to the output columns may be completely different.
example:
t=# create view v2 as select now();
CREATE VIEW
Time: 36.488 ms
t=# create or replace view v2 as select now(),current_user;
CREATE VIEW
Time: 8.551 ms
t=# create or replace view v2 as select now()::text,current_user;
ERROR: cannot change data type of view column "now" from timestamp with time zone to text
Time: 0.430 ms
I guess I had not realised that I can actually use the view without creating it...
So I edited the SQL statement that makes up the view, added the fields that I needed and used the code of the view without having to create a new view (creating a new view would mean outsourcing it to another company, which would cost us money..)
Thanks :)
Windows/NET/ODBC
I would like to get query results to new table on some handy way which I can see through data adapter but I can't find a way to do it.
There is no much examples around to satisfy beginner's level on this.
Don't know temporary or not but after seeing results that table is no more needed so I can delete it 'by hand' or it can be deleted automatically.
This is what I try:
mCmd = New OdbcCommand("CREATE TEMP TABLE temp1 ON COMMIT DROP AS " & _
"SELECT dtbl_id, name, mystr, myint, myouble FROM " & myTable & " " & _
"WHERE myFlag='1' ORDER BY dtbl_id", mCon)
n = mCmd.ExecuteNonQuery
This run's without error and in 'n' I get correct number of matched rows!!
But with pgAdmin I don't see those table no where?? No matter if I look under opened transaction or after transaction is closed.
Second, should I define columns for temp1 table first or they can be made automatically based on query results (that would be nice!).
Please minimal example to illustrate me what to do based on upper code to get new table filled with query results.
A shorter way to do the same thing your current code does is with CREATE TEMPORARY TABLE AS SELECT ... . See the entry for CREATE TABLE AS in the manual.
Temporary tables are not visible outside the session ("connection") that created them, they're intended as a temporary location for data that the session will use in later queries. If you want a created table to be accessible from other sessions, don't use a TEMPORARY table.
Maybe you want UNLOGGED (9.2 or newer) for data that's generated and doesn't need to be durable, but must be visible to other sessions?
See related: Is there a way to access temporary tables of other sessions in PostgreSQL?