Skip to content
Advertisement

Using replace() inside a loop

I am trying to copy indexes from materialized views onto tables. This is the script I am trying to use:

This is the error I am getting:

All, I am trying to do is replace

with

Advertisement

Answer

The immediate cause for the error you report is a switcheroo:
You have indexdef.indexdefname where it should be indexdefname.indexdef.

But there is more. Don’t build a dynamic nested DO statement just for the replacement. That’s a simple function call which can be nested in the initial SELECT right away. Also, there are multiple syntax errors: missing quotes, function call without assignment, and the DDL statements are never executed. Just execute the CREATE INDEX commands. Like:

Careful! This uses all indexes of all materialized views. And the replacement – while looking reasonable – might go wrong for corner cases. You may want to be more selective, or go with a safer, non-dynamic two-step approach:

1. Generate DDL string with

2. Execute the string after checking it does what you want.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement