Setting a User's Valid Until date in the future - amazon-redshift

I was trying to see if there was a way to automatically set a user's VALID UNTIL value three months in the future without having to type out the literal date. Tried the following:
alter user rchung set valuntil = dateadd(day,90,GETDATE());
alter user rchung set valuntil = select dateadd(day,90,GETDATE());
both failed with a syntax error.
alter user rchung valid until dateadd(day,90,GETDATE());
also failed with a syntax error.
Anyone have any success with this?
TIA,
Rich

It appears that this is a limitation on the PostgreSQL side.
CREATE USER, like pretty much all utility statements in Postgres,
won't do any expression evaluation --- the parameters have to be
simple literal constants.
VALID UNTIL programmatically in SQL
Since Amazon Redshift doesn't support plpgsql like PostgreSQL, client side scripting is really the only option. If you're using a semi-modern version (9.3+) of psql the following works:
select dateadd(day,90,GETDATE()) as expiry; \gset
alter user myuser valid until :'expiry';

Related

What does this select statement actually do?

I'm reviewing log of executed PostgreSQL statements and stumble upon one statement I can't totally understand. Can somebody explain what PostgreSQL actually do when such query is executed? What is siq_query?
select *
from siq_query('', '21:1', '', '("my search string")', False, True, 'http://siqfindex:8080/storediq/findex')
I'm running PostgreSQL 9.2
siq_query(...) is a server-side function taking 7 input parameters (or more). It's not part of any standard Postgres distribution I know (certainly not mainline Postgres 9.2), so it has to be user-defined or part of some extension you installed. It does whatever is defined in the function. This can include basically anything your Postgres user is allowed to do. Unless it's a SECURITY DEFINER function, then it ca do whatever the owner of the function is allowed to do.
The way it is called (SELECT * FROM), only makes sense if it returns multiple rows and/or columns, most likely a set of rows, making it a "set-returning function", which can be used almost like a table in SQL queries.
Since the function name is not schema-qualified, it has to reside in a visible schema. See:
How does the search_path influence identifier resolution and the "current schema"
Long story short, you need to see the function definition to know what it does exactly. You can use psql (\df+ siq_query), pgAdmin (browse and select it to see its definition in the SQL pane) or any other client tool to look it up. Or query the system catalog pg_proc directly:
SELECT * FROM pg_proc WHERE proname = 'siq_query';
Pay special attention to the column prosrc, which holds the function body for some languages like plpgsql.
There might be multiple variants of that name, Postgres allows function overloading.

ERROR In Sequences query if postgres

I create one sequence in postgres and fire one query which is mentioned below
SELECT M_PRODUCTSEQ.NEXTVAL from DUAL;
but it gives me the below error:
ERROR: relation "dual" does not exist.
Kindly help me out. How can i made the relation with dual?
PostgreSQL does NOT support the from DUAL syntax. It does however make the from portion of a query like this optional, so getting the next value (nextval) of a sequence you would do something like this:
SELECT nextval('m_productseq');

saving point type data to pg server

I have a function in postgres that inserts data to a table, and one of the columns is of type point.
I don't know how to save my gotten data to an acceptable form for the function to save my data.
The problematic code in question is :
CREATE OR REPLACE FUNCTION db."setMessage"(eml text, msg text, lat text, lng text)
...
INSERT INTO
db.messages
VALUES
(DEFAULT,
id,
$2,
DEFAULT,
ST_SetSRID(ST_MakePoint($3::float, $4::float), 4326)
);
The error received when i call the function is:
ERROR: function st_makepoint(double precision, double precision) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Context: PL/pgSQL function "setMessage" line 9 at SQL statement
I have tries to save the point in string format ('POINT(12.22343 64.22233)'), tried not casting the st_makepoint arguments, and inserting without the outer function (ST_SetSRID).
could anyone tell me what I'm doing wrong?
SOLUTION:
So the method of success was following this link to get the needed libraries. after that i created the required extensions, and now i have access to the desired functions.
To install PostGIS, follow the instructions for your operating system at https://postgis.net/install.
Then, to enable it on your db you can do CREATE EXTENSION postgis; from psql or PGAdmin.
I had a similar problem, that postgres didn't find the function st_makepoint even though postgis extension was installed and I used the correct types for arguments. It turned out that the search path did not contain postgis.
Solution was: alter database my_db set search_path = my_schema, public, postgis;
Hope it helps somebody.
I tried to re-install postgis extension and have tried many soluations but did not work for me.
I just edit my query and put extensions keyword before functions name like this:
INSERT INTO
db.messages
VALUES
(DEFAULT,
id,
$2,
DEFAULT,
extensions.ST_SetSRID(extensions.ST_MakePoint($3::float, $4::float), 4326)
);
That worked for me.

Is it possible to get explain plan with bind variables in DB2?

With Oracle, the syntax is:
explain plan for
select * from users WHERE user_name = :user_name AND user_dob = :user_dob
Is it possible to do the same in DB2? The statement below does not seem to work.
explain plan with snapshot for
select * from users WHERE user_name = :user_name AND user_dob = :user_dob
Thank you.
The answer may depend on your DB2 version and platform, which you chose not to share with us for some reason. This works fine on DB2 for LUW (v10.1, but I'm sure it would work with v9.7 and up):
$ db2 "explain plan with snapshot for select * from syscat.schemata where schemaname = :blah"
DB20000I The SQL command completed successfully.
You may want to try replacing named parameter markers with questions marks.
Apparently, the answer is in the IBM website, but it is not easy to make sense of.
http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=%2Fcom.ibm.db2.udb.admin.doc%2Fdoc%2Fr0000952.htm
FOR explainable-sql-statement
Specifies the SQL statement to be explained. This statement can be any
valid CALL, Compound SQL (Dynamic), DELETE, INSERT, MERGE, SELECT,
SELECT INTO, UPDATE, VALUES, or VALUES INTO SQL statement. If the
EXPLAIN statement is embedded in a program, the
explainable-sql-statement can contain references to host variables
(these variables must be defined in the program). Similarly, if
EXPLAIN is being dynamically prepared, the explainable-sql-statement
can contain parameter markers.
But it does not tell you what "parameter markers" are, so you have to go and search for it.

How do I turn on DB2 syntax in HyperSQL (HSQL)?

I am trying to use HSQL for testing. In production, the code runs against a DB2 database. I would like to turn on the DB2 syntax of HSQL and run the production SQL against it (or as much of it as possible).
According to the documentation, in DB2 syntax mode, the nextval for expression is supported.
I've turned on DB2 syntax mode both with the connection property, sql.syntax_db2=true, and with:
set database sql syntax db2 true;
However, I am not able to use nextval for, though the syntax next value for works correctly:
values (next value for test_seq); -- returns the next value
values (nextval for test_seq); -- results in an error
The error is:
Error: user lacks privilege or object not found: NEXTVAL
SQLState: 42501
ErrorCode: -5501
Am I not correctly activating DB2 syntax mode, or am I doing something else wrong? I am logged in as SA, so can it be a privilege issue?
There is a mistake in the documentation. The DUAL table is supported the same way as in Oracle syntax, but NEXTVAL is an Oracle form used as test_seq.NEXTVAL.
The DB2 supports NEXT VALUE FOR test_seq and allows NEXTVAL as an alternative to NEXT VALUE. This will be supported in the next release of HSQLDB.